commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject cvs commit: jakarta-commons/lang/src/test/org/apache/commons/lang StringUtilsTest.java
Date Thu, 19 Sep 2002 06:58:13 GMT
bayard      2002/09/18 23:58:13

  Modified:    lang/src/java/org/apache/commons/lang StringUtils.java
               lang/src/test/org/apache/commons/lang StringUtilsTest.java
  Log:
  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.
  
  Revision  Changes    Path
  1.11      +31 -2     jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java
  
  Index: StringUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- StringUtils.java	31 Aug 2002 19:05:32 -0000	1.10
  +++ StringUtils.java	19 Sep 2002 06:58:13 -0000	1.11
  @@ -81,6 +81,7 @@
    * @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>
  + * @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
    * @version $Id$
    */
   public class StringUtils {
  @@ -1547,6 +1548,34 @@
           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;
  +    }
  +}
  
  
  
  1.4       +27 -1     jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java
  
  Index: StringUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- StringUtilsTest.java	26 Jul 2002 01:40:11 -0000	1.3
  +++ StringUtilsTest.java	19 Sep 2002 06:58:13 -0000	1.4
  @@ -67,6 +67,7 @@
    * @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>
  + * @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
    * @version $Id$
    */
   public class StringUtilsTest extends TestCase
  @@ -325,6 +326,31 @@
                        7, StringUtils.getLevenshteinDistance("hippo", "elephant") );
           assertEquals("getLevenshteinDistance(String, String) failed",
                        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));
       }
   
   }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message