From 077df4205a38d0e90693439b9ce46523458a8658 Mon Sep 17 00:00:00 2001 From: Henri Yandell Date: Mon, 13 Sep 2010 05:36:39 +0000 Subject: [PATCH] Adding t/f and y/n options to toBooleanObject(String). LANG-649 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@996421 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang3/BooleanUtils.java | 14 ++++++++++++++ .../org/apache/commons/lang3/BooleanUtilsTest.java | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/BooleanUtils.java b/src/main/java/org/apache/commons/lang3/BooleanUtils.java index 3d13d75df..567fdb356 100644 --- a/src/main/java/org/apache/commons/lang3/BooleanUtils.java +++ b/src/main/java/org/apache/commons/lang3/BooleanUtils.java @@ -557,6 +557,20 @@ public class BooleanUtils { return null; } switch (str.length()) { + case 1: { + char ch0 = str.charAt(0); + if ((ch0 == 'y' || ch0 == 'Y') || + (ch0 == 't' || ch0 == 'T')) + { + return Boolean.TRUE; + } + if ((ch0 == 'n' || ch0 == 'N') || + (ch0 == 'f' || ch0 == 'F')) + { + return Boolean.FALSE; + } + break; + } case 2: { char ch0 = str.charAt(0); char ch1 = str.charAt(1); diff --git a/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java b/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java index 561db20ab..447d655ce 100644 --- a/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/BooleanUtilsTest.java @@ -241,6 +241,17 @@ public class BooleanUtilsTest extends TestCase { assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("ON")); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("YES")); assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("TruE")); + assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("TruE")); + + assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("y")); + assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("Y")); + assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("t")); + assertEquals(Boolean.TRUE, BooleanUtils.toBooleanObject("T")); + assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("f")); + assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("F")); + assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("n")); + assertEquals(Boolean.FALSE, BooleanUtils.toBooleanObject("N")); + assertEquals(null, BooleanUtils.toBooleanObject("z")); assertEquals(null, BooleanUtils.toBooleanObject("ab")); assertEquals(null, BooleanUtils.toBooleanObject("yoo"));