From 54facb4fd93c2ff59b3096c6e64b9be1b3c9cd88 Mon Sep 17 00:00:00 2001 From: Eduardo Martins Date: Tue, 5 May 2015 15:32:46 -0300 Subject: [PATCH] DOC: StringUtils.containsAny. ADD: containsAllWords to WordUtils --- .../org/apache/commons/lang3/StringUtils.java | 2 +- .../apache/commons/lang3/text/WordUtils.java | 39 +++++++++++++++++++ .../commons/lang3/text/WordUtilsTest.java | 19 +++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 279c472b9..aa7b23e32 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -1685,7 +1685,7 @@ public class StringUtils { * StringUtils.containsAny("", *) = false * StringUtils.containsAny(*, null) = false * StringUtils.containsAny(*, []) = false - * StringUtils.containsAny("abcd", "ab", "cd") = false + * StringUtils.containsAny("abcd", "ab", "cd") = true * StringUtils.containsAny("abc", "d", "abc") = true * * diff --git a/src/main/java/org/apache/commons/lang3/text/WordUtils.java b/src/main/java/org/apache/commons/lang3/text/WordUtils.java index ce21d92b2..b38cb0fed 100644 --- a/src/main/java/org/apache/commons/lang3/text/WordUtils.java +++ b/src/main/java/org/apache/commons/lang3/text/WordUtils.java @@ -16,9 +16,12 @@ */ package org.apache.commons.lang3.text; +import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.SystemUtils; +import java.util.regex.Pattern; + /** *

Operations on Strings that contain words.

* @@ -562,6 +565,42 @@ public class WordUtils { return new String(buf, 0, count); } + /** + *

Checks if the String contains all words in the given array.

+ * + *

+ * A {@code null} String will return {@code false}. A {@code null, zero + * length search array or if one element of array is null will return {@code false}. + *

+ * + *
+     * WordUtils.containsAllWords(null, *)            = false
+     * WordUtils.containsAllWords("", *)              = false
+     * WordUtils.containsAllWords(*, null)            = false
+     * WordUtils.containsAllWords(*, [])              = false
+     * WordUtils.containsAllWords("abcd", "ab", "cd") = false
+     * WordUtils.containsAllWords("abc def", "def", "abc") = true
+     * 
+ * + * + * @param str The str to check, may be null + * @param words The array of String words to search for, may be null + * @return {@code true} if all search words are found, {@code false} otherwise + */ + public static boolean containsAllWords(String word, String... words) { + if (StringUtils.isEmpty(word) || ArrayUtils.isEmpty(words) + || (words.length == 1 && StringUtils.isBlank(words[0]))) { + return false; + } + for (String w : words) { + Pattern p = Pattern.compile(".*\\b" + w + "\\b.*"); + if (!p.matcher(word).matches()) { + return false; + } + } + return true; + } + //----------------------------------------------------------------------- /** * Is the character a delimiter. diff --git a/src/test/java/org/apache/commons/lang3/text/WordUtilsTest.java b/src/test/java/org/apache/commons/lang3/text/WordUtilsTest.java index 6be8ded79..f905a52c6 100644 --- a/src/test/java/org/apache/commons/lang3/text/WordUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/text/WordUtilsTest.java @@ -221,6 +221,25 @@ public class WordUtilsTest { assertEquals("I Am.fine", WordUtils.capitalizeFully("i am.fine", null) ); } + @Test + public void testContainsAllWords_StringString() { + assertFalse(WordUtils.containsAllWords(null, (String) null)); + assertFalse(WordUtils.containsAllWords(null, "")); + assertFalse(WordUtils.containsAllWords(null, "ab")); + + assertFalse(WordUtils.containsAllWords("", (String) null)); + assertFalse(WordUtils.containsAllWords("", "")); + assertFalse(WordUtils.containsAllWords("", "ab")); + + assertFalse(WordUtils.containsAllWords("foo", (String) null)); + assertFalse(WordUtils.containsAllWords("bar", "")); + assertFalse(WordUtils.containsAllWords("zzabyycdxx", "by")); + assertTrue(WordUtils.containsAllWords("lorem ipsum dolor sit amet", "ipsum", "lorem", "dolor")); + assertFalse(WordUtils.containsAllWords("lorem ipsum dolor sit amet", "ipsum", null, "lorem", "dolor")); + assertFalse(WordUtils.containsAllWords("ab", "b")); + assertFalse(WordUtils.containsAllWords("ab", "z")); + } + @Test public void testUncapitalize_String() { assertEquals(null, WordUtils.uncapitalize(null));