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).

    If the precondition is not met, the Preconditions method throws an unchecked exception of a specified type, which helps the method in which the exception was thrown communicate that its caller has made a mistake.

    • Method Detail

      • requireNonNegative

        public static byte requireNonNegative​(byte obj)
        Checks that the specified number is non-negative. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireNonNegative(bar);
         }
         
        Parameters:
        obj - the number to check for negativity
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requireNonNegative

        public static byte requireNonNegative​(byte obj,
                                              String message)
        Checks that the specified number is non-negative and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireNonNegative(bar, "bar must be non-negative");
         }
         
        Parameters:
        obj - the number to check for negativity
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requireNonNegative

        public static int requireNonNegative​(int obj)
        Checks that the specified number is non-negative. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireNonNegative(bar);
         }
         
        Parameters:
        obj - the number to check for negativity
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requireNonNegative

        public static int requireNonNegative​(int obj,
                                             String message)
        Checks that the specified number is non-negative and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireNonNegative(bar, "bar must be non-negative");
         }
         
        Parameters:
        obj - the number to check for negativity
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requireNonNegative

        public static long requireNonNegative​(long obj)
        Checks that the specified number is non-negative. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireNonNegative(bar);
         }
         
        Parameters:
        obj - the number to check for negativity
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requireNonNegative

        public static long requireNonNegative​(long obj,
                                              String message)
        Checks that the specified number is non-negative and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireNonNegative(bar, "bar must be non-negative");
         }
         
        Parameters:
        obj - the number to check for negativity
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requireNonNegative

        public static short requireNonNegative​(short obj)
        Checks that the specified number is non-negative. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireNonNegative(bar);
         }
         
        Parameters:
        obj - the number to check for negativity
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requireNonNegative

        public static short requireNonNegative​(short obj,
                                               String message)
        Checks that the specified number is non-negative and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireNonNegative(bar, "bar must be non-negative");
         }
         
        Parameters:
        obj - the number to check for negativity
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requireNonNegative

        public static float requireNonNegative​(float obj)
        Checks that the specified number is non-negative. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireNonNegative(bar);
         }
         
        Parameters:
        obj - the number to check for negativity
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requireNonNegative

        public static float requireNonNegative​(float obj,
                                               String message)
        Checks that the specified number is non-negative and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireNonNegative(bar, "bar must be non-negative");
         }
         
        Parameters:
        obj - the number to check for negativity
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requireNonNegative

        public static double requireNonNegative​(double obj)
        Checks that the specified number is non-negative. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireNonNegative(bar);
         }
         
        Parameters:
        obj - the number to check for negativity
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requireNonNegative

        public static double requireNonNegative​(double obj,
                                                String message)
        Checks that the specified number is non-negative and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireNonNegative(bar, "bar must be non-negative");
         }
         
        Parameters:
        obj - the number to check for negativity
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requireNonNegative

        public static Duration requireNonNegative​(Duration obj)
        Checks that the specified number is non-negative. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireNonNegative(bar);
         }
         
        Parameters:
        obj - the number to check for negativity
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
        NullPointerException - if obj is null
      • requireNonNegative

        public static Duration requireNonNegative​(Duration obj,
                                                  String message)
        Checks that the specified number is non-negative and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireNonNegative(bar, "bar must be non-negative");
         }
         
        Parameters:
        obj - the number to check for negativity
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
        NullPointerException - if obj is null
      • requirePositive

        public static byte requirePositive​(byte obj)
        Checks that the specified number is positive. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requirePositive(bar);
         }
         
        Parameters:
        obj - the number to check for negativity
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requirePositive

        public static byte requirePositive​(byte obj,
                                           String message)
        Checks that the specified number is positive and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requirePositive(bar, "bar must be positive");
         }
         
        Parameters:
        obj - the number to check for positivity
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if positive
        Throws:
        IllegalArgumentException - if obj is not positive
      • requirePositive

        public static int requirePositive​(int obj)
        Checks that the specified number is positive. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requirePositive(bar);
         }
         
        Parameters:
        obj - the number to check for negativity
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requirePositive

        public static int requirePositive​(int obj,
                                          String message)
        Checks that the specified number is positive and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requirePositive(bar, "bar must be positive");
         }
         
        Parameters:
        obj - the number to check for positivity
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if positive
        Throws:
        IllegalArgumentException - if obj is not positive
      • requirePositive

        public static long requirePositive​(long obj)
        Checks that the specified number is positive. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requirePositive(bar);
         }
         
        Parameters:
        obj - the number to check for negativity
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requirePositive

        public static long requirePositive​(long obj,
                                           String message)
        Checks that the specified number is positive and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requirePositive(bar, "bar must be positive");
         }
         
        Parameters:
        obj - the number to check for positivity
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if positive
        Throws:
        IllegalArgumentException - if obj is not positive
      • requirePositive

        public static short requirePositive​(short obj)
        Checks that the specified number is positive. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requirePositive(bar);
         }
         
        Parameters:
        obj - the number to check for positivity
        Returns:
        obj if positive
        Throws:
        IllegalArgumentException - if obj is not positive
      • requirePositive

        public static short requirePositive​(short obj,
                                            String message)
        Checks that the specified number is positive and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requirePositive(bar, "bar must be positive");
         }
         
        Parameters:
        obj - the number to check for positivity
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if positive
        Throws:
        IllegalArgumentException - if obj is not positive
      • requirePositive

        public static float requirePositive​(float obj)
        Checks that the specified number is positive. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requirePositive(bar);
         }
         
        Parameters:
        obj - the number to check for negativity
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requirePositive

        public static float requirePositive​(float obj,
                                            String message)
        Checks that the specified number is positive and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requirePositive(bar, "bar must be positive");
         }
         
        Parameters:
        obj - the number to check for positivity
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if positive
        Throws:
        IllegalArgumentException - if obj is not positive
      • requirePositive

        public static double requirePositive​(double obj)
        Checks that the specified number is positive. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requirePositive(bar);
         }
         
        Parameters:
        obj - the number to check for negativity
        Returns:
        obj if non-negative
        Throws:
        IllegalArgumentException - if obj is negative
      • requirePositive

        public static double requirePositive​(double obj,
                                             String message)
        Checks that the specified number is positive and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requirePositive(bar, "bar must be positive");
         }
         
        Parameters:
        obj - the number to check for positivity
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if positive
        Throws:
        IllegalArgumentException - if obj is not positive
      • requireInRange

        public static byte requireInRange​(byte obj,
                                          byte min,
                                          byte max)
        Checks that the specified number is in the given range [min, max]. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireInRange(bar, min, max);
         }
         
        Parameters:
        obj - the number to check for range
        Returns:
        obj if in range
        Throws:
        IllegalArgumentException - if obj is not in range
      • requireInRange

        public static byte requireInRange​(byte obj,
                                          byte min,
                                          byte max,
                                          String message)
        Checks that the specified number is in the given range [min, max] and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireInRange(bar, min, max, "bar must be positive");
         }
         
        Parameters:
        obj - the number to check for range
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if in range
        Throws:
        IllegalArgumentException - if obj is not in range
      • requireInRange

        public static int requireInRange​(int obj,
                                         int min,
                                         int max)
        Checks that the specified number is in the given range [min, max]. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireInRange(bar, min, max);
         }
         
        Parameters:
        obj - the number to check for range
        Returns:
        obj if in range
        Throws:
        IllegalArgumentException - if obj is not in range
      • requireInRange

        public static int requireInRange​(int obj,
                                         int min,
                                         int max,
                                         String message)
        Checks that the specified number is in the given range [min, max] and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireInRange(bar, min, max, "bar must be positive");
         }
         
        Parameters:
        obj - the number to check for range
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if in range
        Throws:
        IllegalArgumentException - if obj is not in range
      • requireInRange

        public static long requireInRange​(long obj,
                                          long min,
                                          long max)
        Checks that the specified number is in the given range [min, max]. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireInRange(bar, min, max);
         }
         
        Parameters:
        obj - the number to check for range
        Returns:
        obj if in range
        Throws:
        IllegalArgumentException - if obj is not in range
      • requireInRange

        public static long requireInRange​(long obj,
                                          long min,
                                          long max,
                                          String message)
        Checks that the specified number is in the given range [min, max] and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireInRange(bar, min, max, "bar must be positive");
         }
         
        Parameters:
        obj - the number to check for range
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if in range
        Throws:
        IllegalArgumentException - if obj is not in range
      • requireInRange

        public static short requireInRange​(short obj,
                                           short min,
                                           short max)
        Checks that the specified number is in the given range [min, max]. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireInRange(bar, min, max);
         }
         
        Parameters:
        obj - the number to check for range
        Returns:
        obj if in range
        Throws:
        IllegalArgumentException - if obj is not in range
      • requireInRange

        public static short requireInRange​(short obj,
                                           short min,
                                           short max,
                                           String message)
        Checks that the specified number is in the given range [min, max] and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireInRange(bar, min, max, "bar must be positive");
         }
         
        Parameters:
        obj - the number to check for range
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if in range
        Throws:
        IllegalArgumentException - if obj is not in range
      • requireInRange

        public static float requireInRange​(float obj,
                                           float min,
                                           float max)
        Checks that the specified number is in the given range [min, max]. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireInRange(bar, min, max);
         }
         
        Parameters:
        obj - the number to check for range
        Returns:
        obj if in range
        Throws:
        IllegalArgumentException - if obj is not in range
      • requireInRange

        public static float requireInRange​(float obj,
                                           float min,
                                           float max,
                                           String message)
        Checks that the specified number is in the given range [min, max] and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireInRange(bar, min, max, "bar must be positive");
         }
         
        Parameters:
        obj - the number to check for range
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if in range
        Throws:
        IllegalArgumentException - if obj is not in range
      • requireInRange

        public static double requireInRange​(double obj,
                                            double min,
                                            double max)
        Checks that the specified number is in the given range [min, max]. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireInRange(bar, min, max);
         }
         
        Parameters:
        obj - the number to check for range
        Returns:
        obj if in range
        Throws:
        IllegalArgumentException - if obj is not in range
      • requireInRange

        public static double requireInRange​(double obj,
                                            double min,
                                            double max,
                                            String message)
        Checks that the specified number is in the given range [min, max] and throws a customized IllegalArgumentException if it is not. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below:
         public Foo(int bar) {
             this.bar = Preconditions.requireInRange(bar, min, max, "bar must be positive");
         }
         
        Parameters:
        obj - the number to check for range
        message - detail message to be used in the event that a IllegalArgumentException is thrown
        Returns:
        obj if in range
        Throws:
        IllegalArgumentException - if obj is not in range