Package blbutil

Class Validate


  • public final class Validate
    extends java.lang.Object
    Class Validate contains static methods for validating command line arguments.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Map<java.lang.String,​java.lang.String> argsToMap​(java.lang.String[] args, char delim)
      Returns a map with one (key, value) pair for each element of the specified array.
      static boolean booleanArg​(java.lang.String key, java.util.Map<java.lang.String,​java.lang.String> map, boolean isRequired, boolean defaultValue)
      Removes the specified key from the specified map, and returns the boolean value corresponding to the specified key.
      static void confirmEmptyMap​(java.util.Map<java.lang.String,​java.lang.String> argsMap)
      Checks whether the specified map of key-value pairs is empty.
      static double doubleArg​(java.lang.String key, java.util.Map<java.lang.String,​java.lang.String> map, boolean isRequired, double defaultValue, double min, double max)
      Removes the specified key from the specified map, and returns the double value corresponding to the specified key.
      static float floatArg​(java.lang.String key, java.util.Map<java.lang.String,​java.lang.String> map, boolean isRequired, float defaultValue, float min, float max)
      Removes the specified key from the specified map, and returns the float value corresponding to the specified key.
      static java.io.File getFile​(java.lang.String filename)
      Returns a File object corresponding to the specified filename or null if filename == null
      static int intArg​(java.lang.String key, java.util.Map<java.lang.String,​java.lang.String> map, boolean isRequired, int defaultValue, int min, int max)
      Removes the specified key from the specified map, and returns the integer value corresponding to the specified key.
      static long longArg​(java.lang.String key, java.util.Map<java.lang.String,​java.lang.String> map, boolean isRequired, long defaultValue, long min, long max)
      Removes the specified key from the specified map, and returns the long value corresponding to the specified key.
      static java.lang.String stringArg​(java.lang.String key, java.util.Map<java.lang.String,​java.lang.String> map, boolean isRequired, java.lang.String defaultValue, java.lang.String[] possibleValues)
      Removes the specified key from the specified map, and returns the string value corresponding to the specified key.
      • Methods inherited from class java.lang.Object

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

      • argsToMap

        public static java.util.Map<java.lang.String,​java.lang.String> argsToMap​(java.lang.String[] args,
                                                                                       char delim)
        Returns a map with one (key, value) pair for each element of the specified array. Each element of the specified String[] array must contain the specified delimiter character. For each array element s, the key is s.substring(0, s.indexOf(sep)) and the value is s.substring(s.indexOf(sep) + 1).
        Parameters:
        args - a string array
        delim - the delimiter character separating a key and value
        Returns:
        a map with one (key, value) pair for each element of the specified array
        Throws:
        java.lang.IllegalArgumentException - if the specified delimiter character is not found in any string element in the specified String[] array
        java.lang.IllegalArgumentException - if the specified delimiter is the first or last character of each string element in the specified String[] array
        java.lang.IllegalArgumentException - if any two elements of the specified string array have the same key
        java.lang.NullPointerException - if args == null or if args[j] == null for any j satisfying (0 <= j && j <= args.length)
      • confirmEmptyMap

        public static void confirmEmptyMap​(java.util.Map<java.lang.String,​java.lang.String> argsMap)
        Checks whether the specified map of key-value pairs is empty. If the map is non-empty, the method will print an error message and terminate the Java virtual machine.
        Parameters:
        argsMap - a map of key-value pairs
        Throws:
        java.lang.NullPointerException - if argsMap == null
      • getFile

        public static java.io.File getFile​(java.lang.String filename)
        Returns a File object corresponding to the specified filename or null if filename == null
        Parameters:
        filename - a filename
        Returns:
        a file corresponding to the specified filename, or null if filename == null
        Throws:
        java.lang.IllegalArgumentException - if filename.isEmpty() == true
        java.lang.IllegalArgumentException - if filename != null and the specified file does not exist or is a directory
      • intArg

        public static int intArg​(java.lang.String key,
                                 java.util.Map<java.lang.String,​java.lang.String> map,
                                 boolean isRequired,
                                 int defaultValue,
                                 int min,
                                 int max)
        Removes the specified key from the specified map, and returns the integer value corresponding to the specified key.
        Parameters:
        key - the key
        map - a map of key-value pairs
        isRequired - true if the specified key is required to be in the specified map, and false otherwise
        defaultValue - the value that will be returned if (isRequired == false && map.get(key) == null)
        min - the minimum valid integer value
        max - the maximum valid integer value
        Returns:
        the integer value corresponding to the specified key
        Throws:
        java.lang.IllegalArgumentException - if min > max
        java.lang.IllegalArgumentException - if defaultValue < min || defaultValue > max
        java.lang.IllegalArgumentException - if isRequired == true && map.get(key) == null
        java.lang.IllegalArgumentException - if map.get(key) != null && (Integer.parseInt(map.get(key)) < min || Integer.parseInt(map.get(key)) > max)
        java.lang.NumberFormatException - if map.get(key) != null and map.get(key) is not a parsable int
        java.lang.NullPointerException - if key == null || map == null
      • longArg

        public static long longArg​(java.lang.String key,
                                   java.util.Map<java.lang.String,​java.lang.String> map,
                                   boolean isRequired,
                                   long defaultValue,
                                   long min,
                                   long max)
        Removes the specified key from the specified map, and returns the long value corresponding to the specified key.
        Parameters:
        key - the key
        map - a map of key-value pairs
        isRequired - true if the specified key is required to be in the specified map, and false otherwise
        defaultValue - the value that will be returned if (isRequired == false && map.get(key) == null)
        min - the minimum valid long value
        max - the maximum valid long value
        Returns:
        the long value corresponding to the specified key
        Throws:
        java.lang.IllegalArgumentException - if min > max
        java.lang.IllegalArgumentException - if defaultValue < min || defaultValue > max
        java.lang.IllegalArgumentException - if isRequired == true && map.get(key) == null
        java.lang.IllegalArgumentException - if map.get(key) != null && (Long.parseLong(map.get(key)) < min || Long.parseLong(map.get(key)) > max)
        java.lang.NumberFormatException - if map.get(key) != null and map.get(key) is not a parsable long
        java.lang.NullPointerException - if key == null || map == null
      • floatArg

        public static float floatArg​(java.lang.String key,
                                     java.util.Map<java.lang.String,​java.lang.String> map,
                                     boolean isRequired,
                                     float defaultValue,
                                     float min,
                                     float max)
        Removes the specified key from the specified map, and returns the float value corresponding to the specified key.
        Parameters:
        key - the key
        map - a map of key-value pairs
        isRequired - true if the specified key is required to be in the specified map, and false otherwise
        defaultValue - the value that will be returned if (isRequired == false && map.get(key) == null)
        min - the minimum valid float value
        max - the maximum valid float value
        Returns:
        the float value corresponding to the specified key
        Throws:
        java.lang.IllegalArgumentException - if min > max
        java.lang.IllegalArgumentException - if defaultValue < min || defaultValue > max || Float.isNan(defaultValue)==true
        java.lang.IllegalArgumentException - if isRequired == true && map.get(key) == null
        java.lang.IllegalArgumentException - if map.get(key) != null && (Float.parseFloat(map.get(key)) < min || Float.parseFloat(map.get(key)) > max || Float.isNaN(map.get(key))
        java.lang.NumberFormatException - if map.get(key) != null and map.get(key) is not a parsablbe float
        java.lang.NullPointerException - if key == null || map == null
      • doubleArg

        public static double doubleArg​(java.lang.String key,
                                       java.util.Map<java.lang.String,​java.lang.String> map,
                                       boolean isRequired,
                                       double defaultValue,
                                       double min,
                                       double max)
        Removes the specified key from the specified map, and returns the double value corresponding to the specified key.
        Parameters:
        key - the key
        map - a map of key-value pairs
        isRequired - true if the specified key is required to be in the specified map, and false otherwise
        defaultValue - the value that will be returned if (isRequired == false && map.get(key) == null)
        min - the minimum valid double value
        max - the maximum valid double value
        Returns:
        the double value corresponding to the specified key
        Throws:
        java.lang.IllegalArgumentException - if min > max
        java.lang.IllegalArgumentException - if defaultValue < min || defaultValue > max || Double.isNan(defaultValue)==true
        java.lang.IllegalArgumentException - if isRequired == true && map.get(key) == null
        java.lang.IllegalArgumentException - if map.get(key) != null && (Double.parseDouble(map.get(key)) < min || Double.parseDouble(map.get(key)) > max || Double.isNaN(map.get(key))
        java.lang.NumberFormatException - if map.get(key) != null and map.get(key) is not a parsable double
        java.lang.NullPointerException - if key == null || map == null
      • booleanArg

        public static boolean booleanArg​(java.lang.String key,
                                         java.util.Map<java.lang.String,​java.lang.String> map,
                                         boolean isRequired,
                                         boolean defaultValue)
        Removes the specified key from the specified map, and returns the boolean value corresponding to the specified key. If the value is v, then true is returned if (v.equalsIgnoreCase("true") || v.equalsIgnoreCase("t")) and false is returned if (v.equalsIgnoreCase("false") || v.equalsIgnoreCase("f")).
        Parameters:
        key - the key
        map - a map of key-value pairs
        isRequired - true if the specified key is required to be in the specified map, and false otherwise
        defaultValue - the value that will be returned if (isRequired == false && map.get(key) == null)
        Returns:
        the boolean value corresponding to the specified key
        Throws:
        java.lang.IllegalArgumentException - if isRequired == true && map.get(key) == null
        java.lang.IllegalArgumentException - if the value (v = map.get(key)) != null && false == (v.equalsIgnoreCase("true") || v.equalsIgnoreCase("t") || v.equalsIgnoreCase("false") || v.equalsIgnoreCase("f"))
        java.lang.NullPointerException - if key == null || map == null
      • stringArg

        public static java.lang.String stringArg​(java.lang.String key,
                                                 java.util.Map<java.lang.String,​java.lang.String> map,
                                                 boolean isRequired,
                                                 java.lang.String defaultValue,
                                                 java.lang.String[] possibleValues)
        Removes the specified key from the specified map, and returns the string value corresponding to the specified key. The value is permitted to be null
        Parameters:
        key - the key
        map - a map of key-value pairs
        isRequired - true if the specified key is required to be in the specified map, and false otherwise
        defaultValue - the value that will be returned if (isRequired == false && map.get(key) == null)
        possibleValues - an array of valid string values or null if the valid values are null and all non-empty strings.
        Returns:
        the string value corresponding to the specified key
        Throws:
        java.lang.IllegalArgumentException - if isRequired == true && map.get(key) == null
        java.lang.IllegalArgumentException - if possibleValues != null and defaultValue does not equal any element of the possibleValues array
        java.lang.IllegalArgumentException - if possibleValues != null and map.get(key) does not equal any element of the possibleValues array
        java.lang.NullPointerException - if key == null || map == null