Search in sources :

Example 1 with BindingBuilderImpl

use of com.vaadin.flow.data.binder.Binder.BindingBuilderImpl in project flow by vaadin.

the class RequiredFieldConfiguratorUtil method testConvertedDefaultValue.

/**
 * Tests the converted default value of the provided binding builder if
 * possible.
 *
 * @param binding
 *            the binding builder to test
 * @param predicate
 *            predicate for testing the converted default value
 * @return <code>true</code> if a converted default value is available and
 *         it passes the test; <code>false</code> if no converted default
 *         value is available or if it doesn't pass the test
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static boolean testConvertedDefaultValue(BindingBuilder<?, ?> binding, Predicate<Object> predicate) {
    if (binding instanceof BindingBuilderImpl<?, ?, ?>) {
        HasValue<?, ?> field = binding.getField();
        Converter converter = ((BindingBuilderImpl<?, ?, ?>) binding).getConverterValidatorChain();
        Result<?> result = converter.convertToModel(field.getEmptyValue(), BindingImpl.createValueContext(field));
        if (!result.isError()) {
            Object convertedEmptyValue = result.getOrThrow(IllegalStateException::new);
            return predicate.test(convertedEmptyValue);
        }
    }
    return false;
}
Also used : BindingBuilderImpl(com.vaadin.flow.data.binder.Binder.BindingBuilderImpl) Converter(com.vaadin.flow.data.converter.Converter)

Aggregations

BindingBuilderImpl (com.vaadin.flow.data.binder.Binder.BindingBuilderImpl)1 Converter (com.vaadin.flow.data.converter.Converter)1