@Override public Result<T> convertToModel(PRESENTATION value, ValueContext context) { Result<MODEL> model = Converter.this.convertToModel(value, context); return model.flatMap(v -> other.convertToModel(v, context)); }
@Override public PRESENTATION convertToPresentation(T value, ValueContext context) { MODEL model = other.convertToPresentation(value, context); return Converter.this.convertToPresentation(model, context); } };
@SuppressWarnings("unchecked") private Converter<TARGET, Object> createConverter(Class<?> getterType) { return Converter.from(fieldValue -> getterType.cast(fieldValue), propertyValue -> (TARGET) propertyValue, exception -> { throw new RuntimeException(exception); }); }
@Override public BindingBuilder<BEAN, TARGET> withValidator( Validator<? super TARGET> validator) { checkUnbound(); Objects.requireNonNull(validator, "validator cannot be null"); converterValidatorChain = ((Converter<FIELDVALUE, TARGET>) converterValidatorChain) .chain(new ValidatorAsConverter<>(validator)); return this; }
/** * Constructor * @param type Selection value type * @param renderingMode Rendering mode */ public Builder(Class<? extends T> type, RenderingMode renderingMode) { super(type, renderingMode); getInstance().setItemConverter(Converter.identity()); }
.chain(converter);
/** * Constructor * @param type Selection value type * @param renderingMode Rendering mode */ public Builder(Class<? extends T> type, RenderingMode renderingMode) { super(new MultiSelectField<>(type, renderingMode)); getInstance().setItemConverter(Converter.identity()); }
/** * 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); }
@Override public FIELDVALUE convertToPresentation(FIELDVALUE value, ValueContext context) { if (delegate == null) { return value; } else { return delegate.convertToPresentation(value, context); } }
@Override public Result<FIELDVALUE> convertToModel(FIELDVALUE value, ValueContext context) { if (delegate == null) { return Result.ok(value); } else { return delegate.convertToModel(value, context); } }
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; }
private FIELDVALUE convertDataToFieldType(BEAN bean) { TARGET target = getter.apply(bean); ValueContext valueContext = createValueContext(); return converterValidatorChain.convertToPresentation(target, valueContext); }
/** * Returns the field value run through all converters and validators, * but doesn't pass the {@link BindingValidationStatus} to any status * handler. * * @return the result of the conversion */ private Result<TARGET> doConversion() { FIELDVALUE fieldValue = field.getValue(); return converterValidatorChain.convertToModel(fieldValue, createValueContext()); }
SerializableFunction<NEWTARGET, TARGET> toPresentation, String errorMessage) { return withConverter(Converter.from(toModel, toPresentation, exception -> errorMessage));
/** * Formats an object according to a converter suitable for a given type. * * @param object * Object to format. * @param type * Type of the object. * @return String representation of the object, as returned by the * registered converter. */ public <T> String format(T object, Class<? extends T> type) { if (object == null) { return null; } else { Converter<String, Object> converter = findConverterFor( object.getClass()); return converter.convertToPresentation(object, new ValueContext()); } }
/** * Parses a given string as a value of given type. * * @param value * String value to convert. * @param type * Expected result type. * @return String converted to the expected result type using a registered * converter for that type. */ public <T> T parse(String value, Class<? extends T> type) { Converter<String, T> converter = findConverterFor(type); if (converter != null) { Result<T> result = converter.convertToModel(value, new ValueContext()); return result.getOrThrow(msg -> new IllegalArgumentException(msg)); } else { return null; } }
SerializableFunction<TARGET, NEWTARGET> toModel, SerializableFunction<NEWTARGET, TARGET> toPresentation) { return withConverter(Converter.from(toModel, toPresentation, exception -> exception.getMessage()));
/** * Serializes the given value to valid design attribute representation * * @param sourceType * the type of the value * @param value * the value to be serialized * @return the given value as design attribute representation */ private static String toAttributeValue(Class<?> sourceType, Object value) { if (value == null) { // TODO: Handle corner case where sourceType is String and default // value is not null. How to represent null value in attributes? return ""; } @SuppressWarnings("unchecked") Converter<String, Object> converter = getFormatter() .findConverterFor(sourceType); if (converter != null) { return converter.convertToPresentation(value, new ValueContext()); } else { return value.toString(); } }
private MODEL getModelSafely(PRESENTATION value, ValueContext context) { try { return (MODEL) delegate.convertToModel(value, context).getOrThrow(e -> new IllegalArgumentException(String.valueOf(e))); } catch (Throwable throwable) { log.info(throwable.getMessage()); return null; } } }
/** * 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); }