Merge branch 'fix-LANG-1169'

LANG-1169: Add StringUtils methods to compare a string to multiple strings
This closes #109 from github.
Thanks to Arman Sharif for providing a patch.
Thanks to Rafal Glowinski for providing an alternative implementations, see LANG-704.
Thanks to Robert Parr for providing an alternative implementations, see LANG-704.


Conflicts:
	src/changes/changes.xml
This commit is contained in:
Loic Guibert 2015-11-06 16:21:32 +04:00
commit 481137553f
3 changed files with 115 additions and 0 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.5" date="tba" description="tba">
<action issue="LANG-1169" type="add" dev="lguibert" due-to="Rafal Glowinski, Robert Parr, Arman Sharif">Add StringUtils methods to compare a string to multiple strings</action>
<action issue="LANG-1185" type="add" dev="lguibert">Add remove by regular expression methods in StringUtils</action>
<action issue="LANG-1183" type="update" dev="lguibert">Making replacePattern/removePattern methods null safe in StringUtils</action>
<action issue="LANG-1139" type="add" dev="lguibert">Add replace by regular expression methods in StringUtils</action>

View File

@ -1008,6 +1008,67 @@ 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, (CharSequence[]) null) = false
* 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, (CharSequence[]) null) = false
* 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,59 @@ 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()));
assertFalse(StringUtils.equalsAny(null, (CharSequence[]) null));
assertTrue(StringUtils.equalsAny(FOO, new CustomCharSequence("foo")));
assertTrue(StringUtils.equalsAny(FOO, new StringBuilder("foo")));
assertFalse(StringUtils.equalsAny(FOO, new CustomCharSequence("fOo")));
assertFalse(StringUtils.equalsAny(FOO, new StringBuilder("fOo")));
}
@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()));
assertFalse(StringUtils.equalsAnyIgnoreCase(null, (CharSequence[]) null));
assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, new CustomCharSequence("fOo")));
assertTrue(StringUtils.equalsAnyIgnoreCase(FOO, new StringBuilder("fOo")));
}
//-----------------------------------------------------------------------
@Test
public void testCompare_StringString() {