Package de.mhus.lib.core
Class MCast
- java.lang.Object
-
- de.mhus.lib.core.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 Summary
Constructors Constructor Description MCast()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddCaster(Caster<?,?> caster, boolean overwrite)static longbytesToLong(byte[] b)static ObjectfromBinary(byte[] bin)static byte[]fromBinaryString(String in)Convert a string with hex values in a byte array.static ObjectgetDefaultPrimitive(Class<?> type)Return 0 in 7 flavors or nullstatic ObjectgetDefaultPrimitive(String type)static byte[]hexStringToByteArray(String s)Convert a hex string e.g.static bytehexToByte(String in, int offset)Convert a two letter hex value to a single byte value.static byte[]longToBytes(long l)static DateobjectToDate(Object value)static StringobjectToString(Object value)static StringserializeToString(Object value)static BigDecimaltoBigDecimal(Object in, BigDecimal def)static byte[]toBinary(Object value)static StringtoBinaryString(byte[] in)Convert the byte array to a string representation.static StringtoBitsString(byte b)static booleantoboolean(Object _in, boolean _default)Convert String to boolean.static bytetobyte(Object in, byte def)static byte[]toByteArray(String in)static StringtoByteUnit(long p)static CalendartoCalendar(Object in)static CalendartoCalendar(String in)Parse a time date string.static CalendartoCalendar(String in, Calendar def)Parse time and date and return calendar.static CalendartoCalendar(Date _in, TimeZone tz, Locale l)static StringtoCurrencyString(double _in)Will round the value mathematically and return every time a comma as separator and two digits after comma.static DatetoDate(Object in, Date def)Try to parse a String and return the equivalent Date object.static DatetoDate(Object in, Date def, Locale locale)static doubletodouble(Object in, double def)Convert a string to double.static doubletodouble(String in, double def, Locale locale)Convert a string to double.static doubletodoubleEuropean(String in, double def)Convert a string to double.static doubletodoubleUS(String in, double def)Convert a string to double.static <E extends Enum<E>>
EtoEnum(Object value, E def)static floattofloat(Object in, float def)Convert a string to float.static StringtoHex2LowerString(int _in)static StringtoHex2String(byte in)Convert a byte to a two letter hex value.static StringtoHex2String(int _in)Convert integer to two letter hex code.static StringtoHex4String(int _in)Convert integer to four letter hex code.static StringtoHexString(byte[] bytes)Converts an array of bytes into a hex string representation.static Map<String,Object>toIndexedMap(Object... values)Return an indexed map of the values.static inttoint(Object in, int def)Converts String to int.static inttointFromHex(String _in)static int[]toIntIntervalValues(String def, int defStart, int defStop)Return a interval based of the the definition string.static ObjecttoList(Object in, Object def)static longtolong(Object in, long def)Converts a string to long.static long[]toLongIntervalValues(String def)Return a interval based of the the definition string.static ObjecttoMap(Object in, Object def)static StringtoReadableChar(char b)static shorttoshort(Object in, short def)static DatetoSqlDate(Date date)static StringtoString(boolean _in)Convert a boolean to string.static StringtoString(byte[] in)Returns a comma separated list of hex values as string.static StringtoString(double in)Convert a double to string.static StringtoString(float in)Convert a double to string.static StringtoString(int _in)Converts integer to String.static StringtoString(int _in, int _digits)Converts integer to string with the minimum digits.static StringtoString(long _in)Convert long to string.static StringtoString(long _in, int _digits)Converts integer to string with the minimum digits.static StringtoString(Object in)Returns a string in every case even if input is null it returns an empty string.static StringtoString(Object in, String def)static StringtoString(String firstLine, StackTraceElement[] trace)static StringtoString(Throwable t)static String[]toStringArray(List<?> _v)Put all list elements in a string list.static ObjecttoType(Object in, Class<?> type, Object def)static ObjecttoType(String value, String type, String def)static StringtoUnit(long p)static ObjectunserializeFromString(String value, ClassLoader cl)
-
-
-
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
-
toBigDecimal
public static BigDecimal toBigDecimal(Object in, BigDecimal def)
-
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)
-
toString
public static String toString(String firstLine, StackTraceElement[] trace)
-
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
-
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
-
toBinary
public static byte[] toBinary(Object value) throws IOException
- Throws:
IOException
-
fromBinary
public static Object fromBinary(byte[] bin) throws IOException, ClassNotFoundException
- Throws:
IOExceptionClassNotFoundException
-
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 (*)
-
toReadableChar
public static String toReadableChar(char b)
-
toBitsString
public static String toBitsString(byte b)
-
unserializeFromString
public static Object unserializeFromString(String value, ClassLoader cl) throws IOException, ClassNotFoundException
- Throws:
IOExceptionClassNotFoundException
-
serializeToString
public static String serializeToString(Object value) throws IOException
- Throws:
IOException
-
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)
-
-