Package blbutil
Class Validate
java.lang.Object
blbutil.Validate
Class Validate contains static methods for validating command line
arguments.
-
Method Summary
Modifier and TypeMethodDescriptionReturns a map with one (key, value) pair for each element of the specified array.static booleanbooleanArg(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 voidconfirmEmptyMap(Map<String, String> argsMap) Checks whether the specified map of key-value pairs is empty.static doubledoubleArg(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 floatfloatArg(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 FileReturns aFileobject corresponding to the specified filename ornulliffilename == nullstatic intRemoves the specified key from the specified map, and returns the integer value corresponding to the specified key.static longlongArg(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 StringstringArg(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.
-
Method Details
-
argsToMap
Returns a map with one (key, value) pair for each element of the specified array. Each element of the specifiedString[]array must contain the specified delimiter character. For each array elements, the key iss.substring(0, s.indexOf(sep))and the value iss.substring(s.indexOf(sep) + 1).- Parameters:
args- a string arraydelim- 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 specifiedString[]arrayIllegalArgumentException- if the specified delimiter is the first or last character of each string element in the specifiedString[]arrayIllegalArgumentException- if any two elements of the specified string array have the same keyNullPointerException- ifargs == nullor ifargs[j] == nullfor anyjsatisfying(0 <= j && j <= args.length)
-
confirmEmptyMap
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- ifargsMap == null
-
getFile
Returns aFileobject corresponding to the specified filename ornulliffilename == null- Parameters:
filename- a filename- Returns:
- a file corresponding to the specified filename, or
nulliffilename == null - Throws:
IllegalArgumentException- iffilename.isEmpty() == trueIllegalArgumentException- iffilename != nulland 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 keymap- a map of key-value pairsisRequired-trueif the specified key is required to be in the specified map, andfalseotherwisedefaultValue- the value that will be returned if(isRequired == false && map.get(key) == null)min- the minimum valid integer valuemax- the maximum valid integer value- Returns:
- the integer value corresponding to the specified key
- Throws:
IllegalArgumentException- ifmin > maxIllegalArgumentException- ifdefaultValue < min || defaultValue > maxIllegalArgumentException- ifisRequired == true && map.get(key) == nullIllegalArgumentException- ifmap.get(key) != null && (Integer.parseInt(map.get(key)) < min || Integer.parseInt(map.get(key)) > max)NumberFormatException- ifmap.get(key) != nullandmap.get(key)is not a parsableintNullPointerException- ifkey == 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 keymap- a map of key-value pairsisRequired-trueif the specified key is required to be in the specified map, andfalseotherwisedefaultValue- the value that will be returned if(isRequired == false && map.get(key) == null)min- the minimum valid long valuemax- the maximum valid long value- Returns:
- the long value corresponding to the specified key
- Throws:
IllegalArgumentException- ifmin > maxIllegalArgumentException- ifdefaultValue < min || defaultValue > maxIllegalArgumentException- ifisRequired == true && map.get(key) == nullIllegalArgumentException- ifmap.get(key) != null && (Long.parseLong(map.get(key)) < min || Long.parseLong(map.get(key)) > max)NumberFormatException- ifmap.get(key) != nullandmap.get(key)is not a parsablelongNullPointerException- ifkey == 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 keymap- a map of key-value pairsisRequired-trueif the specified key is required to be in the specified map, andfalseotherwisedefaultValue- the value that will be returned if(isRequired == false && map.get(key) == null)min- the minimum valid float valuemax- the maximum valid float value- Returns:
- the float value corresponding to the specified key
- Throws:
IllegalArgumentException- ifmin > maxIllegalArgumentException- ifdefaultValue < min || defaultValue > max || Float.isNan(defaultValue)==trueIllegalArgumentException- ifisRequired == true && map.get(key) == nullIllegalArgumentException- ifmap.get(key) != null && (Float.parseFloat(map.get(key)) < min || Float.parseFloat(map.get(key)) > max || Float.isNaN(map.get(key))NumberFormatException- ifmap.get(key) != nullandmap.get(key)is not a parsablbefloatNullPointerException- ifkey == 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 keymap- a map of key-value pairsisRequired-trueif the specified key is required to be in the specified map, andfalseotherwisedefaultValue- the value that will be returned if(isRequired == false && map.get(key) == null)min- the minimum valid double valuemax- the maximum valid double value- Returns:
- the double value corresponding to the specified key
- Throws:
IllegalArgumentException- ifmin > maxIllegalArgumentException- ifdefaultValue < min || defaultValue > max || Double.isNan(defaultValue)==trueIllegalArgumentException- ifisRequired == true && map.get(key) == nullIllegalArgumentException- ifmap.get(key) != null && (Double.parseDouble(map.get(key)) < min || Double.parseDouble(map.get(key)) > max || Double.isNaN(map.get(key))NumberFormatException- ifmap.get(key) != nullandmap.get(key)is not a parsabledoubleNullPointerException- ifkey == 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 isv, thentrueis returned if(v.equalsIgnoreCase("true") || v.equalsIgnoreCase("t"))andfalseis returned if(v.equalsIgnoreCase("false") || v.equalsIgnoreCase("f")).- Parameters:
key- the keymap- a map of key-value pairsisRequired-trueif the specified key is required to be in the specified map, andfalseotherwisedefaultValue- the value that will be returned if(isRequired == false && map.get(key) == null)- Returns:
- the boolean value corresponding to the specified key
- Throws:
IllegalArgumentException- ifisRequired == true && map.get(key) == nullIllegalArgumentException- if the value(v = map.get(key)) != null && false == (v.equalsIgnoreCase("true") || v.equalsIgnoreCase("t") || v.equalsIgnoreCase("false") || v.equalsIgnoreCase("f"))NullPointerException- ifkey == 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 benull- Parameters:
key- the keymap- a map of key-value pairsisRequired-trueif the specified key is required to be in the specified map, andfalseotherwisedefaultValue- the value that will be returned if(isRequired == false && map.get(key) == null)possibleValues- an array of valid string values ornullif the valid values arenulland all non-empty strings.- Returns:
- the string value corresponding to the specified key
- Throws:
IllegalArgumentException- ifisRequired == true && map.get(key) == nullIllegalArgumentException- ifpossibleValues != nullanddefaultValuedoes not equal any element of thepossibleValuesarrayIllegalArgumentException- ifpossibleValues != nullandmap.get(key)does not equal any element of thepossibleValuesarrayNullPointerException- ifkey == null || map == null
-