Guava Preconditions tool parameter pre-check, guavapreconditions

Source: Internet
Author: User

Guava Preconditions tool parameter pre-check, guavapreconditions

Preconditions is a tool class provided by guava for code verification. It provides many important static verification methods.

It is used to simplify code verification or preprocessing in our work or development. It verifies the rationality before the logic starts to avoid data errors caused by too deep parameter input.

In addition, we can accurately display the problem where the verification criteria are not met.

Guava overload many methods on Preconditions to form the Preconditions tool class. It takes 5 minutes to see it.

1. checkArgument
CheckArgument (boolean expression, @ Nullable String errorMessageTemplate, @ Nullable Object... errorMessageArgs): Check whether the expression is true. If the expression is not true, an error message is displayed. Placeholders are allowed in the error message. // Check whether the expression is correct and use the placeholder to output the error message Preconditions. checkArgument (3> 4, "% s is wrong", "3> 4 ");
2. checkState
CheckState (boolean expression): checks whether the expression is true. It is generally used to verify whether the return value is true. CheckState (boolean expression, @ Nullable Object errorMessage): When the expression is false, the specified error message is displayed. CheckState (boolean expression, @ Nullable String errorMessageTemplate, @ Nullable Object... errorMessageArgs): a validation expression allows placeholders to be used in error messages. // Check whether the expression is correct and use the placeholder to output the error message. Use the method as the expression Preconditions. checkState (testState (), "% s is wrong", "testState ()");
3. checkNotNull
CheckNotNull (T reference): checks whether the object is null. CheckNotNull (T reference, @ Nullable Object errorMessage): if the Object is empty, the specified error message is displayed. CheckNotNull (T reference, @ Nullable String errorMessageTemplate, @ Nullable Object... errorMessageArgs): placeholders can be used in error messages. // Check whether the object is null and use the placeholder to output the error message Preconditions. checkNotNull (testObject (), "% s is null", "testObject ()");
4. checkElementIndex
CheckElementIndex (int index, int size, @ Nullable String desc): Check whether the index value of an element is valid. If the index is greater than or equal to 0 and smaller than the size, the error description is displayed when the element is invalid. CheckElementIndex (int index, int size): the error description is "index ". // Check whether the element index is valid. Use checkPositionIndex to verify Preconditions. checkElementIndex (17, list. size ());
5. checkPositionIndex
CheckPositionIndex (int index, int size, @ Nullable String desc): Check whether the index value of an element is valid. If the index is greater than or equal to 0 and smaller than or equal to size, the error description is displayed when the element is invalid. CheckPositionIndex (int index, int size): the error description is "index ". // Check whether the element index is valid. The checkPositionIndex check does not generate an exception Preconditions. checkPositionIndex (17, list. size () at the critical value ());
6. checkPositionIndexes
CheckPositionIndexes (int start, int end, int size): Check whether the length of the list smaller than or equal to start is size. // Check whether the index interval Preconditions. checkPositionIndexes (3, 11, list. size () is valid ());

 

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.