Interface Converter<PRESENTATION,MODEL>

Type Parameters:
PRESENTATION - The presentation type.
MODEL - The model type.
All Superinterfaces:
Serializable
All Known Implementing Classes:
AbstractStringToNumberConverter, BigDecimalToDoubleConverter, BigDecimalToFloatConverter, BigDecimalToIntegerConverter, BigDecimalToLongConverter, DateToLongConverter, DateToSqlDateConverter, DoubleToBigDecimalConverter, FloatToBigDecimalConverter, FloatToDoubleConverter, IntegerToBigDecimalConverter, IntegerToDoubleConverter, IntegerToLongConverter, LocalDateTimeToDateConverter, LocalDateTimeToInstantConverter, LocalDateToDateConverter, LongToBigDecimalConverter, 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
    • nullSafeConverter

      static <P, M> Converter<P,M> nullSafeConverter(Converter<P,M> converter)
      Wraps the given converter so that null values never reach it: a null presentation value converts to a null model value, and a null model value converts to a null presentation value. Other values are passed to the wrapped converter.

      Use this for fields whose empty value is null, such as a date picker or a combo box. Do not use it for a field that does not accept null, such as a text field: withConverter turns off the binder's default null representation, so a null model value would reach the field as null and the field would throw. Use nullSafeConverter(Converter, Object) with the field's empty value, such as "", instead.

      Example use:

       
       binder.forField(datePicker)
               .withConverter(Converter.nullSafeConverter(Converter.from(
                       date -> date.atStartOfDay(ZoneOffset.UTC).toInstant(),
                       instant -> LocalDate.ofInstant(instant, ZoneOffset.UTC),
                       Exception::getMessage)))
               .bind(Event::getStart, Event::setStart);
       
       
      Type Parameters:
      P - the presentation type
      M - the model type
      Parameters:
      converter - the converter to delegate to for non-null values, not null
      Returns:
      a converter that maps null to null in both directions
      Since:
      25.4
      See Also:
    • nullSafeConverter

      static <P, M> Converter<P,M> nullSafeConverter(Converter<P,M> converter, P nullRepresentation)
      Wraps the given converter so that neither null nor the given nullRepresentation ever reach it:
      • A presentation value that is null or equal to nullRepresentation converts to a null model value.
      • A null model value, or a null returned by the wrapped converter, converts to nullRepresentation.
      Other values are passed to the wrapped converter. A model value that the wrapped converter shows as nullRepresentation, such as a value object holding an empty string, therefore reads back as null.

      This is meant for fields whose empty value is not null, such as an empty string in a text field: an empty field becomes a null model value, and a null model value empties the field, without the wrapped converter having to handle either case.

      When binding with mapping functions rather than an existing converter, Binder.BindingBuilder.withNullSafeConverter(SerializableFunction, SerializableFunction, SerializableFunction, Object) does the same in one call.

      Example use:

       
       binder.forField(textField)
               .withConverter(Converter
                       .nullSafeConverter(Converter.from(LocalDate::parse,
                               LocalDate::toString, Exception::getMessage), ""))
               .bind(Person::getBirthDate, Person::setBirthDate);
       
       
      Type Parameters:
      P - the presentation type
      M - the model type
      Parameters:
      converter - the converter to delegate to for other values, not null
      nullRepresentation - the presentation value that stands for a null model value, for example "" for a text field; may be null
      Returns:
      a converter that maps nullRepresentation to null and back
      Since:
      25.4
      See Also: