public class TypeCoercerExtensible extends java.lang.Object implements TypeCoercer
value
to targetType
.
Maintains a registry of adapter functions for type pairs in a Table
which
is searched after checking various strategies, including the following:
value.asTargetType()
TargetType.fromType(value)
(if value instanceof Type
)
value.targetTypeValue()
(handy for primitives)
TargetType.valueOf(value)
(for enums)
A default set of adapters will handle most common Java-type coercions
as well as String
coercion to:
Set
, List
, Map
and similar -- parses as YAML
Date
-- parses using Time.parseDate(String)
Duration
-- parses using Duration.parse(String)
Modifier and Type | Method and Description |
---|---|
<T> T |
coerce(java.lang.Object value,
<any> targetTypeToken) |
<T> T |
coerce(java.lang.Object value,
java.lang.Class<T> targetType) |
<T> <any> |
function(java.lang.Class<T> type)
Returns a function that does a type coercion to the given type.
|
static TypeCoercerExtensible |
newDefault()
has all the strategies (primitives, collections, etc)
and all the adapters from
CommonAdaptorTypeCoercions |
static TypeCoercerExtensible |
newEmpty()
has all the strategies (primitives, collections, etc) but no adapters,
so caller can pick and choose e.g.
|
<A,B> <any> |
registerAdapter(java.lang.Class<A> sourceType,
java.lang.Class<B> targetType,
<any> fn)
Registers an adapter for use with type coercion.
|
void |
registerAdapter(java.lang.String nameAndOrder,
TryCoercer fn)
Registers a generic adapter for use with type coercion.
|
void |
registerAdapter(TryCoercer fn)
Deprecated.
since introduction, use
registerAdapter(String, TryCoercer) |
<T> Maybe<T> |
tryCoerce(java.lang.Object value,
<any> targetTypeToken) |
<T> Maybe<T> |
tryCoerce(java.lang.Object input,
java.lang.Class<T> type) |
public static TypeCoercerExtensible newDefault()
CommonAdaptorTypeCoercions
public static TypeCoercerExtensible newEmpty()
CommonAdaptorTypeCoercions
public <T> T coerce(java.lang.Object value, java.lang.Class<T> targetType)
coerce
in interface TypeCoercer
public <T> T coerce(java.lang.Object value, <any> targetTypeToken)
coerce
in interface TypeCoercer
public <T> Maybe<T> tryCoerce(java.lang.Object input, java.lang.Class<T> type)
tryCoerce
in interface TypeCoercer
public <T> Maybe<T> tryCoerce(java.lang.Object value, <any> targetTypeToken)
tryCoerce
in interface TypeCoercer
public <T> <any> function(java.lang.Class<T> type)
TypeCoercions.function(Double.class)
will return a function that will
coerce its input value to a Double
(or throw a ClassCoercionException
if that is not possible).public <A,B> <any> registerAdapter(java.lang.Class<A> sourceType, java.lang.Class<B> targetType, <any> fn)
public void registerAdapter(java.lang.String nameAndOrder, TryCoercer fn)
@Deprecated public void registerAdapter(TryCoercer fn)
registerAdapter(String, TryCoercer)