diff options
author | Artur Signell <artur@vaadin.com> | 2013-06-10 20:01:40 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2013-06-11 12:40:09 +0000 |
commit | 3d9d47d222a53e33267cb3f26112782fe9f12caf (patch) | |
tree | a8fdc3a5bcaecf535673f957f8ef1bb7254d1f73 | |
parent | 5793a1c3a2da2c9771bd1721483a4689100d4bc3 (diff) | |
download | vaadin-framework-3d9d47d222a53e33267cb3f26112782fe9f12caf.tar.gz vaadin-framework-3d9d47d222a53e33267cb3f26112782fe9f12caf.zip |
Added type parameter to converter methods (#11895)
Change-Id: I6562c537d9e5a0745eb67bc613123a265578ae00
30 files changed, 241 insertions, 130 deletions
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<T> 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<? extends Number> targetType, Locale locale) throws ConversionException { if (value == null) { return null; @@ -98,7 +99,8 @@ public abstract class AbstractStringToNumberConverter<T> implements * .Object, java.util.Locale) */ @Override - public String convertToPresentation(T value, Locale locale) + public String convertToPresentation(T value, + Class<? extends String> 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. * <p> - * 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. * </p> * <p> * Converters must not have any side effects (never update UI from inside a @@ -55,19 +55,23 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable { * A converter can optionally use locale to do the conversion. * </p> * 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<? extends MODEL> targetType, Locale locale) throws ConversionException; /** @@ -76,26 +80,30 @@ public interface Converter<PRESENTATION, MODEL> extends Serializable { * A converter can optionally use locale to do the conversion. * </p> * 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<? extends PRESENTATION> 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<PRESENTATION, MODEL> 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<PRESENTATION, MODEL> 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<PRESENTATIONTYPE, MODELTYPE> 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<Date, Long> { * * @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<? extends Long> targetType, + Locale locale) { if (value == null) { return null; } @@ -48,10 +49,16 @@ public class DateToLongConverter implements Converter<Date, Long> { * * @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<? extends Date> 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<Date, java.sql.Date> { @Override - public java.sql.Date convertToModel(Date value, Locale locale) + public java.sql.Date convertToModel(Date value, + Class<? extends java.sql.Date> 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<? extends Date> 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<PRESENTATION, MODEL> implements * .lang.Object, java.util.Locale) */ @Override - public MODEL convertToModel(PRESENTATION value, Locale locale) + public MODEL convertToModel(PRESENTATION value, + Class<? extends MODEL> 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<PRESENTATION, MODEL> implements * .Object, java.util.Locale) */ @Override - public PRESENTATION convertToPresentation(MODEL value, Locale locale) + public PRESENTATION convertToPresentation(MODEL value, + Class<? extends PRESENTATION> 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<String, Boolean> { * * @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<? extends Boolean> targetType, Locale locale) throws ConversionException { if (value == null || value.isEmpty()) { return null; @@ -80,10 +81,11 @@ public class StringToBooleanConverter implements Converter<String, Boolean> { * * @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<? extends String> 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<String, Date> { /** - * 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<String, Date> { * * @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<? extends Date> 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<String, Date> { * * @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<? extends String> 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<? extends Double> 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<? extends Float> 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<? extends Integer> 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<? extends Number> 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<T> 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<Object>) modelType, getConverter(), locale); @@ -756,6 +750,24 @@ public abstract class AbstractField<T> 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<T> 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() : ""; } diff --git a/server/tests/src/com/vaadin/tests/data/converter/ConverterFactory.java b/server/tests/src/com/vaadin/tests/data/converter/ConverterFactory.java index 9da8406507..b53c5a930f 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/ConverterFactory.java +++ b/server/tests/src/com/vaadin/tests/data/converter/ConverterFactory.java @@ -30,13 +30,15 @@ public class ConverterFactory extends TestCase { public static class ConvertTo42 implements Converter<String, Integer> { @Override - public Integer convertToModel(String value, Locale locale) + public Integer convertToModel(String value, + Class<? extends Integer> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { return 42; } @Override - public String convertToPresentation(Integer value, Locale locale) + public String convertToPresentation(Integer value, + Class<? extends String> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { return "42"; } diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestAnyEnumToStringConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestAnyEnumToStringConverter.java index c267e012e8..e21382069f 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/TestAnyEnumToStringConverter.java +++ b/server/tests/src/com/vaadin/tests/data/converter/TestAnyEnumToStringConverter.java @@ -33,14 +33,12 @@ public class TestAnyEnumToStringConverter { public class AnyEnumToStringConverter implements Converter<Enum, String> { - private Class<? extends Enum>[] enumClass; - - public AnyEnumToStringConverter(Class<? extends Enum>... enumClass) { - this.enumClass = enumClass; + public AnyEnumToStringConverter() { } @Override - public String convertToModel(Enum value, Locale locale) + public String convertToModel(Enum value, + Class<? extends String> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { if (value == null) { return null; @@ -50,16 +48,15 @@ public class TestAnyEnumToStringConverter { } @Override - public Enum convertToPresentation(String value, Locale locale) + public Enum convertToPresentation(String value, + Class<? extends Enum> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { if (value == null) { return null; } - for (Class<? extends Enum> candidate : enumClass) { - for (Enum e : candidate.getEnumConstants()) { - if (e.toString().equals(value)) { - return e; - } + for (Enum e : targetType.getEnumConstants()) { + if (e.toString().equals(value)) { + return e; } } @@ -82,29 +79,29 @@ public class TestAnyEnumToStringConverter { @Before public void setup() { - converter = new AnyEnumToStringConverter(TestEnum.class, - AnotherTestEnum.class); + converter = new AnyEnumToStringConverter(); } @Test public void nullConversion() { - Assert.assertEquals(null, converter.convertToModel(null, null)); + Assert.assertEquals(null, converter.convertToModel(null, null, null)); } @Test public void enumToStringConversion() { Assert.assertEquals(TestEnum.TWO.toString(), - converter.convertToModel(TestEnum.TWO, null)); - Assert.assertEquals(AnotherTestEnum.TWO.toString(), - converter.convertToModel(AnotherTestEnum.TWO, null)); + converter.convertToModel(TestEnum.TWO, String.class, null)); + Assert.assertEquals(AnotherTestEnum.TWO.toString(), converter + .convertToModel(AnotherTestEnum.TWO, String.class, null)); } @Test public void stringToEnumConversion() { - Assert.assertEquals(TestEnum.TWO, - converter.convertToPresentation(TestEnum.TWO.toString(), null)); + Assert.assertEquals(TestEnum.TWO, converter.convertToPresentation( + TestEnum.TWO.toString(), TestEnum.class, null)); Assert.assertEquals(AnotherTestEnum.TWO, converter - .convertToPresentation(AnotherTestEnum.TWO.toString(), null)); + .convertToPresentation(AnotherTestEnum.TWO.toString(), + AnotherTestEnum.class, null)); } @Test diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestDateToLongConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestDateToLongConverter.java index aab4ae18f0..6b9fa93e27 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/TestDateToLongConverter.java +++ b/server/tests/src/com/vaadin/tests/data/converter/TestDateToLongConverter.java @@ -11,11 +11,11 @@ public class TestDateToLongConverter extends TestCase { DateToLongConverter converter = new DateToLongConverter(); public void testNullConversion() { - assertEquals(null, converter.convertToModel(null, null)); + assertEquals(null, converter.convertToModel(null, Long.class, null)); } public void testValueConversion() { assertEquals(Long.valueOf(946677600000l), - converter.convertToModel(new Date(100, 0, 1), null)); + converter.convertToModel(new Date(100, 0, 1), Long.class, null)); } } diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestSpecificEnumToStringConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestSpecificEnumToStringConverter.java index 93dbe96b56..8ce7b8eab8 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/TestSpecificEnumToStringConverter.java +++ b/server/tests/src/com/vaadin/tests/data/converter/TestSpecificEnumToStringConverter.java @@ -41,7 +41,8 @@ public class TestSpecificEnumToStringConverter { } @Override - public String convertToModel(Enum value, Locale locale) + public String convertToModel(Enum value, + Class<? extends String> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { if (value == null) { return null; @@ -51,7 +52,8 @@ public class TestSpecificEnumToStringConverter { } @Override - public Enum convertToPresentation(String value, Locale locale) + public Enum convertToPresentation(String value, + Class<? extends Enum> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { if (value == null) { return null; @@ -90,19 +92,21 @@ public class TestSpecificEnumToStringConverter { @Test public void nullConversion() { - Assert.assertEquals(null, testEnumConverter.convertToModel(null, null)); + Assert.assertEquals(null, + testEnumConverter.convertToModel(null, null, null)); } @Test public void enumToStringConversion() { - Assert.assertEquals(TestEnum.TWO.toString(), - testEnumConverter.convertToModel(TestEnum.TWO, null)); + Assert.assertEquals(TestEnum.TWO.toString(), testEnumConverter + .convertToModel(TestEnum.TWO, String.class, null)); } @Test public void stringToEnumConversion() { Assert.assertEquals(TestEnum.TWO, testEnumConverter - .convertToPresentation(TestEnum.TWO.toString(), null)); + .convertToPresentation(TestEnum.TWO.toString(), TestEnum.class, + null)); } @Test diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToBooleanConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToBooleanConverter.java index 1c2b919db0..f236bb4000 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/TestStringToBooleanConverter.java +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToBooleanConverter.java @@ -9,15 +9,15 @@ public class TestStringToBooleanConverter extends TestCase { StringToBooleanConverter converter = new StringToBooleanConverter(); public void testNullConversion() { - assertEquals(null, converter.convertToModel(null, null)); + assertEquals(null, converter.convertToModel(null, null, null)); } public void testEmptyStringConversion() { - assertEquals(null, converter.convertToModel("", null)); + assertEquals(null, converter.convertToModel("", Boolean.class, null)); } public void testValueConversion() { - assertTrue(converter.convertToModel("true", null)); - assertFalse(converter.convertToModel("false", null)); + assertTrue(converter.convertToModel("true", Boolean.class, null)); + assertFalse(converter.convertToModel("false", Boolean.class, null)); } } diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToDateConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToDateConverter.java index 2961b93c89..59314bd5ee 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/TestStringToDateConverter.java +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToDateConverter.java @@ -12,15 +12,15 @@ public class TestStringToDateConverter extends TestCase { StringToDateConverter converter = new StringToDateConverter(); public void testNullConversion() { - assertEquals(null, converter.convertToModel(null, null)); + assertEquals(null, converter.convertToModel(null, null, null)); } public void testEmptyStringConversion() { - assertEquals(null, converter.convertToModel("", null)); + assertEquals(null, converter.convertToModel("", Date.class, null)); } public void testValueConversion() { assertEquals(new Date(100, 0, 1), converter.convertToModel( - "Jan 1, 2000 12:00:00 AM", Locale.ENGLISH)); + "Jan 1, 2000 12:00:00 AM", Date.class, Locale.ENGLISH)); } } diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToDoubleConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToDoubleConverter.java index 9903a8deaa..739f9b7d5a 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/TestStringToDoubleConverter.java +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToDoubleConverter.java @@ -9,14 +9,14 @@ public class TestStringToDoubleConverter extends TestCase { StringToDoubleConverter converter = new StringToDoubleConverter(); public void testNullConversion() { - assertEquals(null, converter.convertToModel(null, null)); + assertEquals(null, converter.convertToModel(null, null, null)); } public void testEmptyStringConversion() { - assertEquals(null, converter.convertToModel("", null)); + assertEquals(null, converter.convertToModel("", Double.class, null)); } public void testValueConversion() { - assertEquals(10.0, converter.convertToModel("10", null)); + assertEquals(10.0, converter.convertToModel("10", Double.class, null)); } } diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToFloatConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToFloatConverter.java index 670daec202..3bb2b3649e 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/TestStringToFloatConverter.java +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToFloatConverter.java @@ -9,14 +9,15 @@ public class TestStringToFloatConverter extends TestCase { StringToFloatConverter converter = new StringToFloatConverter(); public void testNullConversion() { - assertEquals(null, converter.convertToModel(null, null)); + assertEquals(null, converter.convertToModel(null, null, null)); } public void testEmptyStringConversion() { - assertEquals(null, converter.convertToModel("", null)); + assertEquals(null, converter.convertToModel("", Float.class, null)); } public void testValueConversion() { - assertEquals(Float.valueOf(10), converter.convertToModel("10", null)); + assertEquals(Float.valueOf(10), + converter.convertToModel("10", Float.class, null)); } } diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToIntegerConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToIntegerConverter.java index 80414cccaf..7f247b791f 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/TestStringToIntegerConverter.java +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToIntegerConverter.java @@ -9,14 +9,15 @@ public class TestStringToIntegerConverter extends TestCase { StringToIntegerConverter converter = new StringToIntegerConverter(); public void testNullConversion() { - assertEquals(null, converter.convertToModel(null, null)); + assertEquals(null, converter.convertToModel(null, null, null)); } public void testEmptyStringConversion() { - assertEquals(null, converter.convertToModel("", null)); + assertEquals(null, converter.convertToModel("", Integer.class, null)); } public void testValueConversion() { - assertEquals(Integer.valueOf(10), converter.convertToModel("10", null)); + assertEquals(Integer.valueOf(10), + converter.convertToModel("10", Integer.class, null)); } } diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToNumberConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToNumberConverter.java index d3f87d628a..9c322cd301 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/TestStringToNumberConverter.java +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToNumberConverter.java @@ -9,15 +9,16 @@ public class TestStringToNumberConverter extends TestCase { StringToNumberConverter converter = new StringToNumberConverter(); public void testNullConversion() { - assertEquals(null, converter.convertToModel(null, null)); + assertEquals(null, converter.convertToModel(null, null, null)); } public void testEmptyStringConversion() { - assertEquals(null, converter.convertToModel("", null)); + assertEquals(null, converter.convertToModel("", Number.class, null)); } public void testValueConversion() { - assertEquals(Long.valueOf(10), converter.convertToModel("10", null)); - assertEquals(10.5, converter.convertToModel("10.5", null)); + assertEquals(Long.valueOf(10), + converter.convertToModel("10", Number.class, null)); + assertEquals(10.5, converter.convertToModel("10.5", Number.class, null)); } } diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbsFieldValueConversions.java b/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbsFieldValueConversions.java index b5e937f27d..9854296538 100644 --- a/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbsFieldValueConversions.java +++ b/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbsFieldValueConversions.java @@ -85,12 +85,14 @@ public class AbsFieldValueConversions extends TestCase { tf.setConverter(new Converter<String, String>() { @Override - public String convertToModel(String value, Locale locale) { + public String convertToModel(String value, + Class<? extends String> targetType, Locale locale) { return value; } @Override - public String convertToPresentation(String value, Locale locale) { + public String convertToPresentation(String value, + Class<? extends String> targetType, Locale locale) { return value; } @@ -150,7 +152,8 @@ public class AbsFieldValueConversions extends TestCase { cb.setConverter(new Converter<Boolean, Boolean>() { @Override - public Boolean convertToModel(Boolean value, Locale locale) { + public Boolean convertToModel(Boolean value, + Class<? extends Boolean> targetType, Locale locale) { // value from a CheckBox should never be null as long as it is // not set to null (handled by conversion below). assertNotNull(value); @@ -158,7 +161,8 @@ public class AbsFieldValueConversions extends TestCase { } @Override - public Boolean convertToPresentation(Boolean value, Locale locale) { + public Boolean convertToPresentation(Boolean value, + Class<? extends Boolean> targetType, Locale locale) { // Datamodel -> field if (value == null) { return false; @@ -184,7 +188,7 @@ public class AbsFieldValueConversions extends TestCase { assertEquals(Boolean.FALSE, property.getValue()); assertEquals(Boolean.FALSE, cb.getValue()); Boolean newDmValue = cb.getConverter().convertToPresentation( - cb.getValue(), new Locale("fi", "FI")); + cb.getValue(), Boolean.class, new Locale("fi", "FI")); assertEquals(Boolean.FALSE, newDmValue); // FIXME: Should be able to set to false here to cause datamodel to be diff --git a/uitest/src/com/vaadin/tests/components/abstractfield/Vaadin6ImplicitDoubleConverter.java b/uitest/src/com/vaadin/tests/components/abstractfield/Vaadin6ImplicitDoubleConverter.java index a9aa4d2a43..33359cc2c6 100644 --- a/uitest/src/com/vaadin/tests/components/abstractfield/Vaadin6ImplicitDoubleConverter.java +++ b/uitest/src/com/vaadin/tests/components/abstractfield/Vaadin6ImplicitDoubleConverter.java @@ -8,7 +8,8 @@ public class Vaadin6ImplicitDoubleConverter implements Converter<String, Double> { @Override - public Double convertToModel(String value, Locale locale) + public Double convertToModel(String value, + Class<? extends Double> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { if (null == value) { return null; @@ -17,7 +18,8 @@ public class Vaadin6ImplicitDoubleConverter implements } @Override - public String convertToPresentation(Double value, Locale locale) + public String convertToPresentation(Double value, + Class<? extends String> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { if (value == null) { return null; diff --git a/uitest/src/com/vaadin/tests/components/table/DoublesInTable.java b/uitest/src/com/vaadin/tests/components/table/DoublesInTable.java index 920ccc2038..355cd43d37 100644 --- a/uitest/src/com/vaadin/tests/components/table/DoublesInTable.java +++ b/uitest/src/com/vaadin/tests/components/table/DoublesInTable.java @@ -150,14 +150,16 @@ public class DoublesInTable extends TestBase { t.setConverter("sex", new Converter<String, Sex>() { @Override - public Sex convertToModel(String value, Locale locale) + public Sex convertToModel(String value, + Class<? extends Sex> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { // not used in this test - Table only converts to presentation return null; } @Override - public String convertToPresentation(Sex value, Locale locale) + public String convertToPresentation(Sex value, + Class<? extends String> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { if (value == null) { value = Sex.UNKNOWN; @@ -178,14 +180,16 @@ public class DoublesInTable extends TestBase { t.setConverter("deceased", new Converter<String, Boolean>() { @Override - public Boolean convertToModel(String value, Locale locale) { + public Boolean convertToModel(String value, + Class<? extends Boolean> targetType, Locale locale) { // not used in this test - Table only converts from source to // target return null; } @Override - public String convertToPresentation(Boolean value, Locale locale) { + public String convertToPresentation(Boolean value, + Class<? extends String> targetType, Locale locale) { if (value == null || value) { return "YES, DEAD!"; } else { @@ -206,7 +210,8 @@ public class DoublesInTable extends TestBase { t.setConverter("age", new Converter<String, Integer>() { @Override - public Integer convertToModel(String value, Locale locale) + public Integer convertToModel(String value, + Class<? extends Integer> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { // not used in this test - Table only converts from source to // target @@ -214,7 +219,8 @@ public class DoublesInTable extends TestBase { } @Override - public String convertToPresentation(Integer value, Locale locale) + public String convertToPresentation(Integer value, + Class<? extends String> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { if (value == null) { return null; @@ -243,14 +249,16 @@ public class DoublesInTable extends TestBase { t.setConverter("address", new Converter<String, Address>() { @Override - public Address convertToModel(String value, Locale locale) + public Address convertToModel(String value, + Class<? extends Address> targetType, Locale locale) throws ConversionException { // not used in this test - Table only converts to presentation return null; } @Override - public String convertToPresentation(Address value, Locale locale) + public String convertToPresentation(Address value, + Class<? extends String> targetType, Locale locale) throws ConversionException { return value.getStreetAddress() + ", " + value.getCity() + " (" + value.getCountry() + ")"; diff --git a/uitest/src/com/vaadin/tests/components/table/TableWithCustomConverterFactory.java b/uitest/src/com/vaadin/tests/components/table/TableWithCustomConverterFactory.java index 60be786e8b..a3f0ce2f13 100644 --- a/uitest/src/com/vaadin/tests/components/table/TableWithCustomConverterFactory.java +++ b/uitest/src/com/vaadin/tests/components/table/TableWithCustomConverterFactory.java @@ -29,14 +29,16 @@ public class TableWithCustomConverterFactory extends AbstractTestUI { Converter<String, Integer> { @Override - public Integer convertToModel(String value, Locale locale) + public Integer convertToModel(String value, + Class<? extends Integer> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { // TODO Auto-generated method stub return null; } @Override - public String convertToPresentation(Integer value, Locale locale) + public String convertToPresentation(Integer value, + Class<? extends String> targetType, Locale locale) throws com.vaadin.data.util.converter.Converter.ConversionException { return "Integer: " + value; } diff --git a/uitest/src/com/vaadin/tests/converter/ConverterThatEnforcesAFormat.java b/uitest/src/com/vaadin/tests/converter/ConverterThatEnforcesAFormat.java index 6158092591..a37aa521ba 100644 --- a/uitest/src/com/vaadin/tests/converter/ConverterThatEnforcesAFormat.java +++ b/uitest/src/com/vaadin/tests/converter/ConverterThatEnforcesAFormat.java @@ -28,7 +28,8 @@ public class ConverterThatEnforcesAFormat extends TestBase { + "). Two-way conversion gives: " + tf.getConverter().convertToPresentation( tf.getConverter().convertToModel(tf.getValue(), - tf.getLocale()), tf.getLocale()) + ")"); + Double.class, tf.getLocale()), + String.class, tf.getLocale()) + ")"); } }); tf.setImmediate(true); diff --git a/uitest/src/com/vaadin/tests/minitutorials/v7a1/StringMyTypeConverter.java b/uitest/src/com/vaadin/tests/minitutorials/v7a1/StringMyTypeConverter.java index 9ec7a82580..8197cd82ae 100644 --- a/uitest/src/com/vaadin/tests/minitutorials/v7a1/StringMyTypeConverter.java +++ b/uitest/src/com/vaadin/tests/minitutorials/v7a1/StringMyTypeConverter.java @@ -52,8 +52,8 @@ public class StringMyTypeConverter extends AbstractTestUI { class StringToNameConverter implements Converter<String, Name> { @Override - public Name convertToModel(String text, Locale locale) - throws ConversionException { + public Name convertToModel(String text, Class<? extends Name> targetType, + Locale locale) throws ConversionException { if (text == null) { return null; } @@ -66,7 +66,8 @@ class StringToNameConverter implements Converter<String, Name> { } @Override - public String convertToPresentation(Name name, Locale locale) + public String convertToPresentation(Name name, + Class<? extends String> targetType, Locale locale) throws ConversionException { if (name == null) { return null; |