commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brit...@apache.org
Subject [1/4] [lang] DOC: StringUtils.containsAny. ADD: containsAllWords to WordUtils
Date Thu, 07 May 2015 19:30:27 GMT
Repository: commons-lang
Updated Branches:
  refs/heads/master dee94449e -> 9604c8530


DOC: StringUtils.containsAny. ADD: containsAllWords to WordUtils


Project: http://git-wip-us.apache.org/repos/asf/commons-lang/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-lang/commit/54facb4f
Tree: http://git-wip-us.apache.org/repos/asf/commons-lang/tree/54facb4f
Diff: http://git-wip-us.apache.org/repos/asf/commons-lang/diff/54facb4f

Branch: refs/heads/master
Commit: 54facb4fd93c2ff59b3096c6e64b9be1b3c9cd88
Parents: 1cb5573
Author: Eduardo Martins <eduardo.martins@luizalabs.com>
Authored: Tue May 5 15:32:46 2015 -0300
Committer: Eduardo Martins <eduardo.martins@luizalabs.com>
Committed: Tue May 5 16:34:48 2015 -0300

----------------------------------------------------------------------
 .../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(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-lang/blob/54facb4f/src/main/java/org/apache/commons/lang3/StringUtils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java
index 279c472..aa7b23e 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
      * </pre>
      *

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/54facb4f/src/main/java/org/apache/commons/lang3/text/WordUtils.java
----------------------------------------------------------------------
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 ce21d92..b38cb0f 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;
+
 /**
  * <p>Operations on Strings that contain words.</p>
  * 
@@ -562,6 +565,42 @@ public class WordUtils {
         return new String(buf, 0, count);
     }
 
+    /**
+     * <p>Checks if the String contains all words in the given array.</p>
+     *
+     * <p>
+     * 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}.
+     * </p>
+     *
+     * <pre>
+     * 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
+     * </pre>
+     *
+     *
+     * @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.

http://git-wip-us.apache.org/repos/asf/commons-lang/blob/54facb4f/src/test/java/org/apache/commons/lang3/text/WordUtilsTest.java
----------------------------------------------------------------------
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 6be8ded..f905a52 100644
--- a/src/test/java/org/apache/commons/lang3/text/WordUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/text/WordUtilsTest.java
@@ -222,6 +222,25 @@ public class WordUtilsTest {
     }
 
     @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));
         assertEquals("", WordUtils.uncapitalize(""));


Mime
View raw message