Guava Study Notes: Preconditions elegant validation parameters

Source: Internet
Author: User

During daily development, we often perform some data format verification on the input parameters of the method to ensure that the method can be executed according to the normal process. For predictable data errors, we must check and Judge beforehand to avoid program process errors, rather than making sure that the process is correctly executed through error handling, after all, error handling is a resource-consuming method. Under normal circumstances, we need to write methods one by one to determine the parameters. There are a lot of code and the reusability is not high, as shown below:

Preconditions () 8, "peida"-9, "peida" 8, "" 8, getPerson (age, String neme) (age> 0 & neme! = & Neme. isEmpty ()! = "A person age:" + age + ", neme:" + "Incorrect Parameter input! "

Note: For parameter verification, we need to add the if statement each time for judgment. Repeated operations will be done multiple times. The getPerson method has only two parameters, and the verification rules are not very complex. If the parameters are too complex and the verification rules are complex, the code above will be poorly readable, And the reusability will be far from enough.

The Guava Class Library provides a tool class for parameter check-Preconditions class, which can greatly simplify the pre-judgment and processing of parameters in our code, let's make it easier and more elegant to verify the input parameters of the method. Let's take a look at the Preconditions class usage example:

Preconditions () 8, "peida"-9, "peida" 8, "" 8, getPersonByPrecondition (age, String neme) "neme is null"> 0, "neme is \ '"> 0, "age must be greater than 0" "a person age:" + age + ", neme:" +

Running result:

a person age:8''

  Methods In Preconditions:

  1. checkArgument (boolean ):
Function Description: checks whether boolean is true. Used as a parameter for checking in a method
The exception type thrown when a failure occurs: IllegalArgumentException

  2. checkNotNull (T ):
Function Description: checks whether the value is null and returns the value directly;
The exception type thrown when a failure occurs: NullPointerException

 3. checkState (boolean ):
Function Description: checks the status of an object and does not rely on method parameters. For example, Iterator can be used to determine whether to call next before removing.
The exception type thrown when a failure occurs: IllegalStateException

 4. checkElementIndex (int index, int size ):
Function Description: checks whether the index is within a valid range of a list, string, or array with a length of size. The range of the index is [0, size) (contains 0 and does not contain size ). You do not need to input list, string, or array directly. You only need to input the size. Returns index.
Exception type thrown when a failure occurs: IndexOutOfBoundsException


  5. checkPositionIndex (int index, int size ):
Function Description: checks whether the position index is in a list with a length of size, which is a valid range of string or array. The range of the index is [0, size) (contains 0 and does not contain size ). You do not need to input list, string, or array directly. You only need to input the size. Returns index.
Exception type thrown when a failure occurs: IndexOutOfBoundsException

 6. checkPositionIndexes (int start, int end, int size ):
Function Description: Check whether [start, end) is a list with a length of size, and a valid range subset of string or array. With the error message.
Exception type thrown when a failure occurs: IndexOutOfBoundsException

A more practical example:

Preconditions () 8, "peida"-9, "peida" 8, "" 8, <Integer> intList = ArrayList <Integer> (I = 0; I <10; I ++ 93133,73, 1713,17616 getPersonByPrecondition (age, String neme) "neme is null"> 0, "neme is \ '"> 0, "age must be greater than 0" "a person age:" + age + ", neme:" + checkState (List <Integer> intList, index) Preconditions. checkState (intList. size () <index, "intList size cannot be greater than" + checkPositionIndex (List <Integer> intList, index) "index" + index + "not in list, List size is: "+ checkPositionIndexes (List <Integer> intList, start, end) checkElementIndex (List <Integer> intList, index)" index is "+ index +" not in list, List size is: "+

Output result:

A person age: 8''' 13 is not in the list. The List size is 9 (13) must not be greater than size (917) must not be greater than size (913) must not be greater than size (916 is not in list, List size is: 9 (16) must be less than size (9)

 

Guava's preconditions has the following advantages:

After static import, the method is clear and unambiguous. checkNotNull can clearly tell you what it is and what exceptions it will throw.
CheckNotNull is directly returned after the verification is passed. You can easily write the code: this. field = checkNotNull (field ).
Simple and powerful Variable Parameter 'printf'-style custom error messages.

 

Related Article

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.