Skip to content

Commit 800c53c

Browse files
author
bentmann
committed
[PLXUTILS-73] Add StringUtils.isBlank() and make StringUtils.isEmpty() not trim
o First part of the show: Added isBlank() and isNotBlank() for people to migrate to (code taken from Commons Lang 2.4, ASL 2.0)
1 parent a50847d commit 800c53c

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

src/main/java/org/codehaus/plexus/util/StringUtils.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ public static boolean isNotEmpty( String str )
163163
/**
164164
* <p>Checks if a (trimmed) String is <code>null</code> or empty.</p>
165165
*
166+
* <p><strong>Note:</strong> In future releases, this method will no longer trim the input string such that it works
167+
* complementary to {@link #isNotEmpty(String)}. Code that wants to test for whitespace-only strings should be
168+
* migrated to use {@link #isBlank(String)} instead.</p>
169+
*
166170
* @param str the String to check
167171
* @return <code>true</code> if the String is <code>null</code>, or
168172
* length zero once trimmed
@@ -172,6 +176,62 @@ public static boolean isEmpty( String str )
172176
return ( ( str == null ) || ( str.trim().length() == 0 ) );
173177
}
174178

179+
/**
180+
* <p>
181+
* Checks if a String is whitespace, empty ("") or null.
182+
* </p>
183+
*
184+
* <pre>
185+
* StringUtils.isBlank(null) = true
186+
* StringUtils.isBlank("") = true
187+
* StringUtils.isBlank(" ") = true
188+
* StringUtils.isBlank("bob") = false
189+
* StringUtils.isBlank(" bob ") = false
190+
* </pre>
191+
*
192+
* @param str the String to check, may be null
193+
* @return <code>true</code> if the String is null, empty or whitespace
194+
* @since 1.5.2
195+
*/
196+
public static boolean isBlank( String str )
197+
{
198+
int strLen;
199+
if ( str == null || ( strLen = str.length() ) == 0 )
200+
{
201+
return true;
202+
}
203+
for ( int i = 0; i < strLen; i++ )
204+
{
205+
if ( !Character.isWhitespace( str.charAt( i ) ) )
206+
{
207+
return false;
208+
}
209+
}
210+
return true;
211+
}
212+
213+
/**
214+
* <p>
215+
* Checks if a String is not empty (""), not null and not whitespace only.
216+
* </p>
217+
*
218+
* <pre>
219+
* StringUtils.isNotBlank(null) = false
220+
* StringUtils.isNotBlank("") = false
221+
* StringUtils.isNotBlank(" ") = false
222+
* StringUtils.isNotBlank("bob") = true
223+
* StringUtils.isNotBlank(" bob ") = true
224+
* </pre>
225+
*
226+
* @param str the String to check, may be null
227+
* @return <code>true</code> if the String is not empty and not null and not whitespace
228+
* @since 1.5.2
229+
*/
230+
public static boolean isNotBlank( String str )
231+
{
232+
return !StringUtils.isBlank( str );
233+
}
234+
175235
// Equals and IndexOf
176236
//--------------------------------------------------------------------------
177237

src/test/java/org/codehaus/plexus/util/StringUtilsTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,45 @@
1313
public class StringUtilsTest
1414
extends TestCase
1515
{
16+
17+
public void testIsEmpty()
18+
{
19+
assertEquals( true, StringUtils.isEmpty( null ) );
20+
assertEquals( true, StringUtils.isEmpty( "" ) );
21+
/* TODO: Some day in the future, make this pass (PLXUTILS-73)
22+
assertEquals( false, StringUtils.isEmpty( " " ) );
23+
//*/
24+
assertEquals( false, StringUtils.isEmpty( "foo" ) );
25+
assertEquals( false, StringUtils.isEmpty( " foo " ) );
26+
}
27+
28+
public void testIsNotEmpty()
29+
{
30+
assertEquals( false, StringUtils.isNotEmpty( null ) );
31+
assertEquals( false, StringUtils.isNotEmpty( "" ) );
32+
assertEquals( true, StringUtils.isNotEmpty( " " ) );
33+
assertEquals( true, StringUtils.isNotEmpty( "foo" ) );
34+
assertEquals( true, StringUtils.isNotEmpty( " foo " ) );
35+
}
36+
37+
public void testIsBlank()
38+
{
39+
assertEquals( true, StringUtils.isBlank( null ) );
40+
assertEquals( true, StringUtils.isBlank( "" ) );
41+
assertEquals( true, StringUtils.isBlank( " \t\r\n" ) );
42+
assertEquals( false, StringUtils.isBlank( "foo" ) );
43+
assertEquals( false, StringUtils.isBlank( " foo " ) );
44+
}
45+
46+
public void testIsNotBlank()
47+
{
48+
assertEquals( false, StringUtils.isNotBlank( null ) );
49+
assertEquals( false, StringUtils.isNotBlank( "" ) );
50+
assertEquals( false, StringUtils.isNotBlank( " \t\r\n" ) );
51+
assertEquals( true, StringUtils.isNotBlank( "foo" ) );
52+
assertEquals( true, StringUtils.isNotBlank( " foo " ) );
53+
}
54+
1655
public void testCapitalizeFirstLetter()
1756
{
1857
assertEquals( "Id", StringUtils.capitalizeFirstLetter( "id" ) );

0 commit comments

Comments
 (0)