Package net.mtrop.doom.struct.utils
Class ValueUtils
java.lang.Object
net.mtrop.doom.struct.utils.ValueUtils
Simple utility functions around values.
- Author:
- Matthew Tropiano
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
This function calls the provided callable and returns its value.static <T> T
Attempts to parse a string to another object.static Boolean
Attempts to parse a boolean from a string.static boolean
parseBoolean
(String s, boolean def) Attempts to parse a boolean from a string.static boolean[]
parseBooleanArray
(String s, boolean[] def) Attempts to parse an array of booleans from a string.static boolean[]
parseBooleanArray
(String s, String separatorRegex, boolean[] def) Attempts to parse an array of booleans from a string.static Byte
Attempts to parse a byte from a string.static byte
Attempts to parse a byte from a string.static byte[]
parseByteArray
(String s, byte[] def) Attempts to parse an array of bytes from a string.static byte[]
parseByteArray
(String s, String separatorRegex, byte[] def) Attempts to parse an array of bytes from a string.static Character
Attempts to parse a char from a string.static char
Attempts to parse a byte from a string.static char[]
parseCharArray
(String s, char[] def) Attempts to parse an array of chars from a string.static char[]
parseCharArray
(String s, String separatorRegex, char[] def) Attempts to parse an array of chars from a string.static Double
Attempts to parse a double from a string.static double
parseDouble
(String s, double def) Attempts to parse a double from a string.static double[]
parseDoubleArray
(String s, double[] def) Attempts to parse an array of doubles from a string.static double[]
parseDoubleArray
(String s, String separatorRegex, double[] def) Attempts to parse an array of doubles from a string.static Float
parseFloat
(String s) Attempts to parse a float from a string.static float
parseFloat
(String s, float def) Attempts to parse a float from a string.static float[]
parseFloatArray
(String s, float[] def) Attempts to parse an array of floats from a string.static float[]
parseFloatArray
(String s, String separatorRegex, float[] def) Attempts to parse an array of floats from a string.static Integer
Attempts to parse an int from a string.static int
Attempts to parse an int from a string.static int[]
parseIntArray
(String s, int[] def) Attempts to parse an array of integers from a string.static int[]
parseIntArray
(String s, String separatorRegex, int[] def) Attempts to parse an array of integers from a string.static Long
Attempts to parse a long from a string.static long
Attempts to parse a long from a string.static long[]
parseLongArray
(String s, long[] def) Attempts to parse an array of longs from a string.static long[]
parseLongArray
(String s, String separatorRegex, long[] def) Attempts to parse an array of longs from a string.static Short
parseShort
(String s) Attempts to parse a short from a string.static short
parseShort
(String s, short def) Attempts to parse a short from a string.static short[]
parseShortArray
(String s, short[] def) Attempts to parse an array of shorts from a string.static short[]
parseShortArray
(String s, String separatorRegex, short[] def) Attempts to parse an array of shorts from a string.
-
Method Details
-
get
This function calls the provided callable and returns its value. This is mostly for filling static fields on classes that would otherwise only need to be put in a static block.- Type Parameters:
T
- the return type.- Parameters:
callable
- the callable to call.- Returns:
- the result of the callable.
- Throws:
RuntimeException
- if an exception occurs.
-
parse
Attempts to parse a string to another object.- Type Parameters:
T
- the parse function return type.- Parameters:
s
- the input string.parseFunction
- the parsing function.- Returns:
- the interpreted object.
-
parseBoolean
Attempts to parse a boolean from a string. If the string is empty or null, this returns null. If the string does not equal "true" (case ignored), this returns false.- Parameters:
s
- the input string.- Returns:
- the interpreted boolean.
-
parseByte
Attempts to parse a byte from a string. If the string is null or the empty string, this returns null.- Parameters:
s
- the input string.- Returns:
- the interpreted byte.
-
parseShort
Attempts to parse a short from a string. If the string is null or the empty string, this returns null.- Parameters:
s
- the input string.- Returns:
- the interpreted short.
-
parseChar
Attempts to parse a char from a string. If the string is null or the empty string, this returns null.- Parameters:
s
- the input string.- Returns:
- the first character in the string.
-
parseInt
Attempts to parse an int from a string. If the string is null or the empty string, this returns null.- Parameters:
s
- the input string.- Returns:
- the interpreted integer.
-
parseLong
Attempts to parse a long from a string. If the string is null or the empty string, this returns null.- Parameters:
s
- the input string.- Returns:
- the interpreted long integer.
-
parseFloat
Attempts to parse a float from a string. If the string is null or the empty string, this returns null.- Parameters:
s
- the input string.- Returns:
- the interpreted float.
-
parseDouble
Attempts to parse a double from a string. If the string is null or the empty string, this returns null.- Parameters:
s
- the input string.- Returns:
- the interpreted double.
-
parseBoolean
Attempts to parse a boolean from a string. If the string is null or the empty string, this returnsdef
. If the string does not equal "true," this returns false.- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the interpreted boolean or def if the input string is blank.
-
parseByte
Attempts to parse a byte from a string. If the string is null or the empty string, this returnsdef
.- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the interpreted byte or def if the input string is blank.
-
parseShort
Attempts to parse a short from a string. If the string is null or the empty string, this returnsdef
.- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the interpreted short or def if the input string is blank.
-
parseChar
Attempts to parse a byte from a string. If the string is null or the empty string, this returnsdef
.- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the first character in the string or def if the input string is blank.
-
parseInt
Attempts to parse an int from a string. If the string is null or the empty string, this returnsdef
.- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the interpreted integer or def if the input string is blank.
-
parseLong
Attempts to parse a long from a string. If the string is null or the empty string, this returnsdef
.- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the interpreted long integer or def if the input string is blank.
-
parseFloat
Attempts to parse a float from a string. If the string is null or the empty string, this returnsdef
.- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the interpreted float or def if the input string is blank.
-
parseDouble
Attempts to parse a double from a string. If the string is null or the empty string, this returnsdef
.- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the interpreted double or def if the input string is blank.
-
parseBooleanArray
Attempts to parse an array of booleans from a string. If the string is null or the empty string, this returnsdef
. This assumes that the elements of the array are separated by comma-or-whitespace characters.Example:
"true, false, apple, false"
becomes[true, false, false, false]
- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the array of booleans or def if the input string is blank.
- See Also:
-
parseByteArray
Attempts to parse an array of bytes from a string. If the string is null or the empty string, this returnsdef
. This assumes that the elements of the array are separated by comma-or-whitespace characters.Example:
"0, -5, 2, grape"
becomes[0, -5, 2, 0]
- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the array of bytes or def if the input string is blank.
- See Also:
-
parseShortArray
Attempts to parse an array of shorts from a string. If the string is null or the empty string, this returnsdef
. This assumes that the elements of the array are separated by comma-or-whitespace characters.Example:
"0, -5, 2, grape"
becomes[0, -5, 2, 0]
- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the array of shorts or def if the input string is blank.
- See Also:
-
parseCharArray
Attempts to parse an array of chars from a string. If the string is null or the empty string, this returnsdef
. This assumes that the elements of the array are separated by comma-or-whitespace characters.Example:
"apple, pear, b, g"
becomes['a', 'p', 'b', 'g']
- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the array of characters or def if the input string is blank.
- See Also:
-
parseIntArray
Attempts to parse an array of integers from a string. If the string is null or the empty string, this returnsdef
. This assumes that the elements of the array are separated by comma-or-whitespace characters.Example:
"0, -5, 2.1, grape"
becomes[0, -5, 2, 0]
- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the array of integers or def if the input string is blank.
- See Also:
-
parseFloatArray
Attempts to parse an array of floats from a string. If the string is null or the empty string, this returnsdef
. This assumes that the elements of the array are separated by comma-or-whitespace characters.Example:
"0.5, -5.4, 2, grape"
becomes[0.5f, -5.4f, 2.0f, 0f]
- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the array of floats or def if the input string is blank.
- See Also:
-
parseLongArray
Attempts to parse an array of longs from a string. If the string is null or the empty string, this returnsdef
. This assumes that the elements of the array are separated by comma-or-whitespace characters.Example:
"0, -5, 2, grape"
becomes[0, -5, 2, 0]
- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the array of long integers or def if the input string is blank.
- See Also:
-
parseDoubleArray
Attempts to parse an array of doubles from a string. If the string is null or the empty string, this returnsdef
. This assumes that the elements of the array are separated by comma-or-whitespace characters.Example:
"0.5, -5.4, 2, grape"
becomes[0.5, -5.4, 2.0, 0.0]
- Parameters:
s
- the input string.def
- the fallback value to return.- Returns:
- the array of doubles or def if the input string is blank.
- See Also:
-
parseBooleanArray
Attempts to parse an array of booleans from a string. If the string is null or the empty string, this returnsdef
.- Parameters:
s
- the input string.separatorRegex
- the regular expression to split the string into tokens.def
- the fallback value to return.- Returns:
- the array of booleans or def if the input string is blank.
- Throws:
NullPointerException
- if separatorRegex is null.- See Also:
-
parseByteArray
Attempts to parse an array of bytes from a string. If the string is null or the empty string, this returnsdef
.- Parameters:
s
- the input string.separatorRegex
- the regular expression to split the string into tokens.def
- the fallback value to return.- Returns:
- the array of bytes or def if the input string is blank.
- Throws:
NullPointerException
- if separatorRegex is null.- See Also:
-
parseShortArray
Attempts to parse an array of shorts from a string. If the string is null or the empty string, this returnsdef
.- Parameters:
s
- the input string.separatorRegex
- the regular expression to split the string into tokens.def
- the fallback value to return.- Returns:
- the array of shorts or def if the input string is blank.
- Throws:
NullPointerException
- if separatorRegex is null.- See Also:
-
parseCharArray
Attempts to parse an array of chars from a string. If the string is null or the empty string, this returnsdef
.- Parameters:
s
- the input string.separatorRegex
- the regular expression to split the string into tokens.def
- the fallback value to return.- Returns:
- the array of characters or def if the input string is blank.
- Throws:
NullPointerException
- if separatorRegex is null.- See Also:
-
parseIntArray
Attempts to parse an array of integers from a string. If the string is null or the empty string, this returnsdef
.- Parameters:
s
- the input string.separatorRegex
- the regular expression to split the string into tokens.def
- the fallback value to return.- Returns:
- the array of integers or def if the input string is blank.
- Throws:
NullPointerException
- if separatorRegex is null.- See Also:
-
parseFloatArray
Attempts to parse an array of floats from a string. If the string is null or the empty string, this returnsdef
.- Parameters:
s
- the input string.separatorRegex
- the regular expression to split the string into tokens.def
- the fallback value to return.- Returns:
- the array of floats or def if the input string is blank.
- Throws:
NullPointerException
- if separatorRegex is null.- See Also:
-
parseLongArray
Attempts to parse an array of longs from a string. If the string is null or the empty string, this returnsdef
.- Parameters:
s
- the input string.separatorRegex
- the regular expression to split the string into tokens.def
- the fallback value to return.- Returns:
- the array of long integers or def if the input string is blank.
- Throws:
NullPointerException
- if separatorRegex is null.- See Also:
-
parseDoubleArray
Attempts to parse an array of doubles from a string. If the string is null or the empty string, this returnsdef
.- Parameters:
s
- the input string.separatorRegex
- the regular expression to split the string into tokens.def
- the fallback value to return.- Returns:
- the array of doubles or def if the input string is blank.
- Throws:
NullPointerException
- if separatorRegex is null.- See Also:
-