Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Ant Financial Services Group Gongsun: elegant check data of guava inquiry series

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Elegant check data-preface to preconditions

According to the requirements of defensive programming, in the daily development, it is necessary to check the various input parameters of the function in order to ensure that the function can be executed according to the expected flow. For example, the value of various rates can not be negative, if the rate is negative, so there is a problem with the data, what we need to do is to pick out the problematic data. It's too tedious to write these check functions by hand, but fortunately, the functions we need are already available:

Guava provides a series of static methods to verify that the constructors of functions and classes meet expectations and call them preconditions. If the precondition verification fails, a specified exception is thrown.

Pre-function feature

The current pre-verification methods have the following characteristics:

If necessary, the checkArgument function in the following example can be replaced with any preconditioned check function

These pre-methods generally accept a Boolean expression as an input parameter And determine whether the expression is true, such as: Preconditions.checkArgument (a > 1) / / if the expression is false, throw IllegalArgumentException in addition to the Boolean expression used for judgment, the front method can accept an additional Object as the input parameter, and when throwing an exception, take Object.toString () as the exception information, such as: public enum ErrorDetail {SC_NOT_FOUND ("404", "Resource could not be fount") / / omit part @ Override public String toString () {return "ErrorDetail {" + "code='" + code +'\''+ ", description='" + description +'\'+'}';} @ Testpublic void testCheckArgument () {Preconditions.checkArgument (1 > 2, ErrorDetail.SC_NOT_FOUND) } / / the result is as follows: / / the pre-expression of java.lang.IllegalArgumentException: ErrorDetail {code='404', description='Resource could not be fount'} Guava also supports formatting error messages like the printf function, but for compatibility and performance reasons, only the% s indicator is supported to format strings, not other types. For example: int iCheckWhat checkArgument (I > = 0, "Argument was% s but expected nonnegative", I); / / the result is as follows: / / java.lang.IllegalArgumentException: introduction to Argument was-1 but expected nonnegative precondition function

It should be noted that the checkArgument, checkArgument and checkState functions introduced below all have three corresponding overloaded functions, which correspond to the three characteristics mentioned above. The following three functions will not be introduced, only the preconditioned functions of the standard format. Take the checkArgument function as an example, the three overloaded functions are (ignore function body):

Public static void checkArgument (boolean expression); public static void checkArgument (boolean expression,@Nullable Object errorMessage); public static void checkArgument (boolean expression,@Nullable String errorMessageTemplate,@Nullable Object...) ErrorMessageArgs) checkArgument

The signature of the function is as follows:

Public static void checkArgument (boolean expression)

The input parameter is a Boolean expression, and the function verifies whether the expression is true, and if it is false, throws IllegalArgumentException. Examples are as follows:

@ Testpublic void testCheckArgument () {Preconditions.checkArgument (1 > 2);} checkNotNull

This is a generic function with the following signature:

Public static T checkNotNull (T reference)

The input parameter is an object of any type, and the function verifies whether the object is null. If it is empty, throw NullPointerException, otherwise the object will be returned directly, so the use of checkNotNull is more interesting. You can check before calling the setter method. Examples are as follows:

PreconditionTest caller = new PreconditionTest (); caller.setErrorDetail (Preconditions.checkNotNull (ErrorDetail.SC_INTERNAL_SERVER_ERROR)); checkState

The signature of the function is as follows:

Public static void checkState (boolean expression)

Looking at this function, I personally feel very strange: this function and checkNotNull function is very similar, the implementation is basically the same, is to judge whether the expression is true, but the exception thrown is not the same, whether it is necessary to develop this function. The implementation of the two functions is as follows:

Public static void checkArgument (boolean expression) {if (! expression) {throw new IllegalArgumentException ();}} public static void checkState (boolean expression) {if (! expression) {throw new IllegalStateException ();}}

In addition, because the two functions are quite similar, the corresponding examples are not shown.

CheckElementIndex

The signature of the function is as follows:

Public static int checkElementIndex (int index, int size)

This function is used to determine whether the subscript of the specified array, list, or string is out of bounds. Index is the subscript, size is the length of the array, list or string, and the valid range of the subscript is [0, array length), that is, 0.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report