Package blbutil

Class Validate

java.lang.Object
blbutil.Validate

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

    Modifier and Type
    Method
    Description
    static Map<String,String>
    argsToMap(String[] args, char delim)
    Returns a map with one (key, value) pair for each element of the specified array.
    static boolean
    booleanArg(String key, Map<String,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
    Checks whether the specified map of key-value pairs is empty.
    static double
    doubleArg(String key, Map<String,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(String key, Map<String,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 File
    getFile(String filename)
    Returns a File object corresponding to the specified filename or null if filename == null
    static int
    intArg(String key, Map<String,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(String key, Map<String,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 String
    stringArg(String key, Map<String,String> map, boolean isRequired, String defaultValue, 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 Details

    • argsToMap

      public static Map<String,String> argsToMap(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:
      IllegalArgumentException - if the specified delimiter character is not found in any string element in the specified String[] array
      IllegalArgumentException - if the specified delimiter is the first or last character of each string element in the specified String[] array
      IllegalArgumentException - if any two elements of the specified string array have the same key
      NullPointerException - if args == null or if args[j] == null for any j satisfying (0 <= j && j <= args.length)
    • confirmEmptyMap

      public static void confirmEmptyMap(Map<String,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:
      NullPointerException - if argsMap == null
    • getFile

      public static File getFile(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:
      IllegalArgumentException - if filename.isEmpty() == true
      IllegalArgumentException - if filename != null and the specified file does not exist or is a directory
    • intArg

      public static int intArg(String key, Map<String,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:
      IllegalArgumentException - if min > max
      IllegalArgumentException - if defaultValue < min || defaultValue > max
      IllegalArgumentException - if isRequired == true && map.get(key) == null
      IllegalArgumentException - if map.get(key) != null && (Integer.parseInt(map.get(key)) < min || Integer.parseInt(map.get(key)) > max)
      NumberFormatException - if map.get(key) != null and map.get(key) is not a parsable int
      NullPointerException - if key == null || map == null
    • longArg

      public static long longArg(String key, Map<String,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:
      IllegalArgumentException - if min > max
      IllegalArgumentException - if defaultValue < min || defaultValue > max
      IllegalArgumentException - if isRequired == true && map.get(key) == null
      IllegalArgumentException - if map.get(key) != null && (Long.parseLong(map.get(key)) < min || Long.parseLong(map.get(key)) > max)
      NumberFormatException - if map.get(key) != null and map.get(key) is not a parsable long
      NullPointerException - if key == null || map == null
    • floatArg

      public static float floatArg(String key, Map<String,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:
      IllegalArgumentException - if min > max
      IllegalArgumentException - if defaultValue < min || defaultValue > max || Float.isNan(defaultValue)==true
      IllegalArgumentException - if isRequired == true && map.get(key) == null
      IllegalArgumentException - if map.get(key) != null && (Float.parseFloat(map.get(key)) < min || Float.parseFloat(map.get(key)) > max || Float.isNaN(map.get(key))
      NumberFormatException - if map.get(key) != null and map.get(key) is not a parsablbe float
      NullPointerException - if key == null || map == null
    • doubleArg

      public static double doubleArg(String key, Map<String,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:
      IllegalArgumentException - if min > max
      IllegalArgumentException - if defaultValue < min || defaultValue > max || Double.isNan(defaultValue)==true
      IllegalArgumentException - if isRequired == true && map.get(key) == null
      IllegalArgumentException - if map.get(key) != null && (Double.parseDouble(map.get(key)) < min || Double.parseDouble(map.get(key)) > max || Double.isNaN(map.get(key))
      NumberFormatException - if map.get(key) != null and map.get(key) is not a parsable double
      NullPointerException - if key == null || map == null
    • booleanArg

      public static boolean booleanArg(String key, Map<String,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:
      IllegalArgumentException - if isRequired == true && map.get(key) == null
      IllegalArgumentException - if the value (v = map.get(key)) != null && false == (v.equalsIgnoreCase("true") || v.equalsIgnoreCase("t") || v.equalsIgnoreCase("false") || v.equalsIgnoreCase("f"))
      NullPointerException - if key == null || map == null
    • stringArg

      public static String stringArg(String key, Map<String,String> map, boolean isRequired, String defaultValue, 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:
      IllegalArgumentException - if isRequired == true && map.get(key) == null
      IllegalArgumentException - if possibleValues != null and defaultValue does not equal any element of the possibleValues array
      IllegalArgumentException - if possibleValues != null and map.get(key) does not equal any element of the possibleValues array
      NullPointerException - if key == null || map == null