Class Convert
java.lang.Object
com.tccc.kos.commons.util.convert.Convert
Parser for converting string values to other formats.
- Since:
- 1.0
- Version:
- 2023-08-25
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
Converts from a string to the specified type.static byte[]
fromHexString
(String str) Converts a hex string to a byte array.static double
fromQ4
(int val) Convert a Q4 to a double.static double
fromQ8
(int val) Converts a Q8 to a double.static boolean
Returns the value of the string as a boolean.static boolean
Returns the value of the specified string as a boolean.static boolean
Returns the value of the specified string as a boolean.static double
Returns the value of the string as a double.static double
Returns the value of the specified string as a double.static double
Returns the value of the specified string as a double.static float
Returns the value of the specified string as a float.static int
Returns the value of the string as an int.static int
Returns the value of the specified string as an int.static int
Returns the value of the specified string as an int.static long
Returns the value of the string as a long.static long
Returns the value of the specified string as a long.static long
Returns the value of the specified string as a long.static short
Converts the input string to a short.static short
Converts the input string to a short.static short
Converts the input string to a short.static String
Returns the specified string.static String
Returns the specified string.static String
Returns the specified string.static double
Converts the given object to adouble
, if possible.static String
toHexString
(byte[] bytes) Converts a byte array to a hex string.static int
Converts the given object to anint
, if possible.static long
Converts the given object to along
, if possible.static int
toQ4
(double val) Convert a double to Q4 format.static int
toQ8
(double val) Converts a double to Q8 format.static short
Converts the given object to ashort
, if possible.
-
Field Details
-
SIMPLE_TYPES
-
-
Constructor Details
-
Convert
public Convert()
-
-
Method Details
-
getStr
Returns the specified string. If it is null, then return the default if not required, or throw an exception if required.- Parameters:
str
- the string to check and returndef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the specified string or def if null
-
getStr
Returns the specified string. If it is null then return the default value.- Parameters:
str
- the string to check and returndef
- the default value- Returns:
- the specified string or def if null
-
getStr
Returns the specified string. If it is null then throw ConvertException.- Parameters:
str
- the string to check and return- Returns:
- the specified string if not null
-
getShort
Converts the input string to a short. If the string is null or is not a short, then an exception is thrown.- Parameters:
str
- the string to parse- Returns:
- the specified string as a short if input not null
-
getShort
Converts the input string to a short. If the string is null, then the default is returned.- Parameters:
str
- the string to parsedef
- the default value- Returns:
- the specified string as a short or def if input is null or cannot be converted
-
getShort
Converts the input string to a short. If the string is null and is not required, then the default value is returned. If the string is null but is required, then an exception is thrown.- Parameters:
str
- the string to parsedef
- the default valueisRequired
- if true, throw an exception if not able to convert, otherwise use default- Returns:
- the specified string as a short or def if input is null
-
getInt
Returns the value of the specified string as an int. If string is null then return the default unless required is true in which case throw an exception instead.- Parameters:
str
- the string to parsedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the specified string as an int or def if null
-
getInt
Returns the value of the specified string as an int. If string is null then return the default.- Parameters:
str
- the string to convertdef
- the default value- Returns:
- the specified string as an int or def if null
-
getInt
Returns the value of the string as an int. If a string is null, or is not an int, throw a ConvertException.- Parameters:
str
- the string to convert- Returns:
- the specified string as an int if not null
-
getLong
Returns the value of the specified string as a long. If string is null then return the default unless required is true in which case throw an exception instead.- Parameters:
str
- the string to parsedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the specified string as a long or def if null
-
getLong
Returns the value of the specified string as a long. If string is null then return the default.- Parameters:
str
- the string to convertdef
- the default value- Returns:
- the specified string as a long or def if null
-
getLong
Returns the value of the string as a long. If a string is null, or is not a long, throw a ConvertException.- Parameters:
str
- the string to convert- Returns:
- the specified string as a long if not null
-
getFloat
Returns the value of the specified string as a float. If string is null then return the default unless required is true in which case throw an exception instead.- Parameters:
str
- the string to parsedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the specified string as a float or def if null
-
getDouble
Returns the value of the specified string as a double. If string is null then return the default unless required is true in which case throw an exception instead.- Parameters:
str
- the string to parsedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the specified string as a double or def if null
-
getDouble
Returns the value of the specified string as a double. If string is null then return the default.- Parameters:
str
- the string to convertdef
- the default value- Returns:
- the specified string as a double or def if null
-
getDouble
Returns the value of the string as a double. If a string is null, or is not a double, throw a ConvertException.- Parameters:
str
- the string to convert- Returns:
- the specified string as a double if not null
-
getBool
Returns the value of the specified string as a boolean. If string is null then return the default unless required is true in which case throw an exception instead.- Parameters:
str
- the string to parsedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the specified string as a boolean or def if null
-
getBool
Returns the value of the specified string as a boolean. If string is null then return the default.- Parameters:
str
- the string to convertdef
- the default value- Returns:
- the specified string as a boolean or def if null
-
getBool
Returns the value of the string as a boolean. If a string is null, or is not a boolean, throw a ConvertException.- Parameters:
str
- the string to convert- Returns:
- the specified string as a boolean if not null
-
toQ8
public static int toQ8(double val) Converts a double to Q8 format.- Parameters:
val
- the value to convert
-
fromQ8
public static double fromQ8(int val) Converts a Q8 to a double.- Parameters:
val
- the Q8 value to convert
-
toQ4
public static int toQ4(double val) Convert a double to Q4 format.- Parameters:
val
- the value to convert
-
fromQ4
public static double fromQ4(int val) Convert a Q4 to a double.- Parameters:
val
- the Q4 value to convert
-
toHexString
Converts a byte array to a hex string.- Parameters:
bytes
- the byte array to convert- Returns:
- the hex string
-
fromHexString
Converts a hex string to a byte array. If the string is an odd number of digits, assumes a leading zero.- Parameters:
str
- the string to convert- Returns:
- the corresponding byte array
-
convert
Converts from a string to the specified type. Supports most base types as well as lists of base types.- Parameters:
str
- the string to convertclazz
- the type to convert to
-
toShort
Converts the given object to ashort
, if possible.- Parameters:
obj
- the object to convert- Returns:
- converted value
-
toInt
Converts the given object to anint
, if possible.- Parameters:
obj
- the object to convert- Returns:
- converted value
-
toLong
Converts the given object to along
, if possible.- Parameters:
obj
- the object to convert- Returns:
- converted value
-
toDouble
Converts the given object to adouble
, if possible.- Parameters:
obj
- the object to convert- Returns:
- converted value
-