public class TypeCoercions
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static java.util.Map<java.lang.Class,java.lang.Class> |
BOXED_TO_UNBOXED_TYPES |
static java.util.Map<java.lang.Class,java.lang.Class> |
UNBOXED_TO_BOXED_TYPES |
| Modifier and Type | Method and Description |
|---|---|
static <T> T |
castPrimitive(java.lang.Object value,
java.lang.Class<T> targetType)
Sometimes need to explicitly cast primitives, rather than relying on Java casting.
|
static <T> T |
coerce(java.lang.Object value,
java.lang.Class<T> targetType)
Attempts to coerce
value to targetType. |
static <T> T |
coerce(java.lang.Object value,
com.google.common.reflect.TypeToken<T> targetTypeToken) |
static java.lang.Object |
getMatchingConstructor(java.lang.Class target,
java.lang.Object... arguments)
for automatic conversion
|
static java.lang.String |
getVerySimpleName(java.lang.Class c)
returns the simple class name, and for any inner class the portion after the $
|
static void |
initStandardAdapters() |
static boolean |
isPrimitiveOrBoxer(java.lang.Class<?> type) |
static <A,B> com.google.common.base.Function |
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.
|
static <E extends java.lang.Enum<E>> |
stringToEnum(java.lang.Class<E> type,
E defaultValue)
Type coercion
function for enums. |
static <T> T |
stringToPrimitive(java.lang.String value,
java.lang.Class<T> targetType) |
static <T> Maybe<T> |
tryCoerce(java.lang.Object value,
com.google.common.reflect.TypeToken<T> targetTypeToken) |
public static final java.util.Map<java.lang.Class,java.lang.Class> BOXED_TO_UNBOXED_TYPES
public static final java.util.Map<java.lang.Class,java.lang.Class> UNBOXED_TO_BOXED_TYPES
public static <T> T coerce(java.lang.Object value,
java.lang.Class<T> targetType)
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)
public static <T> Maybe<T> tryCoerce(java.lang.Object value, com.google.common.reflect.TypeToken<T> targetTypeToken)
coerce(Object, Class)public static <T> T coerce(java.lang.Object value,
com.google.common.reflect.TypeToken<T> targetTypeToken)
coerce(Object, Class)public static <E extends java.lang.Enum<E>> com.google.common.base.Function<java.lang.String,E> stringToEnum(java.lang.Class<E> type,
@Nullable
E defaultValue)
function for enums.
Tries to convert the string to CaseFormat.UPPER_UNDERSCORE first,
handling all of the different format possibilites. Failing
that, it tries a case-insensitive comparison with the valid enum values.
Returns defaultValue if the string cannot be converted.
coerce(Object, Class),
Enum.valueOf(Class, String)public static <T> T castPrimitive(java.lang.Object value,
java.lang.Class<T> targetType)
public static boolean isPrimitiveOrBoxer(java.lang.Class<?> type)
public static <T> T stringToPrimitive(java.lang.String value,
java.lang.Class<T> targetType)
public static java.lang.String getVerySimpleName(java.lang.Class c)
public static java.lang.Object getMatchingConstructor(java.lang.Class target,
java.lang.Object... arguments)
public static <A,B> com.google.common.base.Function registerAdapter(java.lang.Class<A> sourceType,
java.lang.Class<B> targetType,
com.google.common.base.Function<? super A,B> fn)
public static void initStandardAdapters()