This article is a brief introduction Java is used to check incoming parameters Preconditions introduce a friend in need can refer to
Add dependence:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
Preconditions are in a class library Guava for checking incoming parameters, a common usage is as follows:
Preconditions.checkArgument(!ObjectUtils.isEmpty(queryAccountRequest), "入参缺失");
When it is determined parameter is null, an exception is thrown
public static void checkArgument(boolean expression, @Nullable Object errorMessage) {
if(!expression) {
throw new IllegalArgumentException(String.valueOf(errorMessage));
}
}
supplement:
Preconditions open source, we see, guava number of methods have been overloaded, the composition of the Preconditions tools, let's briefly look at all of the 14 static method, as follows:
1.checkArgument (boolean expression): used to verify whether the expression is true, it is generally used as a method of calibration parameters
2.checkArgument (boolean expression, @Nullable Object errorMessage): check whether an expression is true, not true to display the error message specified.
3.checkArgument (boolean expression, @Nullable String errorMessageTemplate, @Nullable Object ... errorMessageArgs): check whether the expression is true, not true when the error message, the error message allowed in the placeholder.
4.checkState (boolean expression): check whether the expression is true, as a general method returns the check is true.
5.checkState (boolean expression, @Nullable Object errorMessage): specified error message when the expression is false when the display.
6.checkState (boolean expression, @ Nullable String errorMessageTemplate, @ Nullable Object ... errorMessageArgs): allows the use of a placeholder in the error message.
7.checkNotNull (T reference): check whether the object is empty.
8.checkNotNull (T reference, @Nullable Object errorMessage): error information of the specified object is empty.
9.checkNotNull (T reference, @Nullable String errorMessageTemplate, @ Nullable Object ... errorMessageArgs): allows the use of a placeholder in the error message.
10.checkElementIndex (int index, int size, @Nullable String desc): index of the element values of the check is valid, index greater than 0 but less than equal size, display an error message when invalid described.
11.checkElementIndex (int index, int size): error description for "index"
12.checkPositionIndex (int index, int size, @Nullable String desc): index of the element values of the check is valid, index greater than or equal to 0 less than or equal size, display an error message when invalid described.
13.checkPositionIndex (int index, int size): error description for "index"
14.checkPositionIndexes (int start, int end, int size): greater than or equal calibration start, is less than the length of the end of the list whether the size.