From 3d9d47d222a53e33267cb3f26112782fe9f12caf Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Mon, 10 Jun 2013 20:01:40 +0300 Subject: Added type parameter to converter methods (#11895) Change-Id: I6562c537d9e5a0745eb67bc613123a265578ae00 --- .../converter/AbstractStringToNumberConverter.java | 6 ++-- .../com/vaadin/data/util/converter/Converter.java | 37 ++++++++++++++-------- .../vaadin/data/util/converter/ConverterUtil.java | 24 ++++++++++++-- .../data/util/converter/DateToLongConverter.java | 15 ++++++--- .../util/converter/DateToSqlDateConverter.java | 17 ++++++++-- .../data/util/converter/ReverseConverter.java | 10 +++--- .../util/converter/StringToBooleanConverter.java | 10 +++--- .../data/util/converter/StringToDateConverter.java | 21 ++++++++---- .../util/converter/StringToDoubleConverter.java | 5 +-- .../util/converter/StringToFloatConverter.java | 5 +-- .../util/converter/StringToIntegerConverter.java | 11 ++++--- .../util/converter/StringToNumberConverter.java | 13 ++++++-- server/src/com/vaadin/ui/AbstractField.java | 28 +++++++++++----- server/src/com/vaadin/ui/Table.java | 3 +- 14 files changed, 146 insertions(+), 59 deletions(-) (limited to 'server/src') diff --git a/server/src/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java b/server/src/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java index 5999d850b4..237f01bb22 100644 --- a/server/src/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java +++ b/server/src/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java @@ -65,7 +65,8 @@ public abstract class AbstractStringToNumberConverter implements * If there was a problem converting the value * @since 7.1 */ - protected Number convertToNumber(String value, Locale locale) + protected Number convertToNumber(String value, + Class targetType, Locale locale) throws ConversionException { if (value == null) { return null; @@ -98,7 +99,8 @@ public abstract class AbstractStringToNumberConverter implements * .Object, java.util.Locale) */ @Override - public String convertToPresentation(T value, Locale locale) + public String convertToPresentation(T value, + Class targetType, Locale locale) throws ConversionException { if (value == null) { return null; diff --git a/server/src/com/vaadin/data/util/converter/Converter.java b/server/src/com/vaadin/data/util/converter/Converter.java index ded7da7fb5..3c391af434 100644 --- a/server/src/com/vaadin/data/util/converter/Converter.java +++ b/server/src/com/vaadin/data/util/converter/Converter.java @@ -22,10 +22,10 @@ import java.util.Locale; /** * Interface that implements conversion between a model and a presentation type. *

- * Typically {@link #convertToPresentation(Object, Locale)} and - * {@link #convertToModel(Object, Locale)} should be symmetric so that chaining - * these together returns the original result for all input but this is not a - * requirement. + * Typically {@link #convertToPresentation(Object, Class, Locale)} and + * {@link #convertToModel(Object, Class, Locale)} should be symmetric so that + * chaining these together returns the original result for all input but this is + * not a requirement. *

*

* Converters must not have any side effects (never update UI from inside a @@ -55,19 +55,23 @@ public interface Converter extends Serializable { * A converter can optionally use locale to do the conversion. *

* A converter should in most cases be symmetric so chaining - * {@link #convertToPresentation(Object, Locale)} and - * {@link #convertToModel(Object, Locale)} should return the original value. + * {@link #convertToPresentation(Object, Class, Locale)} and + * {@link #convertToModel(Object, Class, Locale)} should return the original + * value. * * @param value * The value to convert, compatible with the target type. Can be * null + * @param targetType + * The requested type of the return value * @param locale * The locale to use for conversion. Can be null. * @return The converted value compatible with the source type * @throws ConversionException * If the value could not be converted */ - public MODEL convertToModel(PRESENTATION value, Locale locale) + public MODEL convertToModel(PRESENTATION value, + Class targetType, Locale locale) throws ConversionException; /** @@ -76,26 +80,30 @@ public interface Converter extends Serializable { * A converter can optionally use locale to do the conversion. *

* A converter should in most cases be symmetric so chaining - * {@link #convertToPresentation(Object, Locale)} and - * {@link #convertToModel(Object, Locale)} should return the original value. + * {@link #convertToPresentation(Object, Class, Locale)} and + * {@link #convertToModel(Object, Class, Locale)} should return the original + * value. * * @param value * The value to convert, compatible with the target type. Can be * null + * @param targetType + * The requested type of the return value * @param locale * The locale to use for conversion. Can be null. * @return The converted value compatible with the source type * @throws ConversionException * If the value could not be converted */ - public PRESENTATION convertToPresentation(MODEL value, Locale locale) + public PRESENTATION convertToPresentation(MODEL value, + Class targetType, Locale locale) throws ConversionException; /** * The source type of the converter. * * Values of this type can be passed to - * {@link #convertToPresentation(Object, Locale)}. + * {@link #convertToPresentation(Object, Class, Locale)}. * * @return The source type */ @@ -105,7 +113,7 @@ public interface Converter extends Serializable { * The target type of the converter. * * Values of this type can be passed to - * {@link #convertToModel(Object, Locale)}. + * {@link #convertToModel(Object, Class, Locale)}. * * @return The target type */ @@ -113,8 +121,9 @@ public interface Converter extends Serializable { /** * An exception that signals that the value passed to - * {@link Converter#convertToPresentation(Object, Locale)} or - * {@link Converter#convertToModel(Object, Locale)} could not be converted. + * {@link Converter#convertToPresentation(Object, Class, Locale)} or + * {@link Converter#convertToModel(Object, Class, Locale)} could not be + * converted. * * @author Vaadin Ltd * @since 7.0 diff --git a/server/src/com/vaadin/data/util/converter/ConverterUtil.java b/server/src/com/vaadin/data/util/converter/ConverterUtil.java index 08d7363084..3c62a71b73 100644 --- a/server/src/com/vaadin/data/util/converter/ConverterUtil.java +++ b/server/src/com/vaadin/data/util/converter/ConverterUtil.java @@ -86,7 +86,17 @@ public class ConverterUtil implements Serializable { Converter converter, Locale locale) throws Converter.ConversionException { if (converter != null) { - return converter.convertToPresentation(modelValue, locale); + PRESENTATIONTYPE presentation = converter.convertToPresentation( + modelValue, presentationType, locale); + if (!presentationType.isInstance(presentation)) { + throw new Converter.ConversionException( + "Converter returned an object of type " + + presentation.getClass().getName() + + " when expecting " + + presentationType.getName()); + } + + return presentation; } if (modelValue == null) { return null; @@ -123,7 +133,17 @@ public class ConverterUtil implements Serializable { * If there is a converter, always use it. It must convert or throw * an exception. */ - return converter.convertToModel(presentationValue, locale); + MODELTYPE model = converter.convertToModel(presentationValue, + modelType, locale); + if (!modelType.isInstance(model)) { + throw new Converter.ConversionException( + "Converter returned an object of type " + + model.getClass().getName() + + " when expecting " + modelType.getName()); + } + + return model; + } if (presentationValue == null) { diff --git a/server/src/com/vaadin/data/util/converter/DateToLongConverter.java b/server/src/com/vaadin/data/util/converter/DateToLongConverter.java index 82dccdcacc..6aedc74f6d 100644 --- a/server/src/com/vaadin/data/util/converter/DateToLongConverter.java +++ b/server/src/com/vaadin/data/util/converter/DateToLongConverter.java @@ -32,10 +32,11 @@ public class DateToLongConverter implements Converter { * * @see * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.util.Locale) + * java.lang.Class, java.util.Locale) */ @Override - public Long convertToModel(Date value, Locale locale) { + public Long convertToModel(Date value, Class targetType, + Locale locale) { if (value == null) { return null; } @@ -48,10 +49,16 @@ public class DateToLongConverter implements Converter { * * @see * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang - * .Object, java.util.Locale) + * .Object, java.lang.Class, java.util.Locale) */ @Override - public Date convertToPresentation(Long value, Locale locale) { + public Date convertToPresentation(Long value, + Class targetType, Locale locale) { + if (targetType != getPresentationType()) { + throw new ConversionException("Converter only supports " + + getPresentationType().getName() + " (targetType was " + + targetType.getName() + ")"); + } if (value == null) { return null; } diff --git a/server/src/com/vaadin/data/util/converter/DateToSqlDateConverter.java b/server/src/com/vaadin/data/util/converter/DateToSqlDateConverter.java index 97027cc05b..cddf2d8a42 100644 --- a/server/src/com/vaadin/data/util/converter/DateToSqlDateConverter.java +++ b/server/src/com/vaadin/data/util/converter/DateToSqlDateConverter.java @@ -35,14 +35,27 @@ import java.util.Locale; public class DateToSqlDateConverter implements Converter { @Override - public java.sql.Date convertToModel(Date value, Locale locale) + public java.sql.Date convertToModel(Date value, + Class targetType, Locale locale) throws ConversionException { + if (targetType != getModelType()) { + throw new ConversionException("Converter only supports " + + getModelType().getName() + " (targetType was " + + targetType.getName() + ")"); + } + return new java.sql.Date(value.getTime()); } @Override - public Date convertToPresentation(java.sql.Date value, Locale locale) + public Date convertToPresentation(java.sql.Date value, + Class targetType, Locale locale) throws ConversionException { + if (targetType != getPresentationType()) { + throw new ConversionException("Converter only supports " + + getPresentationType().getName() + " (targetType was " + + targetType.getName() + ")"); + } return new Date(value.getTime()); } diff --git a/server/src/com/vaadin/data/util/converter/ReverseConverter.java b/server/src/com/vaadin/data/util/converter/ReverseConverter.java index 94f333b7f3..6fa07696db 100644 --- a/server/src/com/vaadin/data/util/converter/ReverseConverter.java +++ b/server/src/com/vaadin/data/util/converter/ReverseConverter.java @@ -53,9 +53,10 @@ public class ReverseConverter implements * .lang.Object, java.util.Locale) */ @Override - public MODEL convertToModel(PRESENTATION value, Locale locale) + public MODEL convertToModel(PRESENTATION value, + Class targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { - return realConverter.convertToPresentation(value, locale); + return realConverter.convertToPresentation(value, targetType, locale); } /* @@ -66,9 +67,10 @@ public class ReverseConverter implements * .Object, java.util.Locale) */ @Override - public PRESENTATION convertToPresentation(MODEL value, Locale locale) + public PRESENTATION convertToPresentation(MODEL value, + Class targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { - return realConverter.convertToModel(value, locale); + return realConverter.convertToModel(value, targetType, locale); } /* diff --git a/server/src/com/vaadin/data/util/converter/StringToBooleanConverter.java b/server/src/com/vaadin/data/util/converter/StringToBooleanConverter.java index 1702d3808f..6af0933731 100644 --- a/server/src/com/vaadin/data/util/converter/StringToBooleanConverter.java +++ b/server/src/com/vaadin/data/util/converter/StringToBooleanConverter.java @@ -35,10 +35,11 @@ public class StringToBooleanConverter implements Converter { * * @see * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.util.Locale) + * java.lang.Class, java.util.Locale) */ @Override - public Boolean convertToModel(String value, Locale locale) + public Boolean convertToModel(String value, + Class targetType, Locale locale) throws ConversionException { if (value == null || value.isEmpty()) { return null; @@ -80,10 +81,11 @@ public class StringToBooleanConverter implements Converter { * * @see * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang - * .Object, java.util.Locale) + * .Object, java.lang.Class, java.util.Locale) */ @Override - public String convertToPresentation(Boolean value, Locale locale) + public String convertToPresentation(Boolean value, + Class targetType, Locale locale) throws ConversionException { if (value == null) { return null; diff --git a/server/src/com/vaadin/data/util/converter/StringToDateConverter.java b/server/src/com/vaadin/data/util/converter/StringToDateConverter.java index 0dcf1d4795..e3917c6a52 100644 --- a/server/src/com/vaadin/data/util/converter/StringToDateConverter.java +++ b/server/src/com/vaadin/data/util/converter/StringToDateConverter.java @@ -37,8 +37,9 @@ import java.util.Locale; public class StringToDateConverter implements Converter { /** - * Returns the format used by {@link #convertToPresentation(Date, Locale)} - * and {@link #convertToModel(String, Locale)}. + * Returns the format used by + * {@link #convertToPresentation(Date, Class,Locale)} and + * {@link #convertToModel(String, Class, Locale)}. * * @param locale * The locale to use @@ -60,11 +61,18 @@ public class StringToDateConverter implements Converter { * * @see * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.util.Locale) + * java.lang.Class, java.util.Locale) */ @Override - public Date convertToModel(String value, Locale locale) + public Date convertToModel(String value, Class targetType, + Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { + if (targetType != getModelType()) { + throw new ConversionException("Converter only supports " + + getModelType().getName() + " (targetType was " + + targetType.getName() + ")"); + } + if (value == null) { return null; } @@ -87,10 +95,11 @@ public class StringToDateConverter implements Converter { * * @see * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang - * .Object, java.util.Locale) + * .Object, java.lang.Class, java.util.Locale) */ @Override - public String convertToPresentation(Date value, Locale locale) + public String convertToPresentation(Date value, + Class targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { if (value == null) { return null; diff --git a/server/src/com/vaadin/data/util/converter/StringToDoubleConverter.java b/server/src/com/vaadin/data/util/converter/StringToDoubleConverter.java index 8bb82498b9..c593d256da 100644 --- a/server/src/com/vaadin/data/util/converter/StringToDoubleConverter.java +++ b/server/src/com/vaadin/data/util/converter/StringToDoubleConverter.java @@ -44,9 +44,10 @@ public class StringToDoubleConverter extends * java.util.Locale) */ @Override - public Double convertToModel(String value, Locale locale) + public Double convertToModel(String value, + Class targetType, Locale locale) throws ConversionException { - Number n = convertToNumber(value, locale); + Number n = convertToNumber(value, targetType, locale); return n == null ? null : n.doubleValue(); } diff --git a/server/src/com/vaadin/data/util/converter/StringToFloatConverter.java b/server/src/com/vaadin/data/util/converter/StringToFloatConverter.java index a207654358..6fcb83a770 100644 --- a/server/src/com/vaadin/data/util/converter/StringToFloatConverter.java +++ b/server/src/com/vaadin/data/util/converter/StringToFloatConverter.java @@ -44,9 +44,10 @@ public class StringToFloatConverter extends * java.util.Locale) */ @Override - public Float convertToModel(String value, Locale locale) + public Float convertToModel(String value, + Class targetType, Locale locale) throws ConversionException { - Number n = convertToNumber(value, locale); + Number n = convertToNumber(value, targetType, locale); return n == null ? null : n.floatValue(); } diff --git a/server/src/com/vaadin/data/util/converter/StringToIntegerConverter.java b/server/src/com/vaadin/data/util/converter/StringToIntegerConverter.java index 4f34cf1cd3..bc436112fe 100644 --- a/server/src/com/vaadin/data/util/converter/StringToIntegerConverter.java +++ b/server/src/com/vaadin/data/util/converter/StringToIntegerConverter.java @@ -35,8 +35,8 @@ public class StringToIntegerConverter extends /** * Returns the format used by - * {@link #convertToPresentation(Integer, Locale)} and - * {@link #convertToModel(String, Locale)}. + * {@link #convertToPresentation(Integer, Class, Locale)} and + * {@link #convertToModel(String, Class, Locale)} * * @param locale * The locale to use @@ -55,12 +55,13 @@ public class StringToIntegerConverter extends * * @see * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.util.Locale) + * java.lang.Class, java.util.Locale) */ @Override - public Integer convertToModel(String value, Locale locale) + public Integer convertToModel(String value, + Class targetType, Locale locale) throws ConversionException { - Number n = convertToNumber(value, locale); + Number n = convertToNumber(value, targetType, locale); return n == null ? null : n.intValue(); } diff --git a/server/src/com/vaadin/data/util/converter/StringToNumberConverter.java b/server/src/com/vaadin/data/util/converter/StringToNumberConverter.java index eae73e4cfa..22df42403f 100644 --- a/server/src/com/vaadin/data/util/converter/StringToNumberConverter.java +++ b/server/src/com/vaadin/data/util/converter/StringToNumberConverter.java @@ -37,12 +37,19 @@ public class StringToNumberConverter extends * * @see * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object, - * java.util.Locale) + * java.lang.Class, java.util.Locale) */ @Override - public Number convertToModel(String value, Locale locale) + public Number convertToModel(String value, + Class targetType, Locale locale) throws ConversionException { - return convertToNumber(value, locale); + if (targetType != getModelType()) { + throw new ConversionException("Converter only supports " + + getModelType().getName() + " (targetType was " + + targetType.getName() + ")"); + } + + return convertToNumber(value, targetType, locale); } /* diff --git a/server/src/com/vaadin/ui/AbstractField.java b/server/src/com/vaadin/ui/AbstractField.java index 7ac3a57c46..6a52d6b849 100644 --- a/server/src/com/vaadin/ui/AbstractField.java +++ b/server/src/com/vaadin/ui/AbstractField.java @@ -740,13 +740,7 @@ public abstract class AbstractField extends AbstractComponent implements */ private Object convertToModel(T fieldValue, Locale locale) throws Converter.ConversionException { - Class modelType = null; - Property pd = getPropertyDataSource(); - if (pd != null) { - modelType = pd.getType(); - } else if (getConverter() != null) { - modelType = getConverter().getModelType(); - } + Class modelType = getModelType(); try { return ConverterUtil.convertToModel(fieldValue, (Class) modelType, getConverter(), locale); @@ -755,6 +749,24 @@ public abstract class AbstractField extends AbstractComponent implements } } + /** + * Retrieves the type of the currently used data model. If the field has no + * data source then the model type of the converter is used. + * + * @since 7.1 + * @return The type of the currently used data model or null if no data + * source or converter is set. + */ + protected Class getModelType() { + Property pd = getPropertyDataSource(); + if (pd != null) { + return pd.getType(); + } else if (getConverter() != null) { + return getConverter().getModelType(); + } + return null; + } + /** * Returns the conversion error with {0} replaced by the data source type * and {1} replaced by the exception (localized) message. @@ -935,7 +947,7 @@ public abstract class AbstractField extends AbstractComponent implements if (getConverter() != null) { try { valueToValidate = getConverter().convertToModel(fieldValue, - getLocale()); + getModelType(), getLocale()); } catch (ConversionException e) { throw new InvalidValueException(getConversionError( getConverter().getModelType(), e)); diff --git a/server/src/com/vaadin/ui/Table.java b/server/src/com/vaadin/ui/Table.java index 3d7cb42050..a688b2eb45 100644 --- a/server/src/com/vaadin/ui/Table.java +++ b/server/src/com/vaadin/ui/Table.java @@ -4011,7 +4011,8 @@ public class Table extends AbstractSelect implements Action.Container, } Object value = property.getValue(); if (converter != null) { - return converter.convertToPresentation(value, getLocale()); + return converter.convertToPresentation(value, String.class, + getLocale()); } return (null != value) ? value.toString() : ""; } -- cgit v1.2.3