/** * Creates a value context from the current state of the binding and its * field. * * @return the value context */ protected ValueContext createValueContext() { if (field instanceof Component) { return new ValueContext((Component) field, field); } return new ValueContext(null, field, findLocale()); }
/** * Validates the {@code bean} using validators added using * {@link #withValidator(Validator)} and returns the result of the * validation as a list of validation results. * <p> * * @see #withValidator(Validator) * * @param bean * the bean to validate * @return a list of validation errors or an empty list if validation * succeeded */ private List<ValidationResult> validateBean(BEAN bean) { Objects.requireNonNull(bean, "bean cannot be null"); List<ValidationResult> results = Collections.unmodifiableList(validators .stream() .map(validator -> validator.apply(bean, new ValueContext())) .collect(Collectors.toList())); return results; }
/** * 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; } }
/** * 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(); } }
@Override protected void doSetValue(T value) { this.value = value; // Also set the internal dateString if (value == null) { value = getEmptyValue(); } dateString = formatDate(value); // TODO move range check to internal validator? RangeValidator<T> validator = getRangeValidator(); ValidationResult result = validator.apply(value, new ValueContext(this, this)); if (result.isError()) { currentErrorMessage = getDateOutOfRangeMessage(); } getState().parsable = currentErrorMessage == null; ErrorMessage errorMessage; if (currentErrorMessage == null) { errorMessage = null; } else { errorMessage = new UserError(currentErrorMessage); } setComponentError(errorMessage); updateResolutions(); }
/** * Build the {@link ValueContext} to be used with the converter. * @return the {@link ValueContext} */ private ValueContext _valueContext() { final Component component = getComponent(); if (component != null) { return new ValueContext(component); } else { return new ValueContext(); } }
private ValueContext getValueContext() { if (context != null) { return context; } if (input != null && input.getComponent() != null) { return new ValueContext(input.getComponent()); } return new ValueContext(); }
@Override protected T fromInternalValue(Double value) { return getConverter().convertToModel(value, new ValueContext(findLocale())) .getOrThrow(msg -> new IllegalArgumentException(msg)); }
@Override protected T fromInternalValue(String value) { return getConverter().convertToModel(value, new ValueContext(findLocale())) .getOrThrow(msg -> new IllegalArgumentException(msg)); }
@Override protected LocalDateTime toInternalValue(Date value) { return getConverter().convertToModel(value, new ValueContext(findLocale())) .getOrThrow(msg -> new IllegalArgumentException(msg)); }
protected ITEM toItem(S selection) { return requireItemConverter().convertToModel(selection, new ValueContext(this, this, findLocale())).getOrThrow( m -> new IllegalArgumentException("Failed to convert selection value [" + selection + "]: " + m)); }
@Override protected String toInternalValue(T value) { return getConverter().convertToPresentation(value, new ValueContext(findLocale())); }
@Override protected LocalDate toInternalValue(Date value) { return getConverter().convertToModel(value, new ValueContext(findLocale())) .getOrThrow(msg -> new IllegalArgumentException(msg)); }
@Override protected Double toInternalValue(T value) { return getConverter().convertToPresentation(value, new ValueContext(findLocale())); }
@Override protected Date fromInternalValue(LocalDate value) { return getConverter().convertToPresentation(value, new ValueContext(findLocale())); }
@Override protected Date fromInternalValue(LocalDateTime value) { return getConverter().convertToPresentation(value, new ValueContext(findLocale())); }
protected S toSelection(ITEM item) { return requireItemConverter().convertToPresentation(item, new ValueContext(this, this, findLocale())); }