From 3a68fe8b7e2aecaf75535415d2a7c8268345cd26 Mon Sep 17 00:00:00 2001
From: Arman Sharif Compares given Compares given string
to a CharSequences vararg of searchStrings
,
+ * returning {@code true} if the string
is equal to any of the searchStrings
.
+ * 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
+ *
+ *
+ * @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 searchStrings
;
+ * {@code false} if searchStrings
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;
+ }
+
+
+ /**
+ * string
to a CharSequences vararg of searchStrings
,
+ * returning {@code true} if the string
is equal to any of the searchStrings
, ignoring case.
+ * 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
+ *
+ *
+ * @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 searchStrings
;
+ * {@code false} if searchStrings
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
//-----------------------------------------------------------------------
/**
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
index b45a5f625..cf6b815fc 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java
@@ -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() {
From a06f83a7621f323012ddc69836cda1294a5e2114 Mon Sep 17 00:00:00 2001
From: Loic Guibert string
is equal to any of the searchStrings
.
+ * StringUtils.equalsAny(null, (CharSequence[]) null) = false * StringUtils.equalsAny(null, null, null) = true * StringUtils.equalsAny(null, "abc", "def") = false * StringUtils.equalsAny("abc", null, "def") = false @@ -1043,6 +1044,7 @@ public class StringUtils { * returning {@code true} if thestring
is equal to any of thesearchStrings
, ignoring case. * *+ * StringUtils.equalsAnyIgnoreCase(null, (CharSequence[]) null) = false * StringUtils.equalsAnyIgnoreCase(null, null, null) = true * StringUtils.equalsAnyIgnoreCase(null, "abc", "def") = false * StringUtils.equalsAnyIgnoreCase("abc", null, "def") = false diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java index cf6b815fc..8750de3f1 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsEqualsIndexOfTest.java @@ -595,6 +595,12 @@ public class StringUtilsEqualsIndexOfTest { 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 @@ -617,6 +623,10 @@ public class StringUtilsEqualsIndexOfTest { 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"))); } //----------------------------------------------------------------------- From 2c790ccc312535e679bd4a24e2065d0b7119e014 Mon Sep 17 00:00:00 2001 From: Loic GuibertDate: Fri, 6 Nov 2015 16:13:57 +0400 Subject: [PATCH 3/3] Add LANG-1169 to changes.xml --- src/changes/changes.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 432c77ce4..96f610820 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + Add StringUtils methods to compare a string to multiple strings Making replacePattern/removePattern methods null safe in StringUtils Add replace by regular expression methods in StringUtils Add compare methods in StringUtils