diff --git a/src/changes/changes.xml b/src/changes/changes.xml index bf6b3fec2..4c3927bb1 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + BooleanUtils.toBoolean(String str) javadoc is not updated LocaleUtils test fails with new Locale "ja_JP_JP_#u-ca-japanese" of JDK7 StrSubstitutor does not support StringBuilder or CharSequence Method createNumber from NumberUtils doesn't work for floating point numbers other than Float diff --git a/src/main/java/org/apache/commons/lang3/BooleanUtils.java b/src/main/java/org/apache/commons/lang3/BooleanUtils.java index 9b8046efa..4bbeb625d 100644 --- a/src/main/java/org/apache/commons/lang3/BooleanUtils.java +++ b/src/main/java/org/apache/commons/lang3/BooleanUtils.java @@ -513,26 +513,33 @@ public class BooleanUtils { /** *

Converts a String to a Boolean.

* - *

{@code 'true'}, {@code 'on'} or {@code 'yes'} + *

{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} * (case insensitive) will return {@code true}. - * {@code 'false'}, {@code 'off'} or {@code 'no'} + * {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'} * (case insensitive) will return {@code false}. * Otherwise, {@code null} is returned.

* *

NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean.

* *
+     *   // N.B. case is not significant
      *   BooleanUtils.toBooleanObject(null)    = null
      *   BooleanUtils.toBooleanObject("true")  = Boolean.TRUE
+     *   BooleanUtils.toBooleanObject("T")     = Boolean.TRUE // i.e. T[RUE]
      *   BooleanUtils.toBooleanObject("false") = Boolean.FALSE
+     *   BooleanUtils.toBooleanObject("f")     = Boolean.FALSE // i.e. f[alse]
+     *   BooleanUtils.toBooleanObject("No")    = Boolean.FALSE
+     *   BooleanUtils.toBooleanObject("n")     = Boolean.FALSE // i.e. n[o]
      *   BooleanUtils.toBooleanObject("on")    = Boolean.TRUE
      *   BooleanUtils.toBooleanObject("ON")    = Boolean.TRUE
      *   BooleanUtils.toBooleanObject("off")   = Boolean.FALSE
      *   BooleanUtils.toBooleanObject("oFf")   = Boolean.FALSE
+     *   BooleanUtils.toBooleanObject("yes")   = Boolean.TRUE
+     *   BooleanUtils.toBooleanObject("Y")     = Boolean.TRUE // i.e. Y[ES]
      *   BooleanUtils.toBooleanObject("blue")  = null
      * 
* - * @param str the String to check + * @param str the String to check; upper and lower case are treated as the same * @return the Boolean value of the string, {@code null} if no match or {@code null} input */ public static Boolean toBooleanObject(final String str) { @@ -669,13 +676,13 @@ public class BooleanUtils { /** *

Converts a String to a boolean (optimised for performance).

* - *

{@code 'true'}, {@code 'on'} or {@code 'yes'} + *

{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'} * (case insensitive) will return {@code true}. Otherwise, * {@code false} is returned.

* *

This method performs 4 times faster (JDK1.4) than * {@code Boolean.valueOf(String)}. However, this method accepts - * 'on' and 'yes' as true values. + * 'on' and 'yes', 't', 'y' as true values. * *

      *   BooleanUtils.toBoolean(null)    = false
@@ -686,6 +693,10 @@ public class BooleanUtils {
      *   BooleanUtils.toBoolean("yes")   = true
      *   BooleanUtils.toBoolean("false") = false
      *   BooleanUtils.toBoolean("x gti") = false
+     *   BooleanUtils.toBooleanObject("y") = true
+     *   BooleanUtils.toBooleanObject("n") = false
+     *   BooleanUtils.toBooleanObject("t") = true
+     *   BooleanUtils.toBooleanObject("f") = false 
      * 
* * @param str the String to check diff --git a/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java b/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java index 4d104a0ee..b68979424 100644 --- a/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java @@ -272,13 +272,13 @@ public class BooleanUtilsTest { assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("TruE")); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("TruE")); - assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("y")); + assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("y")); // yes assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("Y")); - assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("t")); + assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("t")); // true assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("T")); - assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("f")); + assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("f")); // false assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("F")); - assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("n")); + assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("n")); // No assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("N")); assertEquals(null, BooleanUtils.toBooleanObject("z"));