Class TypeUtils

java.lang.Object
com.illumon.util.type.TypeUtils
Direct Known Subclasses:
TypeUtils

public class TypeUtils extends Object
Utility functions to convert primitive types.
  • Field Details

    • PRIMITIVE_TYPES

      public static final Set<Class> PRIMITIVE_TYPES
    • BOXED_TYPES

      public static final Set<Class> BOXED_TYPES
    • primitiveClassNameToClass

      public static final Map<String,Class> primitiveClassNameToClass
  • Constructor Details

    • TypeUtils

      public TypeUtils()
  • Method Details

    • getBoxedType

      public static Class getBoxedType(Class type)
      Returns a reference type corresponding to the given type. If type is itself a reference type, then type is returned. If type is a primitive type, then the appropriate boxed type is returned.
      Parameters:
      type - The type
    • getUnboxedType

      public static Class getUnboxedType(Class type)
      Returns the primitive type corresponding to the given type. If type is itself a primitive type, then type is returned. If type is neither a primitive type nor a boxed type, then null is returned.
      Parameters:
      type - The type
      Returns:
      type's primitive equivalent, or null
    • getUnboxedTypeIfBoxed

      public static Class getUnboxedTypeIfBoxed(@NotNull Class type)
      Same as getUnboxedType(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

      public static boolean isConvertibleToPrimitive(Class type)
    • isBoxedType

      public static boolean isBoxedType(Class exprType)
    • nullConstantForType

      public static String nullConstantForType(Class type)
    • isPrimitiveNumeric

      public static boolean isPrimitiveNumeric(@NotNull Class c)
      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

      public static boolean isBoxedNumeric(@NotNull Class c)
      Whether the class is an instance of Number.
      Parameters:
      c - class
      Returns:
      true if Number.class is assignable from c, false otherwise
    • isPrimitiveChar

      public static boolean isPrimitiveChar(@NotNull Class c)
      Whether the class is equal to char.class.
      Parameters:
      c - class
      Returns:
      true if c equals char.class, false otherwise
    • isBoxedChar

      public static boolean isBoxedChar(@NotNull Class c)
      Whether the class is an instance of Character.class.
      Parameters:
      c - class
      Returns:
      true if Character.class is assignable from c, false otherwise
    • isBoxedInteger

      public static boolean isBoxedInteger(@NotNull Class c)
      Whether the class is an instance of Integer.class.
      Parameters:
      c - class
      Returns:
      true if Integer.class is assignable from c, false otherwise
    • isBoxedLong

      public static boolean isBoxedLong(@NotNull Class c)
      Whether the class is an instance of Long.class.
      Parameters:
      c - class
      Returns:
      true if Long.class is assignable from c, false otherwise
    • isBoxedShort

      public static boolean isBoxedShort(@NotNull Class c)
      Whether the class is an instance of Short.class.
      Parameters:
      c - class
      Returns:
      true if Short.class is assignable from c, false otherwise
    • isBoxedFloat

      public static boolean isBoxedFloat(@NotNull Class c)
      Whether the class is an instance of Float.class.
      Parameters:
      c - class
      Returns:
      true if Float.class is assignable from c, false otherwise
    • isBoxedDouble

      public static boolean isBoxedDouble(@NotNull Class c)
      Whether the class is an instance of Double.class.
      Parameters:
      c - class
      Returns:
      true if Double.class is assignable from c, false otherwise
    • isBoxedByte

      public static boolean isBoxedByte(@NotNull Class c)
      Whether the class is an instance of Byte.class.
      Parameters:
      c - class
      Returns:
      true if Byte.class is assignable from c, false otherwise
    • isBoxedArithmetic

      public static boolean isBoxedArithmetic(@NotNull Class c)
      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

      public static boolean isBoxedBoolean(@NotNull Class c)
      Whether the class is an instance of Boolean.class.
      Parameters:
      c - class
      Returns:
      true if Boolean.class is assignable from c, false otherwise
    • isNumeric

      public static boolean isNumeric(@NotNull Class c)
      Parameters:
      c - class
      Returns:
      true if c is numeric, false otherwise
    • isCharacter

      public static boolean isCharacter(@NotNull Class c)
      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 or c equals char.class
    • isDateTime

      public static boolean isDateTime(Class type)
      Whether the class is a DBDateTime, Date, or Instant.
      Parameters:
      type - The class.
      Returns:
      true if the type is a DBDateTime or Date.
    • isString

      public static boolean isString(Class type)
      Whether the class is a String
      Parameters:
      type - the class
      Returns:
      true if the type is a String, false otherwise
    • isBigNumeric

      public static boolean isBigNumeric(Class type)
      Whether the class is a BigInteger or BigDecimal
      Parameters:
      type - the class
      Returns:
      true if the type is BigInteger or BigDecimal, false otherwise
    • isPrimitiveOrSerializable

      public static boolean isPrimitiveOrSerializable(Class type)
      Checks if a type is primitive or Serializable.
      Parameters:
      type - the class
      Returns:
      true if the type is primitive or Serializable
    • isFloatType

      public static boolean isFloatType(Class type)
      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

      public static String objectToString(Object o) throws IOException
      Converts an Object to a String for writing to a workspace. This is meant to be used in conjunction with TypeUtils.fromString. Strings, Numbers, and primitives will all convert using Obect.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

      public static Optional<Object> fromString(String string, String typeString) throws IOException
      Creates an Object from a String. This is meant to be used in conjunction with TypeUtils.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 parse
      typeString - the Canonical Name of the class type
      Returns:
      an object parsed from the String
      Throws:
      RuntimeException - if the string fails to parse
      IOException - if an IO error occurs during conversion
    • fromString

      public static Object fromString(String string, Class<?> type) throws IOException
      Creates an Object from a String. This is meant to be used in conjunction with TypeUtils.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 parse
      type - the type of the object
      Returns:
      an object parsed from the String
      Throws:
      RuntimeException - if the string fails to parse
      IOException - if an IO error occurs during conversion
    • encode64Serializable

      public static String encode64Serializable(Serializable serializable) throws IOException
      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

      public static Object decode64Serializable(String string) throws IOException, ClassNotFoundException
      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 decoded
      ClassNotFoundException - if the Object type is unknown
    • getErasedType

      public static Class getErasedType(Type paramType)
      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

      public static Class classForName(String className) throws ClassNotFoundException
      Throws:
      ClassNotFoundException
    • getTypeBoxer

      public static TypeUtils.TypeBoxer getTypeBoxer(Class type)
    • box

      public static Boolean box(Boolean value)
    • box

      public static Byte box(byte value)
    • box

      public static Character box(char value)
    • box

      public static Double box(double value)
    • box

      public static Float box(float value)
    • box

      public static Integer box(int value)
    • box

      public static Long box(long value)
    • box

      public static Short box(short value)
    • unbox

      public static boolean unbox(Boolean value)
    • unbox

      public static byte unbox(Byte value)
    • unbox

      public static char unbox(Character value)
    • unbox

      public static double unbox(Double value)
    • unbox

      public static float unbox(Float value)
    • unbox

      public static int unbox(Integer value)
    • unbox

      public static long unbox(Long value)
    • unbox

      public static short unbox(Short value)