Skip to content

Commit 1db4208

Browse files
stoniojhoeller
authored andcommitted
Use String#isEmpty()
Closes gh-1335 (cherry picked from commit 7d062df)
1 parent 34a0857 commit 1db4208

File tree

15 files changed

+92
-109
lines changed

15 files changed

+92
-109
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.
@@ -833,7 +833,7 @@ private static class PatternVirtualFileVisitor implements InvocationHandler {
833833
public PatternVirtualFileVisitor(String rootPath, String subPattern, PathMatcher pathMatcher) {
834834
this.subPattern = subPattern;
835835
this.pathMatcher = pathMatcher;
836-
this.rootPath = (rootPath.length() == 0 || rootPath.endsWith("/") ? rootPath : rootPath + "/");
836+
this.rootPath = (rootPath.isEmpty() || rootPath.endsWith("/") ? rootPath : rootPath + "/");
837837
}
838838

839839
@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.
@@ -159,7 +159,7 @@ public static byte[] decodeFromString(String src) {
159159
if (src == null) {
160160
return null;
161161
}
162-
if (src.length() == 0) {
162+
if (src.isEmpty()) {
163163
return new byte[0];
164164
}
165165

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

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

244244
int index = mimeType.indexOf(';');
245245
String fullType = (index >= 0 ? mimeType.substring(0, index) : mimeType).trim();
246-
if (fullType.length() == 0) {
246+
if (fullType.isEmpty()) {
247247
throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty");
248248
}
249249

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public static boolean substringMatch(CharSequence str, int index, CharSequence s
371371
* @param sub string to search for. Return 0 if this is {@code null}.
372372
*/
373373
public static int countOccurrencesOf(String str, String sub) {
374-
if (str == null || sub == null || str.length() == 0 || sub.length() == 0) {
374+
if (!hasLength(str) || !hasLength(sub)) {
375375
return 0;
376376
}
377377
int count = 0;
@@ -513,25 +513,23 @@ public static String uncapitalize(String str) {
513513
}
514514

515515
private static String changeFirstCharacterCase(String str, boolean capitalize) {
516-
if (str == null || str.length() == 0) {
516+
if (!hasLength(str)) {
517517
return str;
518518
}
519+
char baseChar = str.charAt(0);
520+
char updatedChar;
521+
if (capitalize) {
522+
updatedChar = Character.toUpperCase(baseChar);
523+
}
519524
else {
520-
char baseChar = str.charAt(0);
521-
char updatedChar;
522-
if (capitalize) {
523-
updatedChar = Character.toUpperCase(baseChar);
524-
}
525-
else {
526-
updatedChar = Character.toLowerCase(baseChar);
527-
}
528-
if (baseChar == updatedChar) {
529-
return str;
530-
}
531-
char[] chars = str.toCharArray();
532-
chars[0] = updatedChar;
533-
return new String(chars, 0, chars.length);
525+
updatedChar = Character.toLowerCase(baseChar);
526+
}
527+
if (baseChar == updatedChar) {
528+
return str;
534529
}
530+
char[] chars = str.toCharArray();
531+
chars[0] = updatedChar;
532+
return new String(chars, 0, chars.length);
535533
}
536534

537535
/**

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-2014 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

0 commit comments

Comments
 (0)