Apache Jakarta Common LANG

xiaoxiao2021-04-05  252

Whether you are developing in which Java app, you can't write a lot of tool class / tool functions. You know, there are many ready-made tools available, and code quality is very good, don't write, don't use you to debug, as long as you find it. In Apache Jakarta Common, Lang's Java Toolkit is the most widely used in all Apache Jakarta Common projects, almost all of the famous software, including Tomcat, WebLogic, WebSphere, Eclipse, etc. . We will introduce the entire Common project from this package. The biggest in Lang is also the most common class is StringUtils, and StringUTils is the most commonly used function is ISempty. Here is an example to make you familiar with:

String str = NULL;

IF (Stringutils.isempty (STR)) {

}

str = StringUtils.lowerCase (str); str = StringUtils.capitalize (str); str = StringUtils.swapCase (str); StringUtils.isNumeric (str); StringUtils.isAsciiPrintable (str); StringUtils.equalsIgnoreCase (str, "") ;

Where IF (Stringutils.isempty (STR)) {

} Equivalent to IF (str == null || str.length () == 0) {

}

From the above code, you can see the characteristics of this tool class: First, these functions are static functions, and rarely throw an exception, and rarely throw an empty pointer exception. Second, most of these functions is the function of enhancing the Java.lang.String class in standard Java.

In fact, the function of the entire LANG package is basically enhanced the function of the java.lang package. An interesting design is, although the functions inside Stringutils are static functions, according to the usual practice, the constructor is designed to private, but not! StringUtils constructor is public. The comment inside the constructor writes "no init". This is interesting, you can get a StringUTILS, although there is no special benefit, but there is no special or bad place; you can write a StringUTILS subclass, although this is not common. The idea of ​​this seems to be, using this toolkit software developers think how to use it, do not have much limitations when doing tool classes. As for the never throwing because many people have realized that the exception in Java is not a good thing, if you don't have an exception, you can write the program, why should I use anomalies? It is necessary to write a lot of try catch, such as JDBC Driver, which has proven to increase the amount of code and the readability of the code. Will the idea here will affect the habit of everyone writing?

StringUtils has a lot of functions, and it is not listed. LANG mid-term important class ARRAYUTILS, main functions involve comparison, conversion, split, merge, find, increase or decrease, reverse, and so on. In JDK, this type of operation has no ready-made function.

There are many tools in Lang. Here, there are several major: classutils getshteclassname, this function should be in the java.lang.class class, I see many people write this function. GetAllInterfaces, ConvertClassNamestoclasses, Isassignable, Primition, IsinnerClass. Numberutils Conversion of numbers and numbers and strings StringToint, Todouble, Createnumber, Isllzeros, Int Compare (Float LHS, FLOAT RHS), ISNumber (String Str), Double Min (Double [] Array). Randomutils is used to generate random numbers. DateFormatUtils Date Time Format Conversion, and local time and UTC time conversion. Dateutils Date Tool Class. Issameday, truncate, round, modify. Several classes based on reflex mechanisms: Comparetobuilder compares, used in algorithms, sort, and comparison places. ReflectionCompare, append. EqualsBuilder compares through reflective mechanisms. Reflectionequals is used in many projects. HashcodeBuilder can generate Hash Code by reflex, many algorithms are involved in Hash Code, but not everyone knows a method of generating a Hash Code. TostringBuilder When you need to overload the toString function, you can use this function to list all the membership information of the current class.

There are few more than a few classes: SerializationUtils Java has a sequence of sequence comparative mystery, easy to make mistakes. SystemUtils can read some tool classes for JDK information, operating system information.

转载请注明原文地址:https://www.9cbs.com/read-132058.html

New Post(0)