LANG-1169 Added equalsAny() and equalsAnyIgnoreCase() methods to StringUtils.

This commit is contained in:
Arman Sharif 2015-09-02 17:09:06 -07:00
parent 47c1862989
commit 3a68fe8b7e
2 changed files with 102 additions and 0 deletions

View File

@ -1008,6 +1008,65 @@ public class StringUtils {
return str1.compareToIgnoreCase(str2);
}
/**
* <p>Compares given <code>string</code> to a CharSequences vararg of <code>searchStrings</code>,
* returning {@code true} if the <code>string</code> is equal to any of the <code>searchStrings</code>.</p>
*
* <pre>
* StringUtils.equalsAny(null, null, null) = true
* StringUtils.equalsAny(null, "abc", "def") = false
* StringUtils.equalsAny("abc", null, "def") = false
* StringUtils.equalsAny("abc", "abc", "def") = true
* StringUtils.equalsAny("abc", "ABC", "DEF") = false
* </pre>
*
* @param string to compare, may be {@code null}.
* @param searchStrings a vararg of strings, may be {@code null}.
* @return {@code true} if the string is equal (case-sensitive) to any other element of <code>searchStrings</code>;
* {@code false} if <code>searchStrings</code> is null or contains no matches.
* @since 3.5
*/
public static boolean equalsAny(final CharSequence string, final CharSequence... searchStrings) {
if (ArrayUtils.isNotEmpty(searchStrings)) {
for (CharSequence next : searchStrings) {
if (equals(string, next)) {
return true;
}
}
}
return false;
}
/**
* <p>Compares given <code>string</code> to a CharSequences vararg of <code>searchStrings</code>,
* returning {@code true} if the <code>string</code> is equal to any of the <code>searchStrings</code>, ignoring case.</p>
*
* <pre>
* StringUtils.equalsAnyIgnoreCase(null, null, null) = true
* StringUtils.equalsAnyIgnoreCase(null, "abc", "def") = false
* StringUtils.equalsAnyIgnoreCase("abc", null, "def") = false
* StringUtils.equalsAnyIgnoreCase("abc", "abc", "def") = true
* StringUtils.equalsAnyIgnoreCase("abc", "ABC", "DEF") = true
* </pre>
*
* @param string to compare, may be {@code null}.
* @param searchStrings a vararg of strings, may be {@code null}.
* @return {@code true} if the string is equal (case-insensitive) to any other element of <code>searchStrings</code>;
* {@code false} if <code>searchStrings</code> is null or contains no matches.
* @since 3.5
*/
public static boolean equalsAnyIgnoreCase(final CharSequence string, final CharSequence...searchStrings) {
if (ArrayUtils.isNotEmpty(searchStrings)) {
for (CharSequence next : searchStrings) {
if (equalsIgnoreCase(string, next)) {
return true;
}
}
}
return false;
}
// IndexOf
//-----------------------------------------------------------------------
/**

View File

@ -576,6 +576,49 @@ public class StringUtilsEqualsIndexOfTest {
assertFalse(StringUtils.equalsIgnoreCase("abcd","abcd "));
}
@Test
public void testEqualsAny() {
assertFalse(StringUtils.equalsAny(FOO));
assertFalse(StringUtils.equalsAny(FOO, new String[]{}));
assertTrue(StringUtils.equalsAny(FOO, FOO));
assertTrue(StringUtils.equalsAny(FOO, BAR, new String(new char[] { 'f', 'o', 'o' })));
assertFalse(StringUtils.equalsAny(FOO, BAR, new String(new char[] { 'f', 'O', 'O' })));
assertFalse(StringUtils.equalsAny(FOO, BAR));
assertFalse(StringUtils.equalsAny(FOO, BAR, null));
assertFalse(StringUtils.equalsAny(null, FOO));
assertFalse(StringUtils.equalsAny(FOO, FOOBAR));
assertFalse(StringUtils.equalsAny(FOOBAR, FOO));
assertTrue(StringUtils.equalsAny(null, null, null));
assertFalse(StringUtils.equalsAny(null, FOO, BAR, FOOBAR));
assertFalse(StringUtils.equalsAny(FOO, null, BAR));
assertTrue(StringUtils.equalsAny(FOO, BAR, null, "", FOO, BAR));
assertFalse(StringUtils.equalsAny(FOO, FOO.toUpperCase()));
}
@Test
public void testEqualsAnyIgnoreCase() {
assertFalse(StringUtils.equalsAnyIgnoreCase(FOO));
assertFalse(StringUtils.equalsAnyIgnoreCase(FOO, new String[]{}));
assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, FOO));
assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, FOO.toUpperCase()));
assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, FOO, new String(new char[]{'f', 'o', 'o'})));
assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, BAR, new String(new char[]{'f', 'O', 'O'})));
assertFalse(StringUtils.equalsAnyIgnoreCase(FOO, BAR));
assertFalse(StringUtils.equalsAnyIgnoreCase(FOO, BAR, null));
assertFalse(StringUtils.equalsAnyIgnoreCase(null, FOO));
assertFalse(StringUtils.equalsAnyIgnoreCase(FOO, FOOBAR));
assertFalse(StringUtils.equalsAnyIgnoreCase(FOOBAR, FOO));
assertTrue(StringUtils.equalsAnyIgnoreCase(null, null, null));
assertFalse(StringUtils.equalsAnyIgnoreCase(null, FOO, BAR, FOOBAR));
assertFalse(StringUtils.equalsAnyIgnoreCase(FOO, null, BAR));
assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, BAR, null, "", FOO.toUpperCase(), BAR));
assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, FOO.toUpperCase()));
}
//-----------------------------------------------------------------------
@Test
public void testCompare_StringString() {