一、前言
参考hutool的hutool-all包的cn.hutool.core.util.BooleanUtil布尔工具类,对"true"、 "yes"、 "y"、"t"、"ok"、"1"、 "on"、"是"、"对"、"真"等常用表达为true布尔值的转换处理。
二、代码说明
package cn.hutool.core.util;@b@@b@import cn.hutool.core.convert.Convert;@b@@b@public class BooleanUtil {@b@ private static final String[] TRUE_ARRAY = new String[]{"true", "yes", "y", "t", "ok", "1", "on", "是", "对", "真"};@b@@b@ public BooleanUtil() {@b@ }@b@@b@ public static Boolean negate(Boolean bool) {@b@ if (bool == null) {@b@ return null;@b@ } else {@b@ return bool ? Boolean.FALSE : Boolean.TRUE;@b@ }@b@ }@b@@b@ public static boolean isTrue(Boolean bool) {@b@ return Boolean.TRUE.equals(bool);@b@ }@b@@b@ public static boolean isFalse(Boolean bool) {@b@ return Boolean.FALSE.equals(bool);@b@ }@b@@b@ public static boolean negate(boolean bool) {@b@ return !bool;@b@ }@b@@b@ public static boolean toBoolean(String valueStr) {@b@ if (StrUtil.isNotBlank(valueStr)) {@b@ valueStr = valueStr.trim().toLowerCase();@b@ if (ArrayUtil.contains(TRUE_ARRAY, valueStr)) {@b@ return true;@b@ }@b@ }@b@@b@ return false;@b@ }@b@@b@ public static int toInt(boolean value) {@b@ return value ? 1 : 0;@b@ }@b@@b@ public static Integer toInteger(boolean value) {@b@ return toInt(value);@b@ }@b@@b@ public static char toChar(boolean value) {@b@ return (char)toInt(value);@b@ }@b@@b@ public static Character toCharacter(boolean value) {@b@ return toChar(value);@b@ }@b@@b@ public static byte toByte(boolean value) {@b@ return (byte)toInt(value);@b@ }@b@@b@ public static Byte toByteObj(boolean value) {@b@ return toByte(value);@b@ }@b@@b@ public static long toLong(boolean value) {@b@ return (long)toInt(value);@b@ }@b@@b@ public static Long toLongObj(boolean value) {@b@ return toLong(value);@b@ }@b@@b@ public static short toShort(boolean value) {@b@ return (short)toInt(value);@b@ }@b@@b@ public static Short toShortObj(boolean value) {@b@ return toShort(value);@b@ }@b@@b@ public static float toFloat(boolean value) {@b@ return (float)toInt(value);@b@ }@b@@b@ public static Float toFloatObj(boolean value) {@b@ return toFloat(value);@b@ }@b@@b@ public static double toDouble(boolean value) {@b@ return (double)toInt(value);@b@ }@b@@b@ public static Double toDoubleObj(boolean value) {@b@ return toDouble(value);@b@ }@b@@b@ public static String toStringTrueFalse(boolean bool) {@b@ return toString(bool, "true", "false");@b@ }@b@@b@ public static String toStringOnOff(boolean bool) {@b@ return toString(bool, "on", "off");@b@ }@b@@b@ public static String toStringYesNo(boolean bool) {@b@ return toString(bool, "yes", "no");@b@ }@b@@b@ public static String toString(boolean bool, String trueString, String falseString) {@b@ return bool ? trueString : falseString;@b@ }@b@@b@ public static boolean and(boolean... array) {@b@ if (ArrayUtil.isEmpty(array)) {@b@ throw new IllegalArgumentException("The Array must not be empty !");@b@ } else {@b@ boolean[] arr$ = array;@b@ int len$ = array.length;@b@@b@ for(int i$ = 0; i$ < len$; ++i$) {@b@ boolean element = arr$[i$];@b@ if (!element) {@b@ return false;@b@ }@b@ }@b@@b@ return true;@b@ }@b@ }@b@@b@ public static Boolean and(Boolean... array) {@b@ if (ArrayUtil.isEmpty(array)) {@b@ throw new IllegalArgumentException("The Array must not be empty !");@b@ } else {@b@ boolean[] primitive = (boolean[])Convert.convert(boolean[].class, array);@b@ return and(primitive);@b@ }@b@ }@b@@b@ public static boolean or(boolean... array) {@b@ if (ArrayUtil.isEmpty(array)) {@b@ throw new IllegalArgumentException("The Array must not be empty !");@b@ } else {@b@ boolean[] arr$ = array;@b@ int len$ = array.length;@b@@b@ for(int i$ = 0; i$ < len$; ++i$) {@b@ boolean element = arr$[i$];@b@ if (element) {@b@ return true;@b@ }@b@ }@b@@b@ return false;@b@ }@b@ }@b@@b@ public static Boolean or(Boolean... array) {@b@ if (ArrayUtil.isEmpty(array)) {@b@ throw new IllegalArgumentException("The Array must not be empty !");@b@ } else {@b@ boolean[] primitive = (boolean[])Convert.convert(boolean[].class, array);@b@ return or(primitive);@b@ }@b@ }@b@@b@ public static boolean xor(boolean... array) {@b@ if (ArrayUtil.isEmpty(array)) {@b@ throw new IllegalArgumentException("The Array must not be empty");@b@ } else {@b@ boolean result = false;@b@ boolean[] arr$ = array;@b@ int len$ = array.length;@b@@b@ for(int i$ = 0; i$ < len$; ++i$) {@b@ boolean element = arr$[i$];@b@ result ^= element;@b@ }@b@@b@ return result;@b@ }@b@ }@b@@b@ public static Boolean xor(Boolean... array) {@b@ if (ArrayUtil.isEmpty(array)) {@b@ throw new IllegalArgumentException("The Array must not be empty !");@b@ } else {@b@ boolean[] primitive = (boolean[])Convert.convert(boolean[].class, array);@b@ return xor(primitive);@b@ }@b@ }@b@}