LANG-1270: Add StringUtils#isAnyNotEmpty and #isAnyNotBlank (closes #193)

This commit is contained in:
Pierre Templier 2016-09-24 00:49:32 +02:00 committed by pascalschumacher
parent 8592cfe496
commit 5acf310d08
2 changed files with 93 additions and 7 deletions

View File

@ -256,7 +256,36 @@ public static boolean isAnyEmpty(final CharSequence... css) {
}
return false;
}
/**
* <p>Checks if any one of the CharSequences are not empty ("") or null.</p>
*
* <pre>
* StringUtils.isAnyNotEmpty(null) = false
* StringUtils.isAnyNotEmpty(null, "foo") = true
* StringUtils.isAnyNotEmpty("", "bar") = true
* StringUtils.isAnyNotEmpty("bob", "") = true
* StringUtils.isAnyNotEmpty(" bob ", null) = true
* StringUtils.isAnyNotEmpty(" ", "bar") = true
* StringUtils.isAnyNotEmpty("foo", "bar") = true
* </pre>
*
* @param css the CharSequences to check, may be null or empty
* @return {@code true} if any of the CharSequences are empty or null
* @since 3.6
*/
public static boolean isAnyNotEmpty(final CharSequence... css) {
if (ArrayUtils.isEmpty(css)) {
return true;
}
for (final CharSequence cs : css) {
if (isNotEmpty(cs)) {
return true;
}
}
return false;
}
/**
* <p>Checks if none of the CharSequences are empty ("") or null.</p>
*
@ -276,7 +305,8 @@ public static boolean isAnyEmpty(final CharSequence... css) {
*/
public static boolean isNoneEmpty(final CharSequence... css) {
return !isAnyEmpty(css);
}
}
/**
* <p>Checks if a CharSequence is whitespace, empty ("") or null.</p>
*
@ -326,9 +356,9 @@ public static boolean isBlank(final CharSequence cs) {
public static boolean isNotBlank(final CharSequence cs) {
return !isBlank(cs);
}
/**
* <p>Checks if any one of the CharSequences are blank ("") or null and not whitespace only..</p>
/**
* <p>Checks if any one of the CharSequences are blank ("") or null and not whitespace only.</p>
*
* <pre>
* StringUtils.isAnyBlank(null) = true
@ -356,9 +386,39 @@ public static boolean isAnyBlank(final CharSequence... css) {
}
return false;
}
/**
* <p>Checks if none of the CharSequences are blank ("") or null and whitespace only..</p>
* <p>Checks if any one of the CharSequences are not blank ("") or null and not whitespace only.</p>
*
* <pre>
* StringUtils.isAnyNotBlank(null) = false
* StringUtils.isAnyNotBlank(null, "foo") = true
* StringUtils.isAnyNotBlank(null, null) = false
* StringUtils.isAnyNotBlank("", "bar") = true
* StringUtils.isAnyNotBlank("bob", "") = true
* StringUtils.isAnyNotBlank(" bob ", null) = true
* StringUtils.isAnyNotBlank(" ", "bar") = true
* StringUtils.isAnyNotBlank("foo", "bar") = false
* </pre>
*
* @param css the CharSequences to check, may be null or empty
* @return {@code true} if any of the CharSequences are not blank or null or whitespace only
* @since 3.6
*/
public static boolean isAnyNotBlank(final CharSequence... css) {
if (ArrayUtils.isEmpty(css)) {
return true;
}
for (final CharSequence cs : css) {
if (isNotBlank(cs)) {
return true;
}
}
return false;
}
/**
* <p>Checks if none of the CharSequences are blank ("") or null and whitespace only.</p>
*
* <pre>
* StringUtils.isNoneBlank(null) = false

View File

@ -56,6 +56,19 @@ public void testIsAnyEmpty() {
assertFalse(StringUtils.isAnyEmpty("foo", "bar"));
}
@Test
public void testIsAnyNotEmpty() {
assertFalse(StringUtils.isAnyNotEmpty((String) null));
assertTrue(StringUtils.isAnyNotEmpty((String[]) null));
assertTrue(StringUtils.isAnyNotEmpty(null, "foo"));
assertTrue(StringUtils.isAnyNotEmpty("", "bar"));
assertTrue(StringUtils.isAnyNotEmpty("bob", ""));
assertTrue(StringUtils.isAnyNotEmpty(" bob ", null));
assertTrue(StringUtils.isAnyNotEmpty(" ", "bar"));
assertTrue(StringUtils.isAnyNotEmpty("foo", "bar"));
assertFalse(StringUtils.isAnyNotEmpty("", null));
}
@Test
public void testIsNoneEmpty() {
assertFalse(StringUtils.isNoneEmpty((String) null));
@ -99,6 +112,19 @@ public void testIsAnyBlank() {
assertFalse(StringUtils.isAnyBlank("foo", "bar"));
}
@Test
public void testIsAnyNotBlank() {
assertFalse(StringUtils.isAnyNotBlank((String) null));
assertTrue(StringUtils.isAnyNotBlank((String[]) null));
assertTrue(StringUtils.isAnyNotBlank(null, "foo"));
assertFalse(StringUtils.isAnyNotBlank(null, null));
assertTrue(StringUtils.isAnyNotBlank("", "bar"));
assertTrue(StringUtils.isAnyNotBlank("bob", ""));
assertTrue(StringUtils.isAnyNotBlank(" bob ", null));
assertTrue(StringUtils.isAnyNotBlank(" ", "bar"));
assertTrue(StringUtils.isAnyNotBlank("foo", "bar"));
}
@Test
public void testIsNoneBlank() {
assertFalse(StringUtils.isNoneBlank((String) null));