public final class Coercions extends java.lang.Object
Coercions.Coerce
constants are the default coercions for the specified type.
To override the behavior, you must create a new CoercibleType
and assign it to the Fields
field position/name the custom behavior should apply.
Coercions are always used if Tuple
elements are accessed via a TupleEntry
wrapper instance.CoercibleType
Modifier and Type | Class and Description |
---|---|
static class |
Coercions.Coerce<T> |
Modifier and Type | Field and Description |
---|---|
static Coercions.Coerce<java.math.BigDecimal> |
BIG_DECIMAL |
static Coercions.Coerce<java.lang.Boolean> |
BOOLEAN |
static Coercions.Coerce<java.lang.Boolean> |
BOOLEAN_OBJECT |
static Coercions.Coerce<java.lang.Character> |
CHARACTER |
static Coercions.Coerce<java.lang.Character> |
CHARACTER_OBJECT |
static java.util.Map<java.lang.reflect.Type,Coercions.Coerce> |
coercions |
static Coercions.Coerce<java.lang.Double> |
DOUBLE |
static Coercions.Coerce<java.lang.Double> |
DOUBLE_OBJECT |
static Coercions.Coerce<java.lang.Float> |
FLOAT |
static Coercions.Coerce<java.lang.Float> |
FLOAT_OBJECT |
static Coercions.Coerce<java.lang.Integer> |
INTEGER |
static Coercions.Coerce<java.lang.Integer> |
INTEGER_OBJECT |
static Coercions.Coerce<java.lang.Long> |
LONG |
static Coercions.Coerce<java.lang.Long> |
LONG_OBJECT |
static Coercions.Coerce<java.lang.Object> |
OBJECT |
static java.util.Map<java.lang.Class,java.lang.Class> |
primitives |
static Coercions.Coerce<java.lang.Short> |
SHORT |
static Coercions.Coerce<java.lang.Short> |
SHORT_OBJECT |
static Coercions.Coerce<java.lang.String> |
STRING |
static java.util.Map<java.lang.String,java.lang.reflect.Type> |
types |
Constructor and Description |
---|
Coercions() |
Modifier and Type | Method and Description |
---|---|
static java.lang.Class |
asClass(java.lang.reflect.Type type)
Method asClass is a convenience method for casting the given type to a
Class if an instance of Class
or to Object if not. |
static java.lang.Class |
asNonPrimitive(java.lang.Class type)
Returns the primitive wrapper fo the given type, if the given type represents a primitive, otherwise
the type is returned.
|
static java.lang.Class[] |
asNonPrimitive(java.lang.Class[] types) |
static java.lang.reflect.Type |
asType(java.lang.String typeName)
Method asType is a convenience method for looking up a type name (like
"int" or "java.lang.String"
to its corresponding Class or instance of CoercibleType. |
static java.lang.Object |
coerce(CoercibleType currentType,
java.lang.Object value,
java.lang.reflect.Type type)
Method coerce will coerce the given value to the given type using the given
CoercibleType . |
static <T> T |
coerce(java.lang.Object value,
java.lang.reflect.Type type)
Method coerce will coerce the given value to the given type using any
CoercibleType mapping available. |
static CoercibleType[] |
coercibleArray(Fields fields)
Method coercibleArray will return an array of
CoercibleType instances based on the
given field type information. |
static CoercibleType[] |
coercibleArray(int size,
java.lang.reflect.Type[] types)
Method coercibleArray will return an array of
CoercibleType instances based on the
given type array. |
static CoercibleType |
coercibleTypeFor(java.lang.reflect.Type type)
Method coercibleTypeFor returns the
CoercibleType for the given Type instance. |
static java.lang.Class[] |
getCanonicalTypes(java.lang.reflect.Type[] types) |
static java.lang.String[] |
getTypeNames(java.lang.reflect.Type[] types) |
static java.lang.reflect.Type[] |
getTypes(java.lang.String[] names) |
public static final java.util.Map<java.lang.reflect.Type,Coercions.Coerce> coercions
public static final java.util.Map<java.lang.String,java.lang.reflect.Type> types
public static final Coercions.Coerce<java.lang.Object> OBJECT
public static final Coercions.Coerce<java.lang.String> STRING
public static final Coercions.Coerce<java.lang.Character> CHARACTER
public static final Coercions.Coerce<java.lang.Character> CHARACTER_OBJECT
public static final Coercions.Coerce<java.lang.Short> SHORT
public static final Coercions.Coerce<java.lang.Short> SHORT_OBJECT
public static final Coercions.Coerce<java.lang.Integer> INTEGER
public static final Coercions.Coerce<java.lang.Integer> INTEGER_OBJECT
public static final Coercions.Coerce<java.lang.Double> DOUBLE
public static final Coercions.Coerce<java.lang.Double> DOUBLE_OBJECT
public static final Coercions.Coerce<java.lang.Long> LONG
public static final Coercions.Coerce<java.lang.Long> LONG_OBJECT
public static final Coercions.Coerce<java.lang.Float> FLOAT
public static final Coercions.Coerce<java.lang.Float> FLOAT_OBJECT
public static final Coercions.Coerce<java.lang.Boolean> BOOLEAN
public static final Coercions.Coerce<java.lang.Boolean> BOOLEAN_OBJECT
public static final Coercions.Coerce<java.math.BigDecimal> BIG_DECIMAL
public static final java.util.Map<java.lang.Class,java.lang.Class> primitives
public Coercions()
public static java.lang.Class asNonPrimitive(java.lang.Class type)
type
- of type Classpublic static java.lang.Class[] asNonPrimitive(java.lang.Class[] types)
public static CoercibleType coercibleTypeFor(java.lang.reflect.Type type)
CoercibleType
for the given Type
instance.
If type is null, the OBJECT
CoercibleType is returned.
If type is an instance of CoercibleType, the given type is returned.
If no mapping is found, an IllegalStateException
will be thrown.type
- the type to look uppublic static final <T> T coerce(java.lang.Object value, java.lang.reflect.Type type)
CoercibleType
mapping available.
If no mapping is found, the OBJECT
CoercibleType will be use.T
- the type expectedvalue
- the value to coerce, may be null.type
- the type to coerce to via any mapped CoercibleTypepublic static final java.lang.Object coerce(CoercibleType currentType, java.lang.Object value, java.lang.reflect.Type type)
CoercibleType
.
If the given CoercibleType is equivalent (Object.equals(Object)
) to the given Type, the value
is returned. Note the Type can be itself a CoercibleType, so unnecessary work is prevented.currentType
- the current Type of the value.value
- the value to coerce, may be null.type
- the type to coerce to via any mapped CoercibleTypepublic static CoercibleType[] coercibleArray(Fields fields)
CoercibleType
instances based on the
given field type information. Each element of Fields.getTypes()
will be used to lookup the corresponding CoercibleType.fields
- an instance of Fields with optional type information.public static CoercibleType[] coercibleArray(int size, java.lang.reflect.Type[] types)
CoercibleType
instances based on the
given type array. Each element of the type array
will be used to lookup the corresponding CoercibleType.size
- the size of the expected array, must equal types.length
if types != null
types
- an array of types to lookuppublic static java.lang.Class asClass(java.lang.reflect.Type type)
Class
if an instance of Class
or to Object
if not.type
- of type Typepublic static java.lang.reflect.Type asType(java.lang.String typeName)
"int"
or "java.lang.String"
to its corresponding Class
or instance of CoercibleType.
If the name is not in the types
map, the classname will be loaded from the current ClassLoader
.typeName
- a string class or type nam.public static java.lang.String[] getTypeNames(java.lang.reflect.Type[] types)
public static java.lang.reflect.Type[] getTypes(java.lang.String[] names)
public static java.lang.Class[] getCanonicalTypes(java.lang.reflect.Type[] types)
Copyright © 2007-2015 Xplenty, Inc. All Rights Reserved.