Patch from Fredrik Westermarck <fredrik.westermarck@mdh.se> that adds the
containsOnly method. I added a couple more unit tests and modified the code slightly. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137047 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
aeee2518e4
commit
d788e182bc
|
@ -81,7 +81,8 @@ import java.util.Iterator;
|
|||
* @author <a href="mailto:ed@apache.org">Ed Korthof</a>
|
||||
* @author <a href="mailto:rand_mcneely@yahoo.com>Rand McNeely</a>
|
||||
* @author <a href="mailto:scolebourne@joda.org>Stephen Colebourne</a>
|
||||
* @version $Id: StringUtils.java,v 1.10 2002/08/31 19:05:32 bayard Exp $
|
||||
* @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
|
||||
* @version $Id: StringUtils.java,v 1.11 2002/09/19 06:58:13 bayard Exp $
|
||||
*/
|
||||
public class StringUtils {
|
||||
|
||||
|
@ -1547,6 +1548,34 @@ public class StringUtils {
|
|||
return d[n][m];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the String contains only certain chars.
|
||||
*
|
||||
* @param str the String to check
|
||||
* @param valid an array of valid chars
|
||||
* @return true if it only contains valid chars and is non-null
|
||||
*/
|
||||
public static boolean containsOnly(String str, char[] valid) {
|
||||
if(str == null || valid == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int strSize = str.length();
|
||||
int validSize = valid.length;
|
||||
|
||||
for(int i=0; i<strSize; i++) {
|
||||
boolean contains = false;
|
||||
for(int j=0; j<validSize; j++) {
|
||||
if(valid[j] == str.charAt(i)) {
|
||||
contains = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!contains) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -67,7 +67,8 @@ import junit.textui.TestRunner;
|
|||
* @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a>
|
||||
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
|
||||
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
|
||||
* @version $Id: StringUtilsTest.java,v 1.3 2002/07/26 01:40:11 dlr Exp $
|
||||
* @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
|
||||
* @version $Id: StringUtilsTest.java,v 1.4 2002/09/19 06:58:13 bayard Exp $
|
||||
*/
|
||||
public class StringUtilsTest extends TestCase
|
||||
{
|
||||
|
@ -327,5 +328,30 @@ public class StringUtilsTest extends TestCase
|
|||
1, StringUtils.getLevenshteinDistance("hello", "hallo") );
|
||||
}
|
||||
|
||||
public void testContainsOnly() {
|
||||
String str1 = "a";
|
||||
String str2 = "b";
|
||||
String str3 = "ab";
|
||||
char[] chars1= {'b'};
|
||||
char[] chars2= {'a'};
|
||||
char[] chars3= {'a', 'b'};
|
||||
char[] emptyChars = new char[0];
|
||||
assertEquals("containsOnly(null, null) failed", false, StringUtils.containsOnly(null, null));
|
||||
assertEquals("containsOnly(empty-string, null) failed", false, StringUtils.containsOnly("", null));
|
||||
assertEquals("containsOnly(null, empty-string) failed", false, StringUtils.containsOnly(null, emptyChars));
|
||||
assertEquals("containsOnly(str1, empty-char-array) failed", false, StringUtils.containsOnly(str1, emptyChars));
|
||||
assertEquals("containsOnly(empty-string, chars1) failed", true, StringUtils.containsOnly("", chars1));
|
||||
assertEquals("containsOnly(str1, chars1) failed", false, StringUtils.containsOnly(str1, chars1));
|
||||
assertEquals("containsOnly(str1, chars1) failed", false, StringUtils.containsOnly(str1, chars1));
|
||||
assertEquals("containsOnly(str1, chars1) success", true, StringUtils.containsOnly(str1, chars2));
|
||||
assertEquals("containsOnly(str1, chars1) success", true, StringUtils.containsOnly(str1, chars3));
|
||||
assertEquals("containsOnly(str2, chars2) success", true, StringUtils.containsOnly(str2, chars1));
|
||||
assertEquals("containsOnly(str2, chars2) failed", false, StringUtils.containsOnly(str2, chars2));
|
||||
assertEquals("containsOnly(str2, chars2) success", true, StringUtils.containsOnly(str2, chars3));
|
||||
assertEquals("containsOnly(String3, chars3) failed", false, StringUtils.containsOnly(str3, chars1));
|
||||
assertEquals("containsOnly(String3, chars3) failed", false, StringUtils.containsOnly(str3, chars2));
|
||||
assertEquals("containsOnly(String3, chars3) success", true, StringUtils.containsOnly(str3, chars3));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue