commons-lang3工具类学习(二)

来源:互联网 发布:比价软件哪个好 编辑:程序博客网 时间:2024/05/22 17:02

摘自http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/官方文档,借此整理一遍,方便自己记忆

三、BooleanUtils

布尔工具类

and(boolean… array) 逻辑与

BooleanUtils.and(true, true)         = trueBooleanUtils.and(false, false)       = falseBooleanUtils.and(true, false)        = falseBooleanUtils.and(true, true, false)  = falseBooleanUtils.and(true, true, true)   = true

compare(boolean x, boolean y) 比较两个布尔值并返回int类型 如果x == y返回0, !x && y 返回小于 0 ,x && !y 返回大于0

isFalse(Boolean bool) 是否是假并返回boolean

isTrue(Boolean bool) 是否是真并返回boolean

negate(Boolean bool) 逻辑非

BooleanUtils.negate(Boolean.TRUE)  = Boolean.FALSE;BooleanUtils.negate(Boolean.FALSE) = Boolean.TRUE;BooleanUtils.negate(null)          = null;

or(boolean… array) 逻辑或

BooleanUtils.or(true, true)          = trueBooleanUtils.or(false, false)        = falseBooleanUtils.or(true, false)         = trueBooleanUtils.or(true, true, false)   = trueBooleanUtils.or(true, true, true)    = trueBooleanUtils.or(false, false, false) = false

toBoolean(Boolean bool) 将对象类型转换为基本数据类型并返回

BooleanUtils.toBoolean(Boolean.TRUE)  = trueBooleanUtils.toBoolean(Boolean.FALSE) = falseBooleanUtils.toBoolean(null)          = false

toBoolean(int value) 将int类型转换为boolean类型并返回

BooleanUtils.toBoolean(0) = falseBooleanUtils.toBoolean(1) = trueBooleanUtils.toBoolean(2) = true

toBoolean(String str) 将string类型转换为boolean类型并返回

BooleanUtils.toBoolean(null)    = falseBooleanUtils.toBoolean("true")  = trueBooleanUtils.toBoolean("TRUE")  = trueBooleanUtils.toBoolean("tRUe")  = trueBooleanUtils.toBoolean("on")    = trueBooleanUtils.toBoolean("yes")   = trueBooleanUtils.toBoolean("false") = falseBooleanUtils.toBoolean("x gti") = falseBooleanUtils.toBooleanObject("y") = trueBooleanUtils.toBooleanObject("n") = falseBooleanUtils.toBooleanObject("t") = trueBooleanUtils.toBooleanObject("f") = false

toInteger(boolean bool) 将boolean类型数据转换为int类型并返回

BooleanUtils.toInteger(true)  = 1BooleanUtils.toInteger(false) = 0

toStringOnOff(boolean bool) 将boolean类型数据转换为String类型’on’ or ‘off’并返回

BooleanUtils.toStringOnOff(true)   = "on"BooleanUtils.toStringOnOff(false)  = "off"

toStringTrueFalse(Boolean bool) 将boolean类型数据转换为String类型”true’ or ‘false’并返回

BooleanUtils.toStringTrueFalse(true)   = "true"BooleanUtils.toStringTrueFalse(false)  = "false"

toStringYesNo(boolean bool) 将boolean类型数据转换为String类型’yes’ or ‘no’并返回

BooleanUtils.toStringYesNo(true)   = "yes"BooleanUtils.toStringYesNo(false)  = "no"

xor(boolean… array) 异或

BooleanUtils.xor(true, true)   = falseBooleanUtils.xor(false, false) = falseBooleanUtils.xor(true, false)  = true

四、ClassPathUtils

class路径工具

toFullyQualifiedName 返回一个由class包名+resourceName拼接的字符串

toFullyQualifiedName(Class<?> context, String resourceName)ClassPathUtils.toFullyQualifiedName(StringUtils.class, "StringUtils.properties") = "org.apache.commons.lang3.StringUtils.properties"

toFullyQualifiedName(Package context, String resourceName) 返回一个由class包名+resourceName拼接的字符串

ClassPathUtils.toFullyQualifiedName(StringUtils.class.getPackage(), "StringUtils.properties") = "org.apache.commons.lang3.StringUtils.properties"

toFullyQualifiedPath 返回一个由class包名+resourceName拼接的字符串

toFullyQualifiedPath(Class<?> context, String resourceName) ClassPathUtils.toFullyQualifiedPath(StringUtils.class, "StringUtils.properties") = "org/apache/commons/lang3/StringUtils.properties"

toFullyQualifiedPath(Package context, String resourceName) 返回一个由class包名+resourceName拼接的字符串

ClassPathUtils.toFullyQualifiedPath(StringUtils.class, "StringUtils.properties") = "org/apache/commons/lang3/StringUtils.properties"

五、EnumUtils

枚举工具类

getEnum(Class enumClass, String enumName) 通过类返回一个枚举,可能返回空

getEnumList(Class enumClass) 通过类返回一个枚举集合

getEnumMap(Class enumClass) 通过类返回一个枚举map

isValidEnum(Class enumClass, String enumName) 验证enumName是否在枚举中,返回true false

demo

枚举类public enum EnumDemo {    AA("1"), BB("2");    private String value;    EnumDemo(String value) {        this.value = value;    }    public String getValue() {        return value;    }}测试EnumDemo enumDemo = EnumUtils.getEnum(EnumDemo.class, "");System.out.println(enumDemo);System.out.println("-----");List<EnumDemo> list = EnumUtils.getEnumList(EnumDemo.class);for (EnumDemo a : list) {    System.out.println(a + ":" + a.getValue());}System.out.println("-----");Map<String, EnumDemo> enumMap = EnumUtils.getEnumMap(EnumDemo.class);for (Map.Entry<String, EnumDemo> entry : enumMap.entrySet()) {    Object key = entry.getKey();    EnumDemo value = entry.getValue();    System.out.println(key + ":" + value.getValue());}System.out.println("-----");System.out.println(EnumUtils.isValidEnum(EnumDemo.class, "aa"));输出AA-----AA:1BB:2-----AA:1BB:2-----false
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 问道手游密码忘了怎么办 毒蚂蚁咬了红肿怎么办 电脑开机后有电流声怎么办 小学生阅读方面不会总结怎么办 天龙八部手游转职装备怎么办 投简历没回应该怎么办? 开车开的腰疼怎么办 开车腰疼怎么办吃什么 58简历投错了怎么办 开了一天车腰疼怎么办 开车腰疼怎么办怎么解决好 球球大作战找不到主界面怎么办 在QQ上找不到主界面怎么办 mac地址被过滤了怎么办 续贷声明含非法字符怎么办 辞职老板不给结工资怎么办 辞职后老板不发工资怎么办 联想手机不自带系统升级怎么办 苹果6玩崩坏3卡怎么办 机票名字打错了怎么办? 安装软件后显示名字乱码怎么办 户口上民族错了怎么办 改名字后行驶证怎么办 眼角撞到了肿了怎么办 想不想修真邪气怎么办 进户门比房间门低怎么办 吃了药反胃想吐怎么办 药吃了胃难受怎么办 吃了牙痛药胃痛怎么办 吃了药刺激胃怎么办 吃凉的刺激到胃怎么办 误食打农药的菜怎么办 狗把蛇咬死了怎么办 吃过毒死的狗怎么办 偷用室友东西被发现怎么办 室友看综艺太吵怎么办 被甲鱼咬住不放怎么办 凤仙叶子干焦怎么办 香槟开了没喝完怎么办 土豆酸了吃了怎么办 吃了发酸的土豆怎么办