Skip to content

Commit

Permalink
Add ValueContext parameter for Converters
Browse files Browse the repository at this point in the history
Change-Id: I47179b06b9e345f5a454ac1806d0bc9bcac24bcf
  • Loading branch information
Teemu Suo-Anttila committed Oct 24, 2016
1 parent 6d75c88 commit c72beed
Show file tree
Hide file tree
Showing 40 changed files with 394 additions and 144 deletions.
24 changes: 19 additions & 5 deletions server/src/main/java/com/vaadin/data/Binder.java
Expand Up @@ -34,6 +34,7 @@

import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.StringToIntegerConverter;
import com.vaadin.data.util.converter.ValueContext;
import com.vaadin.event.EventRouter;
import com.vaadin.server.ErrorMessage;
import com.vaadin.server.UserError;
Expand Down Expand Up @@ -563,10 +564,23 @@ public ValidationStatus<TARGET> validate() {
private ValidationStatus<TARGET> doValidation() {
FIELDVALUE fieldValue = field.getValue();
Result<TARGET> dataValue = converterValidatorChain
.convertToModel(fieldValue, findLocale());
.convertToModel(fieldValue, createValueContext());
return new ValidationStatus<>(this, dataValue);
}

/**
* 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);
}
return new ValueContext(findLocale());
}

private void unbind() {
onValueChange.remove();
}
Expand All @@ -584,8 +598,8 @@ private void setFieldValue(BEAN bean) {
}

private FIELDVALUE convertDataToFieldType(BEAN bean) {
return converterValidatorChain
.convertToPresentation(getter.apply(bean), findLocale());
return converterValidatorChain.convertToPresentation(
getter.apply(bean), createValueContext());
}

/**
Expand Down Expand Up @@ -661,7 +675,7 @@ public ValidatorAsConverter(Validator<? super T> validator) {
}

@Override
public Result<T> convertToModel(T value, Locale locale) {
public Result<T> convertToModel(T value, ValueContext context) {
Result<? super T> validationResult = validator.apply(value);
if (validationResult.isError()) {
return Result.error(validationResult.getMessage().get());
Expand All @@ -671,7 +685,7 @@ public Result<T> convertToModel(T value, Locale locale) {
}

@Override
public T convertToPresentation(T value, Locale locale) {
public T convertToPresentation(T value, ValueContext context) {
return value;
}

Expand Down
Expand Up @@ -109,12 +109,12 @@ protected String getErrorMessage() {
}

@Override
public String convertToPresentation(T value, Locale locale) {
public String convertToPresentation(T value, ValueContext context) {
if (value == null) {
return null;
}

return getFormat(locale).format(value);
return getFormat(context.getLocale().orElse(null)).format(value);
}

}
42 changes: 28 additions & 14 deletions server/src/main/java/com/vaadin/data/util/converter/Converter.java
Expand Up @@ -45,11 +45,17 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable {
*
* @param value
* The value to convert. Can be null
* @param locale
* The locale to use for conversion. Can be null.
* @param context
* The value context for the conversion.
* @return The converted value compatible with the source type
*/
public Result<MODEL> convertToModel(PRESENTATION value, Locale locale);
public Result<MODEL> convertToModel(PRESENTATION value,
ValueContext context);

default public Result<MODEL> convertToModel(PRESENTATION value,
Locale locale) {
return convertToModel(value, new ValueContext(locale));
}

/**
* Converts the given value from presentation type to model type.
Expand All @@ -58,11 +64,17 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable {
*
* @param value
* The value to convert. Can be null
* @param locale
* The locale to use for conversion. Can be null.
* @param context
* The value context for the conversion.
* @return The converted value compatible with the source type
*/
public PRESENTATION convertToPresentation(MODEL value, Locale locale);
public PRESENTATION convertToPresentation(MODEL value,
ValueContext context);

default public PRESENTATION convertToPresentation(MODEL value,
Locale locale) {
return convertToPresentation(value, new ValueContext());
}

/**
* Returns a converter that returns its input as-is in both directions.
Expand Down Expand Up @@ -124,12 +136,12 @@ public static <P, M> Converter<P, M> from(Function<P, Result<M>> toModel,
return new Converter<P, M>() {

@Override
public Result<M> convertToModel(P value, Locale locale) {
public Result<M> convertToModel(P value, ValueContext context) {
return toModel.apply(value);
}

@Override
public P convertToPresentation(M value, Locale locale) {
public P convertToPresentation(M value, ValueContext context) {
return toPresentation.apply(value);
}
};
Expand Down Expand Up @@ -157,16 +169,18 @@ public default <T> Converter<PRESENTATION, T> chain(
Converter<MODEL, T> other) {
return new Converter<PRESENTATION, T>() {
@Override
public Result<T> convertToModel(PRESENTATION value, Locale locale) {
public Result<T> convertToModel(PRESENTATION value,
ValueContext context) {
Result<MODEL> model = Converter.this.convertToModel(value,
locale);
return model.flatMap(v -> other.convertToModel(v, locale));
context);
return model.flatMap(v -> other.convertToModel(v, context));
}

@Override
public PRESENTATION convertToPresentation(T value, Locale locale) {
MODEL model = other.convertToPresentation(value, locale);
return Converter.this.convertToPresentation(model, locale);
public PRESENTATION convertToPresentation(T value,
ValueContext context) {
MODEL model = other.convertToPresentation(value, context);
return Converter.this.convertToPresentation(model, context);
}
};
}
Expand Down
Expand Up @@ -17,7 +17,6 @@
package com.vaadin.data.util.converter;

import java.util.Date;
import java.util.Locale;

import com.vaadin.data.Result;

Expand All @@ -30,7 +29,7 @@
public class DateToLongConverter implements Converter<Date, Long> {

@Override
public Result<Long> convertToModel(Date value, Locale locale) {
public Result<Long> convertToModel(Date value, ValueContext context) {
if (value == null) {
return Result.ok(null);
}
Expand All @@ -39,7 +38,7 @@ public Result<Long> convertToModel(Date value, Locale locale) {
}

@Override
public Date convertToPresentation(Long value, Locale locale) {
public Date convertToPresentation(Long value, ValueContext context) {
if (value == null) {
return null;
}
Expand Down
Expand Up @@ -20,7 +20,6 @@
package com.vaadin.data.util.converter;

import java.util.Date;
import java.util.Locale;

import com.vaadin.data.Result;

Expand All @@ -37,7 +36,8 @@
public class DateToSqlDateConverter implements Converter<Date, java.sql.Date> {

@Override
public Result<java.sql.Date> convertToModel(Date value, Locale locale) {
public Result<java.sql.Date> convertToModel(Date value,
ValueContext context) {
if (value == null) {
return Result.ok(null);
}
Expand All @@ -46,7 +46,8 @@ public Result<java.sql.Date> convertToModel(Date value, Locale locale) {
}

@Override
public Date convertToPresentation(java.sql.Date value, Locale locale) {
public Date convertToPresentation(java.sql.Date value,
ValueContext context) {
if (value == null) {
return null;
}
Expand Down
Expand Up @@ -60,8 +60,9 @@ protected NumberFormat getFormat(Locale locale) {
}

@Override
public Result<BigDecimal> convertToModel(String value, Locale locale) {
return convertToNumber(value, locale)
public Result<BigDecimal> convertToModel(String value,
ValueContext context) {
return convertToNumber(value, context.getLocale().orElse(null))
.map(number -> (BigDecimal) number);
}

Expand Down
Expand Up @@ -60,14 +60,16 @@ protected NumberFormat getFormat(Locale locale) {
}

@Override
public Result<BigInteger> convertToModel(String value, Locale locale) {
return convertToNumber(value, locale).map(number -> {
if (number == null) {
return null;
} else {
return ((BigDecimal) number).toBigInteger();
}
});
public Result<BigInteger> convertToModel(String value,
ValueContext context) {
return convertToNumber(value, context.getLocale().orElse(null))
.map(number -> {
if (number == null) {
return null;
} else {
return ((BigDecimal) number).toBigInteger();
}
});
}

}
Expand Up @@ -72,14 +72,15 @@ public StringToBooleanConverter(String errorMessage, String trueString,
}

@Override
public Result<Boolean> convertToModel(String value, Locale locale) {
public Result<Boolean> convertToModel(String value, ValueContext context) {
if (value == null) {
return Result.ok(null);
}

// Remove leading and trailing white space
value = value.trim();

Locale locale = context.getLocale().orElse(null);
if (getTrueString(locale).equals(value)) {
return Result.ok(true);
} else if (getFalseString(locale).equals(value)) {
Expand All @@ -92,10 +93,11 @@ public Result<Boolean> convertToModel(String value, Locale locale) {
}

@Override
public String convertToPresentation(Boolean value, Locale locale) {
public String convertToPresentation(Boolean value, ValueContext context) {
if (value == null) {
return null;
}
Locale locale = context.getLocale().orElse(null);
if (value) {
return getTrueString(locale);
} else {
Expand Down
Expand Up @@ -58,7 +58,7 @@ protected DateFormat getFormat(Locale locale) {
}

@Override
public Result<Date> convertToModel(String value, Locale locale) {
public Result<Date> convertToModel(String value, ValueContext context) {
if (value == null) {
return Result.ok(null);
}
Expand All @@ -67,7 +67,8 @@ public Result<Date> convertToModel(String value, Locale locale) {
value = value.trim();

ParsePosition parsePosition = new ParsePosition(0);
Date parsedValue = getFormat(locale).parse(value, parsePosition);
Date parsedValue = getFormat(context.getLocale().orElse(null))
.parse(value, parsePosition);
if (parsePosition.getIndex() != value.length()) {
return Result.error("Could not convert '" + value);
}
Expand All @@ -76,12 +77,12 @@ public Result<Date> convertToModel(String value, Locale locale) {
}

@Override
public String convertToPresentation(Date value, Locale locale) {
public String convertToPresentation(Date value, ValueContext context) {
if (value == null) {
return null;
}

return getFormat(locale).format(value);
return getFormat(context.getLocale().orElse(null)).format(value);
}

}
Expand Up @@ -49,8 +49,9 @@ public StringToDoubleConverter(String errorMessage) {
}

@Override
public Result<Double> convertToModel(String value, Locale locale) {
Result<Number> n = convertToNumber(value, locale);
public Result<Double> convertToModel(String value, ValueContext context) {
Result<Number> n = convertToNumber(value,
context.getLocale().orElse(null));

return n.map(number -> {
if (number == null) {
Expand Down
Expand Up @@ -47,8 +47,9 @@ public StringToFloatConverter(String errorMessage) {
}

@Override
public Result<Float> convertToModel(String value, Locale locale) {
Result<Number> n = convertToNumber(value, locale);
public Result<Float> convertToModel(String value, ValueContext context) {
Result<Number> n = convertToNumber(value,
context.getLocale().orElse(null));

return n.map(number -> {
if (number == null) {
Expand Down
Expand Up @@ -63,8 +63,9 @@ protected NumberFormat getFormat(Locale locale) {
}

@Override
public Result<Integer> convertToModel(String value, Locale locale) {
Result<Number> n = convertToNumber(value, locale);
public Result<Integer> convertToModel(String value, ValueContext context) {
Result<Number> n = convertToNumber(value,
context.getLocale().orElse(null));
return n.flatMap(number -> {
if (number == null) {
return Result.ok(null);
Expand Down
Expand Up @@ -62,8 +62,9 @@ protected NumberFormat getFormat(Locale locale) {
}

@Override
public Result<Long> convertToModel(String value, Locale locale) {
Result<Number> n = convertToNumber(value, locale);
public Result<Long> convertToModel(String value, ValueContext context) {
Result<Number> n = convertToNumber(value,
context.getLocale().orElse(null));
return n.map(number -> {
if (number == null) {
return null;
Expand Down

0 comments on commit c72beed

Please sign in to comment.