Boolean 常用工具类


 

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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值