首页

通过apache的cayenne包中BooleanUtils工具类对整型、字符串类型等常见转换进行统一处理

标签:布尔类型,工具类,booleanutils,apache,cayenne     发布时间:2018-01-06   

一、前言

基于apache的cayenne源码包(2.0.4)中的org.apache.commons.lang.BooleanUtils工具类,对常见的对象类型isFalse或isTrue判断、整型转换toBoolean、字符串转换toBoolean等常用方式的通用处理。

二、源码说明

package org.apache.commons.lang;@b@@b@import org.apache.commons.lang.math.NumberUtils;@b@@b@public class BooleanUtils@b@{@b@  public static boolean isFalse(Boolean bool)@b@  {@b@    if (bool == null)@b@      return false;@b@@b@    return (!(bool.booleanValue()));@b@  }@b@@b@  public static boolean isTrue(Boolean bool)@b@  {@b@    if (bool == null)@b@      return false;@b@@b@    return (bool.booleanValue());@b@  }@b@@b@  public static Boolean negate(Boolean bool)@b@  {@b@    if (bool == null)@b@      return null;@b@@b@    return ((bool.booleanValue()) ? Boolean.FALSE : Boolean.TRUE);@b@  }@b@@b@  public static boolean toBoolean(int value)@b@  {@b@    return (value != 0);@b@  }@b@@b@  public static boolean toBoolean(int value, int trueValue, int falseValue)@b@  {@b@    if (value == trueValue)@b@      return true;@b@    if (value == falseValue) {@b@      return false;@b@    }@b@@b@    throw new IllegalArgumentException("The Integer did not match either specified value");@b@  }@b@@b@  public static boolean toBoolean(Boolean bool)@b@  {@b@    if (bool == null)@b@      return false;@b@@b@    return (bool.booleanValue());@b@  }@b@@b@  public static boolean toBoolean(Integer value, Integer trueValue, Integer falseValue)@b@  {@b@    if (value == null) {@b@      if (trueValue == null)@b@        return true;@b@      if (falseValue != null) break label36;@b@      return false;@b@    }@b@    if (value.equals(trueValue))@b@      return true;@b@    if (value.equals(falseValue)) {@b@      return false;@b@    }@b@@b@    label36: throw new IllegalArgumentException("The Integer did not match either specified value");@b@  }@b@@b@  public static boolean toBoolean(String str)@b@  {@b@    char ch;@b@    if (str == "true")@b@      return true;@b@@b@    if (str == null)@b@      return false;@b@@b@    switch (str.length())@b@    {@b@    case 2:@b@      char ch0 = str.charAt(0);@b@      char ch1 = str.charAt(1);@b@      return @b@        ((((ch0 == 'o') || (ch0 == 'O'))) && ((@b@        (ch1 == 'n') || (ch1 == 'N'))));@b@    case 3:@b@      ch = str.charAt(0);@b@      if (ch == 'y')@b@        return @b@          ((((str.charAt(1) == 'e') || (str.charAt(1) == 'E'))) && ((@b@          (str.charAt(2) == 's') || (str.charAt(2) == 'S'))));@b@@b@      if (ch != 'Y') break label196;@b@      return @b@        ((((str.charAt(1) == 'E') || (str.charAt(1) == 'e'))) && ((@b@        (str.charAt(2) == 'S') || (str.charAt(2) == 's'))));@b@    case 4:@b@      label196: ch = str.charAt(0);@b@      if (ch == 't')@b@        return @b@          ((((str.charAt(1) == 'r') || (str.charAt(1) == 'R'))) && @b@          (((str.charAt(2) == 'u') || (str.charAt(2) == 'U'))) && ((@b@          (str.charAt(3) == 'e') || (str.charAt(3) == 'E'))));@b@@b@      if (ch != 'T') break label346;@b@      return @b@        ((((str.charAt(1) == 'R') || (str.charAt(1) == 'r'))) && @b@        (((str.charAt(2) == 'U') || (str.charAt(2) == 'u'))) && ((@b@        (str.charAt(3) == 'E') || (str.charAt(3) == 'e'))));@b@    }@b@@b@    label346: return false;@b@  }@b@@b@  public static boolean toBoolean(String str, String trueString, String falseString)@b@  {@b@    if (str == null) {@b@      if (trueString == null)@b@        return true;@b@      if (falseString != null) break label36;@b@      return false;@b@    }@b@    if (str.equals(trueString))@b@      return true;@b@    if (str.equals(falseString)) {@b@      return false;@b@    }@b@@b@    label36: throw new IllegalArgumentException("The String did not match either specified value");@b@  }@b@@b@  public static boolean toBooleanDefaultIfNull(Boolean bool, boolean valueIfNull)@b@  {@b@    if (bool == null)@b@      return valueIfNull;@b@@b@    return (bool.booleanValue());@b@  }@b@@b@  public static Boolean toBooleanObject(int value)@b@  {@b@    return ((value == 0) ? Boolean.FALSE : Boolean.TRUE);@b@  }@b@@b@  public static Boolean toBooleanObject(int value, int trueValue, int falseValue, int nullValue)@b@  {@b@    if (value == trueValue)@b@      return Boolean.TRUE;@b@    if (value == falseValue)@b@      return Boolean.FALSE;@b@    if (value == nullValue) {@b@      return null;@b@    }@b@@b@    throw new IllegalArgumentException("The Integer did not match any specified value");@b@  }@b@@b@  public static Boolean toBooleanObject(Integer value)@b@  {@b@    if (value == null)@b@      return null;@b@@b@    return ((value.intValue() == 0) ? Boolean.FALSE : Boolean.TRUE);@b@  }@b@@b@  public static Boolean toBooleanObject(Integer value, Integer trueValue, Integer falseValue, Integer nullValue)@b@  {@b@    if (value == null) {@b@      if (trueValue == null)@b@        return Boolean.TRUE;@b@      if (falseValue == null)@b@        return Boolean.FALSE;@b@      if (nullValue != null) break label60;@b@      return null;@b@    }@b@    if (value.equals(trueValue))@b@      return Boolean.TRUE;@b@    if (value.equals(falseValue))@b@      return Boolean.FALSE;@b@    if (value.equals(nullValue)) {@b@      return null;@b@    }@b@@b@    label60: throw new IllegalArgumentException("The Integer did not match any specified value");@b@  }@b@@b@  public static Boolean toBooleanObject(String str)@b@  {@b@    if ("true".equalsIgnoreCase(str))@b@      return Boolean.TRUE;@b@    if ("false".equalsIgnoreCase(str))@b@      return Boolean.FALSE;@b@    if ("on".equalsIgnoreCase(str))@b@      return Boolean.TRUE;@b@    if ("off".equalsIgnoreCase(str))@b@      return Boolean.FALSE;@b@    if ("yes".equalsIgnoreCase(str))@b@      return Boolean.TRUE;@b@    if ("no".equalsIgnoreCase(str)) {@b@      return Boolean.FALSE;@b@    }@b@@b@    return null;@b@  }@b@@b@  public static Boolean toBooleanObject(String str, String trueString, String falseString, String nullString)@b@  {@b@    if (str == null) {@b@      if (trueString == null)@b@        return Boolean.TRUE;@b@      if (falseString == null)@b@        return Boolean.FALSE;@b@      if (nullString != null) break label60;@b@      return null;@b@    }@b@    if (str.equals(trueString))@b@      return Boolean.TRUE;@b@    if (str.equals(falseString))@b@      return Boolean.FALSE;@b@    if (str.equals(nullString)) {@b@      return null;@b@    }@b@@b@    label60: throw new IllegalArgumentException("The String did not match any specified value");@b@  }@b@@b@  public static Boolean toBooleanObject(boolean bool)@b@  {@b@    return ((bool) ? Boolean.TRUE : Boolean.FALSE);@b@  }@b@@b@  public static int toInteger(Boolean bool, int trueValue, int falseValue, int nullValue)@b@  {@b@    if (bool == null)@b@      return nullValue;@b@@b@    return ((bool.booleanValue()) ? trueValue : falseValue);@b@  }@b@@b@  public static int toInteger(boolean bool)@b@  {@b@    return ((bool) ? 1 : 0);@b@  }@b@@b@  public static int toInteger(boolean bool, int trueValue, int falseValue)@b@  {@b@    return ((bool) ? trueValue : falseValue);@b@  }@b@@b@  public static Integer toIntegerObject(Boolean bool)@b@  {@b@    if (bool == null)@b@      return null;@b@@b@    return ((bool.booleanValue()) ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO);@b@  }@b@@b@  public static Integer toIntegerObject(Boolean bool, Integer trueValue, Integer falseValue, Integer nullValue)@b@  {@b@    if (bool == null)@b@      return nullValue;@b@@b@    return ((bool.booleanValue()) ? trueValue : falseValue);@b@  }@b@@b@  public static Integer toIntegerObject(boolean bool)@b@  {@b@    return ((bool) ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO);@b@  }@b@@b@  public static Integer toIntegerObject(boolean bool, Integer trueValue, Integer falseValue)@b@  {@b@    return ((bool) ? trueValue : falseValue);@b@  }@b@@b@  public static String toString(Boolean bool, String trueString, String falseString, String nullString)@b@  {@b@    if (bool == null)@b@      return nullString;@b@@b@    return ((bool.booleanValue()) ? trueString : falseString);@b@  }@b@@b@  public static String toString(boolean bool, String trueString, String falseString)@b@  {@b@    return ((bool) ? trueString : falseString);@b@  }@b@@b@  public static String toStringOnOff(Boolean bool)@b@  {@b@    return toString(bool, "on", "off", null);@b@  }@b@@b@  public static String toStringOnOff(boolean bool)@b@  {@b@    return toString(bool, "on", "off");@b@  }@b@@b@  public static String toStringTrueFalse(Boolean bool)@b@  {@b@    return toString(bool, "true", "false", null);@b@  }@b@@b@  public static String toStringTrueFalse(boolean bool)@b@  {@b@    return toString(bool, "true", "false");@b@  }@b@@b@  public static String toStringYesNo(Boolean bool)@b@  {@b@    return toString(bool, "yes", "no", null);@b@  }@b@@b@  public static String toStringYesNo(boolean bool)@b@  {@b@    return toString(bool, "yes", "no");@b@  }@b@@b@  public static Boolean xor(Boolean[] array)@b@  {@b@    if (array == null)@b@      throw new IllegalArgumentException("The Array must not be null");@b@    if (array.length == 0)@b@      throw new IllegalArgumentException("Array is empty");@b@@b@    boolean[] primitive = null;@b@    try {@b@      primitive = ArrayUtils.toPrimitive(array);@b@    } catch (NullPointerException localNullPointerException) {@b@      throw new IllegalArgumentException("The array must not contain any null elements");@b@    }@b@    return ((xor(primitive)) ? Boolean.TRUE : Boolean.FALSE);@b@  }@b@@b@  public static boolean xor(boolean[] array)@b@  {@b@    if (array == null)@b@      throw new IllegalArgumentException("The Array must not be null");@b@    if (array.length == 0) {@b@      throw new IllegalArgumentException("Array is empty");@b@    }@b@@b@    int trueCount = 0;@b@    for (int i = 0; i < array.length; ++i)@b@    {@b@      if (array[i] != 0) {@b@        if (trueCount < 1)@b@          ++trueCount;@b@        else@b@          return false;@b@@b@      }@b@@b@    }@b@@b@    return (trueCount == 1);@b@  }@b@}