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 @interface
static class
-
Field Summary
FieldsModifier and TypeFieldDescription -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Byte
box
(byte value) static Character
box
(char value) static Double
box
(double value) static Float
box
(float value) static Integer
box
(int value) static Long
box
(long value) static Short
box
(short value) static Boolean
static Class
classForName
(String className) static Object
decode64Serializable
(String string) Decodes a Serializable Object from a base64 encoded String.static String
encode64Serializable
(Serializable serializable) Encodes a Serializable Object into base64 String.static Object
fromString
(String string, Class<?> type) Creates an Object from a String.fromString
(String string, String typeString) Creates an Object from a String.static Class
getBoxedType
(Class type) Returns a reference type corresponding to the giventype
.static Class
getErasedType
(Type paramType) Determine the Class from the Type.static TypeUtils.TypeBoxer
getTypeBoxer
(Class type) static Class
getUnboxedType
(Class type) Returns the primitive type corresponding to the giventype
.static Class
getUnboxedTypeIfBoxed
(Class type) Same asgetUnboxedType(Class)
, but returns non-wrapper classes unmolested.static boolean
isBigNumeric
(Class type) Whether the class is aBigInteger
orBigDecimal
static boolean
Whether the class is a boxed arithmetic type (Long, Integer, Short, Byte)static boolean
Whether the class is an instance of Boolean.class.static boolean
isBoxedByte
(Class c) Whether the class is an instance of Byte.class.static boolean
isBoxedChar
(Class c) Whether the class is an instance of Character.class.static boolean
Whether the class is an instance of Double.class.static boolean
Whether the class is an instance of Float.class.static boolean
Whether the class is an instance of Integer.class.static boolean
isBoxedLong
(Class c) Whether the class is an instance of Long.class.static boolean
Whether the class is an instance ofNumber
.static boolean
Whether the class is an instance of Short.class.static boolean
isBoxedType
(Class exprType) static boolean
isCharacter
(Class c) Whether the class equals char.class or Character.class is assignable from it.static boolean
static boolean
isDateTime
(Class type) Whether the class is a DBDateTime, Date, or Instant.static boolean
isFloatType
(Class type) Checks if the type is a primitive or Boxed floate type (double or float).static boolean
Whether the class isisPrimitiveNumeric(Class)
orisBoxedNumeric(Class)
static boolean
Whether the class is equal to char.class.static boolean
Whether the class is equal to one of the six numeric primitives: float, double, int, long, short, or byte.static boolean
Checks if a type is primitive orSerializable
.static boolean
Whether the class is aString
static String
nullConstantForType
(Class type) static String
Converts 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 boolean
static byte
static char
static double
static float
static int
static long
static short
-
Field Details
-
PRIMITIVE_TYPES
-
BOXED_TYPES
-
primitiveClassNameToClass
-
-
Constructor Details
-
TypeUtils
public TypeUtils()
-
-
Method Details
-
getBoxedType
Returns a reference type corresponding to the giventype
. Iftype
is itself a reference type, thentype
is returned. Iftype
is a primitive type, then the appropriate boxed type is returned.- Parameters:
type
- The type
-
getUnboxedType
Returns the primitive type corresponding to the giventype
. Iftype
is itself a primitive type, thentype
is returned. Iftype
is neither a primitive type nor a boxed type, thennull
is 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
c
is 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
c
equals 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
c
is 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
c
orc
equals 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 aBigInteger
orBigDecimal
- 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.objectToString
Strings, 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.objectToString
Strings, 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
-