Class Preconditions


  • public final class Preconditions
    extends Object
    Static convenience methods that help a method or constructor check whether it was invoked correctly (that is, whether its preconditions were met).
    • Method Detail

      • checkNotNull

        public static <T> T checkNotNull​(T reference)
        Ensures that an object reference passed as a parameter to the calling method is not null.
        Parameters:
        reference - an object reference
        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.
        Parameters:
        expression - a boolean expression that must be checked to be true
        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.
        Parameters:
        expression - a boolean expression that must be checked to be true
        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