[Translation of Guava official documentation] 3. Preconditions (Explained)

Source: Internet
Author: User

Guava provides utilities for checking preconditions. We strongly recommend static import of these utilities. (How to easily implement it in Eclipse .)

The parameter list of these methods has three types of Deformation:

    • No additional parameters are attached. No error message is returned when an exception is thrown.
    • An additional parameter of the Object type is attached. When an exception is thrown, the error message is object. toString.
    • An additional String parameter and several Object parameters are attached. The function is similar to printf, but to be compatible with GWT and performance, only the % s identifier is allowed. For example:

 

checkArgument(i >=0,"Argument was %s but expected nonnegative", i);
checkArgument(i < j,"Expected i < j, but %s > %s", i, j);

 

Basic Format (excluding additional parameters) Description Exception thrown upon failure
CheckArgument (boolean) BooleanIs it.Used to verify whether the parameters of the method are valid. IllegalArgumentException
CheckNotNull (T) Check whether the parameter value is non-null. Directly return this value, so you can use it internallyCheckNotNull (value). NullPointerException
CheckState (boolean) Check the status of an object, which does not depend on method parameters. For example:IteratorBefore callingYou can use this method to check before removing NextWhether it has been called. IllegalStateException
CheckElementIndex (int index, int size) Check ParametersIndexWhether it is a legal index of the list, string, array, or a given size. A valid index must be greater than or equal to 0 and <size. You do not need to directly pass in the list, string, or array parameter; you just need to pass in its size. ReturnIndex. IndexOutOfBoundsException
CheckPositionIndex (int index, int size) Check ParametersIndexWhether it is the legal position of the list, string, array, or a given size. A valid position must be greater than or equal to 0 and <= size. You do not need to directly pass in the list, string, or array parameter; you just need to pass in its size. ReturnIndex. IndexOutOfBoundsException
CheckPositionIndexes (int start, int end, int size) Check[Start, end)Is a legal subset of the list, string, array, or given size. An exception is thrown with its own error message. IndexOutOfBoundsException

 

Compared with other components, such as comparable utilities in Apache Commons, we prefer to use our own pre-condition check, mainly for the following reasons. Piotr jaielski briefly explains why he chose our utilities:

  • After static import is used, the Guava method is clear and unambiguous.CheckNotNullIt clearly indicates what it is, and the exception that may be thrown.
  • CheckNotNullAfter verification, the parameters are directly returned. Therefore, you can simply write a line in the constructor:This. field = checkNotNull (field ).
  • Simply add the "printf style" exception information of the parameter. (This advantage is also recommended for continued use.CheckNotNullInstead of using Objects. requireNonNull in JDK 7 .)

We recommend that you write the code for each pre-condition check in different lines so that you can see which item failed during debugging. In addition, you should attach a meaningful error message, so it is easier for each check to separate a line.

Explained Translator:

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.