Class TypeConversionHelper


  • public class TypeConversionHelper
    extends Object
    Class with methods for type conversion.
    • Constructor Detail

      • TypeConversionHelper

        public TypeConversionHelper()
    • Method Detail

      • getBooleanArrayFromBitSet

        public static boolean[] getBooleanArrayFromBitSet​(BitSet value)
        Convert an instance of our value class into a boolean[].
        Parameters:
        value - Object to be converted
        Returns:
        converted boolean array
      • getBitSetFromBooleanArray

        public static BitSet getBitSetFromBooleanArray​(boolean[] buf)
        Convert a boolean[] into an instance of our value class.
        Parameters:
        buf - boolean array to be converted
        Returns:
        converted boolean array as BitSet
      • getBooleanArrayFromByteArray

        public static boolean[] getBooleanArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted boolean array as object
      • getByteArrayFromBooleanArray

        public static byte[] getByteArrayFromBooleanArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Object to be converted
        Returns:
        converted byte array
      • getCharArrayFromByteArray

        public static char[] getCharArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted char array as object
      • getByteArrayFromCharArray

        public static byte[] getByteArrayFromCharArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Object to be converted
        Returns:
        converted byte array
      • getDoubleArrayFromByteArray

        public static double[] getDoubleArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted double array as object
      • getByteArrayFromDoubleArray

        public static byte[] getByteArrayFromDoubleArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Object to be converted
        Returns:
        converted byte array
      • getFloatArrayFromByteArray

        public static float[] getFloatArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted float array as object
      • getByteArrayFromFloatArray

        public static byte[] getByteArrayFromFloatArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Object to be converted
        Returns:
        converted byte array
      • getIntArrayFromByteArray

        public static int[] getIntArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted int array as object
      • getByteArrayFromIntArray

        public static byte[] getByteArrayFromIntArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Object to be converted
        Returns:
        converted byte array
      • getLongArrayFromByteArray

        public static long[] getLongArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted long array as object
      • getByteArrayFromLongArray

        public static byte[] getByteArrayFromLongArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Object to be converted
        Returns:
        converted byte array
      • getShortArrayFromByteArray

        public static short[] getShortArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted short array as object
      • getByteArrayFromShortArray

        public static byte[] getByteArrayFromShortArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Object to be converted
        Returns:
        converted byte array
      • getByteArrayFromBigDecimalArray

        public static byte[] getByteArrayFromBigDecimalArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Object to be converted
        Returns:
        converted byte array
      • getBigDecimalArrayFromByteArray

        public static BigDecimal[] getBigDecimalArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted BigDecimal array as object
      • getByteArrayFromBigIntegerArray

        public static byte[] getByteArrayFromBigIntegerArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Object to be converted
        Returns:
        converted byte array
      • getBigIntegerArrayFromByteArray

        public static BigInteger[] getBigIntegerArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted short array as object
      • getByteArrayFromBooleanObjectArray

        public static byte[] getByteArrayFromBooleanObjectArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Boolean[] to be converted
        Returns:
        converted byte array
      • getBooleanObjectArrayFromByteArray

        public static Boolean[] getBooleanObjectArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted Boolean array as object
      • getByteArrayFromByteObjectArray

        public static byte[] getByteArrayFromByteObjectArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Byte[] to be converted
        Returns:
        converted byte array
      • getByteObjectArrayFromByteArray

        public static Byte[] getByteObjectArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted Byte array as object
      • getByteArrayFromCharObjectArray

        public static byte[] getByteArrayFromCharObjectArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Character array to be converted
        Returns:
        converted byte array
      • getCharObjectArrayFromByteArray

        public static Character[] getCharObjectArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted Character array as object
      • getByteArrayFromDoubleObjectArray

        public static byte[] getByteArrayFromDoubleObjectArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Double array to be converted
        Returns:
        converted byte array
      • getDoubleObjectArrayFromByteArray

        public static Double[] getDoubleObjectArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted Double array as object
      • getByteArrayFromFloatObjectArray

        public static byte[] getByteArrayFromFloatObjectArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Float array to be converted
        Returns:
        converted byte array
      • getFloatObjectArrayFromByteArray

        public static Float[] getFloatObjectArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted Float array as object
      • getByteArrayFromIntObjectArray

        public static byte[] getByteArrayFromIntObjectArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Integer array to be converted
        Returns:
        converted byte array
      • getIntObjectArrayFromByteArray

        public static Integer[] getIntObjectArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted Integer array as object
      • getByteArrayFromLongObjectArray

        public static byte[] getByteArrayFromLongObjectArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Long array to be converted
        Returns:
        converted byte array
      • getLongObjectArrayFromByteArray

        public static Long[] getLongObjectArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted Long array as object
      • getByteArrayFromShortObjectArray

        public static byte[] getByteArrayFromShortObjectArray​(Object value)
        Convert an instance of our value class into a byte[].
        Parameters:
        value - Short array to be converted
        Returns:
        converted byte array
      • getShortObjectArrayFromByteArray

        public static Short[] getShortObjectArrayFromByteArray​(byte[] buf)
        Convert a byte[] into an instance of our value class.
        Parameters:
        buf - byte array to be converted
        Returns:
        converted Short array as object
      • convertTo

        public static Object convertTo​(Object value,
                                       Class type)
        Convert the value to a instance of the given type. The value is converted only if the type can't be assigned from the current type of the value instance.
        Parameters:
        value - the value to be converted
        type - the type of the expected object returned from the conversion
        Returns:
        the converted object, or the input value if the object can't be converted
      • getBytesFromInt

        public static byte[] getBytesFromInt​(int val)
        Utility to convert an int into a byte array
        Parameters:
        val - The int
        Returns:
        The bytes
      • getBytesFromShort

        public static byte[] getBytesFromShort​(short val)
        Utility to convert a short into a a byte array
        Parameters:
        val - The short
        Returns:
        The bytes
      • getStringFromInt

        public static String getStringFromInt​(int val)
        Utility to convert an int into a byte-generated String
        Parameters:
        val - The int
        Returns:
        The String form of the bytes
      • getStringFromShort

        public static String getStringFromShort​(short val)
        Utility to convert a short into a byte-generated String
        Parameters:
        val - The short
        Returns:
        The String form of the bytes
      • getHexFromInt

        public static String getHexFromInt​(int val)
        Utility to convert an int into a 8-char hex String
        Parameters:
        val - The int
        Returns:
        The hex String form of the int
      • getHexFromShort

        public static String getHexFromShort​(short val)
        Utility to convert a short into a 4-char hex String
        Parameters:
        val - The short
        Returns:
        The hex String form of the short
      • getIntFromByteArray

        public static int getIntFromByteArray​(byte[] bytes)
        Utility to convert a byte array to an int.
        Parameters:
        bytes - The byte array
        Returns:
        The int
      • stringToTimestamp

        public static Timestamp stringToTimestamp​(String s,
                                                  Calendar cal)
        Converts a string in JDBC timestamp escape format to a Timestamp object. To be precise, we prefer to find a JDBC escape type sequence in the format "yyyy-mm-dd hh:mm:ss.fffffffff", but this does not accept other separators of fields, so as long as the numbers are in the order year, month, day, hour, minute, second then we accept it.
        Parameters:
        s - Timestamp string
        cal - The Calendar to use for conversion
        Returns:
        Corresponding java.sql.Timestamp value.
        Throws:
        IllegalArgumentException - Thrown if the format of the String is invalid
      • timestampToString

        public static String timestampToString​(Timestamp ts,
                                               Calendar cal)
        Formats a timestamp in JDBC timestamp escape format using the timezone of the passed Calendar.
        Parameters:
        ts - The timestamp to be formatted.
        cal - The Calendar
        Returns:
        A String in yyyy-mm-dd hh:mm:ss.fffffffff format.
        See Also:
        Timestamp
      • intFromString

        public static int intFromString​(String str,
                                        int dflt)
        Convert a string into an integer. Returns the default value if not convertable.
        Parameters:
        str - The string
        dflt - The default value
        Returns:
        The converted int value
      • getStoredValueFromEnum

        public static Object getStoredValueFromEnum​(AbstractMemberMetaData mmd,
                                                    FieldRole role,
                                                    Enum myEnum)
        Convenience method to return the "value" of an Enum, for a field and role. Firstly checks for a defined method on the Enum that returns the "value", otherwise falls back to use the ordinal.
        Parameters:
        mmd - Metadata for the member
        role - Role of the Enum in this member
        myEnum - The enum
        Returns:
        The "value" (String or Integer)