Introduction : The Preconditions Class provides a list of static methods for checking that a method or a constructor is invoked with valid parameter values. If a precondition fails, a tailored exception is thrown.
- These methods generally accept a boolean expression which is expected to be true.
- In the case of checkNotNull, these methods accept object reference which is expected to be non-null.
- When false/null is passed instead, the Preconditions method throws an unchecked exception.
@GwtCompatible public final class Preconditions extends Object
Example 1 : In this example, checkArgument throws an IllegalArgumentException to indicate that examplePre1 made an error in its call to sqrt as the passed argument is negative.
Example 2 : Let us say we are given a method which takes a List as an argument. When this method is invoked we want to check that the list is not null and it is not empty. A simple Java solution for the problem would look like :
But when we use Guava’s Preconditions, the amount of code is reduced significantly. The solution looks something like :
Example 3 : If one wants to validate age to make sure it is greater than 18, one can use Precondition.checkArgument().
Following table shows some of the methods provided by Guava Preconditions.
Each method has three variants, which are as listed below :
- No extra arguments : Any exceptions are thrown without error messages.
- An extra Object argument : Any exception is thrown with the error message object.toString().
- An extra String argument : This method takes an extra String argument with an arbitrary number of additional Object arguments. This behaves something like printf, but for GWT compatibility and efficiency, it only allows %s indicators.
Examples of the third variant :
checkArgument(i >= 0, "Argument was %s but expected nonnegative", i); checkArgument(i < j, "Expected i = %s", i, j);
Some other methods provided by Guava Preconditions are :
Important Points : Below given are some important points regarding Guava Preconditions.
- It is of course possible to use the methods of this class to check for invalid conditions which are not the caller’s fault. Doing so is not recommended because it is misleading to future readers of the code.
- Projects which use com.google.common should generally avoid the use of Objects.requireNonNull(Object). Instead, use whichever of checkNotNull(Object) or Verify.verifyNotNull(Object) is appropriate to the situation.
- In Preconditions error message template strings, only the “%s” specifier is supported, not the full range of Formatter specifiers.
Illegal Argument, Second parameter is Null.
Reference : Google Guava
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.