You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/10/22 01:23:06 UTC

cvs commit: jakarta-commons/lang/src/java/org/apache/commons/lang BooleanUtils.java

scolebourne    2003/10/21 16:23:06

  Modified:    lang/src/test/org/apache/commons/lang BooleanUtilsTest.java
               lang/src/java/org/apache/commons/lang BooleanUtils.java
  Log:
  Improve performance of BooleanUtils.toBoolean(String)
  
  Revision  Changes    Path
  1.8       +31 -5     jakarta-commons/lang/src/test/org/apache/commons/lang/BooleanUtilsTest.java
  
  Index: BooleanUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/BooleanUtilsTest.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- BooleanUtilsTest.java	18 Aug 2003 02:22:25 -0000	1.7
  +++ BooleanUtilsTest.java	21 Oct 2003 23:23:06 -0000	1.8
  @@ -308,13 +308,39 @@
           assertEquals(false, BooleanUtils.toBoolean(""));
           assertEquals(false, BooleanUtils.toBoolean("off"));
           assertEquals(false, BooleanUtils.toBoolean("oof"));
  -        assertEquals(true, BooleanUtils.toBoolean("true"));
  -        assertEquals(true, BooleanUtils.toBoolean("yes"));
  -        assertEquals(true, BooleanUtils.toBoolean("on"));
  +        assertEquals(false, BooleanUtils.toBoolean("yep"));
  +        assertEquals(false, BooleanUtils.toBoolean("trux"));
  +        assertEquals(false, BooleanUtils.toBoolean("false"));
  +        assertEquals(false, BooleanUtils.toBoolean("a"));
  +        assertEquals(true, BooleanUtils.toBoolean("true")); // interned handled differently
  +        assertEquals(true, BooleanUtils.toBoolean(new StringBuffer("tr").append("ue").toString()));
  +        assertEquals(true, BooleanUtils.toBoolean("truE"));
  +        assertEquals(true, BooleanUtils.toBoolean("trUe"));
  +        assertEquals(true, BooleanUtils.toBoolean("trUE"));
  +        assertEquals(true, BooleanUtils.toBoolean("tRue"));
  +        assertEquals(true, BooleanUtils.toBoolean("tRuE"));
  +        assertEquals(true, BooleanUtils.toBoolean("tRUe"));
  +        assertEquals(true, BooleanUtils.toBoolean("tRUE"));
           assertEquals(true, BooleanUtils.toBoolean("TRUE"));
  +        assertEquals(true, BooleanUtils.toBoolean("TRUe"));
  +        assertEquals(true, BooleanUtils.toBoolean("TRuE"));
  +        assertEquals(true, BooleanUtils.toBoolean("TRue"));
  +        assertEquals(true, BooleanUtils.toBoolean("TrUE"));
  +        assertEquals(true, BooleanUtils.toBoolean("TrUe"));
  +        assertEquals(true, BooleanUtils.toBoolean("TruE"));
  +        assertEquals(true, BooleanUtils.toBoolean("True"));
  +        assertEquals(true, BooleanUtils.toBoolean("on"));
  +        assertEquals(true, BooleanUtils.toBoolean("oN"));
  +        assertEquals(true, BooleanUtils.toBoolean("On"));
           assertEquals(true, BooleanUtils.toBoolean("ON"));
  +        assertEquals(true, BooleanUtils.toBoolean("yes"));
  +        assertEquals(true, BooleanUtils.toBoolean("yeS"));
  +        assertEquals(true, BooleanUtils.toBoolean("yEs"));
  +        assertEquals(true, BooleanUtils.toBoolean("yES"));
  +        assertEquals(true, BooleanUtils.toBoolean("Yes"));
  +        assertEquals(true, BooleanUtils.toBoolean("YeS"));
  +        assertEquals(true, BooleanUtils.toBoolean("YEs"));
           assertEquals(true, BooleanUtils.toBoolean("YES"));
  -        assertEquals(true, BooleanUtils.toBoolean("TruE"));
       }
   
       public void test_toBoolean_String_String_String() {
  
  
  
  1.17      +71 -9     jakarta-commons/lang/src/java/org/apache/commons/lang/BooleanUtils.java
  
  Index: BooleanUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/BooleanUtils.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- BooleanUtils.java	23 Sep 2003 19:45:14 -0000	1.16
  +++ BooleanUtils.java	21 Oct 2003 23:23:06 -0000	1.17
  @@ -502,12 +502,14 @@
        * Otherwise, <code>null</code> is returned.</p>
        *
        * <pre>
  +     *   BooleanUtils.toBooleanObject(null)    = null
        *   BooleanUtils.toBooleanObject("true")  = Boolean.TRUE
        *   BooleanUtils.toBooleanObject("false") = Boolean.FALSE
        *   BooleanUtils.toBooleanObject("on")    = Boolean.TRUE
        *   BooleanUtils.toBooleanObject("ON")    = Boolean.TRUE
        *   BooleanUtils.toBooleanObject("off")   = Boolean.FALSE
        *   BooleanUtils.toBooleanObject("oFf")   = Boolean.FALSE
  +     *   BooleanUtils.toBooleanObject("blue")  = null
        * </pre>
        *
        * @param str  the String to check
  @@ -574,34 +576,94 @@
       // String to boolean methods
       //-----------------------------------------------------------------------
       /**
  -     * <p>Converts a String to a boolean.</p>
  +     * <p>Converts a String to a boolean (optimised for performance).</p>
        * 
        * <p><code>'true'</code>, <code>'on'</code> or <code>'yes'</code>
        * (case insensitive) will return <code>true</code>. Otherwise,
        * <code>false</code> is returned.</p>
  +     * 
  +     * <p>This method performs 4 times faster (JDK1.4) than
  +     * <code>Boolean.valueOf(String)</code>. However, this method accepts
  +     * 'on' and 'yes' as true values.
        *
        * <pre>
  +     *   BooleanUtils.toBoolean(null)    = false
        *   BooleanUtils.toBoolean("true")  = true
  +     *   BooleanUtils.toBoolean("TRUE")  = true
  +     *   BooleanUtils.toBoolean("tRUe")  = true
        *   BooleanUtils.toBoolean("on")    = true
        *   BooleanUtils.toBoolean("yes")   = true
        *   BooleanUtils.toBoolean("false") = false
  +     *   BooleanUtils.toBoolean("x gti") = false
        * </pre>
        *
        * @param str  the String to check
        * @return the boolean value of the string, <code>false</code> if no match
        */
       public static boolean toBoolean(String str) {
  -        if ("true".equalsIgnoreCase(str)) {
  -            return true;
  -        } else if ("on".equalsIgnoreCase(str)) {
  -            return true;
  -        } else if ("yes".equalsIgnoreCase(str)) {
  +        // Previously used equalsIgnoreCase, which was fast for interned 'true'.
  +        // Non interned 'true' matched 15 times slower.
  +        // 
  +        // Optimisation provides same performance as before for interned 'true'.
  +        // Similar performance for null, 'false', and other strings not length 2/3/4.
  +        // 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower.
  +        if (str == "true") {
               return true;
           }
  -        // no match
  +        if (str == null) {
  +            return false;
  +        }
  +        switch (str.length()) {
  +            case 2: {
  +                char ch0 = str.charAt(0);
  +                char ch1 = str.charAt(1);
  +                return 
  +                    (ch0 == 'o' || ch0 == 'O') &&
  +                    (ch1 == 'n' || ch1 == 'N');
  +            }
  +            case 3: {
  +                char ch = str.charAt(0);
  +                if (ch == 'y') {
  +                    return 
  +                        (str.charAt(1) == 'e' || str.charAt(1) == 'E') &&
  +                        (str.charAt(2) == 's' || str.charAt(2) == 'S');
  +                }
  +                if (ch == 'Y') {
  +                    return 
  +                        (str.charAt(1) == 'E' || str.charAt(1) == 'e') &&
  +                        (str.charAt(2) == 'S' || str.charAt(2) == 's');
  +                }
  +            }
  +            case 4: {
  +                char ch = str.charAt(0);
  +                if (ch == 't') {
  +                    return 
  +                        (str.charAt(1) == 'r' || str.charAt(1) == 'R') &&
  +                        (str.charAt(2) == 'u' || str.charAt(2) == 'U') &&
  +                        (str.charAt(3) == 'e' || str.charAt(3) == 'E');
  +                }
  +                if (ch == 'T') {
  +                    return 
  +                        (str.charAt(1) == 'R' || str.charAt(1) == 'r') &&
  +                        (str.charAt(2) == 'U' || str.charAt(2) == 'u') &&
  +                        (str.charAt(3) == 'E' || str.charAt(3) == 'e');
  +                }
  +            }
  +        }
           return false;
       }
  -
  +    
  +//    public static void main(String[] args) {
  +//        long start = System.currentTimeMillis();
  +//        boolean flag = true;
  +//        int count = 0;
  +//        for (int i = 0; i < 100000000; i++) {
  +//            flag = toBoolean("YES");
  +//        }
  +//        long end = System.currentTimeMillis();
  +//        System.out.println((end - start) + " " + flag + " " + count);
  +//    }
  +    
       /**
        * <p>Converts a String to a Boolean throwing an exception if no match found.</p>
        * 
  
  
  

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