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 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
confirmEmptyMap
(Map<String, String> argsMap) 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
Returns aFile
object corresponding to the specified filename ornull
iffilename == null
static int
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.
-
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 == null
or ifargs[j] == null
for anyj
satisfying(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 aFile
object corresponding to the specified filename ornull
iffilename == null
- Parameters:
filename
- a filename- Returns:
- a file corresponding to the specified filename, or
null
iffilename == null
- Throws:
IllegalArgumentException
- iffilename.isEmpty() == true
IllegalArgumentException
- iffilename != 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 keymap
- a map of key-value pairsisRequired
-true
if the specified key is required to be in the specified map, andfalse
otherwisedefaultValue
- 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 > max
IllegalArgumentException
- ifdefaultValue < min || defaultValue > max
IllegalArgumentException
- ifisRequired == true && map.get(key) == null
IllegalArgumentException
- ifmap.get(key) != null && (Integer.parseInt(map.get(key)) < min || Integer.parseInt(map.get(key)) > max)
NumberFormatException
- ifmap.get(key) != null
andmap.get(key)
is not a parsableint
NullPointerException
- 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
-true
if the specified key is required to be in the specified map, andfalse
otherwisedefaultValue
- 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 > max
IllegalArgumentException
- ifdefaultValue < min || defaultValue > max
IllegalArgumentException
- ifisRequired == true && map.get(key) == null
IllegalArgumentException
- ifmap.get(key) != null && (Long.parseLong(map.get(key)) < min || Long.parseLong(map.get(key)) > max)
NumberFormatException
- ifmap.get(key) != null
andmap.get(key)
is not a parsablelong
NullPointerException
- 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
-true
if the specified key is required to be in the specified map, andfalse
otherwisedefaultValue
- 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 > max
IllegalArgumentException
- ifdefaultValue < min || defaultValue > max || Float.isNan(defaultValue)==true
IllegalArgumentException
- ifisRequired == true && map.get(key) == null
IllegalArgumentException
- 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) != null
andmap.get(key)
is not a parsablbefloat
NullPointerException
- 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
-true
if the specified key is required to be in the specified map, andfalse
otherwisedefaultValue
- 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 > max
IllegalArgumentException
- ifdefaultValue < min || defaultValue > max || Double.isNan(defaultValue)==true
IllegalArgumentException
- ifisRequired == true && map.get(key) == null
IllegalArgumentException
- 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) != null
andmap.get(key)
is not a parsabledouble
NullPointerException
- 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
, thentrue
is returned if(v.equalsIgnoreCase("true") || v.equalsIgnoreCase("t"))
andfalse
is returned if(v.equalsIgnoreCase("false") || v.equalsIgnoreCase("f"))
.- Parameters:
key
- the keymap
- a map of key-value pairsisRequired
-true
if the specified key is required to be in the specified map, andfalse
otherwisedefaultValue
- 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) == null
IllegalArgumentException
- 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
-true
if the specified key is required to be in the specified map, andfalse
otherwisedefaultValue
- the value that will be returned if(isRequired == false && map.get(key) == null)
possibleValues
- an array of valid string values ornull
if the valid values arenull
and all non-empty strings.- Returns:
- the string value corresponding to the specified key
- Throws:
IllegalArgumentException
- ifisRequired == true && map.get(key) == null
IllegalArgumentException
- ifpossibleValues != null
anddefaultValue
does not equal any element of thepossibleValues
arrayIllegalArgumentException
- ifpossibleValues != null
andmap.get(key)
does not equal any element of thepossibleValues
arrayNullPointerException
- ifkey == null || map == null
-