Class MCast


  • public final class MCast
    extends Object
    Smplifies casts between java classes. Some functions in this class only make the code readable. e.g. from string to int.

    All Funktions are static.

    Author:
    jesus
    • Constructor Detail

      • MCast

        public MCast()
    • Method Detail

      • toCurrencyString

        public static String toCurrencyString​(double _in)
        Will round the value mathematically and return every time a comma as separator and two digits after comma.
        Parameters:
        _in -
        Returns:
        nice string
      • addCaster

        public static void addCaster​(Caster<?,​?> caster,
                                     boolean overwrite)
      • toDate

        public static Date toDate​(Object in,
                                  Date def)
        Try to parse a String and return the equivalent Date object. The string should contain a iso date string like "yyyy-mm-dd" syntax: "yyyy-mm-dd[[ HH:MM:SS].xxx]" where xxx is Millisecond. Milliseconds are ignored. For the date part there are alternative syntax: "dd.mm.yyyy" or "mm/dd/yyyy". Or a timestamp.

        If the time is not in the string, it will be set to "00:00:00". It is possible to leave year, in this case it will be replaced with the actuall year. If you leave month, it will be replaced with the actuall month.

        Parameters:
        in -
        def -
        Returns:
        In all cases an Date() object. Is getTime() is 0, it occurs an error: ACast.toDate( in ).getTime == 0.
      • toCalendar

        public static Calendar toCalendar​(String in)
        Parse a time date string.
        Parameters:
        in -
        Returns:
        the calendar
      • toCalendar

        public static Calendar toCalendar​(String in,
                                          Calendar def)
        Parse time and date and return calendar.
        Parameters:
        in -
        def -
        Returns:
        the calendar
      • toBinaryString

        public static String toBinaryString​(byte[] in)
        Convert the byte array to a string representation. It stores for every byte a two letter hex value in the string.
        Parameters:
        in -
        Returns:
        as binary
      • fromBinaryString

        public static byte[] fromBinaryString​(String in)
        Convert a string with hex values in a byte array.
        Parameters:
        in -
        Returns:
        the array
      • hexToByte

        public static byte hexToByte​(String in,
                                     int offset)
        Convert a two letter hex value to a single byte value.
        Parameters:
        in -
        offset -
        Returns:
        a byte
      • toHex2String

        public static String toHex2String​(byte in)
        Convert a byte to a two letter hex value.
        Parameters:
        in -
        Returns:
        hex string
      • hexStringToByteArray

        public static byte[] hexStringToByteArray​(String s)
        Convert a hex string e.g. from DM5() to a byte array. Every two characters will be converted to one signed byte (-128 to 127)
        Parameters:
        s - The hex encoded string
        Returns:
        The resulting byte array
      • toHexString

        public static String toHexString​(byte[] bytes)
        Converts an array of bytes into a hex string representation. Every byte will be presented by two characters.
        Parameters:
        bytes - Array of bytes
        Returns:
        Hex String
      • toboolean

        public static boolean toboolean​(Object _in,
                                        boolean _default)
        Convert String to boolean. If the conversion was not possible it returns "_default". Valid true values: yes, true, ja, 1, t valid false values: no, false, nein, 0, f
        Parameters:
        _in -
        _default -
        Returns:
        a boolean
      • tofloat

        public static float tofloat​(Object in,
                                    float def)
        Convert a string to float. If the string is malformed it returns "_def".
        Parameters:
        in -
        def -
        Returns:
        a float
      • todouble

        public static double todouble​(Object in,
                                      double def)
        Convert a string to double. If the string is malformed it returns "def". This method will use the default double format without thousands separator and a dot as decimal separator.
        Parameters:
        in -
        def -
        Returns:
        a double
      • todoubleEuropean

        public static double todoubleEuropean​(String in,
                                              double def)
        Convert a string to double. If the string is malformed it returns "def". This method will use the european format expect GB. As thousands separator space or dot and as decimal comma separator. 4 294 967.295,000 Reference to http://docs.oracle.com/cd/E19455-01/806-0169/overview-9/index.html
        Parameters:
        in -
        def -
        Returns:
        a double
      • todoubleUS

        public static double todoubleUS​(String in,
                                        double def)
        Convert a string to double. If the string is malformed it returns "def". This method will use the US and GB format. As thousands separator space or comma and as decimal dot separator. 4,294,967,295.00 Reference to http://docs.oracle.com/cd/E19455-01/806-0169/overview-9/index.html
        Parameters:
        in -
        def -
        Returns:
        a double
      • todouble

        public static double todouble​(String in,
                                      double def,
                                      Locale locale)
        Convert a string to double. If the string is malformed it returns "def". This method will use NumberFormat to parse the string.
        Parameters:
        in -
        def -
        locale - Locale or null for default locale
        Returns:
        a double
      • toint

        public static int toint​(Object in,
                                int def)
        Converts String to int. If the string is malformed then it returns "_def". A valid format is also the hex 0x (e.g. 0xFFFF) variant.
        Parameters:
        in -
        def -
        Returns:
        an integer
      • tolong

        public static long tolong​(Object in,
                                  long def)
        Converts a string to long. If the string is malformed then it returns "_def". A valid format is also the hex 0x (e.g. 0xFFFF) variant.
        Parameters:
        in -
        def -
        Returns:
        a long
      • tobyte

        public static byte tobyte​(Object in,
                                  byte def)
      • toshort

        public static short toshort​(Object in,
                                    short def)
      • toString

        public static String toString​(double in)
        Convert a double to string. The separator is a dot.
        Parameters:
        in -
        Returns:
        corresponding string
      • toString

        public static String toString​(float in)
        Convert a double to string. The separator is a dot.
        Parameters:
        in -
        Returns:
        corresponding string
      • toString

        public static String toString​(boolean _in)
        Convert a boolean to string. Values are "true", "false".
        Parameters:
        _in -
        Returns:
        corresponding string
      • toString

        public static String toString​(int _in)
        Converts integer to String.
        Parameters:
        _in -
        Returns:
        corresponding string
      • toString

        public static String toString​(int _in,
                                      int _digits)
        Converts integer to string with the minimum digits.
        Parameters:
        _in -
        _digits -
        Returns:
        corresponding string
      • toString

        public static String toString​(long _in,
                                      int _digits)
        Converts integer to string with the minimum digits.
        Parameters:
        _in -
        _digits -
        Returns:
        corresponding string
      • toString

        public static String toString​(long _in)
        Convert long to string.
        Parameters:
        _in -
        Returns:
        corresponding string
      • toHex2String

        public static String toHex2String​(int _in)
        Convert integer to two letter hex code. Ignores negative values.
        Parameters:
        _in -
        Returns:
        corresponding string as hex
      • toHex2LowerString

        public static String toHex2LowerString​(int _in)
      • toHex4String

        public static String toHex4String​(int _in)
        Convert integer to four letter hex code. Ignores negative values.
        Parameters:
        _in -
        Returns:
        corresponding string as hex
      • toStringArray

        public static String[] toStringArray​(List<?> _v)
        Put all list elements in a string list. Use the toString method.
        Parameters:
        _v -
        Returns:
        corresponding string array, never null
      • tointFromHex

        public static int tointFromHex​(String _in)
      • toString

        public static String toString​(byte[] in)
        Returns a comma separated list of hex values as string. Not the corresponding char set decoded string.
        Parameters:
        in -
        Returns:
        List of hex values
      • toByteArray

        public static byte[] toByteArray​(String in)
      • toIndexedMap

        public static Map<String,​Object> toIndexedMap​(Object... values)
        Return an indexed map of the values. The first value has the index "0" and so on.
        Parameters:
        values -
        Returns:
        corresponding map
      • objectToString

        public static String objectToString​(Object value)
      • objectToDate

        public static Date objectToDate​(Object value)
      • toSqlDate

        public static Date toSqlDate​(Date date)
      • toString

        public static String toString​(Object in)
        Returns a string in every case even if input is null it returns an empty string.
        Parameters:
        in -
        Returns:
        corresponding string
      • getDefaultPrimitive

        public static Object getDefaultPrimitive​(Class<?> type)
        Return 0 in 7 flavors or null
        Parameters:
        type -
        Returns:
        the default value for the class
      • getDefaultPrimitive

        public static Object getDefaultPrimitive​(String type)
      • toLongIntervalValues

        public static long[] toLongIntervalValues​(String def)
        Return a interval based of the the definition string. Allowed are lists separated by comma (1,2,3,4) and intervals (6/2 => 0,2,4,6 or 3-7/2 => 3,5,7) or '*' for null TODO also allow mixed lists and intervals like 4,6-10/2
        Parameters:
        def -
        Returns:
        the values for the interval or null if should be ignored (*)
      • toIntIntervalValues

        public static int[] toIntIntervalValues​(String def,
                                                int defStart,
                                                int defStop)
        Return a interval based of the the definition string. Allowed are lists separated by comma (1,2,3,4) and intervals (6/2 => 0,2,4,6 or 3-7/2 => 3,5,7) or '*' for null TODO also allow mixed lists and intervals like 4,6-10/2
        Parameters:
        def -
        defStart -
        defStop -
        Returns:
        the values for the interval or null if should be ignored (*)
      • toEnum

        public static <E extends Enum<E>> E toEnum​(Object value,
                                                   E def)
      • toReadableChar

        public static String toReadableChar​(char b)
      • toBitsString

        public static String toBitsString​(byte b)
      • toUnit

        public static String toUnit​(long p)
      • toByteUnit

        public static String toByteUnit​(long p)
      • longToBytes

        public static byte[] longToBytes​(long l)
      • bytesToLong

        public static long bytesToLong​(byte[] b)