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,
java.lang.Class<T> targetType) |
<T> T |
coerce(java.lang.Object value,
com.google.common.reflect.TypeToken<T> targetTypeToken) |
<T> com.google.common.base.Function<java.lang.Object,T> |
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> com.google.common.base.Function<? super A,B> |
registerAdapter(java.lang.Class<A> sourceType,
java.lang.Class<B> targetType,
com.google.common.base.Function<? super A,B> fn)
Registers an adapter for use with type coercion.
|
<T> Maybe<T> |
tryCoerce(java.lang.Object input,
java.lang.Class<T> type) |
<T> Maybe<T> |
tryCoerce(java.lang.Object value,
com.google.common.reflect.TypeToken<T> targetTypeToken) |
public static TypeCoercerExtensible newDefault()
CommonAdaptorTypeCoercionspublic static TypeCoercerExtensible newEmpty()
CommonAdaptorTypeCoercionspublic <T> T coerce(java.lang.Object value,
java.lang.Class<T> targetType)
coerce in interface TypeCoercerpublic <T> T coerce(java.lang.Object value,
com.google.common.reflect.TypeToken<T> targetTypeToken)
public <T> Maybe<T> tryCoerce(java.lang.Object input, java.lang.Class<T> type)
tryCoerce in interface TypeCoercerpublic <T> Maybe<T> tryCoerce(java.lang.Object value, com.google.common.reflect.TypeToken<T> targetTypeToken)
tryCoerce in interface TypeCoercerpublic <T> com.google.common.base.Function<java.lang.Object,T> 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> com.google.common.base.Function<? super A,B> registerAdapter(java.lang.Class<A> sourceType,
java.lang.Class<B> targetType,
com.google.common.base.Function<? super A,B> fn)