提交 7d062df9 编写于 作者: S stonio 提交者: Stephane Nicoll

Use String#isEmpty()

Closes gh-1335
上级 c85f063d
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -150,10 +150,10 @@ public abstract class PropertyMatches {
* @return the distance value
*/
private static int calculateStringDistance(String s1, String s2) {
if (s1.length() == 0) {
if (s1.isEmpty()) {
return s2.length();
}
if (s2.length() == 0) {
if (s2.isEmpty()) {
return s1.length();
}
int d[][] = new int[s1.length() + 1][s2.length() + 1];
......
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -28,7 +28,7 @@ final class StringToCharacterConverter implements Converter<String, Character> {
@Override
public Character convert(String source) {
if (source.length() == 0) {
if (source.isEmpty()) {
return null;
}
if (source.length() > 1) {
......
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -45,7 +45,7 @@ final class StringToEnumConverterFactory implements ConverterFactory<String, Enu
@Override
public T convert(String source) {
if (source.length() == 0) {
if (source.isEmpty()) {
// It's an empty enum identifier: reset the enum value to null.
return null;
}
......
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -56,7 +56,7 @@ final class StringToNumberConverterFactory implements ConverterFactory<String, N
@Override
public T convert(String source) {
if (source.length() == 0) {
if (source.isEmpty()) {
return null;
}
return NumberUtils.parseNumber(source, this.targetType);
......
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -809,7 +809,7 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
public PatternVirtualFileVisitor(String rootPath, String subPattern, PathMatcher pathMatcher) {
this.subPattern = subPattern;
this.pathMatcher = pathMatcher;
this.rootPath = (rootPath.length() == 0 || rootPath.endsWith("/") ? rootPath : rootPath + "/");
this.rootPath = (rootPath.isEmpty() || rootPath.endsWith("/") ? rootPath : rootPath + "/");
}
@Override
......
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -120,7 +120,7 @@ public abstract class Base64Utils {
if (src == null) {
return null;
}
if (src.length() == 0) {
if (src.isEmpty()) {
return new byte[0];
}
return decode(src.getBytes(DEFAULT_CHARSET));
......
......@@ -180,7 +180,7 @@ public abstract class MimeTypeUtils {
int index = mimeType.indexOf(';');
String fullType = (index >= 0 ? mimeType.substring(0, index) : mimeType).trim();
if (fullType.length() == 0) {
if (fullType.isEmpty()) {
throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty");
}
......
......@@ -373,7 +373,7 @@ public abstract class StringUtils {
* @param sub string to search for. Return 0 if this is {@code null}.
*/
public static int countOccurrencesOf(String str, String sub) {
if (str == null || sub == null || str.length() == 0 || sub.length() == 0) {
if (!hasLength(str) || !hasLength(sub)) {
return 0;
}
int count = 0;
......@@ -515,7 +515,7 @@ public abstract class StringUtils {
}
private static String changeFirstCharacterCase(String str, boolean capitalize) {
if (str == null || str.length() == 0) {
if (!hasLength(str)) {
return str;
}
else {
......
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -223,8 +223,8 @@ abstract class AbstractStaxHandler implements ContentHandler, LexicalHandler {
protected boolean isNamespaceDeclaration(QName qName) {
String prefix = qName.getPrefix();
String localPart = qName.getLocalPart();
return (XMLConstants.XMLNS_ATTRIBUTE.equals(localPart) && prefix.length() == 0) ||
(XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) && localPart.length() != 0);
return (XMLConstants.XMLNS_ATTRIBUTE.equals(localPart) && prefix.isEmpty()) ||
(XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) && !localPart.isEmpty());
}
......
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -79,7 +79,7 @@ public abstract class TemplateAwareExpressionParser implements ExpressionParser
private Expression parseTemplate(String expressionString, ParserContext context)
throws ParseException {
if (expressionString.length() == 0) {
if (expressionString.isEmpty()) {
return new LiteralExpression("");
}
Expression[] expressions = parseExpressions(expressionString, context);
......@@ -145,7 +145,7 @@ public abstract class TemplateAwareExpressionParser implements ExpressionParser
suffixIndex);
expr = expr.trim();
if (expr.length() == 0) {
if (expr.isEmpty()) {
throw new ParseException(expressionString, prefixIndex,
"No expression defined within delimiter '" + prefix + suffix
+ "' at character " + prefixIndex);
......
......@@ -142,7 +142,7 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
*/
private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) {
String name = info.name;
if (info.name.length() == 0) {
if (info.name.isEmpty()) {
name = parameter.getParameterName();
if (name == null) {
throw new IllegalArgumentException("Name for argument type [" + parameter.getParameterType().getName() +
......
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -160,7 +160,7 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
*/
private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) {
String name = info.name;
if (info.name.length() == 0) {
if (info.name.isEmpty()) {
name = parameter.getParameterName();
if (name == null) {
throw new IllegalArgumentException(
......
......@@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.Map;
import org.springframework.util.PathMatcher;
import static org.springframework.util.StringUtils.hasLength;
/**
* Represents a parsed path pattern. Includes a chain of path elements
......@@ -132,9 +133,9 @@ public class PathPattern implements Comparable<PathPattern> {
*/
public boolean matches(String path) {
if (head == null) {
return (path == null) || (path.length() == 0);
return !hasLength(path);
}
else if (path == null || path.length() == 0) {
else if (!hasLength(path)) {
if (head instanceof WildcardTheRestPathElement || head instanceof CaptureTheRestPathElement) {
path = ""; // Will allow CaptureTheRest to bind the variable to empty
}
......@@ -152,9 +153,9 @@ public class PathPattern implements Comparable<PathPattern> {
*/
public boolean matchStart(String path) {
if (head == null) {
return (path == null || path.length() == 0);
return !hasLength(path);
}
else if (path == null || path.length() == 0) {
else if (!hasLength(path)) {
return true;
}
MatchingContext matchingContext = new MatchingContext(path, false);
......@@ -172,7 +173,7 @@ public class PathPattern implements Comparable<PathPattern> {
return matchingContext.getExtractedVariables();
}
else {
if (path == null || path.length() == 0) {
if (!hasLength(path)) {
return NO_VARIABLES_MAP;
}
else {
......@@ -434,15 +435,15 @@ public class PathPattern implements Comparable<PathPattern> {
*/
public String combine(String pattern2string) {
// If one of them is empty the result is the other. If both empty the result is ""
if (patternString == null || patternString.length() == 0) {
if (pattern2string == null || pattern2string.length() == 0) {
if (!hasLength(patternString)) {
if (!hasLength(pattern2string)) {
return "";
}
else {
return pattern2string;
}
}
else if (pattern2string == null || pattern2string.length() == 0) {
else if (!hasLength(pattern2string)) {
return patternString;
}
......@@ -504,4 +505,4 @@ public class PathPattern implements Comparable<PathPattern> {
}
}
}
\ No newline at end of file
}
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -258,13 +258,13 @@ public class CommonsMultipartResolverTests {
binder.setBindEmptyMultipartFiles(false);
String firstBound = mtb2.getField2();
binder.bind(request);
assertTrue(mtb2.getField2().length() > 0);
assertFalse(mtb2.getField2().isEmpty());
assertEquals(firstBound, mtb2.getField2());
request = resolver.resolveMultipart(originalRequest);
binder.setBindEmptyMultipartFiles(true);
binder.bind(request);
assertTrue(mtb2.getField2().length() == 0);
assertTrue(mtb2.getField2().isEmpty());
}
@Test
......
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -132,7 +132,7 @@ public abstract class AbstractNamedValueArgumentResolver implements HandlerMetho
*/
private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) {
String name = info.name;
if (info.name.length() == 0) {
if (info.name.isEmpty()) {
name = parameter.getParameterName();
if (name == null) {
String type = parameter.getNestedParameterType().getName();
......
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -159,7 +159,7 @@ public class RequestPartMethodArgumentResolver extends AbstractMessageConverterM
private String getPartName(MethodParameter methodParam, RequestPart requestPart) {
String partName = (requestPart != null ? requestPart.name() : "");
if (partName.length() == 0) {
if (partName.isEmpty()) {
partName = methodParam.getParameterName();
if (partName == null) {
throw new IllegalArgumentException("Request part name for argument type [" +
......
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -25,6 +25,7 @@ import java.util.Set;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.util.StringUtils;
/**
* A specialization of {@link ResponseBodyEmitter} for sending
......@@ -234,7 +235,7 @@ public class SseEmitter extends ResponseBodyEmitter {
@Override
public Set<DataWithMediaType> build() {
if ((this.sb == null || this.sb.length() == 0) && this.dataToSend.isEmpty()) {
if (!StringUtils.hasLength(this.sb) && this.dataToSend.isEmpty()) {
return Collections.emptySet();
}
append("\n");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册