Skip to content

Refactor with String#isEmpty() #1335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -150,10 +150,10 @@ else if (i == this.possibleMatches.length - 2) {
* @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];
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -45,7 +45,7 @@ public StringToEnum(Class<T> enumType) {

@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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -56,7 +56,7 @@ public StringToNumber(Class<T> targetType) {

@Override
public T convert(String source) {
if (source.length() == 0) {
if (source.isEmpty()) {
return null;
}
return NumberUtils.parseNumber(source, this.targetType);
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -809,7 +809,7 @@ private static class PatternVirtualFileVisitor implements InvocationHandler {
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -120,7 +120,7 @@ public static byte[] decodeFromString(String src) {
if (src == null) {
return null;
}
if (src.length() == 0) {
if (src.isEmpty()) {
return new byte[0];
}
return decode(src.getBytes(DEFAULT_CHARSET));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public static MimeType parseMimeType(String mimeType) {

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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public static boolean substringMatch(CharSequence str, int index, CharSequence s
* @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;
Expand Down Expand Up @@ -515,7 +515,7 @@ public static String uncapitalize(String str) {
}

private static String changeFirstCharacterCase(String str, boolean capitalize) {
if (str == null || str.length() == 0) {
if (!hasLength(str)) {
return str;
}
else {
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -223,8 +223,8 @@ protected QName toQName(String namespaceUri, String qualifiedName) {
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());
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -79,7 +79,7 @@ public Expression parseExpression(String expressionString, ParserContext context

private Expression parseTemplate(String expressionString, ParserContext context)
throws ParseException {
if (expressionString.length() == 0) {
if (expressionString.isEmpty()) {
return new LiteralExpression("");
}
Expression[] expressions = parseExpressions(expressionString, context);
Expand Down Expand Up @@ -145,7 +145,7 @@ private Expression[] parseExpressions(String expressionString, ParserContext con
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private NamedValueInfo getNamedValueInfo(MethodParameter parameter) {
*/
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() +
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -160,7 +160,7 @@ private NamedValueInfo getNamedValueInfo(MethodParameter parameter) {
*/
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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
Expand Down Expand Up @@ -132,9 +133,9 @@ public PathPattern(String patternText, PathElement head, char separator, boolean
*/
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
}
Expand All @@ -152,9 +153,9 @@ else if (path == null || path.length() == 0) {
*/
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);
Expand All @@ -172,7 +173,7 @@ public Map<String, String> matchAndExtract(String path) {
return matchingContext.getExtractedVariables();
}
else {
if (path == null || path.length() == 0) {
if (!hasLength(path)) {
return NO_VARIABLES_MAP;
}
else {
Expand Down Expand Up @@ -434,15 +435,15 @@ public int scanAhead(int pos) {
*/
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;
}

Expand Down Expand Up @@ -504,4 +505,4 @@ else if (path1EndsWithSeparator || path2StartsWithSeparator) {
}
}

}
}
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -258,13 +258,13 @@ private void doTestBinding(MockCommonsMultipartResolver resolver, MockHttpServle
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -132,7 +132,7 @@ private NamedValueInfo getNamedValueInfo(MethodParameter parameter) {
*/
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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -159,7 +159,7 @@ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer m

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 [" +
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -25,6 +25,7 @@
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
Expand Down Expand Up @@ -234,7 +235,7 @@ SseEventBuilderImpl append(String text) {

@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");
Expand Down