Add a CharSetUtils.containsAny(String, String) per LANG-886
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1470130 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
90be7a25c6
commit
bb95f8a194
|
@ -22,6 +22,7 @@
|
|||
<body>
|
||||
|
||||
<release version="3.2" date="TBA" description="Next release">
|
||||
<action issue="LANG-886" type="add">Added CharSetUtils.containsAny(String, String)</action>
|
||||
<action issue="LANG-846" type="update">Provide CharSequenceUtils.regionMatches with a proper green implementation instead of inefficiently converting to Strings</action>
|
||||
<action issue="LANG-797" type="add">Added escape/unescapeJson to StringEscapeUtils</action>
|
||||
<action issue="LANG-875" type="add">Added appendIfMissing and prependIfMissing methods to StringUtils</action>
|
||||
|
|
|
@ -83,6 +83,39 @@ public class CharSetUtils {
|
|||
return buffer.toString();
|
||||
}
|
||||
|
||||
// ContainsAny
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* <p>Takes an argument in set-syntax, see evaluateSet,
|
||||
* and identifies whether any of the characters are present in the specified string.</p>
|
||||
*
|
||||
* <pre>
|
||||
* CharSetUtils.containsAny(null, *) = false
|
||||
* CharSetUtils.containsAny("", *) = false
|
||||
* CharSetUtils.containsAny(*, null) = false
|
||||
* CharSetUtils.containsAny(*, "") = false
|
||||
* CharSetUtils.containsAny("hello", "k-p") = true
|
||||
* CharSetUtils.containsAny("hello", "a-d") = false
|
||||
* </pre>
|
||||
*
|
||||
* @see CharSet#getInstance(java.lang.String...) for set-syntax.
|
||||
* @param str String to look for characters in, may be null
|
||||
* @param set String[] set of characters to identify, may be null
|
||||
* @return whether or not the characters in the set are in the primary string
|
||||
*/
|
||||
public static boolean containsAny(final String str, final String... set) {
|
||||
if (StringUtils.isEmpty(str) || deepEmpty(set)) {
|
||||
return false;
|
||||
}
|
||||
final CharSet chars = CharSet.getInstance(set);
|
||||
for (final char c : str.toCharArray()) {
|
||||
if (chars.contains(c)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Count
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
|
|
|
@ -85,6 +85,46 @@ public class CharSetUtilsTest {
|
|||
assertEquals("fof", CharSetUtils.squeeze("fooooff", new String[] { "fo" }));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@Test
|
||||
public void testContainsAny_StringString() {
|
||||
assertFalse(CharSetUtils.containsAny(null, (String) null));
|
||||
assertFalse(CharSetUtils.containsAny(null, ""));
|
||||
|
||||
assertFalse(CharSetUtils.containsAny("", (String) null));
|
||||
assertFalse(CharSetUtils.containsAny("", ""));
|
||||
assertFalse(CharSetUtils.containsAny("", "a-e"));
|
||||
|
||||
assertFalse(CharSetUtils.containsAny("hello", (String) null));
|
||||
assertFalse(CharSetUtils.containsAny("hello", ""));
|
||||
assertTrue(CharSetUtils.containsAny("hello", "a-e"));
|
||||
assertTrue(CharSetUtils.containsAny("hello", "l-p"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContainsAny_StringStringarray() {
|
||||
assertFalse(CharSetUtils.containsAny(null, (String[]) null));
|
||||
assertFalse(CharSetUtils.containsAny(null, new String[0]));
|
||||
assertFalse(CharSetUtils.containsAny(null, new String[] {null}));
|
||||
assertFalse(CharSetUtils.containsAny(null, new String[] {"a-e"}));
|
||||
|
||||
assertFalse(CharSetUtils.containsAny("", (String[]) null));
|
||||
assertFalse(CharSetUtils.containsAny("", new String[0]));
|
||||
assertFalse(CharSetUtils.containsAny("", new String[] {null}));
|
||||
assertFalse(CharSetUtils.containsAny("", new String[] {"a-e"}));
|
||||
|
||||
assertFalse(CharSetUtils.containsAny("hello", (String[]) null));
|
||||
assertFalse(CharSetUtils.containsAny("hello", new String[0]));
|
||||
assertFalse(CharSetUtils.containsAny("hello", new String[] {null}));
|
||||
assertTrue(CharSetUtils.containsAny("hello", new String[] {"a-e"}));
|
||||
|
||||
assertTrue(CharSetUtils.containsAny("hello", new String[] { "el" }));
|
||||
assertFalse(CharSetUtils.containsAny("hello", new String[] { "x" }));
|
||||
assertTrue(CharSetUtils.containsAny("hello", new String[] { "e-i" }));
|
||||
assertTrue(CharSetUtils.containsAny("hello", new String[] { "a-z" }));
|
||||
assertFalse(CharSetUtils.containsAny("hello", new String[] { "" }));
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@Test
|
||||
public void testCount_StringString() {
|
||||
|
|
Loading…
Reference in New Issue