diff --git a/src/java/org/apache/commons/lang/CharSet.java b/src/java/org/apache/commons/lang/CharSet.java index e2779c241..d9e1e26b6 100644 --- a/src/java/org/apache/commons/lang/CharSet.java +++ b/src/java/org/apache/commons/lang/CharSet.java @@ -58,13 +58,12 @@ import java.util.LinkedList; import java.util.List; /** - *
A set of characters. You can iterate over the characters in the - * set.
+ *A set of characters.
* * @author Henri Yandell * @author Stephen Colebourne * @since 1.0 - * @version $Id: CharSet.java,v 1.8 2003/03/23 17:59:09 scolebourne Exp $ + * @version $Id: CharSet.java,v 1.9 2003/07/31 21:32:47 scolebourne Exp $ */ public class CharSet { @@ -75,9 +74,18 @@ public class CharSet { * *Use the factory method * {@link CharSetUtils#evaluateSet(java.lang.String[])}.
+ */ + protected CharSet(String set) { + add(set); + } + + /** + *Restricted constructor.
* - * @throws NullPointerException if any of set[i] isnull
- * or if set is null
+ * Use the factory method + * {@link CharSetUtils#evaluateSet(java.lang.String[])}.
+ * + * @throws NullPointerException if set isnull
*/
protected CharSet(String[] set) {
int sz = set.length;
@@ -116,31 +124,33 @@ public class CharSet {
* Add a set definition string to the CharSet
.
str
is null
*/
protected void add(String str) {
+ if (str == null) {
+ return;
+ }
int sz = str.length();
CharRange range = null;
- if("-".equals(str)) {
+ if ("-".equals(str)) {
range = new CharRange('-');
set.add(range);
return;
- }
+ }
boolean end = false;
boolean negated = false;
- for(int i=0; iCharSet
.
*
- * This class tries to handle null
input gracefully.
- * An exception will generally not be thrown for a null
input.
+ *
This class handles null
input gracefully.
+ * An exception will not be thrown for a null
input.
* Each method documents its behaviour in more detail.
Creates a CharSetUtils
object which allows a certain amount of
* set logic to be performed.
+ * CharSetUtils.evaluateSet(null) = null + * CharSetUtils.evaluateSet("") = CharSet matching nothing + * CharSetUtils.evaluateSet("a-e") = CharSet matching a,b,c,d,e + ** - * @param set the set, must not be null - * @return a CharSet instance - * @throws NullPointerException if any of set[i] is null or if set is null + * @param set the set, may be null + * @return a CharSet instance,
null
if null input
+ */
+ public static CharSet evaluateSet(String set) {
+ if (set == null) {
+ return null;
+ }
+ return new CharSet(new String[] {set});
+ }
+
+ /**
+ * Creates a CharSetUtils
object which allows a certain amount of
+ * set logic to be performed.
The syntax is:
+ *+ * CharSetUtils.evaluateSet(null) = null + * CharSetUtils.evaluateSet([]) = CharSet matching nothing + * CharSetUtils.evaluateSet(["a-e"]) = CharSet matching a,b,c,d,e + *+ * + * @param set the set, may be null + * @return a CharSet instance,
null
if null input
*/
public static CharSet evaluateSet(String[] set) {
+ if (set == null) {
+ return null;
+ }
return new CharSet(set);
}
+ //-----------------------------------------------------------------------
/**
* Squeezes any repititions of a character that is mentioned in the * supplied set.
* - *An example is:
- *+ * CharSetUtils.squeeze(null, *) = null + * CharSetUtils.squeeze("", *) = "" + * CharSetUtils.squeeze(*, null) = * + * CharSetUtils.squeeze(*, "") = * + * CharSetUtils.squeeze("hello", "k-p") = "helo" + * CharSetUtils.squeeze("hello", "a-e") = "hello" + ** + * @see #evaluateSet(java.lang.String[]) for set-syntax. * @param str the string to squeeze, may be null - * @param set the character set to use for manipulation, must not be null + * @param set the character set to use for manipulation, may be null * @return modified String,
null
if null string input
- * @throws NullPointerException if set
is null
*/
public static String squeeze(String str, String set) {
- if (str == null) {
- return null;
+ if (str == null || str.length() == 0 || set == null || set.length() == 0) {
+ return str;
}
String[] strs = new String[1];
strs[0] = set;
@@ -128,17 +168,15 @@ public class CharSetUtils {
* null
if null string input
- * @throws NullPointerException if set
is null
- * or any element is null
*/
public static String squeeze(String str, String[] set) {
- if (str == null) {
- return null;
+ if (str == null || str.length() == 0 || set == null || set.length == 0) {
+ return str;
}
CharSet chars = evaluateSet(set);
StringBuffer buffer = new StringBuffer(str.length());
@@ -159,22 +197,27 @@ public class CharSetUtils {
return buffer.toString();
}
+ //-----------------------------------------------------------------------
/**
* Takes an argument in set-syntax, see evaluateSet, * and returns the number of characters present in the specified string.
* - *An example would be:
- *+ * CharSetUtils.count(null, *) = 0 + * CharSetUtils.count("", *) = 0 + * CharSetUtils.count(*, null) = 0 + * CharSetUtils.count(*, "") = 0 + * CharSetUtils.count("hello", "k-p") = 3 + * CharSetUtils.count("hello", "a-e") = 1 + ** + * @see #evaluateSet(java.lang.String[]) for set-syntax. * @param str String to count characters in, may be null - * @param set String set of characters to count, must not be null + * @param set String set of characters to count, may be null * @return character count, zero if null string input - * @throws NullPointerException if
set
is null
*/
public static int count(String str, String set) {
- if (str == null) {
+ if (str == null || str.length() == 0 || set == null || set.length() == 0) {
return 0;
}
String[] strs = new String[1];
@@ -191,14 +234,13 @@ public class CharSetUtils {
* set
is null
- * or any element is null
*/
public static int count(String str, String[] set) {
- if (str == null) {
+ if (str == null || str.length() == 0 || set == null || set.length == 0) {
return 0;
}
CharSet chars = evaluateSet(set);
@@ -213,24 +255,32 @@ public class CharSetUtils {
return count;
}
+ //-----------------------------------------------------------------------
/**
* Takes an argument in set-syntax, see evaluateSet, * and keeps any of characters present in the specified string.
* - *An example would be:
- *+ * CharSetUtils.keep(null, *) = null + * CharSetUtils.keep("", *) = "" + * CharSetUtils.keep(*, null) = "" + * CharSetUtils.keep(*, "") = "" + * CharSetUtils.keep("hello", "hl") = "hll" + * CharSetUtils.keep("hello", "le") = "ell" + ** + * @see #evaluateSet(java.lang.String[]) for set-syntax. * @param str String to keep characters from, may be null - * @param set String set of characters to keep, must not be null + * @param set String set of characters to keep, may be null * @return modified String,
null
if null string input
- * @throws NullPointerException if set
is null
*/
public static String keep(String str, String set) {
if (str == null) {
return null;
}
+ if (str.length() == 0 || set == null || set.length() == 0) {
+ return "";
+ }
String[] strs = new String[1];
strs[0] = set;
return keep(str, strs);
@@ -246,36 +296,43 @@ public class CharSetUtils {
* returns "hll"
*
*
+ * @see #evaluateSet(java.lang.String[]) for set-syntax.
* @param str String to keep characters from, may be null
- * @param set String[] set of characters to keep, must not be null
+ * @param set String[] set of characters to keep, may be null
* @return modified String, null
if null string input
- * @throws NullPointerException if set
is null
- * or any element is null
*/
public static String keep(String str, String[] set) {
if (str == null) {
return null;
}
+ if (str.length() == 0 || set == null || set.length == 0) {
+ return "";
+ }
return modify(str, set, true);
}
+ //-----------------------------------------------------------------------
/**
* Takes an argument in set-syntax, see evaluateSet, * and deletes any of characters present in the specified string.
* - *An example would be:
- *+ * CharSetUtils.delete(null, *) = null + * CharSetUtils.delete("", *) = "" + * CharSetUtils.delete(*, null) = * + * CharSetUtils.delete(*, "") = * + * CharSetUtils.delete("hello", "hl") = "hll" + * CharSetUtils.delete("hello", "le") = "ell" + ** + * @see #evaluateSet(java.lang.String[]) for set-syntax. * @param str String to delete characters from, may be null - * @param set String set of characters to delete, must not be null + * @param set String set of characters to delete, may be null * @return modified String,
null
if null string input
- * @throws NullPointerException if set
is null
*/
public static String delete(String str, String set) {
- if (str == null) {
- return null;
+ if (str == null || str.length() == 0 || set == null || set.length() == 0) {
+ return str;
}
String[] strs = new String[1];
strs[0] = set;
@@ -292,19 +349,19 @@ public class CharSetUtils {
* "hll"
*
*
+ * @see #evaluateSet(java.lang.String[]) for set-syntax.
* @param str String to delete characters from, may be null
- * @param set String[] set of characters to delete, must not be null
+ * @param set String[] set of characters to delete, may be null
* @return modified String, null
if null string input
- * @throws NullPointerException if set
is null
- * or any element is null
*/
public static String delete(String str, String[] set) {
- if (str == null) {
- return null;
+ if (str == null || str.length() == 0 || set == null || set.length == 0) {
+ return str;
}
return modify(str, set, false);
}
+ //-----------------------------------------------------------------------
// Implementation of delete and keep
private static String modify(String str, String[] set, boolean expect) {
CharSet chars = evaluateSet(set);
@@ -319,6 +376,7 @@ public class CharSetUtils {
return buffer.toString();
}
+ //-----------------------------------------------------------------------
/**
* Translate characters in a String. * This is a multi character search and replace routine.
diff --git a/src/test/org/apache/commons/lang/CharSetUtilsTest.java b/src/test/org/apache/commons/lang/CharSetUtilsTest.java index 83079a515..c7d5de5ad 100644 --- a/src/test/org/apache/commons/lang/CharSetUtilsTest.java +++ b/src/test/org/apache/commons/lang/CharSetUtilsTest.java @@ -67,7 +67,7 @@ import junit.textui.TestRunner; * @author Henri Yandell * @author Ringo De Smet * @author Stephen Colebourne - * @version $Id: CharSetUtilsTest.java,v 1.10 2003/07/30 22:21:39 scolebourne Exp $ + * @version $Id: CharSetUtilsTest.java,v 1.11 2003/07/31 21:32:47 scolebourne Exp $ */ public class CharSetUtilsTest extends TestCase { @@ -104,72 +104,167 @@ public class CharSetUtilsTest extends TestCase { } //----------------------------------------------------------------------- - public void testSqueeze() { - assertEquals(null, CharSetUtils.squeeze(null, (String[]) null)); + public void testEvaluateSet_String() { + assertEquals(null, CharSetUtils.evaluateSet((String) null)); + assertEquals("[]", CharSetUtils.evaluateSet("").toString()); + assertEquals("[a-e]", CharSetUtils.evaluateSet("a-e").toString()); + } + + public void testEvaluateSet_Stringarray() { + assertEquals(null, CharSetUtils.evaluateSet((String[]) null)); + assertEquals("[]", CharSetUtils.evaluateSet(new String[0]).toString()); + assertEquals("[]", CharSetUtils.evaluateSet(new String[] {null}).toString()); + assertEquals("[a-e]", CharSetUtils.evaluateSet(new String[] {"a-e"}).toString()); + } + + //----------------------------------------------------------------------- + public void testSqueeze_StringString() { assertEquals(null, CharSetUtils.squeeze(null, (String) null)); - assertEquals(null, CharSetUtils.squeeze(null, new String[] { "el" })); - assertEquals("helo", CharSetUtils.squeeze("hello", new String[] { "el" })); + assertEquals(null, CharSetUtils.squeeze(null, "")); + + assertEquals("", CharSetUtils.squeeze("", (String) null)); + assertEquals("", CharSetUtils.squeeze("", "")); + assertEquals("", CharSetUtils.squeeze("", "a-e")); + + assertEquals("hello", CharSetUtils.squeeze("hello", (String) null)); assertEquals("hello", CharSetUtils.squeeze("hello", "")); - assertEquals("", CharSetUtils.squeeze("", new String[] { "el" })); + assertEquals("hello", CharSetUtils.squeeze("hello", "a-e")); + assertEquals("helo", CharSetUtils.squeeze("hello", "l-p")); + } + + public void testSqueeze_StringStringarray() { + assertEquals(null, CharSetUtils.squeeze(null, (String[]) null)); + assertEquals(null, CharSetUtils.squeeze(null, new String[0])); + assertEquals(null, CharSetUtils.squeeze(null, new String[] {null})); + assertEquals(null, CharSetUtils.squeeze(null, new String[] {"el"})); + + assertEquals("", CharSetUtils.squeeze("", (String[]) null)); + assertEquals("", CharSetUtils.squeeze("", new String[0])); + assertEquals("", CharSetUtils.squeeze("", new String[] {null})); + assertEquals("", CharSetUtils.squeeze("", new String[] {"a-e"})); + + assertEquals("hello", CharSetUtils.squeeze("hello", (String[]) null)); + assertEquals("hello", CharSetUtils.squeeze("hello", new String[0])); + assertEquals("hello", CharSetUtils.squeeze("hello", new String[] {null})); + assertEquals("hello", CharSetUtils.squeeze("hello", new String[] {"a-e"})); + + assertEquals("helo", CharSetUtils.squeeze("hello", new String[] { "el" })); assertEquals("hello", CharSetUtils.squeeze("hello", new String[] { "e" })); assertEquals("fofof", CharSetUtils.squeeze("fooffooff", new String[] { "of" })); assertEquals("fof", CharSetUtils.squeeze("fooooff", new String[] { "fo" })); - try { - CharSetUtils.squeeze("hello", (String[]) null); - fail("Expecting NullPointerException"); - } catch (NullPointerException ex) {} - try { - CharSetUtils.squeeze("hello", new String[] { "", null }); - fail("Expecting NullPointerException"); - } catch (NullPointerException ex) {} } - public void testCount() { - assertEquals(0, CharSetUtils.count(null, (String[]) null)); + //----------------------------------------------------------------------- + public void testCount_StringString() { assertEquals(0, CharSetUtils.count(null, (String) null)); - assertEquals(0, CharSetUtils.count(null, new String[] { "el" })); + assertEquals(0, CharSetUtils.count(null, "")); + + assertEquals(0, CharSetUtils.count("", (String) null)); + assertEquals(0, CharSetUtils.count("", "")); + assertEquals(0, CharSetUtils.count("", "a-e")); + + assertEquals(0, CharSetUtils.count("hello", (String) null)); + assertEquals(0, CharSetUtils.count("hello", "")); + assertEquals(1, CharSetUtils.count("hello", "a-e")); + assertEquals(3, CharSetUtils.count("hello", "l-p")); + } + + public void testCount_StringStringarray() { + assertEquals(0, CharSetUtils.count(null, (String[]) null)); + assertEquals(0, CharSetUtils.count(null, new String[0])); + assertEquals(0, CharSetUtils.count(null, new String[] {null})); + assertEquals(0, CharSetUtils.count(null, new String[] {"a-e"})); + + assertEquals(0, CharSetUtils.count("", (String[]) null)); + assertEquals(0, CharSetUtils.count("", new String[0])); + assertEquals(0, CharSetUtils.count("", new String[] {null})); + assertEquals(0, CharSetUtils.count("", new String[] {"a-e"})); + + assertEquals(0, CharSetUtils.count("hello", (String[]) null)); + assertEquals(0, CharSetUtils.count("hello", new String[0])); + assertEquals(0, CharSetUtils.count("hello", new String[] {null})); + assertEquals(1, CharSetUtils.count("hello", new String[] {"a-e"})); + assertEquals(3, CharSetUtils.count("hello", new String[] { "el" })); - assertEquals(0, CharSetUtils.count("", new String[] { "el" })); assertEquals(0, CharSetUtils.count("hello", new String[] { "x" })); assertEquals(2, CharSetUtils.count("hello", new String[] { "e-i" })); assertEquals(5, CharSetUtils.count("hello", new String[] { "a-z" })); assertEquals(0, CharSetUtils.count("hello", new String[] { "" })); - assertEquals(0, CharSetUtils.count("hello", "")); - try { - CharSetUtils.count("hello", (String[]) null); - fail("Expecting NullPointerException"); - } catch (NullPointerException ex) {} - try { - CharSetUtils.count("hello", new String[] { "", null }); - fail("Expecting NullPointerException"); - } catch (NullPointerException ex) {} } - public void testKeep() { - assertEquals(null, CharSetUtils.keep(null, (String[]) null)); + //----------------------------------------------------------------------- + public void testKeep_StringString() { assertEquals(null, CharSetUtils.keep(null, (String) null)); - assertEquals(null, CharSetUtils.keep(null, new String[] { "el" })); + assertEquals(null, CharSetUtils.keep(null, "")); + + assertEquals("", CharSetUtils.keep("", (String) null)); + assertEquals("", CharSetUtils.keep("", "")); + assertEquals("", CharSetUtils.keep("", "a-e")); + + assertEquals("", CharSetUtils.keep("hello", (String) null)); + assertEquals("", CharSetUtils.keep("hello", "")); + assertEquals("", CharSetUtils.keep("hello", "xyz")); + assertEquals("hello", CharSetUtils.keep("hello", "a-z")); + assertEquals("hello", CharSetUtils.keep("hello", "oleh")); + assertEquals("ell", CharSetUtils.keep("hello", "el")); + } + + public void testKeep_StringStringarray() { + assertEquals(null, CharSetUtils.keep(null, (String[]) null)); + assertEquals(null, CharSetUtils.keep(null, new String[0])); + assertEquals(null, CharSetUtils.keep(null, new String[] {null})); + assertEquals(null, CharSetUtils.keep(null, new String[] {"a-e"})); + + assertEquals("", CharSetUtils.keep("", (String[]) null)); + assertEquals("", CharSetUtils.keep("", new String[0])); + assertEquals("", CharSetUtils.keep("", new String[] {null})); + assertEquals("", CharSetUtils.keep("", new String[] {"a-e"})); + + assertEquals("", CharSetUtils.keep("hello", (String[]) null)); + assertEquals("", CharSetUtils.keep("hello", new String[0])); + assertEquals("", CharSetUtils.keep("hello", new String[] {null})); + assertEquals("e", CharSetUtils.keep("hello", new String[] {"a-e"})); + + assertEquals("e", CharSetUtils.keep("hello", new String[] { "a-e" })); assertEquals("ell", CharSetUtils.keep("hello", new String[] { "el" })); assertEquals("hello", CharSetUtils.keep("hello", new String[] { "elho" })); - assertEquals("", CharSetUtils.keep("hello", new String[] { "" })); - assertEquals("", CharSetUtils.keep("hello", "")); assertEquals("hello", CharSetUtils.keep("hello", new String[] { "a-z" })); assertEquals("----", CharSetUtils.keep("----", new String[] { "-" })); assertEquals("ll", CharSetUtils.keep("hello", new String[] { "l" })); - try { - CharSetUtils.keep("hello", (String[]) null); - fail("Expecting NullPointerException"); - } catch (NullPointerException ex) {} - try { - CharSetUtils.keep("hello", new String[] { "", null}); - fail("Expecting NullPointerException"); - } catch (NullPointerException ex) {} } - public void testDelete() { + //----------------------------------------------------------------------- + public void testDelete_StringString() { + assertEquals(null, CharSetUtils.delete(null, (String) null)); + assertEquals(null, CharSetUtils.delete(null, "")); + + assertEquals("", CharSetUtils.delete("", (String) null)); + assertEquals("", CharSetUtils.delete("", "")); + assertEquals("", CharSetUtils.delete("", "a-e")); + + assertEquals("hello", CharSetUtils.delete("hello", (String) null)); + assertEquals("hello", CharSetUtils.delete("hello", "")); + assertEquals("hllo", CharSetUtils.delete("hello", "a-e")); + assertEquals("he", CharSetUtils.delete("hello", "l-p")); + assertEquals("hello", CharSetUtils.delete("hello", "z")); + } + + public void testDelete_StringStringarray() { assertEquals(null, CharSetUtils.delete(null, (String[]) null)); - assertEquals(null, CharSetUtils.delete(null,(String) null)); - assertEquals(null, CharSetUtils.delete(null, new String[] { "el" })); + assertEquals(null, CharSetUtils.delete(null, new String[0])); + assertEquals(null, CharSetUtils.delete(null, new String[] {null})); + assertEquals(null, CharSetUtils.delete(null, new String[] {"el"})); + + assertEquals("", CharSetUtils.delete("", (String[]) null)); + assertEquals("", CharSetUtils.delete("", new String[0])); + assertEquals("", CharSetUtils.delete("", new String[] {null})); + assertEquals("", CharSetUtils.delete("", new String[] {"a-e"})); + + assertEquals("hello", CharSetUtils.delete("hello", (String[]) null)); + assertEquals("hello", CharSetUtils.delete("hello", new String[0])); + assertEquals("hello", CharSetUtils.delete("hello", new String[] {null})); + assertEquals("hello", CharSetUtils.delete("hello", new String[] {"xyz"})); + assertEquals("ho", CharSetUtils.delete("hello", new String[] { "el" })); assertEquals("", CharSetUtils.delete("hello", new String[] { "elho" })); assertEquals("hello", CharSetUtils.delete("hello", new String[] { "" })); @@ -177,16 +272,9 @@ public class CharSetUtilsTest extends TestCase { assertEquals("", CharSetUtils.delete("hello", new String[] { "a-z" })); assertEquals("", CharSetUtils.delete("----", new String[] { "-" })); assertEquals("heo", CharSetUtils.delete("hello", new String[] { "l" })); - try { - CharSetUtils.delete("hello", (String[]) null); - fail("Expecting NullPointerException"); - } catch (NullPointerException ex) {} - try { - CharSetUtils.delete("hello", new String[] { "-", null }); - fail("Expecting NullPointerException"); - } catch (NullPointerException ex) {} } + //----------------------------------------------------------------------- public void testTranslate() { assertEquals(null, CharSetUtils.translate(null, null, null)); assertEquals("", CharSetUtils.translate("","a", "b"));