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
Converters must not have any side effects (never update UI from inside a converter).
- Since:
- 1.0.
- Author:
- Vaadin Ltd
-
Method Summary
Modifier and TypeMethodDescriptiondefault <T> Converter<PRESENTATION, T> Returns a converter that chains together this converter with the given type-compatible converter.convertToModel(PRESENTATION value, ValueContext context) Converts the given value from presentation type to model type.convertToPresentation(MODEL value, ValueContext context) Converts the given value from model type to presentation type.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.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.static <T> Converter<T, T> identity()Returns a converter that returns its input as-is in both directions.static <P,M> Converter <P, M> nullSafeConverter(Converter<P, M> converter) Wraps the givenconverterso 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.static <P,M> Converter <P, M> nullSafeConverter(Converter<P, M> converter, P nullRepresentation) Wraps the givenconverterso that neither null nor the givennullRepresentationever reach it: A presentation value that is null or equal tonullRepresentationconverts to a null model value. A null model value, or a null returned by the wrapped converter, converts tonullRepresentation. Other values are passed to the wrapped converter.
-
Method Details
-
convertToModel
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 nullcontext- The value context for the conversion.- Returns:
- The converted value compatible with the source type
-
convertToPresentation
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 nullcontext- The value context for the conversion.- Returns:
- The converted value compatible with the source type
-
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. AnyExceptioninstances thrown from thetoModelfunction are converted into error-bearingResultobjects using the givenonErrorfunction.- Type Parameters:
P- the presentation typeM- the model type- Parameters:
toModel- the function to convert to modeltoPresentation- the function to convert to presentationonError- 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 typeM- the model type- Parameters:
toModel- the function to convert to modeltoPresentation- the function to convert to presentation- Returns:
- the new converter
- See Also:
-
chain
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
Wraps the givenconverterso 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:
withConverterturns off the binder's default null representation, so a null model value would reach the field as null and the field would throw. UsenullSafeConverter(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 typeM- 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
Wraps the givenconverterso that neither null nor the givennullRepresentationever reach it:- A presentation value that is null or equal to
nullRepresentationconverts to a null model value. - A null model value, or a null returned by the wrapped converter,
converts to
nullRepresentation.
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 typeM- the model type- Parameters:
converter- the converter to delegate to for other values, not nullnullRepresentation- the presentation value that stands for a null model value, for example""for a text field; may be null- Returns:
- a converter that maps
nullRepresentationto null and back - Since:
- 25.4
- See Also:
- A presentation value that is null or equal to
-