Class Validate


  • public final class Validate
    extends java.lang.Object
    Validators to check that method arguments meet expectations.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Validate()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static boolean assertFail​(java.lang.String msg)
      Cause a failure, but return false so it can be used in an assert statement.
      static java.lang.Object ensureNotNull​(java.lang.Object obj)
      Verifies the input object is not null, and returns that object.
      static java.lang.Object ensureNotNull​(java.lang.Object obj, java.lang.String msg, java.lang.Object... args)
      Verifies the input object is not null, and returns that object.
      static void fail​(java.lang.String msg)
      Cause a failure.
      static void fail​(java.lang.String msg, java.lang.Object... args)
      Cause a failure.
      static void isFalse​(boolean val)
      Validates that the value is false
      static void isFalse​(boolean val, java.lang.String msg)
      Validates that the value is false
      static void isTrue​(boolean val)
      Validates that the value is true
      static void isTrue​(boolean val, java.lang.String msg)
      Validates that the value is true
      static void noNullElements​(java.lang.Object[] objects)
      Validates that the array contains no null elements
      static void noNullElements​(java.lang.Object[] objects, java.lang.String msg)
      Validates that the array contains no null elements
      static void notEmpty​(java.lang.String string)
      Validates that the string is not null and is not empty
      static void notEmpty​(java.lang.String string, java.lang.String msg)
      Validates that the string is not null and is not empty
      static void notEmptyParam​(java.lang.String string, java.lang.String param)
      Validates that the string parameter is not null and is not empty
      static void notNull​(java.lang.Object obj)
      Validates that the object is not null
      static void notNull​(java.lang.Object obj, java.lang.String msg)
      Validates that the object is not null
      static void notNullParam​(java.lang.Object obj, java.lang.String param)
      Validates that the parameter is not null
      static void wtf​(java.lang.String msg)
      Blow up if we reach an unexpected state.
      • Methods inherited from class java.lang.Object

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

      • Validate

        private Validate()
    • Method Detail

      • notNull

        public static void notNull​(java.lang.Object obj)
        Validates that the object is not null
        Parameters:
        obj - object to test
        Throws:
        ValidationException - if the object is null
      • notNullParam

        public static void notNullParam​(java.lang.Object obj,
                                        java.lang.String param)
        Validates that the parameter is not null
        Parameters:
        obj - the parameter to test
        param - the name of the parameter, for presentation in the validation exception.
        Throws:
        ValidationException - if the object is null
      • notNull

        public static void notNull​(java.lang.Object obj,
                                   java.lang.String msg)
        Validates that the object is not null
        Parameters:
        obj - object to test
        msg - message to include in the Exception if validation fails
        Throws:
        ValidationException - if the object is null
      • ensureNotNull

        public static java.lang.Object ensureNotNull​(java.lang.Object obj)
        Verifies the input object is not null, and returns that object. Effectively this casts a nullable object to a non- null object. (Works around lack of Objects.requestNonNull in Android version.)
        Parameters:
        obj - nullable object to case to not-null
        Returns:
        the object, or throws an exception if it is null
        Throws:
        ValidationException - if the object is null
      • ensureNotNull

        public static java.lang.Object ensureNotNull​(java.lang.Object obj,
                                                     java.lang.String msg,
                                                     java.lang.Object... args)
        Verifies the input object is not null, and returns that object. Effectively this casts a nullable object to a non- null object. (Works around lack of Objects.requestNonNull in Android version.)
        Parameters:
        obj - nullable object to case to not-null
        msg - the String format message to include in the validation exception when thrown
        args - the arguments to the msg
        Returns:
        the object, or throws an exception if it is null
        Throws:
        ValidationException - if the object is null
      • isTrue

        public static void isTrue​(boolean val)
        Validates that the value is true
        Parameters:
        val - object to test
        Throws:
        ValidationException - if the object is not true
      • isTrue

        public static void isTrue​(boolean val,
                                  java.lang.String msg)
        Validates that the value is true
        Parameters:
        val - object to test
        msg - message to include in the Exception if validation fails
        Throws:
        ValidationException - if the object is not true
      • isFalse

        public static void isFalse​(boolean val)
        Validates that the value is false
        Parameters:
        val - object to test
        Throws:
        ValidationException - if the object is not false
      • isFalse

        public static void isFalse​(boolean val,
                                   java.lang.String msg)
        Validates that the value is false
        Parameters:
        val - object to test
        msg - message to include in the Exception if validation fails
        Throws:
        ValidationException - if the object is not false
      • noNullElements

        public static void noNullElements​(java.lang.Object[] objects)
        Validates that the array contains no null elements
        Parameters:
        objects - the array to test
        Throws:
        ValidationException - if the array contains a null element
      • noNullElements

        public static void noNullElements​(java.lang.Object[] objects,
                                          java.lang.String msg)
        Validates that the array contains no null elements
        Parameters:
        objects - the array to test
        msg - message to include in the Exception if validation fails
        Throws:
        ValidationException - if the array contains a null element
      • notEmpty

        public static void notEmpty​(java.lang.String string)
        Validates that the string is not null and is not empty
        Parameters:
        string - the string to test
        Throws:
        ValidationException - if the string is null or empty
      • notEmptyParam

        public static void notEmptyParam​(java.lang.String string,
                                         java.lang.String param)
        Validates that the string parameter is not null and is not empty
        Parameters:
        string - the string to test
        param - the name of the parameter, for presentation in the validation exception.
        Throws:
        ValidationException - if the string is null or empty
      • notEmpty

        public static void notEmpty​(java.lang.String string,
                                    java.lang.String msg)
        Validates that the string is not null and is not empty
        Parameters:
        string - the string to test
        msg - message to include in the Exception if validation fails
        Throws:
        ValidationException - if the string is null or empty
      • wtf

        public static void wtf​(java.lang.String msg)
        Blow up if we reach an unexpected state.
        Parameters:
        msg - message to think about
        Throws:
        java.lang.IllegalStateException - if we reach this state
      • fail

        public static void fail​(java.lang.String msg)
        Cause a failure.
        Parameters:
        msg - message to output.
        Throws:
        java.lang.IllegalStateException - if we reach this state
      • assertFail

        static boolean assertFail​(java.lang.String msg)
        Cause a failure, but return false so it can be used in an assert statement.
        Parameters:
        msg - message to output.
        Returns:
        false, always
        Throws:
        java.lang.IllegalStateException - if we reach this state
      • fail

        public static void fail​(java.lang.String msg,
                                java.lang.Object... args)
        Cause a failure.
        Parameters:
        msg - message to output.
        args - the format arguments to the msg
        Throws:
        java.lang.IllegalStateException - if we reach this state