Merge branch 'LANG-1135'
LANG-1135: Add method containsAllWords to WordUtils. Thanks to Eduardo Martins.
This commit is contained in:
commit
9604c85306
|
@ -22,6 +22,7 @@
|
|||
<body>
|
||||
|
||||
<release version="3.5" date="tba" description="tba">
|
||||
<action issue="LANG-1135" type="add" dev="britter" due-to="Eduardo Martins">Add method containsAllWords to WordUtils</action>
|
||||
<action issue="LANG-1132" type="add" dev="britter" due-to="Jack Tan">ReflectionToStringBuilder doesn't throw IllegalArgumentException when the constructor's object param is null</action>
|
||||
<action issue="LANG-1122" type="fix" dev="britter" due-to="Adrian Ber">Inconsistent behavior of swap for malformed inputs</action>
|
||||
<action issue="LANG-701" type="add" dev="britter" due-to="James Sawle">StringUtils join with var args</action>
|
||||
|
|
|
@ -1685,8 +1685,8 @@ public class StringUtils {
|
|||
* StringUtils.containsAny("", *) = false
|
||||
* StringUtils.containsAny(*, null) = false
|
||||
* StringUtils.containsAny(*, []) = false
|
||||
* StringUtils.containsAny("abcd", "ab", "cd") = false
|
||||
* StringUtils.containsAny("abcd", "ab", null) = false
|
||||
* StringUtils.containsAny("abcd", "ab", "cd") = true
|
||||
* StringUtils.containsAny("abc", "d", "abc") = true
|
||||
* </pre>
|
||||
*
|
||||
|
|
|
@ -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>
|
||||
*
|
||||
|
@ -561,6 +564,45 @@ 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(CharSequence word, CharSequence... words) {
|
||||
if (StringUtils.isEmpty(word) || ArrayUtils.isEmpty(words)) {
|
||||
return false;
|
||||
}
|
||||
for (CharSequence w : words) {
|
||||
if (StringUtils.isBlank(w)) {
|
||||
return false;
|
||||
}
|
||||
Pattern p = Pattern.compile(".*\\b" + w + "\\b.*");
|
||||
if (!p.matcher(word).matches()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Is the character a delimiter.
|
||||
|
|
|
@ -219,6 +219,26 @@ 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("lorem ipsum null 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));
|
||||
|
|
Loading…
Reference in New Issue