Class Preconditions


  • public final class Preconditions
    extends Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void checkArgument​(boolean expression)
      Validates the expression is true.
      static void checkArgument​(boolean expression, Object errorMessage)
      Validates the expression is true.
      static void checkArgument​(boolean expression, String errorMessageTemplate, Object... errorMessageArgs)
      Validates the expression is true.
      static <T> T checkNotNull​(T reference)
      Validates the object is not null.
      static <T> T checkNotNull​(T reference, Object errorMessage)
      Validates the object is not null.
      static <T> T checkNotNull​(T reference, String errorMessageTemplate, Object... errorMessageArgs)
      Validates the object is not null.
      static void checkState​(boolean expression)
      Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.
      static void checkState​(boolean expression, Object errorMessage)
      Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.
    • Method Detail

      • checkArgument

        public static void checkArgument​(boolean expression)
        Validates the expression is true.
        Parameters:
        expression - a boolean expression
        Throws:
        IllegalArgumentException - if expression is false
      • checkArgument

        public static void checkArgument​(boolean expression,
                                         @Nullable
                                         Object errorMessage)
        Validates the expression is true.
        Parameters:
        expression - a boolean expression
        errorMessage - Error message to use.
        Throws:
        IllegalArgumentException - if expression is false
      • checkArgument

        public static void checkArgument​(boolean expression,
                                         @Nullable
                                         String errorMessageTemplate,
                                         @Nullable
                                         Object... errorMessageArgs)
        Validates the expression is true.
        Parameters:
        expression - a boolean expression
        errorMessageTemplate - String template
        errorMessageArgs - Arguments for the string template
        Throws:
        IllegalArgumentException - if expression is false
        NullPointerException - if the check fails and either errorMessageTemplate or errorMessageArgs is null (don't let this happen)
      • checkNotNull

        public static <T> T checkNotNull​(T reference)
        Validates the object is not null.
        Parameters:
        reference - an object reference
        Returns:
        the non-null reference that was validated
        Throws:
        NullPointerException - if reference is null
      • checkNotNull

        public static <T> T checkNotNull​(T reference,
                                         @Nullable
                                         Object errorMessage)
        Validates the object is not null.
        Parameters:
        reference - an object reference
        errorMessage - the exception message to use if the check fails; will be converted to a string using String.valueOf(Object)
        Returns:
        the non-null reference that was validated
        Throws:
        NullPointerException - if reference is null
      • checkNotNull

        public static <T> T checkNotNull​(T reference,
                                         @Nullable
                                         String errorMessageTemplate,
                                         @Nullable
                                         Object... errorMessageArgs)
        Validates the object is not null.
        Parameters:
        reference - an object reference
        errorMessageTemplate - Template to be used in the message.
        errorMessageArgs - Arguments to be supplied to the message
        Returns:
        the non-null reference that was validated
        Throws:
        NullPointerException - if reference is null
      • checkState

        public static void checkState​(boolean expression)
        Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.
        Parameters:
        expression - a boolean expression
        Throws:
        IllegalStateException - if expression is false
      • checkState

        public static void checkState​(boolean expression,
                                      @Nullable
                                      Object errorMessage)
        Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.
        Parameters:
        expression - a boolean expression
        errorMessage - the exception message to use if the check fails; will be converted to a string using String.valueOf(Object)
        Throws:
        IllegalStateException - if expression is false