Class ValueMapper<T>

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. See find(java.lang.Class) to access pre-registered ValueMappers.
  • Constructor Details

    • ValueMapper

      protected ValueMapper(Type type, Class<? extends Value> valueCls)
      Base constructor.
      Parameters:
      type - the Java type
      valueCls - the Value class
    • ValueMapper

      protected ValueMapper(Type type, Value.Type<?> valueType)
      Base constructor.
      Parameters:
      type - the Java type
      valueType - the Value Type
  • Method Details

    • 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, or PString.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 from toValue(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