(二十八)Java工具类BooleanUtils源码详解

原文链接:https://blog.csdn.net/yaomingyang/article/details/79315591

1.BooleanUtils工具类详解

package org.apache.commons.lang3;
import org.apache.commons.lang3.math.NumberUtils;

 public class BooleanUtils
 {
   public BooleanUtils() {}
   //否定指定的布尔值,若果参数为true,则返回false;如果参数为null,则返回null
   public static Boolean negate(Boolean bool)
   {
     if (bool == null) {
       return null;
     }
     return bool.booleanValue() ? Boolean.FALSE : Boolean.TRUE;
   }

   //参数是否为true,如果为true,则返回true
   public static boolean isTrue(Boolean bool)
   {
     return Boolean.TRUE.equals(bool);
   }

   //参数是否为true,如果为true则返回false,如果为null、false时返回true
   public static boolean isNotTrue(Boolean bool)
   {
     return !isTrue(bool);
   }
   //参数是否为false,如果为false,则返回true
   public static boolean isFalse(Boolean bool)
   {
     return Boolean.FALSE.equals(bool);
   }
   //参数如果不为false,则返回true,参数可以是true、null
   public static boolean isNotFalse(Boolean bool)
   {
     return !isFalse(bool);
   }
   //将参数转换为基本数据类型的boolean值
   public static boolean toBoolean(Boolean bool)
   {
     return (bool != null) && (bool.booleanValue());
   }
   //将参数转换为基本数据类型的boolean值,如果为null,则返回默认值;
   public static boolean toBooleanDefaultIfNull(Boolean bool, boolean valueIfNull)
   {
     if (bool == null) {
       return valueIfNull;
     }
     return bool.booleanValue();
   }
   //将数字转换为boolean值,如果为0返回false,否则返回true
   public static boolean toBoolean(int value)
   {
     return value != 0;
   }
   //将数字转换为布尔型的包装类型
   public static Boolean toBooleanObject(int value)
   {
     return value == 0 ? Boolean.FALSE : Boolean.TRUE;
   }
   //将包装类型的整数值转换为布尔值的包装类型,如果为null则返回null
   public static Boolean toBooleanObject(Integer value)
   {
     if (value == null) {
       return null;
     }
     return value.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
   }
   //根据传入的三个参数判断返回的boolean值
   public static boolean toBoolean(int value, int trueValue, int falseValue)
   {
     if (value == trueValue) {
       return true;
     }
     if (value == falseValue) {
       return false;
     }

     throw new IllegalArgumentException("The Integer did not match either specified value");
   }
   //根据传入的三个包装类型参数返回比较的结果
   public static boolean toBoolean(Integer value, Integer trueValue, Integer falseValue)
   {
     if (value == null) {
       if (trueValue == null) {
         return true;
       }
       if (falseValue == null)
         return false;
     } else {
       if (value.equals(trueValue))
         return true;
       if (value.equals(falseValue)) {
         return false;
       }
     }
     throw new IllegalArgumentException("The Integer did not match either specified value");
   }
   //根据传入的参数比较包装类型结果
   public static Boolean toBooleanObject(int value, int trueValue, int falseValue, int nullValue)
   {
     if (value == trueValue) {
       return Boolean.TRUE;
     }
     if (value == falseValue) {
       return Boolean.FALSE;
     }
     if (value == nullValue) {
       return null;
     }

     throw new IllegalArgumentException("The Integer did not match any specified value");
   }
   //根据传入的参数比较返回的结果
   public static Boolean toBooleanObject(Integer value, Integer trueValue, Integer falseValue, Integer nullValue)
   {
     if (value == null) {
       if (trueValue == null) {
         return Boolean.TRUE;
       }
       if (falseValue == null) {
         return Boolean.FALSE;
       }
       if (nullValue == null)
         return null;
     } else {
       if (value.equals(trueValue))
         return Boolean.TRUE;
       if (value.equals(falseValue))
         return Boolean.FALSE;
       if (value.equals(nullValue)) {
         return null;
       }
     }
     throw new IllegalArgumentException("The Integer did not match any specified value");
   }
   //将布尔型转换为数字,true--1,false--0
   public static int toInteger(boolean bool)
   {
     return bool ? 1 : 0;
   }
   //将布尔型转换为包装类型数字,true--1 ,false--0
   public static Integer toIntegerObject(boolean bool)
  {
    return bool ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO;
  }
  //将包装类型布尔值转换为包装类型的值,null--null,true--1,false--0
  public static Integer toIntegerObject(Boolean bool)
  {
    if (bool == null) {
      return null;
    }
    return bool.booleanValue() ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO;
  }
  //根据布尔值返回不同的int值
  public static int toInteger(boolean bool, int trueValue, int falseValue)
  {
    return bool ? trueValue : falseValue;
  }
  //根据包装类型布尔值返回不同的int值
  public static int toInteger(Boolean bool, int trueValue, int falseValue, int nullValue)
  {
    if (bool == null) {
      return nullValue;
    }
    return bool.booleanValue() ? trueValue : falseValue;
  }
  //根据布尔值返回包装类型的数据
  public static Integer toIntegerObject(boolean bool, Integer trueValue, Integer falseValue)
  {
    return bool ? trueValue : falseValue;
  }
  //根据包装类型的布尔值返回不同的包装类型整数值
  public static Integer toIntegerObject(Boolean bool, Integer trueValue, Integer falseValue, Integer nullValue)
  {
    if (bool == null) {
      return nullValue;
    }
    return bool.booleanValue() ? trueValue : falseValue;
  }
  //将字符串转换成不同类型的包装类型布尔值
  public static Boolean toBooleanObject(String str)
  {
    if (str == "true") {
      return Boolean.TRUE;
    }
    if (str == null) {
      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);
      if (((ch0 == 'o') || (ch0 == 'O')) && ((ch1 == 'n') || (ch1 == 'N')))
      {
        return Boolean.TRUE;
      }
      if (((ch0 == 'n') || (ch0 == 'N')) && ((ch1 == 'o') || (ch1 == 'O')))
      {
        return Boolean.FALSE;
      }

      break;
    case 3: 
      char ch0 = str.charAt(0);
      char ch1 = str.charAt(1);
      char ch2 = str.charAt(2);
      if (((ch0 == 'y') || (ch0 == 'Y')) && ((ch1 == 'e') || (ch1 == 'E')) && ((ch2 == 's') || (ch2 == 'S')))
      {

        return Boolean.TRUE;
      }
      if (((ch0 == 'o') || (ch0 == 'O')) && ((ch1 == 'f') || (ch1 == 'F')) && ((ch2 == 'f') || (ch2 == 'F')))
      {

        return Boolean.FALSE;
      }

      break;
    case 4: 
      char ch0 = str.charAt(0);
      char ch1 = str.charAt(1);
      char ch2 = str.charAt(2);
      char ch3 = str.charAt(3);
      if (((ch0 == 't') || (ch0 == 'T')) && ((ch1 == 'r') || (ch1 == 'R')) && ((ch2 == 'u') || (ch2 == 'U')) && ((ch3 == 'e') || (ch3 == 'E')))
      {


        return Boolean.TRUE;
      }

      break;
    case 5: 
      char ch0 = str.charAt(0);
      char ch1 = str.charAt(1);
      char ch2 = str.charAt(2);
      char ch3 = str.charAt(3);
      char ch4 = str.charAt(4);
      if (((ch0 == 'f') || (ch0 == 'F')) && ((ch1 == 'a') || (ch1 == 'A')) && ((ch2 == 'l') || (ch2 == 'L')) && ((ch3 == 's') || (ch3 == 'S')) && ((ch4 == 'e') || (ch4 == 'E')))
      {



        return Boolean.FALSE;
      }


      break;
    }


    return null;
  }
  //将给定的字符串比较返回包装类型的布尔值
  public static Boolean toBooleanObject(String str, String trueString, String falseString, String nullString)
  {
    if (str == null) {
      if (trueString == null) {
        return Boolean.TRUE;
      }
      if (falseString == null) {
        return Boolean.FALSE;
      }
      if (nullString == null)
        return null;
    } else {
      if (str.equals(trueString))
        return Boolean.TRUE;
      if (str.equals(falseString))
        return Boolean.FALSE;
      if (str.equals(nullString)) {
        return null;
      }
    }
    throw new IllegalArgumentException("The String did not match any specified value");
  }
  //将字符串转换成不同类型的布尔值
  public static boolean toBoolean(String str)
  {
    return toBooleanObject(str) == Boolean.TRUE;
  }
  //将字符串与给定的字符串比较返回布尔值
  public static boolean toBoolean(String str, String trueString, String falseString)
  {
    if (str == trueString)
      return true;
    if (str == falseString)
      return false;
    if (str != null) {
      if (str.equals(trueString))
        return true;
      if (str.equals(falseString)) {
        return false;
      }
    }

    throw new IllegalArgumentException("The String did not match either specified value");
  }
  //将包装类型的布尔值转换为字符串true或false
  public static String toStringTrueFalse(Boolean bool)
  {
    return toString(bool, "true", "false", null);
  }
  //将布尔值转为字符串on或者off
  public static String toStringOnOff(Boolean bool)
  {
    return toString(bool, "on", "off", null);
  }
  //将布尔值转换为yes或no
  public static String toStringYesNo(Boolean bool)
  {
    return toString(bool, "yes", "no", null);
  }
  //将包装类型的布尔值转为指定的字符串
  public static String toString(Boolean bool, String trueString, String falseString, String nullString)
  {
    if (bool == null) {
      return nullString;
    }
    return bool.booleanValue() ? trueString : falseString;
  }
  //将布尔型转换为字符串类型的true或false
  public static String toStringTrueFalse(boolean bool)
  {
    return toString(bool, "true", "false");
  }
  //如果布尔值为true--on,false-off
  public static String toStringOnOff(boolean bool)
  {
    return toString(bool, "on", "off");
  }
  //如果布尔值为true-yes,false-no
  public static String toStringYesNo(boolean bool)
  {
    return toString(bool, "yes", "no");
  }
  //将布尔值转为指定的字符串
  public static String toString(boolean bool, String trueString, String falseString)
  {
    return bool ? trueString : falseString;
  }
  //数组中的所有布尔值都为true的时候才返回true
  public static boolean and(boolean... array)
  {
    if (array == null) {
      throw new IllegalArgumentException("The Array must not be null");
    }
    if (array.length == 0) {
      throw new IllegalArgumentException("Array is empty");
    }
    for (boolean element : array) {
      if (!element) {
        return false;
      }
    }
    return true;
  }
  //包装类型中的所有布尔值都返回true的时候才返回true
  public static Boolean and(Boolean... array)
  {
    if (array == null) {
      throw new IllegalArgumentException("The Array must not be null");
    }
    if (array.length == 0) {
      throw new IllegalArgumentException("Array is empty");
    }
    try {
      boolean[] primitive = ArrayUtils.toPrimitive(array);
      return and(primitive) ? Boolean.TRUE : Boolean.FALSE;
    } catch (NullPointerException ex) {
      throw new IllegalArgumentException("The array must not contain any null elements");
    }
  }
  //数组中有一个为true就返回
  public static boolean or(boolean... array)
  {
    if (array == null) {
      throw new IllegalArgumentException("The Array must not be null");
    }
    if (array.length == 0) {
      throw new IllegalArgumentException("Array is empty");
    }
    for (boolean element : array) {
      if (element) {
        return true;
      }
    }
    return false;
  }
  //包装类型有一个为true就返回true
  public static Boolean or(Boolean... array)
  {
    if (array == null) {
      throw new IllegalArgumentException("The Array must not be null");
    }
    if (array.length == 0) {
      throw new IllegalArgumentException("Array is empty");
    }
    try {
      boolean[] primitive = ArrayUtils.toPrimitive(array);
      return or(primitive) ? Boolean.TRUE : Boolean.FALSE;
    } catch (NullPointerException ex) {
      throw new IllegalArgumentException("The array must not contain any null elements");
    }
  }
  //对一组布尔值异或
  public static boolean xor(boolean... array)
  {
    if (array == null) {
      throw new IllegalArgumentException("The Array must not be null");
    }
    if (array.length == 0) {
      throw new IllegalArgumentException("Array is empty");
    }


    boolean result = false;
    for (boolean element : array) {
      result ^= element;
    }

    return result;
  }
  //对一组布尔值异或
  public static Boolean xor(Boolean... array)
  {
    if (array == null) {
      throw new IllegalArgumentException("The Array must not be null");
    }
    if (array.length == 0) {
      throw new IllegalArgumentException("Array is empty");
    }
    try {
      boolean[] primitive = ArrayUtils.toPrimitive(array);
      return xor(primitive) ? Boolean.TRUE : Boolean.FALSE;
    } catch (NullPointerException ex) {
      throw new IllegalArgumentException("The array must not contain any null elements");
    }
  }
  //比较两个布尔值的大小
  public static int compare(boolean x, boolean y)
  {
    if (x == y) {
      return 0;
    }
    return x ? 1 : -1;
  }
}

猜你喜欢

转载自blog.csdn.net/jarniyy/article/details/80446680