Class Validator


  • public abstract class Validator
    extends Object
    Static utility methods for validating input.
    Author:
    bratseth
    • Constructor Detail

      • Validator

        public Validator()
    • Method Detail

      • ensureNotNull

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

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

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

        public static void ensureInRange​(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​(String smallDescription,
                                         int small,
                                         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​(String smallDescription,
                                         Comparable small,
                                         String largeDescription,
                                         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​(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:
        IllegalArgumentException - if the given condition was false
      • ensure

        public static void ensure​(boolean condition,
                                  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​(String description,
                                            Object item,
                                            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:
        IllegalArgumentException - if the given item is not of the correct type
      • ensureNotInstanceOf

        public static void ensureNotInstanceOf​(String description,
                                               Object item,
                                               Class<?> type)
        Ensures that an item is not 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 NOT be instanceof
        Throws:
        IllegalArgumentException - if the given item is of the wrong type