- java.lang.Object
-
- java.lang.Enum<SimpleType>
-
- org.refcodes.struct.SimpleType
-
- All Implemented Interfaces:
Serializable
,Comparable<SimpleType>
public enum SimpleType extends Enum<SimpleType>
TheSimpleType
enumeration provides functionality useful when working wit Java's primitive types, their wrapper counterparts as well as with some other crucial types such asString
,Date
,Enum
,Class
orLocale
types. This enumeration is the single point for "simple type" conversion for many other functionality so code is not scattered around the various libraries disjunct from each other.
-
-
Enum Constant Summary
Enum Constants Enum Constant Description DEFAULT
Convert primitive types (e.g.KEEP_TYPES
Keep primitive types (e.g.KEEP_TYPES_WITH_ARRAY_TYPES
Keep primitive types (e.g.PRIMITIVE_TYPES
Convert wrapper types (e.g.PRIMITIVE_TYPES_WITH_ARRAY_TYPES
Convert wrapper types (e.g.WRAPPER_TYPES
Convert primitive types (e.g.WRAPPER_TYPES_WITH_ARRAY_TYPES
Convert primitive types (e.g.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> String
fromSimpleType(T aValue)
Converts a simple type to aString
which can be converted back with the companion methodtoSimpleType(String, Class)
.boolean
hasArrayTypeSupport()
Returns true in case primitive type arrays or arrays of their wrappers are supported types.boolean
hasPrimitiveTypeSupport()
Returns true in case primitive (array) types are supported types.boolean
hasWrapperTypeSupport()
Returns true in case wrapper (array) types are supported types.static <T> boolean
invokeSimpleType(T aToArray, int aToIndex, String aToValue, Class<?> aToType)
static <T> boolean
invokeSimpleType(T aToInstance, Field aToField, String aToValue, Class<?> aToType)
static <T> boolean
invokeSimpleType(T aToInstance, Method aToMethod, String aToValue, Class<?> aToType)
static boolean
isCompositeArrayType(Class<?> aClass)
Tests whether the given class represents a composite type array.static boolean
isCompositeType(Class<?> aClass)
Tests whether the given class represents a composite type.static boolean
isCompositeType(Class<?> aClass, boolean hasArraySupport)
Tests whether the given class represents a composite type.static boolean
isPrimitiveArrayType(Class<?> aClass)
Tests whether the given class represents a primitive array type.static boolean
isPrimitiveType(Class<?> aClass)
Tests whether the given class represents a primitive type.static boolean
isPrimitiveType(Class<?> aClass, boolean hasArraySupport)
Tests whether the given class represents a primitive (array) type.static boolean
isSimpleArrayType(Class<?> aClass)
Tests whether the given class represents a simple type array.static boolean
isSimpleType(Class<?> aClass)
Tests whether the given class represents a simple type.static boolean
isSimpleType(Class<?> aClass, boolean hasArraySupport)
Tests whether the given class represents a simple type.static boolean
isWrapperArrayType(Class<?> aClass)
Tests whether the given class represents a wrapper array type of a primitive array type.static boolean
isWrapperType(Class<?> aClass)
Tests whether the given class represents a wrapper type of a primitive type.static boolean
isWrapperType(Class<?> aClass, boolean hasArraySupport)
Tests whether the given class represents a wrapper (array) type.static Class<?>
toPrimitiveType(Class<?> aClass)
Converts the given type to a primitive type if possible and returns the result or null if the given type is not a wrapper type.static Class<?>
toPrimitiveType(Class<?> aClass, boolean hasArraySupport)
Converts the given type to a primitive type if possible and returns the result or null if the given type is not a wrapper type.static <TYPE> TYPE
toSimpleType(String aValue, Class<TYPE> aType)
Converts aString
to a simple type which can be converted back with the companion methodfromSimpleType(Object)
.Class<?>
toType(Class<?> aClass)
Converts the passed type as of theSimpleType
.static Class<?>
toWrapperType(Class<?> aClass)
Converts the given type to a wrapper type if possible and returns the result or null if the given type is not a primitive type.static Class<?>
toWrapperType(Class<?> aClass, boolean hasArraySupport)
Converts the given type to a wrapper type if possible and returns the result or null if the given type is not a primitive type.static SimpleType
valueOf(String name)
Returns the enum constant of this type with the specified name.static SimpleType[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.
-
-
-
Enum Constant Detail
-
DEFAULT
public static final SimpleType DEFAULT
Convert primitive types (e.g.Byte.TYPE
) to their wrapper types (e.g.Byte
). Array types are converted to non array types with the according array notation (as ofWRAPPER_TYPES
).
-
KEEP_TYPES
public static final SimpleType KEEP_TYPES
-
WRAPPER_TYPES
public static final SimpleType WRAPPER_TYPES
-
PRIMITIVE_TYPES
public static final SimpleType PRIMITIVE_TYPES
-
KEEP_TYPES_WITH_ARRAY_TYPES
public static final SimpleType KEEP_TYPES_WITH_ARRAY_TYPES
-
WRAPPER_TYPES_WITH_ARRAY_TYPES
public static final SimpleType WRAPPER_TYPES_WITH_ARRAY_TYPES
-
PRIMITIVE_TYPES_WITH_ARRAY_TYPES
public static final SimpleType PRIMITIVE_TYPES_WITH_ARRAY_TYPES
-
-
Method Detail
-
values
public static SimpleType[] values()
Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:for (SimpleType c : SimpleType.values()) System.out.println(c);
- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
public static SimpleType valueOf(String name)
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is null
-
hasPrimitiveTypeSupport
public boolean hasPrimitiveTypeSupport()
Returns true in case primitive (array) types are supported types.- Returns:
- True in case primitive (array) types are supported types.
-
hasWrapperTypeSupport
public boolean hasWrapperTypeSupport()
Returns true in case wrapper (array) types are supported types.- Returns:
- True in case wrapper (array) types are supported types.
-
hasArrayTypeSupport
public boolean hasArrayTypeSupport()
Returns true in case primitive type arrays or arrays of their wrappers are supported types.- Returns:
- True in case primitive type arrays or arrays of their wrappers are supported types.
-
toType
public Class<?> toType(Class<?> aClass)
Converts the passed type as of theSimpleType
.- Parameters:
aClass
- The type to be converted.- Returns:
- The converted type.
-
toPrimitiveType
public static Class<?> toPrimitiveType(Class<?> aClass)
Converts the given type to a primitive type if possible and returns the result or null if the given type is not a wrapper type. Convenience method fortoPrimitiveType(Class, boolean)
without array support.- Parameters:
aClass
- The type to be converted.- Returns:
- The converted type.
-
toPrimitiveType
public static Class<?> toPrimitiveType(Class<?> aClass, boolean hasArraySupport)
Converts the given type to a primitive type if possible and returns the result or null if the given type is not a wrapper type.- Parameters:
aClass
- The type to be converted.hasArraySupport
- True in case primitive type arrays or arrays of their wrappers are supported types.- Returns:
- The converted type.
-
toWrapperType
public static Class<?> toWrapperType(Class<?> aClass)
Converts the given type to a wrapper type if possible and returns the result or null if the given type is not a primitive type. Convenience method fortoWrapperType(Class, boolean)
without array support.- Parameters:
aClass
- The type to be converted.- Returns:
- The converted type.
-
toWrapperType
public static Class<?> toWrapperType(Class<?> aClass, boolean hasArraySupport)
Converts the given type to a wrapper type if possible and returns the result or null if the given type is not a primitive type.- Parameters:
aClass
- The type to be converted.hasArraySupport
- True in case primitive type arrays or arrays of their wrappers are supported types.- Returns:
- The converted type.
-
isPrimitiveType
public static boolean isPrimitiveType(Class<?> aClass, boolean hasArraySupport)
Tests whether the given class represents a primitive (array) type.- Parameters:
aClass
- The type to be tested.hasArraySupport
- True in case primitive type arrays or arrays of their wrappers are supported types.- Returns:
- True in case of being a primitive type, else false. A return
value of false does not mean that we have a wrapper type (use
isWrapperType(Class)
).
-
isPrimitiveType
public static boolean isPrimitiveType(Class<?> aClass)
Tests whether the given class represents a primitive type.- Parameters:
aClass
- The type to be tested.- Returns:
- True in case of being a primitive type, else false. A return
value of false does not mean that we have a wrapper type (use
isWrapperType(Class)
).
-
isPrimitiveArrayType
public static boolean isPrimitiveArrayType(Class<?> aClass)
Tests whether the given class represents a primitive array type.- Parameters:
aClass
- The type to be tested.- Returns:
- True in case of being a primitive array type, else false. A
return value of false does not mean that we have a wrapper array
type (use
isWrapperArrayType(Class)
).
-
isWrapperType
public static boolean isWrapperType(Class<?> aClass, boolean hasArraySupport)
Tests whether the given class represents a wrapper (array) type.- Parameters:
aClass
- The type to be tested.hasArraySupport
- True in case wrapper type arrays or arrays of their wrappers are supported types.- Returns:
- True in case of being a wrapper type, else false. A return value
of false does not mean that we have a wrapper type (use
isWrapperType(Class)
).
-
isWrapperType
public static boolean isWrapperType(Class<?> aClass)
Tests whether the given class represents a wrapper type of a primitive type.- Parameters:
aClass
- The type to be tested.- Returns:
- True in case of being a wrapper type of a primitive type, else
false. A return value of false does not mean that we have a
primitive type (use
isPrimitiveType(Class)
).
-
isWrapperArrayType
public static boolean isWrapperArrayType(Class<?> aClass)
Tests whether the given class represents a wrapper array type of a primitive array type.- Parameters:
aClass
- The type to be tested.- Returns:
- True in case of being a wrapper array type of a primitive array
type, else false. A return value of false does not mean that we
have a primitive array type (use
isPrimitiveArrayType(Class)
).
-
isSimpleType
public static boolean isSimpleType(Class<?> aClass, boolean hasArraySupport)
Tests whether the given class represents a simple type. A simple type is reckoned to be one of the wrapper typesBoolean
,Byte
,Short
,Character
,Integer
,Long
,Float
orDouble
as well as their wrapper types as well as the (not that primitive) typesString
,Enum
andClass
- Parameters:
aClass
- The type to be tested.hasArraySupport
- True in case simple type arrays are supported types.- Returns:
- True in case of being a simple type.
-
isSimpleType
public static boolean isSimpleType(Class<?> aClass)
Tests whether the given class represents a simple type. A simple type is reckoned to be one of the wrapper typesBoolean
,Byte
,Short
,Character
,Integer
,Long
,Float
orDouble
as well as their wrapper types as well as the (not that primitive) typesString
,Enum
andClass
- Parameters:
aClass
- The type to be tested.- Returns:
- True in case of being a simple type.
-
isSimpleArrayType
public static boolean isSimpleArrayType(Class<?> aClass)
Tests whether the given class represents a simple type array. A simple type is reckoned to be one of the wrapper typesBoolean
,Byte
,Short
,Character
,Integer
,Long
,Float
orDouble
as well as their wrapper types as well as the (not that primitive) typesString
,Enum
andClass
- Parameters:
aClass
- The type to be tested.- Returns:
- True in case of being a simple type array.
-
isCompositeType
public static boolean isCompositeType(Class<?> aClass, boolean hasArraySupport)
Tests whether the given class represents a composite type. A composite type is not a simple type (nor a primitive type and neither a wrapper type).- Parameters:
aClass
- The type to be tested.hasArraySupport
- True in case composite type arrays are supported types.- Returns:
- True in case of being a composite type.
-
isCompositeType
public static boolean isCompositeType(Class<?> aClass)
Tests whether the given class represents a composite type. A composite type is not a simple type (nor a primitive type and neither a wrapper type).- Parameters:
aClass
- The type to be tested.- Returns:
- True in case of being a composite type.
-
isCompositeArrayType
public static boolean isCompositeArrayType(Class<?> aClass)
Tests whether the given class represents a composite type array. A simple type is reckoned to be one of the wrapper typesBoolean
,Byte
,Short
,Character
,Integer
,Long
,Float
orDouble
as well as their wrapper types as well as the (not that primitive) typesString
,Enum
andClass
- Parameters:
aClass
- The type to be tested.- Returns:
- True in case of being a composite type array.
-
fromSimpleType
public static <T> String fromSimpleType(T aValue)
Converts a simple type to aString
which can be converted back with the companion methodtoSimpleType(String, Class)
.- Type Parameters:
T
- The simple type's type.- Parameters:
aValue
- The value of the simple type to be converted.- Returns:
- The according
String
or if the value was not a simple type.
-
toSimpleType
public static <TYPE> TYPE toSimpleType(String aValue, Class<TYPE> aType)
Converts aString
to a simple type which can be converted back with the companion methodfromSimpleType(Object)
.- Type Parameters:
TYPE
- the generic type- Parameters:
aValue
- The value to be converted to the simple type.aType
- the a type- Returns:
- The according simple type or null if the type was not a simple type.
-
invokeSimpleType
public static <T> boolean invokeSimpleType(T aToInstance, Method aToMethod, String aToValue, Class<?> aToType) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException
Invokes a method with a simple type value'sString
representation, converting theString
instance into the according simple type.- Type Parameters:
T
- The type of the instance on which to do the method invocation.- Parameters:
aToInstance
- The instance on which to invoke the method.aToMethod
- The method to be invoked.aToValue
- TheString
representation of the value to be passed to the method.aToType
- The (simple type) to which the value is to be converted.- Returns:
- True in case a simple type was detected and invocation was done.
False in case the type of the value to be invoked was not a
simple type. See also
isSimpleType(Class)
. - Throws:
NoSuchMethodException
- in case there is none such method for the given instance.IllegalAccessException
- in case method invocation has access restrictions.InvocationTargetException
- in case upon invocation an exceptional situation occurred.InstantiationException
- thrown in case instantiation of required types failed.ClassNotFoundException
- thrown in case a required type's class was not found on the class path.
-
invokeSimpleType
public static <T> boolean invokeSimpleType(T aToInstance, Field aToField, String aToValue, Class<?> aToType) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, ClassNotFoundException
Invokes a field with a simple type value'sString
representation, converting theString
instance into the according simple type.- Type Parameters:
T
- The type of the instance on which to do the field invocation.- Parameters:
aToInstance
- The instance on which to invoke the field.aToField
- the field to be invoked.aToValue
- TheString
representation of the value to be passed to the field.aToType
- The (simple type) to which the value is to be converted.- Returns:
- True in case a simple type was detected and invocation was done.
False in case the type of the value to be invoked was not a
simple type. See also
isSimpleType(Class)
. - Throws:
NoSuchMethodException
- in case there is none such method for the given instance.IllegalAccessException
- in case method invocation has access restrictions.InstantiationException
- thrown in case instantiation of required types failed.InvocationTargetException
- in case upon invocation an exceptional situation occurred.ClassNotFoundException
- thrown in case a required type's class was not found on the class path.
-
invokeSimpleType
public static <T> boolean invokeSimpleType(T aToArray, int aToIndex, String aToValue, Class<?> aToType) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException
Invokes a array at the given index with a simple type value'sString
representation, converting theString
instance into the according simple type.- Type Parameters:
T
- The type of the instance on which to do the array invocation.- Parameters:
aToArray
- the array where to invoke the simple type's value.aToIndex
- the index into the array where to invoke the value to.aToValue
- TheString
representation of the value to be passed to the array.aToType
- The (simple type) to which the value is to be converted.- Returns:
- True in case a simple type was detected and invocation was done.
False in case the type of the value to be invoked was not a
simple type. See also
isSimpleType(Class)
. - Throws:
NoSuchMethodException
- in case there is none such method for the given instance.InstantiationException
- thrown in case instantiation of required types failed.IllegalAccessException
- in case method invocation has access restrictions.InvocationTargetException
- in case upon invocation an exceptional situation occurred.ClassNotFoundException
- thrown in case a required type's class was not found on the class path.
-
-