- java.lang.Object
-
- org.praxislive.core.ValueMapper<T>
-
- Type Parameters:
T
- Java type
public abstract class ValueMapper<T> extends Object
ValueMappers translate values of type Value to another Java type. Seefind(java.lang.Class)
to access pre-registered ValueMappers.
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ValueMapper(Type type, Class<? extends Value> valueCls)
Base constructor.protected
ValueMapper(Type type, Value.Type<?> valueType)
Base constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static <T> ValueMapper<T>
find(Class<T> type)
Find a ValueMapper capable of mapping Values to and from the provided Class.abstract T
fromValue(Value value)
Map the provided Value to a value of type T.abstract Value
toValue(T value)
Map the provided T value to a Value.Type
type()
Type of T.Value.Type<?>
valueType()
The preferred Value.Type for mapping to type T.
-
-
-
Constructor Detail
-
ValueMapper
protected ValueMapper(Type type, Class<? extends Value> valueCls)
Base constructor.- Parameters:
type
- the Java typevalueCls
- the Value class
-
ValueMapper
protected ValueMapper(Type type, Value.Type<?> valueType)
Base constructor.- Parameters:
type
- the Java typevalueType
- the Value Type
-
-
Method Detail
-
fromValue
public abstract T fromValue(Value value)
Map the provided Value to a value of type T. If the input is empty, some implementations may return null. Invalid values may cause an exception.- Parameters:
value
- input Value- Returns:
- mapped value, or null
- Throws:
IllegalArgumentException
- on invalid input
-
toValue
public abstract Value toValue(T value)
Map the provided T value to a Value. Invalid input values may cause an exception. Null input is allowed, and will return either the Value Type's empty value if present, orPString.EMPTY
.- Parameters:
value
- input value- Returns:
- mapped Value (never null)
- Throws:
IllegalArgumentException
- on invalid input
-
type
public final Type type()
Type of T.- Returns:
- type of T
-
valueType
public final Value.Type<?> valueType()
The preferred Value.Type for mapping to type T. This is the type that Values will be coerced to, and will be the type usually (but not always) returned fromtoValue(java.lang.Object)
.- Returns:
- preferred value type
-
find
public static <T> ValueMapper<T> find(Class<T> type)
Find a ValueMapper capable of mapping Values to and from the provided Class. Mappers for String, Boolean, Integer, Float and Double are provided, along with their primitive counterparts where applicable. Mappers will be created on demand for any Enum type. Mappers for all registered Value types are provided as a convenience.- Type Parameters:
T
- mapped class- Parameters:
type
- mapped class- Returns:
- value mapper or null
-
-