Class Preconditions


  • public class Preconditions
    extends Object
    Simple static methods to be called at the start of your own methods to verify correct arguments and state.
    • Constructor Detail

      • Preconditions

        public Preconditions()
    • Method Detail

      • checkArgument

        public static void checkArgument​(boolean expression)
      • 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
      • checkNotNull

        public static <T> T checkNotNull​(T reference,
                                         Object errorMessage)
        Ensures that an object reference passed as a parameter to the calling method 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
      • 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
      • checkFlagsArgument

        public static void checkFlagsArgument​(int requestedFlags,
                                              int allowedFlags)
        Check the requested flags, throwing if any requested flags are outside the allowed set.
      • checkArgumentNonnegative

        public static int checkArgumentNonnegative​(int value,
                                                   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
        Returns:
        the validated numeric value
        Throws:
        IllegalArgumentException - if value was negative
      • checkArgumentNonnegative

        public static long checkArgumentNonnegative​(long value,
                                                    String errorMessage)
        Ensures that that the argument numeric value is non-negative.
        Parameters:
        value - a numeric long value
        errorMessage - the exception message to use if the check fails
        Returns:
        the validated numeric value
        Throws:
        IllegalArgumentException - if value was negative
      • checkArgumentPositive

        public static int checkArgumentPositive​(int value,
                                                String errorMessage)
        Ensures that that the argument numeric value is positive.
        Parameters:
        value - a numeric int value
        errorMessage - the exception message to use if the check fails
        Returns:
        the validated numeric value
        Throws:
        IllegalArgumentException - if value was not positive
      • checkArgumentFinite

        public static float checkArgumentFinite​(float value,
                                                String valueName)
        Ensures that the argument floating point value is a finite number.

        A finite number is defined to be both representable (that is, not NaN) and not infinite (that is neither positive or negative infinity).

        Parameters:
        value - a floating point value
        valueName - the name of the argument to use if the check fails
        Returns:
        the validated floating point value
        Throws:
        IllegalArgumentException - if value was not finite
      • checkArgumentInRange

        public static float checkArgumentInRange​(float value,
                                                 float lower,
                                                 float upper,
                                                 String valueName)
        Ensures that the argument floating point value is within the inclusive range.

        While this can be used to range check against +/- infinity, note that all NaN numbers will always be out of range.

        Parameters:
        value - a floating point 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
        Returns:
        the validated floating point value
        Throws:
        IllegalArgumentException - if value was not within the range
      • checkArgumentInRange

        public static int checkArgumentInRange​(int value,
                                               int lower,
                                               int upper,
                                               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
        Returns:
        the validated int value
        Throws:
        IllegalArgumentException - if value was not within the range
      • checkArrayElementsNotNull

        public static <T> T[] checkArrayElementsNotNull​(T[] value,
                                                        String valueName)
        Ensures that the array is not null, and none of its elements are null.
        Parameters:
        value - an array of boxed objects
        valueName - the name of the argument to use if the check fails
        Returns:
        the validated array
        Throws:
        NullPointerException - if the value or any of its elements were null
      • checkCollectionElementsNotNull

        public static <T> Collection<T> checkCollectionElementsNotNull​(Collection<T> value,
                                                                       String valueName)
        Ensures that the Collection is not null, and none of its elements are null.
        Parameters:
        value - a Collection of boxed objects
        valueName - the name of the argument to use if the check fails
        Returns:
        the validated Collection
        Throws:
        NullPointerException - if the value or any of its elements were null
      • checkArrayElementsInRange

        public static float[] checkArrayElementsInRange​(float[] value,
                                                        float lower,
                                                        float upper,
                                                        String valueName)
        Ensures that all elements in the argument floating point array are within the inclusive range

        While this can be used to range check against +/- infinity, note that all NaN numbers will always be out of range.

        Parameters:
        value - a floating point array of values
        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
        Returns:
        the validated floating point value
        Throws:
        IllegalArgumentException - if any of the elements in value were out of range
        NullPointerException - if the value was null