天天看點

常用的布爾工具類

目錄

  • ​​commons-lang3的BooleanUtils​​
  • ​​常用常量​​
  • ​​判斷、邏輯運算​​
  • ​​類型轉換​​
  • ​​hutool的BooleanUtil​​

commons-lang3的BooleanUtils

常用常量

String aTrue = BooleanUtils.TRUE;
String aFalse = BooleanUtils.FALSE;

String yes = BooleanUtils.YES;
String no = BooleanUtils.NO;

String on = BooleanUtils.ON;
String off = BooleanUtils.OFF;      

Boolean自帶的2個常量

Boolean aTrue = Boolean.TRUE;
Boolean aFalse = Boolean.FALSE;      

判斷、邏輯運算

//分别傳回 true、false 基本|包裝類型組成的數組
boolean[] primitiveValues = BooleanUtils.primitiveValues();
Boolean[] booleanValues = BooleanUtils.booleanValues();


boolean isTrue = BooleanUtils.isTrue(Boolean);
boolean isFalse = BooleanUtils.isFalse(Boolean);
boolean isNotTrue = BooleanUtils.isNotTrue(Boolean);
boolean isNotFalse = BooleanUtils.isNotFalse(Boolean);


//做 &&、|| 運算,如果參數中存在為null的包裝類型,會抛出異常
boolean|Boolean and = BooleanUtils.and(boolean|Boolean...);
boolean|Boolean or = BooleanUtils.or(boolean|Boolean...);

//取反,做 !運算,null傳回null
Boolean negate = BooleanUtils.negate(Boolean);      

類型轉換

toBoolean()、toInteger() 隻是camel命名方式,傳回的是基本類型 boolean、int;toBooleanObject()、toIntegerObject() 傳回的才是包裝類型Boolean。

包裝類型轉基本類型

//包裝類型轉基本類型,Boolean.TRUE傳回true,Boolean.FALSE、null傳回false
boolean b3 = BooleanUtils.toBoolean(Boolean);
//可以指定預設值,當Boolean為null時傳回預設值
boolean b3 = BooleanUtils.toBooleanDefaultIfNull(Boolean, defaultValue);      

其它類型轉布爾

//int轉boolean|Boolean,0傳回fasle,非0傳回true
boolean b1 = BooleanUtils.toBoolean(int);
Boolean B1 = BooleanUtils.toBooleanObject(int);
//null傳回null
Boolean B1 = BooleanUtils.toBooleanObject(Integer);


//String轉boolean|Boolean,判斷時 "true"、"yes" 等特殊值不區分大小寫
//"true"、"yes"、"t"、"y"、"on"、"1" 傳回true,t是true的簡寫,y是yes的簡寫,其它情況傳回false
boolean b2 = BooleanUtils.toBoolean(str);
//"true"、"yes"、"t"、"y"、"on"、"1" 傳回true,"false"、"no"、"f"、"n"、"off"、"0"傳回false,其它情況傳回null
Boolean B2 = BooleanUtils.toBooleanObject(str);


//intValue|integerValue|strValue 等于trueValue 則傳回true,等于falseValue則傳回false,如果是其它值會抛出異常
//基本類型用==進行判斷,引用類型用equals進行判斷
boolean b3 = BooleanUtils.toBoolean(intValue, trueValue, falseValue);
boolean b3 = BooleanUtils.toBoolean(integerValue, trueValue, falseValue);
boolean b3 = BooleanUtils.toBoolean(strValue, trueValue, falseValue);


//傳回包裝類型,當 intValue|integerValue|strValue 等于nullValue時傳回null
Boolean B4 = BooleanUtils.toBooleanObject(intValue, trueValue, falseValue, nullValue);
Boolean B4 = BooleanUtils.toBooleanObject(integerValue, trueValue, falseValue, nullValue);
Boolean B4 = BooleanUtils.toBooleanObject(strValue, trueValue, falseValue, nullValue);      

布爾轉其它類型

//布爾轉整型

//true=>1,false=>0
int i = BooleanUtils.toInteger(boolean);
Integer integer = BooleanUtils.toIntegerObject(boolean);
//null=>null
Integer integer = BooleanUtils.toIntegerObject(Boolean);

//true=>trueValue,false=>falseValue
int i = BooleanUtils.toInteger(boolean, int trueValue, int falseValue);
Integer integer = BooleanUtils.toIntegerObject(boolean, Integer trueValue, Integer falseValue);

//null=>nullValue
int i = BooleanUtils.toInteger(Boolean, int trueValue, int falseValue, int nullValue);
Integer integer = BooleanUtils.toIntegerObject(Boolean, Integer trueValue, Integer falseValue, Integer nullValue);


//布爾轉String
//true=>trueString,false=>falseString
String str = BooleanUtils.toString(boolean, String trueString, String falseString);
//null=>nullString
String str = BooleanUtils.toString(Boolean, String trueString, String falseString, String nullString);


//布爾轉String特殊值,包裝類型null=>null
//"true"、"false"
String str = BooleanUtils.toStringTrueFalse(boolean|Boolean);
//"on"、"off"
String str = BooleanUtils.toStringOnOff(boolean|Boolean);
//"yes"、"no"
String str = BooleanUtils.toStringYesNo(boolean|Boolean);      

hutool的BooleanUtil