Package blbutil
Class Validate
- java.lang.Object
-
- blbutil.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 aFile
object corresponding to the specified filename ornull
iffilename == 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.
-
-
-
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 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:
java.lang.IllegalArgumentException
- if the specified delimiter character is not found in any string element in the specifiedString[]
arrayjava.lang.IllegalArgumentException
- if the specified delimiter is the first or last character of each string element in the specifiedString[]
arrayjava.lang.IllegalArgumentException
- if any two elements of the specified string array have the same keyjava.lang.NullPointerException
- ifargs == null
or ifargs[j] == null
for anyj
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
- ifargsMap == null
-
getFile
public static java.io.File getFile(java.lang.String filename)
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:
java.lang.IllegalArgumentException
- iffilename.isEmpty() == true
java.lang.IllegalArgumentException
- iffilename != 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 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:
java.lang.IllegalArgumentException
- ifmin > max
java.lang.IllegalArgumentException
- ifdefaultValue < min || defaultValue > max
java.lang.IllegalArgumentException
- ifisRequired == true && map.get(key) == null
java.lang.IllegalArgumentException
- ifmap.get(key) != null && (Integer.parseInt(map.get(key)) < min || Integer.parseInt(map.get(key)) > max)
java.lang.NumberFormatException
- ifmap.get(key) != null
andmap.get(key)
is not a parsableint
java.lang.NullPointerException
- ifkey == 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 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:
java.lang.IllegalArgumentException
- ifmin > max
java.lang.IllegalArgumentException
- ifdefaultValue < min || defaultValue > max
java.lang.IllegalArgumentException
- ifisRequired == true && map.get(key) == null
java.lang.IllegalArgumentException
- ifmap.get(key) != null && (Long.parseLong(map.get(key)) < min || Long.parseLong(map.get(key)) > max)
java.lang.NumberFormatException
- ifmap.get(key) != null
andmap.get(key)
is not a parsablelong
java.lang.NullPointerException
- ifkey == 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 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:
java.lang.IllegalArgumentException
- ifmin > max
java.lang.IllegalArgumentException
- ifdefaultValue < min || defaultValue > max || Float.isNan(defaultValue)==true
java.lang.IllegalArgumentException
- ifisRequired == true && map.get(key) == null
java.lang.IllegalArgumentException
- ifmap.get(key) != null && (Float.parseFloat(map.get(key)) < min || Float.parseFloat(map.get(key)) > max || Float.isNaN(map.get(key))
java.lang.NumberFormatException
- ifmap.get(key) != null
andmap.get(key)
is not a parsablbefloat
java.lang.NullPointerException
- ifkey == 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 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:
java.lang.IllegalArgumentException
- ifmin > max
java.lang.IllegalArgumentException
- ifdefaultValue < min || defaultValue > max || Double.isNan(defaultValue)==true
java.lang.IllegalArgumentException
- ifisRequired == true && map.get(key) == null
java.lang.IllegalArgumentException
- ifmap.get(key) != null && (Double.parseDouble(map.get(key)) < min || Double.parseDouble(map.get(key)) > max || Double.isNaN(map.get(key))
java.lang.NumberFormatException
- ifmap.get(key) != null
andmap.get(key)
is not a parsabledouble
java.lang.NullPointerException
- ifkey == 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 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:
java.lang.IllegalArgumentException
- ifisRequired == 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
- ifkey == 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 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:
java.lang.IllegalArgumentException
- ifisRequired == true && map.get(key) == null
java.lang.IllegalArgumentException
- ifpossibleValues != null
anddefaultValue
does not equal any element of thepossibleValues
arrayjava.lang.IllegalArgumentException
- ifpossibleValues != null
andmap.get(key)
does not equal any element of thepossibleValues
arrayjava.lang.NullPointerException
- ifkey == null || map == null
-
-