Skip to content

Commit 9a88ebd

Browse files
committed
Consistent hasText checks for CharSequence vs String
Issue: SPR-15540
1 parent 9288990 commit 9a88ebd

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,7 @@ public static boolean hasLength(@Nullable String str) {
139139
* @see Character#isWhitespace
140140
*/
141141
public static boolean hasText(@Nullable CharSequence str) {
142-
if (!hasLength(str)) {
143-
return false;
144-
}
145-
146-
int strLen = str.length();
147-
for (int i = 0; i < strLen; i++) {
148-
if (!Character.isWhitespace(str.charAt(i))) {
149-
return true;
150-
}
151-
}
152-
return false;
142+
return (hasLength(str) && containsText(str));
153143
}
154144

155145
/**
@@ -163,9 +153,19 @@ public static boolean hasText(@Nullable CharSequence str) {
163153
* @see #hasText(CharSequence)
164154
*/
165155
public static boolean hasText(@Nullable String str) {
166-
return (str != null && !str.isEmpty() && hasText((CharSequence) str));
156+
return (hasLength(str) && containsText(str));
167157
}
168158

159+
private static boolean containsText(CharSequence str) {
160+
int strLen = str.length();
161+
for (int i = 0; i < strLen; i++) {
162+
if (!Character.isWhitespace(str.charAt(i))) {
163+
return true;
164+
}
165+
}
166+
return false;
167+
}
168+
169169
/**
170170
* Check whether the given {@code CharSequence} contains any whitespace characters.
171171
* @param str the {@code CharSequence} to check (may be {@code null})

spring-core/src/test/java/org/springframework/util/StringUtilsTests.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@
3232
public class StringUtilsTests {
3333

3434
@Test
35-
public void testHasTextBlank() throws Exception {
35+
public void testHasTextBlank() {
3636
String blank = " ";
3737
assertEquals(false, StringUtils.hasText(blank));
3838
}
3939

4040
@Test
41-
public void testHasTextNullEmpty() throws Exception {
41+
public void testHasTextNullEmpty() {
4242
assertEquals(false, StringUtils.hasText(null));
4343
assertEquals(false, StringUtils.hasText(""));
4444
}
4545

4646
@Test
47-
public void testHasTextValid() throws Exception {
47+
public void testHasTextValid() {
4848
assertEquals(true, StringUtils.hasText("t"));
4949
}
5050

5151
@Test
52-
public void testContainsWhitespace() throws Exception {
52+
public void testContainsWhitespace() {
5353
assertFalse(StringUtils.containsWhitespace(null));
5454
assertFalse(StringUtils.containsWhitespace(""));
5555
assertFalse(StringUtils.containsWhitespace("a"));
@@ -62,7 +62,7 @@ public void testContainsWhitespace() throws Exception {
6262
}
6363

6464
@Test
65-
public void testTrimWhitespace() throws Exception {
65+
public void testTrimWhitespace() {
6666
assertEquals(null, StringUtils.trimWhitespace(null));
6767
assertEquals("", StringUtils.trimWhitespace(""));
6868
assertEquals("", StringUtils.trimWhitespace(" "));
@@ -75,7 +75,7 @@ public void testTrimWhitespace() throws Exception {
7575
}
7676

7777
@Test
78-
public void testTrimAllWhitespace() throws Exception {
78+
public void testTrimAllWhitespace() {
7979
assertEquals("", StringUtils.trimAllWhitespace(""));
8080
assertEquals("", StringUtils.trimAllWhitespace(" "));
8181
assertEquals("", StringUtils.trimAllWhitespace("\t"));
@@ -87,7 +87,7 @@ public void testTrimAllWhitespace() throws Exception {
8787
}
8888

8989
@Test
90-
public void testTrimLeadingWhitespace() throws Exception {
90+
public void testTrimLeadingWhitespace() {
9191
assertEquals(null, StringUtils.trimLeadingWhitespace(null));
9292
assertEquals("", StringUtils.trimLeadingWhitespace(""));
9393
assertEquals("", StringUtils.trimLeadingWhitespace(" "));
@@ -100,7 +100,7 @@ public void testTrimLeadingWhitespace() throws Exception {
100100
}
101101

102102
@Test
103-
public void testTrimTrailingWhitespace() throws Exception {
103+
public void testTrimTrailingWhitespace() {
104104
assertEquals(null, StringUtils.trimTrailingWhitespace(null));
105105
assertEquals("", StringUtils.trimTrailingWhitespace(""));
106106
assertEquals("", StringUtils.trimTrailingWhitespace(" "));
@@ -113,7 +113,7 @@ public void testTrimTrailingWhitespace() throws Exception {
113113
}
114114

115115
@Test
116-
public void testTrimLeadingCharacter() throws Exception {
116+
public void testTrimLeadingCharacter() {
117117
assertEquals(null, StringUtils.trimLeadingCharacter(null, ' '));
118118
assertEquals("", StringUtils.trimLeadingCharacter("", ' '));
119119
assertEquals("", StringUtils.trimLeadingCharacter(" ", ' '));
@@ -126,7 +126,7 @@ public void testTrimLeadingCharacter() throws Exception {
126126
}
127127

128128
@Test
129-
public void testTrimTrailingCharacter() throws Exception {
129+
public void testTrimTrailingCharacter() {
130130
assertEquals(null, StringUtils.trimTrailingCharacter(null, ' '));
131131
assertEquals("", StringUtils.trimTrailingCharacter("", ' '));
132132
assertEquals("", StringUtils.trimTrailingCharacter(" ", ' '));
@@ -166,7 +166,7 @@ public void testCountOccurrencesOf() {
166166
}
167167

168168
@Test
169-
public void testReplace() throws Exception {
169+
public void testReplace() {
170170
String inString = "a6AazAaa77abaa";
171171
String oldPattern = "aa";
172172
String newPattern = "foo";
@@ -189,7 +189,7 @@ public void testReplace() throws Exception {
189189
}
190190

191191
@Test
192-
public void testDelete() throws Exception {
192+
public void testDelete() {
193193
String inString = "The quick brown fox jumped over the lazy dog";
194194

195195
String noThe = StringUtils.delete(inString, "the");
@@ -216,7 +216,7 @@ public void testDelete() throws Exception {
216216
}
217217

218218
@Test
219-
public void testDeleteAny() throws Exception {
219+
public void testDeleteAny() {
220220
String inString = "Able was I ere I saw Elba";
221221

222222
String res = StringUtils.deleteAny(inString, "I");
@@ -598,67 +598,67 @@ public void testEndsWithIgnoreCase() {
598598

599599

600600
@Test
601-
public void testParseLocaleStringSunnyDay() throws Exception {
601+
public void testParseLocaleStringSunnyDay() {
602602
Locale expectedLocale = Locale.UK;
603603
Locale locale = StringUtils.parseLocaleString(expectedLocale.toString());
604604
assertNotNull("When given a bona-fide Locale string, must not return null.", locale);
605605
assertEquals(expectedLocale, locale);
606606
}
607607

608608
@Test
609-
public void testParseLocaleStringWithMalformedLocaleString() throws Exception {
609+
public void testParseLocaleStringWithMalformedLocaleString() {
610610
Locale locale = StringUtils.parseLocaleString("_banjo_on_my_knee");
611611
assertNotNull("When given a malformed Locale string, must not return null.", locale);
612612
}
613613

614614
@Test
615-
public void testParseLocaleStringWithEmptyLocaleStringYieldsNullLocale() throws Exception {
615+
public void testParseLocaleStringWithEmptyLocaleStringYieldsNullLocale() {
616616
Locale locale = StringUtils.parseLocaleString("");
617617
assertNull("When given an empty Locale string, must return null.", locale);
618618
}
619619

620620
@Test // SPR-8637
621-
public void testParseLocaleWithMultiSpecialCharactersInVariant() throws Exception {
621+
public void testParseLocaleWithMultiSpecialCharactersInVariant() {
622622
String variant = "proper-northern";
623623
String localeString = "en_GB_" + variant;
624624
Locale locale = StringUtils.parseLocaleString(localeString);
625625
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
626626
}
627627

628628
@Test // SPR-3671
629-
public void testParseLocaleWithMultiValuedVariant() throws Exception {
629+
public void testParseLocaleWithMultiValuedVariant() {
630630
String variant = "proper_northern";
631631
String localeString = "en_GB_" + variant;
632632
Locale locale = StringUtils.parseLocaleString(localeString);
633633
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
634634
}
635635

636636
@Test // SPR-3671
637-
public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparators() throws Exception {
637+
public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparators() {
638638
String variant = "proper northern";
639639
String localeString = "en GB " + variant;
640640
Locale locale = StringUtils.parseLocaleString(localeString);
641641
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
642642
}
643643

644644
@Test // SPR-3671
645-
public void testParseLocaleWithMultiValuedVariantUsingMixtureOfUnderscoresAndSpacesAsSeparators() throws Exception {
645+
public void testParseLocaleWithMultiValuedVariantUsingMixtureOfUnderscoresAndSpacesAsSeparators() {
646646
String variant = "proper northern";
647647
String localeString = "en_GB_" + variant;
648648
Locale locale = StringUtils.parseLocaleString(localeString);
649649
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
650650
}
651651

652652
@Test // SPR-3671
653-
public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception {
653+
public void testParseLocaleWithMultiValuedVariantUsingSpacesAsSeparatorsWithLotsOfLeadingWhitespace() {
654654
String variant = "proper northern";
655655
String localeString = "en GB " + variant; // lots of whitespace
656656
Locale locale = StringUtils.parseLocaleString(localeString);
657657
assertEquals("Multi-valued variant portion of the Locale not extracted correctly.", variant, locale.getVariant());
658658
}
659659

660660
@Test // SPR-3671
661-
public void testParseLocaleWithMultiValuedVariantUsingUnderscoresAsSeparatorsWithLotsOfLeadingWhitespace() throws Exception {
661+
public void testParseLocaleWithMultiValuedVariantUsingUnderscoresAsSeparatorsWithLotsOfLeadingWhitespace() {
662662
String variant = "proper_northern";
663663
String localeString = "en_GB_____" + variant; // lots of underscores
664664
Locale locale = StringUtils.parseLocaleString(localeString);

0 commit comments

Comments
 (0)