Class Preconditions


  • public final class Preconditions
    extends Object
    Verifies correct argument values and state. Borrowed from Guava.
    Author:
    [email protected] (Alex Ruiz)
    • Constructor Detail

      • Preconditions

        private Preconditions()
    • Method Detail

      • checkNotNullOrEmpty

        public static CharSequence checkNotNullOrEmpty​(CharSequence s)
        Verifies that the given CharSequence is not null or empty.
        Parameters:
        s - the given CharSequence.
        Returns:
        the validated CharSequence.
        Throws:
        NullPointerException - if the given CharSequence is null.
        IllegalArgumentException - if the given CharSequence is empty.
      • checkNotNullOrEmpty

        public static CharSequence checkNotNullOrEmpty​(CharSequence s,
                                                       String message)
        Verifies that the given CharSequence is not null or empty.
        Parameters:
        s - the given CharSequence.
        message - error message in case of empty String.
        Returns:
        the validated CharSequence.
        Throws:
        NullPointerException - if the given CharSequence is null.
        IllegalArgumentException - if the given CharSequence is empty.
      • checkNotNullOrEmpty

        public static <T> T[] checkNotNullOrEmpty​(T[] array)
        Verifies that the given array is not null or empty.
        Type Parameters:
        T - the type of elements of the array.
        Parameters:
        array - the given array.
        Returns:
        the validated array.
        Throws:
        NullPointerException - if the given array is null.
        IllegalArgumentException - if the given array is empty.
      • checkNotNull

        @Deprecated
        public static <T> T checkNotNull​(T reference)
        Deprecated.
        Verifies that the given object reference is not null.
        Type Parameters:
        T - the type of the reference to check.
        Parameters:
        reference - the given object reference.
        Returns:
        the non-null reference that was validated.
        Throws:
        NullPointerException - if the given object reference is null.
      • checkNotNull

        @Deprecated
        public static <T> T checkNotNull​(T reference,
                                         String message)
        Deprecated.
        Verifies that the given object reference is not null.
        Type Parameters:
        T - the type of the reference to check.
        Parameters:
        reference - the given object reference.
        message - error message in case of null reference.
        Returns:
        the non-null reference that was validated.
        Throws:
        NullPointerException - if the given object reference is null.
      • checkArgument

        public static void checkArgument​(boolean expression,
                                         String errorMessageTemplate,
                                         Object... errorMessageArgs)
        Ensures the truth of an expression involving one or more parameters to the calling method.

        Borrowed from Guava.

        Parameters:
        expression - a boolean expression
        errorMessageTemplate - a template for the exception message should the check fail. The message is formed by calling String.format(String, Object...) with the given parameters.
        errorMessageArgs - the arguments to be substituted into the message template.
        Throws:
        IllegalArgumentException - if expression is false
        NullPointerException - if the check fails and either errorMessageTemplate or errorMessageArgs is null (don't let this happen)
      • checkState

        public static void checkState​(boolean expression,
                                      String errorMessageTemplate,
                                      Object... errorMessageArgs)
        Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.

        Borrowed from Guava.

        Parameters:
        expression - a boolean expression
        errorMessageTemplate - a template for the exception message should the check fail.The message is formed by calling String.format(String, Object...) with the given parameters.
        errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to strings using String.valueOf(Object).
        Throws:
        IllegalStateException - if expression is false
        NullPointerException - if the check fails and either errorMessageTemplate or errorMessageArgs is null (don't let this happen)
      • throwExceptionForBeingEmpty

        private static void throwExceptionForBeingEmpty()
      • throwExceptionForBeingEmpty

        private static void throwExceptionForBeingEmpty​(String message)