Use String#isEmpty()

Closes gh-1335
This commit is contained in:
stonio 2017-02-21 16:23:40 +01:00 committed by Stephane Nicoll
parent c85f063d92
commit 7d062df992
17 changed files with 45 additions and 43 deletions

View File

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * @return the distance value
*/ */
private static int calculateStringDistance(String s1, String s2) { private static int calculateStringDistance(String s1, String s2) {
if (s1.length() == 0) { if (s1.isEmpty()) {
return s2.length(); return s2.length();
} }
if (s2.length() == 0) { if (s2.isEmpty()) {
return s1.length(); return s1.length();
} }
int d[][] = new int[s1.length() + 1][s2.length() + 1]; int d[][] = new int[s1.length() + 1][s2.length() + 1];

View File

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 @Override
public Character convert(String source) { public Character convert(String source) {
if (source.length() == 0) { if (source.isEmpty()) {
return null; return null;
} }
if (source.length() > 1) { if (source.length() > 1) {

View File

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 @Override
public T convert(String source) { public T convert(String source) {
if (source.length() == 0) { if (source.isEmpty()) {
// It's an empty enum identifier: reset the enum value to null. // It's an empty enum identifier: reset the enum value to null.
return null; return null;
} }

View File

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 @Override
public T convert(String source) { public T convert(String source) {
if (source.length() == 0) { if (source.isEmpty()) {
return null; return null;
} }
return NumberUtils.parseNumber(source, this.targetType); return NumberUtils.parseNumber(source, this.targetType);

View File

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { public PatternVirtualFileVisitor(String rootPath, String subPattern, PathMatcher pathMatcher) {
this.subPattern = subPattern; this.subPattern = subPattern;
this.pathMatcher = pathMatcher; this.pathMatcher = pathMatcher;
this.rootPath = (rootPath.length() == 0 || rootPath.endsWith("/") ? rootPath : rootPath + "/"); this.rootPath = (rootPath.isEmpty() || rootPath.endsWith("/") ? rootPath : rootPath + "/");
} }
@Override @Override

View File

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { if (src == null) {
return null; return null;
} }
if (src.length() == 0) { if (src.isEmpty()) {
return new byte[0]; return new byte[0];
} }
return decode(src.getBytes(DEFAULT_CHARSET)); return decode(src.getBytes(DEFAULT_CHARSET));

View File

@ -180,7 +180,7 @@ public abstract class MimeTypeUtils {
int index = mimeType.indexOf(';'); int index = mimeType.indexOf(';');
String fullType = (index >= 0 ? mimeType.substring(0, index) : mimeType).trim(); 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"); throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty");
} }

View File

@ -373,7 +373,7 @@ public abstract class StringUtils {
* @param sub string to search for. Return 0 if this is {@code null}. * @param sub string to search for. Return 0 if this is {@code null}.
*/ */
public static int countOccurrencesOf(String str, String sub) { 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; return 0;
} }
int count = 0; int count = 0;
@ -515,7 +515,7 @@ public abstract class StringUtils {
} }
private static String changeFirstCharacterCase(String str, boolean capitalize) { private static String changeFirstCharacterCase(String str, boolean capitalize) {
if (str == null || str.length() == 0) { if (!hasLength(str)) {
return str; return str;
} }
else { else {

View File

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { protected boolean isNamespaceDeclaration(QName qName) {
String prefix = qName.getPrefix(); String prefix = qName.getPrefix();
String localPart = qName.getLocalPart(); String localPart = qName.getLocalPart();
return (XMLConstants.XMLNS_ATTRIBUTE.equals(localPart) && prefix.length() == 0) || return (XMLConstants.XMLNS_ATTRIBUTE.equals(localPart) && prefix.isEmpty()) ||
(XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) && localPart.length() != 0); (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) && !localPart.isEmpty());
} }

View File

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) private Expression parseTemplate(String expressionString, ParserContext context)
throws ParseException { throws ParseException {
if (expressionString.length() == 0) { if (expressionString.isEmpty()) {
return new LiteralExpression(""); return new LiteralExpression("");
} }
Expression[] expressions = parseExpressions(expressionString, context); Expression[] expressions = parseExpressions(expressionString, context);
@ -145,7 +145,7 @@ public abstract class TemplateAwareExpressionParser implements ExpressionParser
suffixIndex); suffixIndex);
expr = expr.trim(); expr = expr.trim();
if (expr.length() == 0) { if (expr.isEmpty()) {
throw new ParseException(expressionString, prefixIndex, throw new ParseException(expressionString, prefixIndex,
"No expression defined within delimiter '" + prefix + suffix "No expression defined within delimiter '" + prefix + suffix
+ "' at character " + prefixIndex); + "' at character " + prefixIndex);

View File

@ -142,7 +142,7 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
*/ */
private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) { private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) {
String name = info.name; String name = info.name;
if (info.name.length() == 0) { if (info.name.isEmpty()) {
name = parameter.getParameterName(); name = parameter.getParameterName();
if (name == null) { if (name == null) {
throw new IllegalArgumentException("Name for argument type [" + parameter.getParameterType().getName() + throw new IllegalArgumentException("Name for argument type [" + parameter.getParameterType().getName() +

View File

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) {
String name = info.name; String name = info.name;
if (info.name.length() == 0) { if (info.name.isEmpty()) {
name = parameter.getParameterName(); name = parameter.getParameterName();
if (name == null) { if (name == null) {
throw new IllegalArgumentException( throw new IllegalArgumentException(

View File

@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.springframework.util.PathMatcher; import org.springframework.util.PathMatcher;
import static org.springframework.util.StringUtils.hasLength;
/** /**
* Represents a parsed path pattern. Includes a chain of path elements * 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) { public boolean matches(String path) {
if (head == null) { 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) { if (head instanceof WildcardTheRestPathElement || head instanceof CaptureTheRestPathElement) {
path = ""; // Will allow CaptureTheRest to bind the variable to empty 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) { public boolean matchStart(String path) {
if (head == null) { 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; return true;
} }
MatchingContext matchingContext = new MatchingContext(path, false); MatchingContext matchingContext = new MatchingContext(path, false);
@ -172,7 +173,7 @@ public class PathPattern implements Comparable<PathPattern> {
return matchingContext.getExtractedVariables(); return matchingContext.getExtractedVariables();
} }
else { else {
if (path == null || path.length() == 0) { if (!hasLength(path)) {
return NO_VARIABLES_MAP; return NO_VARIABLES_MAP;
} }
else { else {
@ -434,15 +435,15 @@ public class PathPattern implements Comparable<PathPattern> {
*/ */
public String combine(String pattern2string) { public String combine(String pattern2string) {
// If one of them is empty the result is the other. If both empty the result is "" // If one of them is empty the result is the other. If both empty the result is ""
if (patternString == null || patternString.length() == 0) { if (!hasLength(patternString)) {
if (pattern2string == null || pattern2string.length() == 0) { if (!hasLength(pattern2string)) {
return ""; return "";
} }
else { else {
return pattern2string; return pattern2string;
} }
} }
else if (pattern2string == null || pattern2string.length() == 0) { else if (!hasLength(pattern2string)) {
return patternString; return patternString;
} }

View File

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -258,13 +258,13 @@ public class CommonsMultipartResolverTests {
binder.setBindEmptyMultipartFiles(false); binder.setBindEmptyMultipartFiles(false);
String firstBound = mtb2.getField2(); String firstBound = mtb2.getField2();
binder.bind(request); binder.bind(request);
assertTrue(mtb2.getField2().length() > 0); assertFalse(mtb2.getField2().isEmpty());
assertEquals(firstBound, mtb2.getField2()); assertEquals(firstBound, mtb2.getField2());
request = resolver.resolveMultipart(originalRequest); request = resolver.resolveMultipart(originalRequest);
binder.setBindEmptyMultipartFiles(true); binder.setBindEmptyMultipartFiles(true);
binder.bind(request); binder.bind(request);
assertTrue(mtb2.getField2().length() == 0); assertTrue(mtb2.getField2().isEmpty());
} }
@Test @Test

View File

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) {
String name = info.name; String name = info.name;
if (info.name.length() == 0) { if (info.name.isEmpty()) {
name = parameter.getParameterName(); name = parameter.getParameterName();
if (name == null) { if (name == null) {
String type = parameter.getNestedParameterType().getName(); String type = parameter.getNestedParameterType().getName();

View File

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { private String getPartName(MethodParameter methodParam, RequestPart requestPart) {
String partName = (requestPart != null ? requestPart.name() : ""); String partName = (requestPart != null ? requestPart.name() : "");
if (partName.length() == 0) { if (partName.isEmpty()) {
partName = methodParam.getParameterName(); partName = methodParam.getParameterName();
if (partName == null) { if (partName == null) {
throw new IllegalArgumentException("Request part name for argument type [" + throw new IllegalArgumentException("Request part name for argument type [" +

View File

@ -1,5 +1,5 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpResponse; import org.springframework.http.server.ServerHttpResponse;
import org.springframework.util.StringUtils;
/** /**
* A specialization of {@link ResponseBodyEmitter} for sending * A specialization of {@link ResponseBodyEmitter} for sending
@ -234,7 +235,7 @@ public class SseEmitter extends ResponseBodyEmitter {
@Override @Override
public Set<DataWithMediaType> build() { 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(); return Collections.emptySet();
} }
append("\n"); append("\n");