Interface Converter<PRESENTATION,MODEL>

Type Parameters:
PRESENTATION - The presentation type.
MODEL - The model type.
All Superinterfaces:
Serializable
All Known Implementing Classes:
AbstractStringToNumberConverter, DateToLongConverter, DateToSqlDateConverter, LocalDateTimeToDateConverter, LocalDateTimeToInstantConverter, LocalDateToDateConverter, StringToBigDecimalConverter, StringToBigIntegerConverter, StringToBooleanConverter, StringToDateConverter, StringToDoubleConverter, StringToFloatConverter, StringToIntegerConverter, StringToLongConverter, StringToUuidConverter

public interface Converter<PRESENTATION,MODEL> extends Serializable
Defines conversion between a model and a presentation type.

Converters must not have any side effects (never update UI from inside a converter).

Since:
1.0.
Author:
Vaadin Ltd
  • Method Details

    • convertToModel

      Result<MODEL> convertToModel(PRESENTATION value, ValueContext context)
      Converts the given value from presentation type to model type.

      A converter can optionally use locale to do the conversion.

      Parameters:
      value - The value to convert. Can be null
      context - The value context for the conversion.
      Returns:
      The converted value compatible with the source type
    • convertToPresentation

      PRESENTATION convertToPresentation(MODEL value, ValueContext context)
      Converts the given value from model type to presentation type.

      A converter can optionally use locale to do the conversion.

      Parameters:
      value - The value to convert. Can be null
      context - The value context for the conversion.
      Returns:
      The converted value compatible with the source type
    • identity

      static <T> Converter<T,T> identity()
      Returns a converter that returns its input as-is in both directions.
      Type Parameters:
      T - the input and output type
      Returns:
      an identity converter
    • from

      static <P, M> Converter<P,M> from(SerializableFunction<P,M> toModel, SerializableFunction<M,P> toPresentation, SerializableFunction<Exception,String> onError)
      Constructs a converter from two functions. Any Exception instances thrown from the toModel function are converted into error-bearing Result objects using the given onError function.
      Type Parameters:
      P - the presentation type
      M - the model type
      Parameters:
      toModel - the function to convert to model
      toPresentation - the function to convert to presentation
      onError - the function to provide error messages
      Returns:
      the new converter
      See Also:
    • from

      static <P, M> Converter<P,M> from(SerializableFunction<P,Result<M>> toModel, SerializableFunction<M,P> toPresentation)
      Constructs a converter from a filter and a function.
      Type Parameters:
      P - the presentation type
      M - the model type
      Parameters:
      toModel - the function to convert to model
      toPresentation - the function to convert to presentation
      Returns:
      the new converter
      See Also:
    • chain

      default <T> Converter<PRESENTATION,T> chain(Converter<MODEL,T> other)
      Returns a converter that chains together this converter with the given type-compatible converter.

      The chained converters will form a new converter capable of converting from the presentation type of this converter to the model type of the other converter.

      In most typical cases you should not need this method but instead only need to define one converter for a binding using Binder.BindingBuilder.withConverter(Converter).

      Type Parameters:
      T - the model type of the resulting converter
      Parameters:
      other - the converter to chain, not null
      Returns:
      a chained converter