Class Assertions


  • public final class Assertions
    extends java.lang.Object

    Design by contract assertions.

    This class is not part of the public API and may be removed or changed at any time.

    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T convertToType​(java.lang.Class<T> clazz, java.lang.Object value, java.lang.String errorMessage)
      Cast an object to the given class and return it, or throw IllegalArgumentException if it's not assignable to that class.
      static void isTrue​(java.lang.String name, boolean condition)
      Throw IllegalStateException if the condition if false.
      static void isTrueArgument​(java.lang.String name, boolean condition)
      Throw IllegalArgumentException if the condition if false.
      static <T> T isTrueArgument​(java.lang.String name, T value, boolean condition)
      Throw IllegalArgumentException if the condition if false, otherwise return the value.
      static <T> T notNull​(java.lang.String name, T value)
      Throw IllegalArgumentException if the value is null.
      • Methods inherited from class java.lang.Object

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

      • notNull

        public static <T> T notNull​(java.lang.String name,
                                    T value)
        Throw IllegalArgumentException if the value is null.
        Type Parameters:
        T - the value type
        Parameters:
        name - the parameter name
        value - the value that should not be null
        Returns:
        the value
        Throws:
        java.lang.IllegalArgumentException - if value is null
      • isTrue

        public static void isTrue​(java.lang.String name,
                                  boolean condition)
        Throw IllegalStateException if the condition if false.
        Parameters:
        name - the name of the state that is being checked
        condition - the condition about the parameter to check
        Throws:
        java.lang.IllegalStateException - if the condition is false
      • isTrueArgument

        public static void isTrueArgument​(java.lang.String name,
                                          boolean condition)
        Throw IllegalArgumentException if the condition if false.
        Parameters:
        name - the name of the state that is being checked
        condition - the condition about the parameter to check
        Throws:
        java.lang.IllegalArgumentException - if the condition is false
      • isTrueArgument

        public static <T> T isTrueArgument​(java.lang.String name,
                                           T value,
                                           boolean condition)
        Throw IllegalArgumentException if the condition if false, otherwise return the value. This is useful when arguments must be checked within an expression, as when using this to call another constructor, which must be the first line of the calling constructor.
        Type Parameters:
        T - the value type
        Parameters:
        name - the name of the state that is being checked
        value - the value of the argument
        condition - the condition about the parameter to check
        Returns:
        the value
        Throws:
        java.lang.IllegalArgumentException - if the condition is false
      • convertToType

        public static <T> T convertToType​(java.lang.Class<T> clazz,
                                          java.lang.Object value,
                                          java.lang.String errorMessage)
        Cast an object to the given class and return it, or throw IllegalArgumentException if it's not assignable to that class.
        Type Parameters:
        T - the Class type
        Parameters:
        clazz - the class to cast to
        value - the value to cast
        errorMessage - the error message to include in the exception
        Returns:
        value cast to clazz
        Throws:
        java.lang.IllegalArgumentException - if value is not assignable to clazz