Class Assertions



  • public final class Assertions
    extends Object

    Design by contract assertions.

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

    • Method Detail

      • notNull

        public static <T> T notNull​(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:
        IllegalArgumentException - if value is null
      • isTrue

        public static void isTrue​(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:
        IllegalStateException - if the condition is false
      • isTrueArgument

        public static void isTrueArgument​(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:
        IllegalArgumentException - if the condition is false
      • isTrueArgument

        public static <T> T isTrueArgument​(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:
        IllegalArgumentException - if the condition is false
      • convertToType

        public static <T> T convertToType​(Class<T> clazz,
                                          Object value,
                                          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:
        IllegalArgumentException - if value is not assignable to clazz