Skip to content

Commit 21ae169

Browse files
committed
Merge pull request spring-projects#1335 from stonio:patch-4
* pr/1335: Use String#isEmpty()
2 parents c85f063 + 7d062df commit 21ae169

File tree

17 files changed

+45
-43
lines changed

17 files changed

+45
-43
lines changed

spring-beans/src/main/java/org/springframework/beans/PropertyMatches.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -150,10 +150,10 @@ else if (i == this.possibleMatches.length - 2) {
150150
* @return the distance value
151151
*/
152152
private static int calculateStringDistance(String s1, String s2) {
153-
if (s1.length() == 0) {
153+
if (s1.isEmpty()) {
154154
return s2.length();
155155
}
156-
if (s2.length() == 0) {
156+
if (s2.isEmpty()) {
157157
return s1.length();
158158
}
159159
int d[][] = new int[s1.length() + 1][s2.length() + 1];

spring-core/src/main/java/org/springframework/core/convert/support/StringToCharacterConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@ final class StringToCharacterConverter implements Converter<String, Character> {
2828

2929
@Override
3030
public Character convert(String source) {
31-
if (source.length() == 0) {
31+
if (source.isEmpty()) {
3232
return null;
3333
}
3434
if (source.length() > 1) {

spring-core/src/main/java/org/springframework/core/convert/support/StringToEnumConverterFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,7 +45,7 @@ public StringToEnum(Class<T> enumType) {
4545

4646
@Override
4747
public T convert(String source) {
48-
if (source.length() == 0) {
48+
if (source.isEmpty()) {
4949
// It's an empty enum identifier: reset the enum value to null.
5050
return null;
5151
}

spring-core/src/main/java/org/springframework/core/convert/support/StringToNumberConverterFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,7 +56,7 @@ public StringToNumber(Class<T> targetType) {
5656

5757
@Override
5858
public T convert(String source) {
59-
if (source.length() == 0) {
59+
if (source.isEmpty()) {
6060
return null;
6161
}
6262
return NumberUtils.parseNumber(source, this.targetType);

spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -809,7 +809,7 @@ private static class PatternVirtualFileVisitor implements InvocationHandler {
809809
public PatternVirtualFileVisitor(String rootPath, String subPattern, PathMatcher pathMatcher) {
810810
this.subPattern = subPattern;
811811
this.pathMatcher = pathMatcher;
812-
this.rootPath = (rootPath.length() == 0 || rootPath.endsWith("/") ? rootPath : rootPath + "/");
812+
this.rootPath = (rootPath.isEmpty() || rootPath.endsWith("/") ? rootPath : rootPath + "/");
813813
}
814814

815815
@Override

spring-core/src/main/java/org/springframework/util/Base64Utils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -120,7 +120,7 @@ public static byte[] decodeFromString(String src) {
120120
if (src == null) {
121121
return null;
122122
}
123-
if (src.length() == 0) {
123+
if (src.isEmpty()) {
124124
return new byte[0];
125125
}
126126
return decode(src.getBytes(DEFAULT_CHARSET));

spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public static MimeType parseMimeType(String mimeType) {
180180

181181
int index = mimeType.indexOf(';');
182182
String fullType = (index >= 0 ? mimeType.substring(0, index) : mimeType).trim();
183-
if (fullType.length() == 0) {
183+
if (fullType.isEmpty()) {
184184
throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty");
185185
}
186186

spring-core/src/main/java/org/springframework/util/StringUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ public static boolean substringMatch(CharSequence str, int index, CharSequence s
373373
* @param sub string to search for. Return 0 if this is {@code null}.
374374
*/
375375
public static int countOccurrencesOf(String str, String sub) {
376-
if (str == null || sub == null || str.length() == 0 || sub.length() == 0) {
376+
if (!hasLength(str) || !hasLength(sub)) {
377377
return 0;
378378
}
379379
int count = 0;
@@ -515,7 +515,7 @@ public static String uncapitalize(String str) {
515515
}
516516

517517
private static String changeFirstCharacterCase(String str, boolean capitalize) {
518-
if (str == null || str.length() == 0) {
518+
if (!hasLength(str)) {
519519
return str;
520520
}
521521
else {

spring-core/src/main/java/org/springframework/util/xml/AbstractStaxHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -223,8 +223,8 @@ protected QName toQName(String namespaceUri, String qualifiedName) {
223223
protected boolean isNamespaceDeclaration(QName qName) {
224224
String prefix = qName.getPrefix();
225225
String localPart = qName.getLocalPart();
226-
return (XMLConstants.XMLNS_ATTRIBUTE.equals(localPart) && prefix.length() == 0) ||
227-
(XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) && localPart.length() != 0);
226+
return (XMLConstants.XMLNS_ATTRIBUTE.equals(localPart) && prefix.isEmpty()) ||
227+
(XMLConstants.XMLNS_ATTRIBUTE.equals(prefix) && !localPart.isEmpty());
228228
}
229229

230230

spring-expression/src/main/java/org/springframework/expression/common/TemplateAwareExpressionParser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -79,7 +79,7 @@ public Expression parseExpression(String expressionString, ParserContext context
7979

8080
private Expression parseTemplate(String expressionString, ParserContext context)
8181
throws ParseException {
82-
if (expressionString.length() == 0) {
82+
if (expressionString.isEmpty()) {
8383
return new LiteralExpression("");
8484
}
8585
Expression[] expressions = parseExpressions(expressionString, context);
@@ -145,7 +145,7 @@ private Expression[] parseExpressions(String expressionString, ParserContext con
145145
suffixIndex);
146146
expr = expr.trim();
147147

148-
if (expr.length() == 0) {
148+
if (expr.isEmpty()) {
149149
throw new ParseException(expressionString, prefixIndex,
150150
"No expression defined within delimiter '" + prefix + suffix
151151
+ "' at character " + prefixIndex);

spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/AbstractNamedValueMethodArgumentResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private NamedValueInfo getNamedValueInfo(MethodParameter parameter) {
142142
*/
143143
private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) {
144144
String name = info.name;
145-
if (info.name.length() == 0) {
145+
if (info.name.isEmpty()) {
146146
name = parameter.getParameterName();
147147
if (name == null) {
148148
throw new IllegalArgumentException("Name for argument type [" + parameter.getParameterType().getName() +

spring-web/src/main/java/org/springframework/web/method/annotation/AbstractNamedValueMethodArgumentResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -160,7 +160,7 @@ private NamedValueInfo getNamedValueInfo(MethodParameter parameter) {
160160
*/
161161
private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) {
162162
String name = info.name;
163-
if (info.name.length() == 0) {
163+
if (info.name.isEmpty()) {
164164
name = parameter.getParameterName();
165165
if (name == null) {
166166
throw new IllegalArgumentException(

spring-web/src/main/java/org/springframework/web/util/patterns/PathPattern.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Map;
2222

2323
import org.springframework.util.PathMatcher;
24+
import static org.springframework.util.StringUtils.hasLength;
2425

2526
/**
2627
* Represents a parsed path pattern. Includes a chain of path elements
@@ -132,9 +133,9 @@ public PathPattern(String patternText, PathElement head, char separator, boolean
132133
*/
133134
public boolean matches(String path) {
134135
if (head == null) {
135-
return (path == null) || (path.length() == 0);
136+
return !hasLength(path);
136137
}
137-
else if (path == null || path.length() == 0) {
138+
else if (!hasLength(path)) {
138139
if (head instanceof WildcardTheRestPathElement || head instanceof CaptureTheRestPathElement) {
139140
path = ""; // Will allow CaptureTheRest to bind the variable to empty
140141
}
@@ -152,9 +153,9 @@ else if (path == null || path.length() == 0) {
152153
*/
153154
public boolean matchStart(String path) {
154155
if (head == null) {
155-
return (path == null || path.length() == 0);
156+
return !hasLength(path);
156157
}
157-
else if (path == null || path.length() == 0) {
158+
else if (!hasLength(path)) {
158159
return true;
159160
}
160161
MatchingContext matchingContext = new MatchingContext(path, false);
@@ -172,7 +173,7 @@ public Map<String, String> matchAndExtract(String path) {
172173
return matchingContext.getExtractedVariables();
173174
}
174175
else {
175-
if (path == null || path.length() == 0) {
176+
if (!hasLength(path)) {
176177
return NO_VARIABLES_MAP;
177178
}
178179
else {
@@ -434,15 +435,15 @@ public int scanAhead(int pos) {
434435
*/
435436
public String combine(String pattern2string) {
436437
// If one of them is empty the result is the other. If both empty the result is ""
437-
if (patternString == null || patternString.length() == 0) {
438-
if (pattern2string == null || pattern2string.length() == 0) {
438+
if (!hasLength(patternString)) {
439+
if (!hasLength(pattern2string)) {
439440
return "";
440441
}
441442
else {
442443
return pattern2string;
443444
}
444445
}
445-
else if (pattern2string == null || pattern2string.length() == 0) {
446+
else if (!hasLength(pattern2string)) {
446447
return patternString;
447448
}
448449

@@ -504,4 +505,4 @@ else if (path1EndsWithSeparator || path2StartsWithSeparator) {
504505
}
505506
}
506507

507-
}
508+
}

spring-web/src/test/java/org/springframework/web/multipart/commons/CommonsMultipartResolverTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -258,13 +258,13 @@ private void doTestBinding(MockCommonsMultipartResolver resolver, MockHttpServle
258258
binder.setBindEmptyMultipartFiles(false);
259259
String firstBound = mtb2.getField2();
260260
binder.bind(request);
261-
assertTrue(mtb2.getField2().length() > 0);
261+
assertFalse(mtb2.getField2().isEmpty());
262262
assertEquals(firstBound, mtb2.getField2());
263263

264264
request = resolver.resolveMultipart(originalRequest);
265265
binder.setBindEmptyMultipartFiles(true);
266266
binder.bind(request);
267-
assertTrue(mtb2.getField2().length() == 0);
267+
assertTrue(mtb2.getField2().isEmpty());
268268
}
269269

270270
@Test

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractNamedValueArgumentResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -132,7 +132,7 @@ private NamedValueInfo getNamedValueInfo(MethodParameter parameter) {
132132
*/
133133
private NamedValueInfo updateNamedValueInfo(MethodParameter parameter, NamedValueInfo info) {
134134
String name = info.name;
135-
if (info.name.length() == 0) {
135+
if (info.name.isEmpty()) {
136136
name = parameter.getParameterName();
137137
if (name == null) {
138138
String type = parameter.getNestedParameterType().getName();

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestPartMethodArgumentResolver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -159,7 +159,7 @@ public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer m
159159

160160
private String getPartName(MethodParameter methodParam, RequestPart requestPart) {
161161
String partName = (requestPart != null ? requestPart.name() : "");
162-
if (partName.length() == 0) {
162+
if (partName.isEmpty()) {
163163
partName = methodParam.getParameterName();
164164
if (partName == null) {
165165
throw new IllegalArgumentException("Request part name for argument type [" +

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/SseEmitter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.springframework.http.HttpHeaders;
2626
import org.springframework.http.MediaType;
2727
import org.springframework.http.server.ServerHttpResponse;
28+
import org.springframework.util.StringUtils;
2829

2930
/**
3031
* A specialization of {@link ResponseBodyEmitter} for sending
@@ -234,7 +235,7 @@ SseEventBuilderImpl append(String text) {
234235

235236
@Override
236237
public Set<DataWithMediaType> build() {
237-
if ((this.sb == null || this.sb.length() == 0) && this.dataToSend.isEmpty()) {
238+
if (!StringUtils.hasLength(this.sb) && this.dataToSend.isEmpty()) {
238239
return Collections.emptySet();
239240
}
240241
append("\n");

0 commit comments

Comments
 (0)