From 397a2c8044188433e39c68e4c025f1f64119dde0 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Sat, 20 Feb 2021 23:05:24 +0100 Subject: [PATCH] true & false String constant (#714) * Add constant: * true && false * Yes && No * On && Off * Update BooleanUtils.java * Update BooleanUtils.java Co-authored-by: Gary Gregory --- .../apache/commons/lang3/BooleanUtils.java | 56 ++++++++++++++++--- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/BooleanUtils.java b/src/main/java/org/apache/commons/lang3/BooleanUtils.java index 6b0d9b051..1dfcdc229 100644 --- a/src/main/java/org/apache/commons/lang3/BooleanUtils.java +++ b/src/main/java/org/apache/commons/lang3/BooleanUtils.java @@ -30,6 +30,48 @@ */ public class BooleanUtils { + /** + * The false String {@code "false"}. + * + * @since 3.12 + */ + public static final String FALSE = "false"; + + /** + * The no String {@code "no"}. + * + * @since 3.12 + */ + public static final String NO = "no"; + + /** + * The off String {@code "off"}. + * + * @since 3.12 + */ + public static final String OFF = "off"; + + /** + * The on String {@code "on"}. + * + * @since 3.12 + */ + public static final String ON = "on"; + + /** + * The true String {@code "true"}. + * + * @since 3.12 + */ + public static final String TRUE = "true"; + + /** + * The yes String {@code "yes"}. + * + * @since 3.12 + */ + public static final String YES = "yes"; + /** *

Performs an 'and' operation on a set of booleans.

* @@ -654,7 +696,7 @@ public static Boolean toBooleanObject(final String str) { // 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") { + if (str == TRUE) { return Boolean.TRUE; } if (str == null) { @@ -970,7 +1012,7 @@ public static String toString(final Boolean bool, final String trueString, final * @return {@code 'on'}, {@code 'off'}, or {@code null} */ public static String toStringOnOff(final boolean bool) { - return toString(bool, "on", "off"); + return toString(bool, ON, OFF); } /** @@ -987,7 +1029,7 @@ public static String toStringOnOff(final boolean bool) { * @return {@code 'on'}, {@code 'off'}, or {@code null} */ public static String toStringOnOff(final Boolean bool) { - return toString(bool, "on", "off", null); + return toString(bool, ON, OFF, null); } /** @@ -1003,7 +1045,7 @@ public static String toStringOnOff(final Boolean bool) { * @return {@code 'true'}, {@code 'false'}, or {@code null} */ public static String toStringTrueFalse(final boolean bool) { - return toString(bool, "true", "false"); + return toString(bool, TRUE, FALSE); } /** @@ -1020,7 +1062,7 @@ public static String toStringTrueFalse(final boolean bool) { * @return {@code 'true'}, {@code 'false'}, or {@code null} */ public static String toStringTrueFalse(final Boolean bool) { - return toString(bool, "true", "false", null); + return toString(bool, TRUE, FALSE, null); } /** @@ -1036,7 +1078,7 @@ public static String toStringTrueFalse(final Boolean bool) { * @return {@code 'yes'}, {@code 'no'}, or {@code null} */ public static String toStringYesNo(final boolean bool) { - return toString(bool, "yes", "no"); + return toString(bool, YES, NO); } /** @@ -1053,7 +1095,7 @@ public static String toStringYesNo(final boolean bool) { * @return {@code 'yes'}, {@code 'no'}, or {@code null} */ public static String toStringYesNo(final Boolean bool) { - return toString(bool, "yes", "no", null); + return toString(bool, YES, NO, null); } /**