Package com.vaadin.flow.data.converter
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
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 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.
-
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. AnyException
instances thrown from thetoModel
function are converted into error-bearingResult
objects using the givenonError
function. -
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
-