commons-lang3 tool class learning (2)

Posted by dvd420 on Fri, 17 Jul 2020 17:51:35 +0200

From http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/ Official documents to organize and remember for yourself

3. BooleanUtils

Boolean Tool Class

and(boolean...array) logic and

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

compare(boolean x, boolean y) compares two Boolean values and returns the int type if x == y returns 0!! x && y returns less than 0, x && y returns greater than 0

Is isFalse(Boolean bool) false and returns boolean

Is isTrue(Boolean bool) true and returns boolean

negate(Boolean bool) logical non

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

or(boolean...array) Logical or

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

toBoolean(Boolean bool) converts object types to basic data types and returns

BooleanUtils.toBoolean(Boolean.TRUE)  = true
BooleanUtils.toBoolean(Boolean.FALSE) = false
BooleanUtils.toBoolean(null)          = false

toBoolean(int value) converts an int type to a boolean type and returns

BooleanUtils.toBoolean(0) = false
BooleanUtils.toBoolean(1) = true
BooleanUtils.toBoolean(2) = true

toBoolean(String str) converts a string type to a boolean type and returns

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

toInteger(boolean bool) converts boolean type data to int type and returns

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

toStringOnOff(boolean bool) converts boolean type data to String type'on'or'off' and returns

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

toStringTrueFalse(Boolean bool) converts boolean type data to String type "true'or'false" and returns

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

toStringYesNo(boolean bool) converts boolean type data to String type'yes'or'no' and returns

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

xor(boolean...array) XOR

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

IV. ClassPathUtils

class Path Tool

toFullyQualifiedName returns a string stitched together by the class package name + resourceName

toFullyQualifiedName(Class<?> context, String resourceName)

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

toFullyQualifiedName(Package context, String resourceName) returns a string stitched together by the class package name + resourceName

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

toFullyQualifiedPath returns a string stitched together by the class package name + resourceName

toFullyQualifiedPath(Class<?> context, String resourceName) 

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

toFullyQualifiedPath(Package context, String resourceName) returns a string stitched together by the class package name + resourceName

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

V. EnumUtils

Enumerate Tool Classes

getEnum(Class enumClass, String enumName) returns an enumeration through a class, possibly empty

getEnumList(Class enumClass) returns an enumerated collection through a class

getEnumMap(Class enumClass) returns an enumerated map through a class

isValidEnum(Class enumClass, String enumName) verifies that enumName is in the enumeration and returns true false

demo

Enum Class
public enum EnumDemo {
    AA("1"), BB("2");
    private String value;

    EnumDemo(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
}

//test
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"));

//output
AA
-----
AA:1
BB:2
-----
AA:1
BB:2
-----
false

Topics: Apache less