@SuppressWarnings("unchecked") private Converter<TARGET, Object> createConverter(Class<?> getterType) { return Converter.from(fieldValue -> getterType.cast(fieldValue), propertyValue -> (TARGET) propertyValue, exception -> { throw new RuntimeException(exception); }); }
/** * Returns a converter that returns its input as-is in both directions. * * @param <T> * the input and output type * @return an identity converter */ public static <T> Converter<T, T> identity() { return from(t -> Result.ok(t), t -> t); }
private <FIELDVALUE> Converter<FIELDVALUE, FIELDVALUE> createNullRepresentationAdapter( HasValue<FIELDVALUE> field) { Converter<FIELDVALUE, FIELDVALUE> nullRepresentationConverter = Converter .from(fieldValue -> fieldValue, modelValue -> Objects.isNull(modelValue) ? field.getEmptyValue() : modelValue, exception -> exception.getMessage()); ConverterDelegate<FIELDVALUE> converter = new ConverterDelegate<>( nullRepresentationConverter); initialConverters.put(field, converter); return converter; }
SerializableFunction<NEWTARGET, TARGET> toPresentation, String errorMessage) { return withConverter(Converter.from(toModel, toPresentation, exception -> errorMessage));
SerializableFunction<TARGET, NEWTARGET> toModel, SerializableFunction<NEWTARGET, TARGET> toPresentation) { return withConverter(Converter.from(toModel, toPresentation, exception -> exception.getMessage()));
/** * Constructs a converter from two functions. Any {@code Exception} * instances thrown from the {@code toModel} function are converted into * error-bearing {@code Result} objects using the given {@code onError} * function. * * @param <P> * the presentation type * @param <M> * the model type * @param toModel * the function to convert to model * @param toPresentation * the function to convert to presentation * @param onError * the function to provide error messages * @return the new converter * * @see Result * @see Function */ public static <P, M> Converter<P, M> from( SerializableFunction<P, M> toModel, SerializableFunction<M, P> toPresentation, SerializableFunction<Exception, String> onError) { return from(val -> Result.of(() -> toModel.apply(val), onError), toPresentation); }
/** * Create a new {@link Input} from another {@link Input} with a different value type, using given * {@link PropertyValueConverter} to perform value conversions. * @param <T> New value type * @param <V> Original value type * @param input Actual input (not null) * @param property Property to provide to the converter * @param converter Value converter (not null) * @return A new {@link Input} of the converted value type */ static <T, V> Input<T> from(Input<V> input, Property<T> property, PropertyValueConverter<T, V> converter) { ObjectUtils.argumentNotNull(converter, "PropertyValueConverter must be not null"); return new InputConverterAdapter<>(input, Converter.from(value -> converter.fromModel(value, property), value -> converter.toModel(value, property), e -> e.getMessage())); }
public void input7() { // tag::input7[] Input<String> stringInput = Components.input.string().build(); Input<Integer> integerInput = Input.from(stringInput, new StringToIntegerConverter("Conversion error")); // <1> Input<Boolean> booleanInput = Input.from(integerInput, // <2> Converter.from(value -> Result.ok((value == null) ? Boolean.FALSE : (value.intValue() > 0)), value -> (value == null) ? null : (value ? 1 : 0))); Input<Long> longInput = Input.from(new TextField(), new StringToLongConverter("Conversion error")); // <3> // end::input7[] }