Class Preconditions


  • public class Preconditions
    extends java.lang.Object
    Static convenience methods that help a method or constructor check whether it was invoked correctly.
    • Constructor Summary

      Constructors 
      Constructor Description
      Preconditions()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void checkElementIndex​(int index, int size)
      Ensures that index specifies a valid element in an array, list or string of size size.
      static void checkElementIndex​(int index, int size, java.lang.String desc)
      Ensures that index specifies a valid element in an array, list or string of size size.
      static <T> T checkNotNull​(T reference)
      Ensures that an object reference passed as a parameter to the calling method is not null.
      static <T> T checkNotNull​(T reference, java.lang.Object errorMessage)
      Ensures that an object reference passed as a parameter to the calling method is not null.
      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.
      static void checkState​(boolean expression, java.lang.Object errorMessage)
      Ensures the truth of an expression involving the state of the calling instance, but not involving any parameters to the calling method.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Preconditions

        public Preconditions()
    • Method Detail

      • checkNotNull

        public static <T> T checkNotNull​(@Nullable
                                         T reference)
        Ensures that an object reference passed as a parameter to the calling method is not null.
        Parameters:
        reference - an object reference
        Throws:
        java.lang.NullPointerException - if reference is null
      • checkNotNull

        public static <T> T checkNotNull​(@Nullable
                                         T reference,
                                         java.lang.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)
        Throws:
        java.lang.NullPointerException - if reference is null
      • checkElementIndex

        public static void checkElementIndex​(int index,
                                             int size)
        Ensures that index specifies a valid element in an array, list or string of size size. An element index may range from zero, inclusive, to size, exclusive.
        Parameters:
        index - a user-supplied index identifying an element of an array, list or string
        size - the size of that array, list or string
        Throws:
        java.lang.IndexOutOfBoundsException - if index is negative or is not less than size
        java.lang.IllegalArgumentException - if size is negative
      • checkElementIndex

        public static void checkElementIndex​(int index,
                                             int size,
                                             java.lang.String desc)
        Ensures that index specifies a valid element in an array, list or string of size size. An element index may range from zero, inclusive, to size, exclusive.
        Parameters:
        index - a user-supplied index identifying an element of an array, list or string
        size - the size of that array, list or string
        desc - the text to use to describe this index in an error message
        Throws:
        java.lang.IndexOutOfBoundsException - if index is negative or is not less than size
        java.lang.IllegalArgumentException - if size is negative
      • 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:
        java.lang.IllegalStateException - if expression is false
      • checkState

        public static void checkState​(boolean expression,
                                      @Nullable
                                      java.lang.Object errorMessage)
        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
        errorMessage - the exception message to use if the check fails; will be converted to a string using String.valueOf(Object)
        Throws:
        java.lang.IllegalStateException - if expression is false