@Override public MODEL toModel(TYPE value) { return converter.toModel(value, property); }
/** * Return the given <code>value</code> against this property, converting it to required model data type if a * {@link PropertyValueConverter} is present. * @param value Property value (can be null) * @return Model value, possibly converted using the {@link PropertyValueConverter} */ default Object getConvertedValue(T value) { return getConverter().map((c) -> (Object) c.toModel(value, this)).orElse(value); }
/** * 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())); }
/** * 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())); }
@SuppressWarnings({ "unchecked", "rawtypes" }) private <V> V read(BeanProperty<?> property, T instance, Class<V> expectedType) { ObjectUtils.argumentNotNull(property, "Property must be not null"); ObjectUtils.argumentNotNull(instance, "Bean instance must be not null"); Object value = null; Object currentInstance = instance; for (BeanProperty<?> p : getPropertyHierarchy(property)) { currentInstance = value = readValue(p, currentInstance); } final Object readValue = value; final V propertyValue = (expectedType == null) ? (V) readValue : (V) property.getConverter() .filter(c -> (readValue == null || (!TypeUtils.isAssignable(readValue.getClass(), expectedType) && TypeUtils.isAssignable(readValue.getClass(), c.getPropertyType())))) .map(cv -> ((PropertyValueConverter) cv).toModel(readValue, property)).orElse(readValue); LOGGER.debug(() -> "BeanPropertySet: read property [" + property + "] value [" + propertyValue + "] from instance [" + instance + "]"); return propertyValue; }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public T write(PropertyBox propertyBox, T instance, boolean ignoreMissing) { ObjectUtils.argumentNotNull(propertyBox, "PropertyBox must be not null"); ObjectUtils.argumentNotNull(instance, "Bean instance must be not null"); propertyBox.stream().filter(p -> !p.isReadOnly()).filter(p -> Path.class.isAssignableFrom(p.getClass())) .map(p -> (Path<?>) p).forEach(p -> { getProperty(p, ignoreMissing).ifPresent(bp -> { final Property<Object> property = ((Property) p); final Object boxValue = propertyBox.getValue(property); Object value = boxValue; // check conversion if (!TypeUtils.isAssignable(bp.getType(), property.getType())) { value = property.getConverter() .filter(c -> TypeUtils.isAssignable(bp.getType(), c.getModelType())) .map(c -> ((PropertyValueConverter) c).toModel(boxValue, property)) .orElse(boxValue); } write(bp, p.getType(), value, instance); }); }); return instance; }
@SuppressWarnings("unchecked") @Override public <MODEL> B elementConverter(PropertyValueConverter<E, MODEL> elementConverter) { ObjectUtils.argumentNotNull(elementConverter, "Element value converter must be not null"); return converter(new CollectionCallbackPropertyValueConverter<>(getType(), elementConverter.getModelType(), v -> elementConverter.fromModel(v, (Property<E>) this), v -> elementConverter.toModel(v, (Property<E>) this), getDefaultInstanceProvider())); }
@SuppressWarnings("unchecked") @Override public <MODEL> B elementConverter(PropertyValueConverter<E, MODEL> elementConverter) { ObjectUtils.argumentNotNull(elementConverter, "Element value converter must be not null"); return converter(new CollectionCallbackPropertyValueConverter<>(getType(), elementConverter.getModelType(), v -> elementConverter.fromModel(v, (Property<E>) this), v -> elementConverter.toModel(v, (Property<E>) this), getDefaultInstanceProvider())); }