Package com.illumon.util.type
Class TypeUtils
java.lang.Object
com.illumon.util.type.TypeUtils
- Direct Known Subclasses:
TypeUtils
Utility functions to convert primitive types.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic @interfacestatic class -
Field Summary
FieldsModifier and TypeFieldDescription -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Bytebox(byte value) static Characterbox(char value) static Doublebox(double value) static Floatbox(float value) static Integerbox(int value) static Longbox(long value) static Shortbox(short value) static Booleanstatic ClassclassForName(String className) static Objectdecode64Serializable(String string) Decodes a Serializable Object from a base64 encoded String.static Stringencode64Serializable(Serializable serializable) Encodes a Serializable Object into base64 String.static ObjectfromString(String string, Class<?> type) Creates an Object from a String.fromString(String string, String typeString) Creates an Object from a String.static ClassgetBoxedType(Class type) Returns a reference type corresponding to the giventype.static ClassgetErasedType(Type paramType) Determine the Class from the Type.static TypeUtils.TypeBoxergetTypeBoxer(Class type) static ClassgetUnboxedType(Class type) Returns the primitive type corresponding to the giventype.static ClassgetUnboxedTypeIfBoxed(Class type) Same asgetUnboxedType(Class), but returns non-wrapper classes unmolested.static booleanisBigNumeric(Class type) Whether the class is aBigIntegerorBigDecimalstatic booleanWhether the class is a boxed arithmetic type (Long, Integer, Short, Byte)static booleanWhether the class is an instance of Boolean.class.static booleanisBoxedByte(Class c) Whether the class is an instance of Byte.class.static booleanisBoxedChar(Class c) Whether the class is an instance of Character.class.static booleanWhether the class is an instance of Double.class.static booleanWhether the class is an instance of Float.class.static booleanWhether the class is an instance of Integer.class.static booleanisBoxedLong(Class c) Whether the class is an instance of Long.class.static booleanWhether the class is an instance ofNumber.static booleanWhether the class is an instance of Short.class.static booleanisBoxedType(Class exprType) static booleanisCharacter(Class c) Whether the class equals char.class or Character.class is assignable from it.static booleanstatic booleanisDateTime(Class type) Whether the class is a DBDateTime, Date, or Instant.static booleanisFloatType(Class type) Checks if the type is a primitive or Boxed floate type (double or float).static booleanWhether the class isisPrimitiveNumeric(Class)orisBoxedNumeric(Class)static booleanWhether the class is equal to char.class.static booleanWhether the class is equal to one of the six numeric primitives: float, double, int, long, short, or byte.static booleanChecks if a type is primitive orSerializable.static booleanWhether the class is aStringstatic StringnullConstantForType(Class type) static StringConverts an Object to a String for writing to a workspace.static byte[]toByteArray(double[] array) static byte[]toByteArray(float[] array) static byte[]toByteArray(int[] array) static byte[]toByteArray(long[] array) static byte[]toByteArray(short[] array) static double[]toDoubleArray(double[] array) static double[]toDoubleArray(float[] array) static double[]toDoubleArray(int[] array) static double[]toDoubleArray(long[] array) static double[]toDoubleArray(short[] array) static float[]toFloatArray(byte[] array) static float[]toFloatArray(double[] array) static float[]toFloatArray(int[] array) static float[]toFloatArray(long[] array) static float[]toFloatArray(short[] array) static int[]toIntArray(byte[] array) static int[]toIntArray(double[] array) static int[]toIntArray(float[] array) static int[]toIntArray(long[] array) static int[]toIntArray(short[] array) static long[]toLongArray(byte[] array) static long[]toLongArray(double[] array) static long[]toLongArray(float[] array) static long[]toLongArray(int[] array) static long[]toLongArray(short[] array) static short[]toShortArray(byte[] array) static short[]toShortArray(double[] array) static short[]toShortArray(float[] array) static short[]toShortArray(int[] array) static short[]toShortArray(long[] array) static booleanstatic bytestatic charstatic doublestatic floatstatic intstatic longstatic short
-
Field Details
-
PRIMITIVE_TYPES
-
BOXED_TYPES
-
primitiveClassNameToClass
-
-
Constructor Details
-
TypeUtils
public TypeUtils()
-
-
Method Details
-
getBoxedType
Returns a reference type corresponding to the giventype. Iftypeis itself a reference type, thentypeis returned. Iftypeis a primitive type, then the appropriate boxed type is returned.- Parameters:
type- The type
-
getUnboxedType
Returns the primitive type corresponding to the giventype. Iftypeis itself a primitive type, thentypeis returned. Iftypeis neither a primitive type nor a boxed type, thennullis returned.- Parameters:
type- The type- Returns:
- type's primitive equivalent, or null
-
getUnboxedTypeIfBoxed
Same asgetUnboxedType(Class), but returns non-wrapper classes unmolested.- Parameters:
type- The type- Returns:
- type's unboxed equivalent, or type
-
toByteArray
public static byte[] toByteArray(float[] array) -
toByteArray
public static byte[] toByteArray(int[] array) -
toByteArray
public static byte[] toByteArray(short[] array) -
toByteArray
public static byte[] toByteArray(long[] array) -
toByteArray
public static byte[] toByteArray(double[] array) -
toFloatArray
public static float[] toFloatArray(byte[] array) -
toFloatArray
public static float[] toFloatArray(int[] array) -
toFloatArray
public static float[] toFloatArray(short[] array) -
toFloatArray
public static float[] toFloatArray(long[] array) -
toFloatArray
public static float[] toFloatArray(double[] array) -
toShortArray
public static short[] toShortArray(float[] array) -
toShortArray
public static short[] toShortArray(int[] array) -
toShortArray
public static short[] toShortArray(byte[] array) -
toShortArray
public static short[] toShortArray(long[] array) -
toShortArray
public static short[] toShortArray(double[] array) -
toLongArray
public static long[] toLongArray(float[] array) -
toLongArray
public static long[] toLongArray(int[] array) -
toLongArray
public static long[] toLongArray(short[] array) -
toLongArray
public static long[] toLongArray(byte[] array) -
toLongArray
public static long[] toLongArray(double[] array) -
toIntArray
public static int[] toIntArray(float[] array) -
toIntArray
public static int[] toIntArray(byte[] array) -
toIntArray
public static int[] toIntArray(short[] array) -
toIntArray
public static int[] toIntArray(long[] array) -
toIntArray
public static int[] toIntArray(double[] array) -
toDoubleArray
public static double[] toDoubleArray(float[] array) -
toDoubleArray
public static double[] toDoubleArray(int[] array) -
toDoubleArray
public static double[] toDoubleArray(short[] array) -
toDoubleArray
public static double[] toDoubleArray(long[] array) -
toDoubleArray
public static double[] toDoubleArray(double[] array) -
isConvertibleToPrimitive
-
isBoxedType
-
nullConstantForType
-
isPrimitiveNumeric
Whether the class is equal to one of the six numeric primitives: float, double, int, long, short, or byte.- Parameters:
c- class- Returns:
- true if
cis a numeric primitive, false otherwise
-
isBoxedNumeric
Whether the class is an instance ofNumber.- Parameters:
c- class- Returns:
- true if Number.class is assignable from
c, false otherwise
-
isPrimitiveChar
Whether the class is equal to char.class.- Parameters:
c- class- Returns:
- true if
cequals char.class, false otherwise
-
isBoxedChar
Whether the class is an instance of Character.class.- Parameters:
c- class- Returns:
- true if Character.class is assignable from
c, false otherwise
-
isBoxedInteger
Whether the class is an instance of Integer.class.- Parameters:
c- class- Returns:
- true if Integer.class is assignable from
c, false otherwise
-
isBoxedLong
Whether the class is an instance of Long.class.- Parameters:
c- class- Returns:
- true if Long.class is assignable from
c, false otherwise
-
isBoxedShort
Whether the class is an instance of Short.class.- Parameters:
c- class- Returns:
- true if Short.class is assignable from
c, false otherwise
-
isBoxedFloat
Whether the class is an instance of Float.class.- Parameters:
c- class- Returns:
- true if Float.class is assignable from
c, false otherwise
-
isBoxedDouble
Whether the class is an instance of Double.class.- Parameters:
c- class- Returns:
- true if Double.class is assignable from
c, false otherwise
-
isBoxedByte
Whether the class is an instance of Byte.class.- Parameters:
c- class- Returns:
- true if Byte.class is assignable from
c, false otherwise
-
isBoxedArithmetic
Whether the class is a boxed arithmetic type (Long, Integer, Short, Byte)- Parameters:
c- class- Returns:
- true if the class is a boxed arithmetic type, false otherwise
-
isBoxedBoolean
Whether the class is an instance of Boolean.class.- Parameters:
c- class- Returns:
- true if Boolean.class is assignable from
c, false otherwise
-
isNumeric
Whether the class isisPrimitiveNumeric(Class)orisBoxedNumeric(Class)- Parameters:
c- class- Returns:
- true if
cis numeric, false otherwise
-
isCharacter
Whether the class equals char.class or Character.class is assignable from it.- Parameters:
c- class- Returns:
- true if Character.class is assignable from
corcequals char.class
-
isDateTime
Whether the class is a DBDateTime, Date, or Instant.- Parameters:
type- The class.- Returns:
- true if the type is a DBDateTime or
Date.
-
isString
Whether the class is aString- Parameters:
type- the class- Returns:
- true if the type is a String, false otherwise
-
isBigNumeric
Whether the class is aBigIntegerorBigDecimal- Parameters:
type- the class- Returns:
- true if the type is BigInteger or BigDecimal, false otherwise
-
isPrimitiveOrSerializable
Checks if a type is primitive orSerializable.- Parameters:
type- the class- Returns:
- true if the type is primitive or Serializable
-
isFloatType
Checks if the type is a primitive or Boxed floate type (double or float).- Parameters:
type- the class- Returns:
- true if it is a float type, false otherwise
-
objectToString
Converts an Object to a String for writing to a workspace. This is meant to be used in conjunction withTypeUtils.fromString. Strings, Numbers, and primitives will all convert usingObect.toString. Serializable objects will be encoded in base64. All others will return null.- Parameters:
o- the object to convert- Returns:
- a String representation of the object, null if it cannot be converted
- Throws:
IOException- if an IO error occurs during conversion
-
fromString
Creates an Object from a String. This is meant to be used in conjunction withTypeUtils.objectToStringStrings, Numbers, and primitives will all parse using their boxed type parsing methods. Serializable types will be decoded from base64. Returns null if the String fails to parse.- Parameters:
string- the String to parsetypeString- the Canonical Name of the class type- Returns:
- an object parsed from the String
- Throws:
RuntimeException- if the string fails to parseIOException- if an IO error occurs during conversion
-
fromString
Creates an Object from a String. This is meant to be used in conjunction withTypeUtils.objectToStringStrings, Numbers, and primitives will all parse using their boxed type parsing methods. Serializable types will be decoded from base64. Returns null if the String fails to parse.- Parameters:
string- the String to parsetype- the type of the object- Returns:
- an object parsed from the String
- Throws:
RuntimeException- if the string fails to parseIOException- if an IO error occurs during conversion
-
encode64Serializable
Encodes a Serializable Object into base64 String.- Parameters:
serializable- the object to encode- Returns:
- the base64 encoded string
- Throws:
IOException- if the string cannot be encoded
-
decode64Serializable
Decodes a Serializable Object from a base64 encoded String.- Parameters:
string- the base64 encoded String- Returns:
- the encoded Object
- Throws:
IOException- if the string cannot be decodedClassNotFoundException- if the Object type is unknown
-
getErasedType
Determine the Class from the Type. This is derived from the method in https://github.com/WeTheInternet/xapi/blob/master/core/model/src/main/java/xapi/model/impl/ModelUtil.java. Please consult with James Nelson for details.- Parameters:
paramType-- Returns:
-
classForName
- Throws:
ClassNotFoundException
-
getTypeBoxer
-
box
-
box
-
box
-
box
-
box
-
box
-
box
-
box
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-
unbox
-