Class Convert

java.lang.Object
com.tccc.kos.commons.util.convert.Convert

public final class Convert extends Object
Parser for converting string values to other formats.
Since:
1.0
Version:
2023-08-25
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Map<Class<?>,String>
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    convert(String str, Class<T> clazz)
    Converts from a string to the specified type.
    static byte[]
    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
    getBool(String str, boolean def)
    Returns the value of the specified string as a boolean.
    static boolean
    getBool(String str, boolean def, boolean required)
    Returns the value of the specified string as a boolean.
    static double
    Returns the value of the string as a double.
    static double
    getDouble(String str, double def)
    Returns the value of the specified string as a double.
    static double
    getDouble(String str, double def, boolean required)
    Returns the value of the specified string as a double.
    static float
    getFloat(String str, float def, boolean required)
    Returns the value of the specified string as a float.
    static int
    Returns the value of the string as an int.
    static int
    getInt(String str, int def)
    Returns the value of the specified string as an int.
    static int
    getInt(String str, int def, boolean required)
    Returns the value of the specified string as an int.
    static long
    Returns the value of the string as a long.
    static long
    getLong(String str, long def)
    Returns the value of the specified string as a long.
    static long
    getLong(String str, long def, boolean required)
    Returns the value of the specified string as a long.
    static short
    Converts the input string to a short.
    static short
    getShort(String str, short def)
    Converts the input string to a short.
    static short
    getShort(String str, short def, boolean isRequired)
    Converts the input string to a short.
    static String
    Returns the specified string.
    static String
    getStr(String str, String def)
    Returns the specified string.
    static String
    getStr(String str, String def, boolean required)
    Returns the specified string.
    static double
    Converts the given object to a double , if possible.
    static String
    toHexString(byte[] bytes)
    Converts a byte array to a hex string.
    static int
    Converts the given object to an int , if possible.
    static long
    Converts the given object to a long , 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 a short , if possible.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SIMPLE_TYPES

      public static final Map<Class<?>,String> SIMPLE_TYPES
  • Constructor Details

    • Convert

      public Convert()
  • Method Details

    • getStr

      public static String getStr(String str, String def, boolean required)
      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 return
      def - the default value
      required - if true, throw an exception if not found
      Returns:
      the specified string or def if null
    • getStr

      public static String getStr(String str, String def)
      Returns the specified string. If it is null then return the default value.
      Parameters:
      str - the string to check and return
      def - the default value
      Returns:
      the specified string or def if null
    • getStr

      public static String getStr(String str)
      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

      public static short getShort(String str)
      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

      public static short getShort(String str, short def)
      Converts the input string to a short. If the string is null, then the default is returned.
      Parameters:
      str - the string to parse
      def - the default value
      Returns:
      the specified string as a short or def if input is null or cannot be converted
    • getShort

      public static short getShort(String str, short def, boolean isRequired)
      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 parse
      def - the default value
      isRequired - 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

      public static int getInt(String str, int def, boolean required)
      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 parse
      def - the default value
      required - if true, throw an exception if not found
      Returns:
      the specified string as an int or def if null
    • getInt

      public static int getInt(String str, int def)
      Returns the value of the specified string as an int. If string is null then return the default.
      Parameters:
      str - the string to convert
      def - the default value
      Returns:
      the specified string as an int or def if null
    • getInt

      public static int getInt(String str)
      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

      public static long getLong(String str, long def, boolean required)
      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 parse
      def - the default value
      required - if true, throw an exception if not found
      Returns:
      the specified string as a long or def if null
    • getLong

      public static long getLong(String str, long def)
      Returns the value of the specified string as a long. If string is null then return the default.
      Parameters:
      str - the string to convert
      def - the default value
      Returns:
      the specified string as a long or def if null
    • getLong

      public static long getLong(String str)
      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

      public static float getFloat(String str, float def, boolean required)
      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 parse
      def - the default value
      required - if true, throw an exception if not found
      Returns:
      the specified string as a float or def if null
    • getDouble

      public static double getDouble(String str, double def, boolean required)
      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 parse
      def - the default value
      required - if true, throw an exception if not found
      Returns:
      the specified string as a double or def if null
    • getDouble

      public static double getDouble(String str, double def)
      Returns the value of the specified string as a double. If string is null then return the default.
      Parameters:
      str - the string to convert
      def - the default value
      Returns:
      the specified string as a double or def if null
    • getDouble

      public static double getDouble(String str)
      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

      public static boolean getBool(String str, boolean def, boolean required)
      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 parse
      def - the default value
      required - if true, throw an exception if not found
      Returns:
      the specified string as a boolean or def if null
    • getBool

      public static boolean getBool(String str, boolean def)
      Returns the value of the specified string as a boolean. If string is null then return the default.
      Parameters:
      str - the string to convert
      def - the default value
      Returns:
      the specified string as a boolean or def if null
    • getBool

      public static boolean getBool(String str)
      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

      public static String toHexString(byte[] bytes)
      Converts a byte array to a hex string.
      Parameters:
      bytes - the byte array to convert
      Returns:
      the hex string
    • fromHexString

      public static byte[] fromHexString(String str)
      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

      public static <T> T convert(String str, Class<T> clazz)
      Converts from a string to the specified type. Supports most base types as well as lists of base types.
      Parameters:
      str - the string to convert
      clazz - the type to convert to
    • toShort

      public static short toShort(Object obj)
      Converts the given object to a short , if possible.
      Parameters:
      obj - the object to convert
      Returns:
      converted value
    • toInt

      public static int toInt(Object obj)
      Converts the given object to an int , if possible.
      Parameters:
      obj - the object to convert
      Returns:
      converted value
    • toLong

      public static long toLong(Object obj)
      Converts the given object to a long , if possible.
      Parameters:
      obj - the object to convert
      Returns:
      converted value
    • toDouble

      public static double toDouble(Object obj)
      Converts the given object to a double , if possible.
      Parameters:
      obj - the object to convert
      Returns:
      converted value