Preconditions is a tool class provided by guava for code verification, which provides many important static verification methods.
It is used to simplify the code verification or preprocessing in our work or development, and to check the rationality before the logic starts, so as to avoid data errors caused by too deep parameter input.
And it can accurately show us the problem where the verification conditions are not met.
guava overloads the Preconditions with a lot of methods and forms the Preconditions tool class. It's easy to take 5 minutes to see.
1.checkArgument
checkArgument(boolean expression, @Nullable String errorMessageTemplate, @Nullable Object... errorMessageArgs): Check whether the expression is true. If it is not true, an error message will be displayed. Placeholders are allowed in the error message. //Verify that the expression is correct and use placeholders to output error information Preconditions.checkArgument(3 > 4, "%s is wrong", "3 > 4");
2.checkState
checkState(boolean expression): Check whether the expression is true, generally used as a check method to return whether it is true. checkState(boolean expression, @Nullable Object errorMessage): Displays the specified error message when the expression is false. checkState(boolean expression,@Nullable String errorMessageTemplate,@Nullable Object... errorMessageArgs): Validation expressions allow placeholders to be used in error messages. //Verify that the expression is correct, use placeholders to output error information, and use methods as expressions Preconditions.checkState(testState(), "%s is wrong", "testState()");
3.checkNotNull
checkNotNull(T reference): Verify that the object is empty. checkNotNull(T reference, @Nullable Object errorMessage): Displays the specified error message when the object is empty. checkNotNull(T reference, @Nullable String errorMessageTemplate,@Nullable Object... errorMessageArgs): Allow placeholders in error messages. //Verify that the object is empty and use placeholders to output error messages Preconditions.checkNotNull(testObject(), "%s is null", "testObject()");
4.checkElementIndex
checkElementIndex( int index, int size, @Nullable String desc): Verify that the index value of the element is valid, index Greater than or equal to 0 less than size,Displays error description information when invalid. checkElementIndex(int index, int size): The error description is“ index". //Check whether the element index is valid, use checkPositionIndex check Preconditions.checkElementIndex(17, list.size());
5.checkPositionIndex
checkPositionIndex(int index, int size, @Nullable String desc): Verify that the index value of the element is valid,index Greater than or equal to 0 less than or equal to size,Displays error description information when invalid. checkPositionIndex(int index, int size): The error description is“ index". //Check whether the element index is valid, use checkPositionIndex Check,No exception at critical value Preconditions.checkPositionIndex(17, list.size());
6.checkPositionIndexes
checkPositionIndexes(int start, int end, int size): Verification greater than or equal to start,less than end Of list Is the length of size. //Check whether it is a valid index range Preconditions.checkPositionIndexes(3, 11, list.size());