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
      • 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