Package 

Class Preconditions


  • 
    public final class Preconditions
    
                        

    Simple static methods to be called at the start of your own methods to verify correct arguments and state.

    • Method Summary

      Modifier and Type Method Description
      static void checkArgument(boolean expression)
      static void checkArgument(boolean expression, @NonNull() Object errorMessage) Ensures that an expression checking an argument is true.
      static <T extends CharSequence> T checkStringNotEmpty(@Nullable() T string) Ensures that an string reference passed as a parameter to the callingmethod is not empty.
      static <T extends CharSequence> T checkStringNotEmpty(@Nullable() T string, @NonNull() Object errorMessage) Ensures that an string reference passed as a parameter to the callingmethod is not empty.
      static <T extends CharSequence> T checkStringNotEmpty(@Nullable() T string, @NonNull() String messageTemplate, @NonNull() Array<Object> messageArgs) Ensures that an string reference passed as a parameter to the calling method is not empty.
      static <T> T checkNotNull(@Nullable() T reference) Ensures that an object reference passed as a parameter to the callingmethod is not null.
      static <T> T checkNotNull(@Nullable() T reference, @NonNull() Object errorMessage) Ensures that an object reference passed as a parameter to the callingmethod is not null.
      static void checkState(boolean expression, @Nullable() String message) Ensures the truth of an expression involving the state of the callinginstance, but not involving any parameters to the calling method.
      static void checkState(boolean expression) Ensures the truth of an expression involving the state of the callinginstance, but not involving any parameters to the calling method.
      static int checkArgumentNonnegative(int value, @Nullable() String errorMessage) Ensures that that the argument numeric value is non-negative.
      static int checkArgumentNonnegative(int value) Ensures that that the argument numeric value is non-negative.
      static int checkArgumentInRange(int value, int lower, int upper, @NonNull() String valueName) Ensures that the argument int value is within the inclusive range.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • checkArgument

         static void checkArgument(boolean expression, @NonNull() Object errorMessage)

        Ensures that an expression checking an argument is true.

        Parameters:
        expression - the expression to check
        errorMessage - the exception message to use if the check fails; willbe converted to a string using valueOf
      • checkStringNotEmpty

        @NonNull() static <T extends CharSequence> T checkStringNotEmpty(@Nullable() T string)

        Ensures that an string reference passed as a parameter to the callingmethod is not empty.

        Parameters:
        string - an string reference
      • checkStringNotEmpty

        @NonNull() static <T extends CharSequence> T checkStringNotEmpty(@Nullable() T string, @NonNull() Object errorMessage)

        Ensures that an string reference passed as a parameter to the callingmethod is not empty.

        Parameters:
        string - an string reference
        errorMessage - the exception message to use if the check fails; willbe converted to a string using valueOf
      • checkStringNotEmpty

        @NonNull() static <T extends CharSequence> T checkStringNotEmpty(@Nullable() T string, @NonNull() String messageTemplate, @NonNull() Array<Object> messageArgs)

        Ensures that an string reference passed as a parameter to the calling method is not empty.

        Parameters:
        string - an string reference
        messageTemplate - a printf-style message template to use if the check fails; will beconverted to a string using format
        messageArgs - arguments for {@code messageTemplate}
      • checkNotNull

        @NonNull() static <T> T checkNotNull(@Nullable() T reference)

        Ensures that an object reference passed as a parameter to the callingmethod is not null.

        Parameters:
        reference - an object reference
      • checkNotNull

        @NonNull() static <T> T checkNotNull(@Nullable() T reference, @NonNull() Object errorMessage)

        Ensures that an object reference passed as a parameter to the callingmethod is not null.

        Parameters:
        reference - an object reference
        errorMessage - the exception message to use if the check fails; willbe converted to a string using valueOf
      • checkState

         static void checkState(boolean expression, @Nullable() String message)

        Ensures the truth of an expression involving the state of the callinginstance, but not involving any parameters to the calling method.

        Parameters:
        expression - a boolean expression
        message - exception message
      • checkState

         static void checkState(boolean expression)

        Ensures the truth of an expression involving the state of the callinginstance, but not involving any parameters to the calling method.

        Parameters:
        expression - a boolean expression
      • checkArgumentNonnegative

         static int checkArgumentNonnegative(int value, @Nullable() String errorMessage)

        Ensures that that the argument numeric value is non-negative.

        Parameters:
        value - a numeric int value
        errorMessage - the exception message to use if the check fails
      • checkArgumentNonnegative

         static int checkArgumentNonnegative(int value)

        Ensures that that the argument numeric value is non-negative.

        Parameters:
        value - a numeric int value
      • checkArgumentInRange

         static int checkArgumentInRange(int value, int lower, int upper, @NonNull() String valueName)

        Ensures that the argument int value is within the inclusive range.

        Parameters:
        value - a int value
        lower - the lower endpoint of the inclusive range
        upper - the upper endpoint of the inclusive range
        valueName - the name of the argument to use if the check fails