This topic-Jakarta commons Lang

Source: Internet
Author: User
Tags comparable
Section 01 Java. Lang. * vs commons-lang .*

The current version is 2.0.

First, let us first think about why Jakarta has a commons-lang opensource attack case? Among the JDK we use, Java is used. lang. * many users ignore the import settings, while Java uses 1.1 ~ 1.4 is currently being developed. For more information, the APIs that can be used are more and more comprehensive. But is it enough ?!

Unfortunately, because JDK needs to be approved by the JCP team, it is a competitive place for major companies, in addition, the release and modification process will take a long time. Therefore, commons-Lang is a feature that is often used in various Jakarta cases, so that he can add Java. lang. * functions ~

Section 02Org. Apache. commons. Lang .*

For Java. Lang. *, commons-lang creates some utils to add functions ~ For example, arrayutils, booleanutils, charsetutils, classutils, numberutils, objectutils, stringutils, systemutils, and wordutils. We can refer to the commonly used stringutils to provide many non-essential functions,

For example, if no replaceall (string RegEx, string replacement) exists in jdk1.3 string, stringutils provides the replacechars (string STR, string searchchars, string replacechars) function to handle all the configurations.

Null is a null value that often causes nullpointerexception in the program. When a hacker sends a request to the user, it is particularly troublesome. Instead, a null kernel is required, it is better to use the empty Null String, so we can set some stringutils. defaultstring (request. getparameter ("XXX") to ensure that it is not null .. you can check the API. Many Methods discuss how to handle null and its principles,

In addition, systemutils adds some processing mechanisms on the java system, such as checking the Java version isjavaversionatleast, and the operating system of parallel lines ..

Section 03Org. Apache. commons. Lang. Builder

Builder .. this is a case where I think commons-Lang is very irrelevant .. the most important application is that when we were working on the case, the VO in the business community was the standard JavaBean coding method, and there would be some getter and setter, the changes and values of the JavaBean can be handled through reflection ..

  • comparetobuilder: comparable the comparable. compareto (object) methods.

    in Vo sorting, comparable can be used to process a specific number or string in a specific object, or use the following method ..

     public class myclass {string field1; int field2; Boolean field3 ;... public int compareto (Object O) {myclass = (myclass) O; return New comparetobuilder (). appendsuper (super. compareto (o ). append (this. field1, myclass. Field1 ). append (this. field2, myclass. field2 ). append (this. field3, myclass. field3 ). tocomparison () ;}}

    if you need to compare the entire object, then simply reflectioncompare, then we can get the comparison result of each bit ~

     Public int compareto (Object O) {return comparetobuilder. reflectioncompare (this, O) ;}
  • equalsbuilder: Add the operator as an object. equals (object) methods.

    similar to equalsbuilder and comparetobuilder, this is only a part equal to the actual addition. If the comparison between the two objects is only limited to the comparison between some operators, then we can use the following methods to confirm whether the values are equal ..

     Public Boolean equals (Object O) {If (! (O instanceof myclass) {return false;} myclass RHS = (myclass) O; return New equalsbuilder (). appendsuper (super. equals (o )). append (field1, RHS. field1 ). append (field2, RHS. field2 ). append (field3, RHS. field3 ). isequals () ;}

    Of course, you can also compare all the features in the object to see if they are equal ..

     Public Boolean equals (Object O) {return javassbuilder. reflectionequals (this, O) ;}
  • hashcodebuilder: Add a token to implement the object. hashcode () methods.

    it is best to have a hashcode in the class so that it can store data in the root component to generate a unique hash value, however, creating a hashcode generator may be very troublesome. You do not need to modify it. Using commons-Lang will make the program simpler ~

     public class person {string name; int age; Boolean issmoker ;... public int hashcode () {// You can abort (hard-coded) to generate a digital value, as long as it is non-zero and an odd value. // It is best to have different hashcode values for each class. return new hashcodebuilder (17, 37 ). append (name ). append (AGE ). append (smoker ). tohashcode () ;}}

    if all attention is generated, it can be handled through reflection.

     Public int hashcode () {return hashcodebuilder. reflectionhashcode (this) ;}
  • tostringbuilder: Add struct as an object. tostring () methods.

    we will require that the project token be written into tostring () to overwrite the object in all vo. tostring (), because this debugging time does not need to be re-checked to find the desired region,

     public class person {string name; int age; Boolean issmoker ;... public String tostring () {return New tostringbuilder (this ). append ("name", name ). append ("Age", age ). append ("smoker", smoker ). tostring () ;}

    if all features are generated You can use reflectiontostring (this) directly. If you want to debug the Program program created by other companies, you can also use tostringbuilder. reflectiontostring (xyzobject); to learn about the features of the object's data value ~

     Public String tostring () {return tostringbuilder. reflectiontostring (this) ;}
  • Reflectiontostringbuilder:Use reflection and annotate to implement object. tostring () methods.

    Reflectiontostringbuilder is extends tostringbuilder, which completes tostring through reflection. Its usage is similar to tostringbuilder. reflectiontostring.

    Public String tostring () {return reflectiontostringbuilder. tostring (this );}

    However, if you do not want to display the password bit, you can use accept to handle the value that is not displayed.

    Public String tostring () {return (New reflectiontostringbuilder (this) {protected Boolean accept (field F) {return Super. Accept (f )&&! F. getname (). Equals ("password") ;}}). tostring ();}

Section 04Org. Apache. commons. Lang. Enum

This enumeration is the sub-package originally built by C producer. You only need extends Enum to obtain Enum and map in a simple manner, list and iterator ~

Public final class colorenum extends Enum {public static final colorenum Red = new colorenum ("red"); public static final colorenum Green = new colorenum ("green "); public static final colorenum Blue = new colorenum ("blue"); Private colorenum (string color) {super (color) ;}public static colorenum getenum (string color) {return (colorenum) getenum (colorenum. class, color);} public static map getenummap () {return getenummap (colorenum. class);} public static list getenumlist () {return getenumlist (colorenum. class);} public static iterator () {return iterator (colorenum. class );}}

You only need to pass through colorenum. getenum (colorenum. Red) to obtain enumeration.

Section 05Others

Others, such as exception, math, and time, are not very likely to be used. Most of the exceptions are handling nestable exceptions. Math is a range and computing tool, in the end, time is a tool for date and time. However, stopwatch is an interesting tool. It is usually used to calculate the time of a row, available. you can test the API on your own and use it directly if necessary.Section 06Conclusion

Jakarta commons-lang provides many useful development tools. In particular, null and reflection are very difficult for general engineering developers. Therefore, if we can solve the problem of using commons-Lang, we can use Java in large-scale development more easily.

test-related topics or related articles
  1. Jakarta commons:
    http://jakarta.apache.org/commons/
  2. Jakarta commons Lang
    http://jakarta.apache.org/commons/lang.html
  3. Jakarta commons Lang API:
    http://jakarta.apache.org/commons/lang/api/index.html
  4. oreilly: using the Jakarta commons, Part 1:
    http://www.onjava.com/pub/a/onjava/2003/06/25/commons.html? Page = 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.