Class Validator


  • public abstract class Validator
    extends java.lang.Object

    Static utility methods for validating input.

    Author:
    bratseth
    • Constructor Summary

      Constructors 
      Constructor Description
      Validator()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void ensure​(boolean condition, java.lang.Object... description)
      Ensure the given argument is true, if not throw IllegalArgumentException concatenating the String representation of the description arguments.
      static void ensure​(java.lang.String description, boolean condition)
      Ensures that the given argument is true
      static void ensureInRange​(java.lang.String argumentDescription, int from, int to, int argument)
      Throws an IllegalArgumentException if the given argument is not in the given range
      static void ensureInstanceOf​(java.lang.String description, java.lang.Object item, java.lang.Class<?> type)
      Ensures that an item is of a particular class
      static void ensureNonEmpty​(java.lang.String argumentDescription, java.lang.String argument)
      Throws NullPointerException if the argument is null
      static void ensureNotInitialized​(java.lang.String fieldDescription, java.lang.Object fieldOwner, java.lang.Object fieldValue)
      Throws an IllegalStateException if the given field value is initialized (not null)
      static void ensureNotNull​(java.lang.String argumentDescription, java.lang.Object argument)
      Throws NullPointerException if the argument is null
      static void ensureSmaller​(java.lang.String smallDescription, int small, java.lang.String largeDescription, int large)
      Throws an IllegalArgumentException if the first argument is not strictly smaller than the second argument
      static void ensureSmaller​(java.lang.String smallDescription, java.lang.Comparable small, java.lang.String largeDescription, java.lang.Comparable large)
      Throws an IllegalArgumentException if the first argument is not strictly smaller than the second argument
      • Methods inherited from class java.lang.Object

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

      • Validator

        public Validator()
    • Method Detail

      • ensureNotNull

        public static void ensureNotNull​(java.lang.String argumentDescription,
                                         java.lang.Object argument)
        Throws NullPointerException if the argument is null
      • ensureNonEmpty

        public static void ensureNonEmpty​(java.lang.String argumentDescription,
                                          java.lang.String argument)
        Throws NullPointerException if the argument is null
      • ensureNotInitialized

        public static void ensureNotInitialized​(java.lang.String fieldDescription,
                                                java.lang.Object fieldOwner,
                                                java.lang.Object fieldValue)
        Throws an IllegalStateException if the given field value is initialized (not null)
      • ensureInRange

        public static void ensureInRange​(java.lang.String argumentDescription,
                                         int from,
                                         int to,
                                         int argument)
        Throws an IllegalArgumentException if the given argument is not in the given range
        Parameters:
        argumentDescription - a description of the argument
        from - the range start, inclusive
        to - the range end, inclusive
        argument - the argument value to check
      • ensureSmaller

        public static void ensureSmaller​(java.lang.String smallDescription,
                                         int small,
                                         java.lang.String largeDescription,
                                         int large)
        Throws an IllegalArgumentException if the first argument is not strictly smaller than the second argument
        Parameters:
        smallDescription - description of the smallest argument
        small - the smallest argument
        largeDescription - description of the lergest argument
        large - the largest argument
      • ensureSmaller

        public static void ensureSmaller​(java.lang.String smallDescription,
                                         java.lang.Comparable small,
                                         java.lang.String largeDescription,
                                         java.lang.Comparable large)
        Throws an IllegalArgumentException if the first argument is not strictly smaller than the second argument
        Parameters:
        smallDescription - description of the smallest argument
        small - the smallest argument
        largeDescription - description of the largest argument
        large - the largest argument
      • ensure

        public static void ensure​(java.lang.String description,
                                  boolean condition)
        Ensures that the given argument is true
        Parameters:
        description - of what is the case if the condition is false
        condition - the condition to ensure is true
        Throws:
        java.lang.IllegalArgumentException - if the given condition was false
      • ensure

        public static void ensure​(boolean condition,
                                  java.lang.Object... description)
        Ensure the given argument is true, if not throw IllegalArgumentException concatenating the String representation of the description arguments.
      • ensureInstanceOf

        public static void ensureInstanceOf​(java.lang.String description,
                                            java.lang.Object item,
                                            java.lang.Class<?> type)
        Ensures that an item is of a particular class
        Parameters:
        description - a description of the item to be checked
        item - the item to check the type of
        type - the type the given item should be instanceof
        Throws:
        java.lang.IllegalArgumentException - if the given item is not of the correct type