commons-lang3工具类学习2

三、BooleanUtils
布尔工具类

and(boolean… array) 逻辑与

BooleanUtils.and(true, true) = true
BooleanUtils.and(false, false) = false
BooleanUtils.and(true, false) = false
BooleanUtils.and(true, true, false) = false
BooleanUtils.and(true, true, true) = true
1
2
3
4
5
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;
1
2
3
or(boolean… array) 逻辑或

BooleanUtils.or(true, true) = true
BooleanUtils.or(false, false) = false
BooleanUtils.or(true, false) = true
BooleanUtils.or(true, true, false) = true
BooleanUtils.or(true, true, true) = true
BooleanUtils.or(false, false, false) = false
1
2
3
4
5
6
toBoolean(Boolean bool) 将对象类型转换为基本数据类型并返回

BooleanUtils.toBoolean(Boolean.TRUE) = true
BooleanUtils.toBoolean(Boolean.FALSE) = false
BooleanUtils.toBoolean(null) = false
1
2
3
toBoolean(int value) 将int类型转换为boolean类型并返回

BooleanUtils.toBoolean(0) = false
BooleanUtils.toBoolean(1) = true
BooleanUtils.toBoolean(2) = true
1
2
3
toBoolean(String str) 将string类型转换为boolean类型并返回

BooleanUtils.toBoolean(null) = false
BooleanUtils.toBoolean(“true”) = true
BooleanUtils.toBoolean(“TRUE”) = true
BooleanUtils.toBoolean(“tRUe”) = true
BooleanUtils.toBoolean(“on”) = true
BooleanUtils.toBoolean(“yes”) = true
BooleanUtils.toBoolean(“false”) = false
BooleanUtils.toBoolean(“x gti”) = false
BooleanUtils.toBooleanObject(“y”) = true
BooleanUtils.toBooleanObject(“n”) = false
BooleanUtils.toBooleanObject(“t”) = true
BooleanUtils.toBooleanObject(“f”) = false
1
2
3
4
5
6
7
8
9
10
11
12
toInteger(boolean bool) 将boolean类型数据转换为int类型并返回

BooleanUtils.toInteger(true) = 1
BooleanUtils.toInteger(false) = 0
1
2
toStringOnOff(boolean bool) 将boolean类型数据转换为String类型’on’ or ‘off’并返回

BooleanUtils.toStringOnOff(true) = “on”
BooleanUtils.toStringOnOff(false) = “off”
1
2
toStringTrueFalse(Boolean bool) 将boolean类型数据转换为String类型”true’ or ‘false’并返回

BooleanUtils.toStringTrueFalse(true) = “true”
BooleanUtils.toStringTrueFalse(false) = “false”
1
2
toStringYesNo(boolean bool) 将boolean类型数据转换为String类型’yes’ or ‘no’并返回

BooleanUtils.toStringYesNo(true) = “yes”
BooleanUtils.toStringYesNo(false) = “no”
1
2
xor(boolean… array) 异或

BooleanUtils.xor(true, true) = false
BooleanUtils.xor(false, false) = false
BooleanUtils.xor(true, false) = true
1
2
3
四、ClassPathUtils
class路径工具

toFullyQualifiedName(Class

ClassPathUtils.toFullyQualifiedName(StringUtils.class, “StringUtils.properties”) = “org.apache.commons.lang3.StringUtils.properties”
1
toFullyQualifiedName(Package context, String resourceName) 返回一个由class包名+resourceName拼接的字符串

ClassPathUtils.toFullyQualifiedName(StringUtils.class.getPackage(), “StringUtils.properties”) = “org.apache.commons.lang3.StringUtils.properties”
1
toFullyQualifiedPath(Class

ClassPathUtils.toFullyQualifiedPath(StringUtils.class, “StringUtils.properties”) = “org/apache/commons/lang3/StringUtils.properties”
1
toFullyQualifiedPath(Package context, String resourceName) 返回一个由class包名+resourceName拼接的字符串

ClassPathUtils.toFullyQualifiedPath(StringUtils.class, “StringUtils.properties”) = “org/apache/commons/lang3/StringUtils.properties”
1
五、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 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(“—–“);</string,></string,>

System.out.println(EnumUtils.isValidEnum(EnumDemo.class, “aa”));

输出

AA

AA:1

BB:2

AA:1

BB:2

false

作者:悠闲咖啡007
原文:https://blog.csdn.net/psh18513234633/article/details/79063157

点赞