]> source.dussan.org Git - vaadin-framework.git/commitdiff
First take on Converters
authorArtur Signell <artur@vaadin.com>
Fri, 25 Nov 2011 15:48:47 +0000 (17:48 +0200)
committerArtur Signell <artur@vaadin.com>
Fri, 25 Nov 2011 15:48:47 +0000 (17:48 +0200)
src/com/vaadin/data/util/converter/BooleanToStringConverter.java [new file with mode: 0644]
src/com/vaadin/data/util/converter/Converter.java [new file with mode: 0644]
src/com/vaadin/data/util/converter/ConverterFactory.java [new file with mode: 0644]
src/com/vaadin/data/util/converter/DateToStringConverter.java [new file with mode: 0644]
src/com/vaadin/data/util/converter/DefaultConverterFactory.java [new file with mode: 0644]
src/com/vaadin/data/util/converter/DoubleToStringConverter.java [new file with mode: 0644]
src/com/vaadin/data/util/converter/FieldValueConverterFactory.java [new file with mode: 0644]
src/com/vaadin/data/util/converter/IntegerToStringConverter.java [new file with mode: 0644]
src/com/vaadin/data/util/converter/LongToDateConverter.java [new file with mode: 0644]
src/com/vaadin/data/util/converter/NumberToStringConverter.java [new file with mode: 0644]
src/com/vaadin/data/util/converter/ReverseConverter.java [new file with mode: 0644]

diff --git a/src/com/vaadin/data/util/converter/BooleanToStringConverter.java b/src/com/vaadin/data/util/converter/BooleanToStringConverter.java
new file mode 100644 (file)
index 0000000..cdb7d9e
--- /dev/null
@@ -0,0 +1,32 @@
+package com.vaadin.data.util.converter;\r
+\r
+import java.util.Locale;\r
+\r
+public class BooleanToStringConverter implements Converter<Boolean, String> {\r
+\r
+    public Boolean convertFromTargetToSource(String value, Locale locale) {\r
+        try {\r
+            return Boolean.valueOf(value);\r
+        } catch (Exception e) {\r
+            throw new ConversionException("Cannot convert " + value\r
+                    + " to Boolean");\r
+        }\r
+    }\r
+\r
+    public String convertFromSourceToTarget(Boolean value, Locale locale) {\r
+        if (value == null) {\r
+            return "";\r
+        }\r
+\r
+        return value.toString();\r
+    }\r
+\r
+    public Class<Boolean> getSourceType() {\r
+        return Boolean.class;\r
+    }\r
+\r
+    public Class<String> getTargetType() {\r
+        return String.class;\r
+    }\r
+\r
+}\r
diff --git a/src/com/vaadin/data/util/converter/Converter.java b/src/com/vaadin/data/util/converter/Converter.java
new file mode 100644 (file)
index 0000000..fc1db77
--- /dev/null
@@ -0,0 +1,72 @@
+package com.vaadin.data.util.converter;\r
+\r
+import java.io.Serializable;\r
+import java.util.Locale;\r
+\r
+public interface Converter<SOURCE, TARGET> extends Serializable {\r
+\r
+    public SOURCE convertFromTargetToSource(TARGET value, Locale locale)\r
+            throws ConversionException;\r
+\r
+    public TARGET convertFromSourceToTarget(SOURCE value, Locale locale)\r
+            throws ConversionException;\r
+\r
+    public Class<SOURCE> getSourceType();\r
+\r
+    public Class<TARGET> getTargetType();\r
+\r
+    /**\r
+     * An exception that signals that the value passed to #convert or\r
+     * Converter.convertFromTargetToSource could not be converted.\r
+     * \r
+     * @author Vaadin Ltd\r
+     * @version\r
+     * @VERSION@\r
+     * @since 7.0\r
+     */\r
+    public class ConversionException extends RuntimeException {\r
+\r
+        /**\r
+         * Constructs a new <code>ConversionException</code> without a detail\r
+         * message.\r
+         */\r
+        public ConversionException() {\r
+        }\r
+\r
+        /**\r
+         * Constructs a new <code>ConversionException</code> with the specified\r
+         * detail message.\r
+         * \r
+         * @param msg\r
+         *            the detail message\r
+         */\r
+        public ConversionException(String msg) {\r
+            super(msg);\r
+        }\r
+\r
+        /**\r
+         * Constructs a new {@code ConversionException} with the specified\r
+         * cause.\r
+         * \r
+         * @param cause\r
+         *            The cause of the the exception\r
+         */\r
+        public ConversionException(Throwable cause) {\r
+            super(cause);\r
+        }\r
+\r
+        /**\r
+         * Constructs a new <code>ConversionException</code> with the specified\r
+         * detail message and cause.\r
+         * \r
+         * @param message\r
+         *            the detail message\r
+         * @param cause\r
+         *            The cause of the the exception\r
+         */\r
+        public ConversionException(String message, Throwable cause) {\r
+            super(message, cause);\r
+        }\r
+    }\r
+\r
+}\r
diff --git a/src/com/vaadin/data/util/converter/ConverterFactory.java b/src/com/vaadin/data/util/converter/ConverterFactory.java
new file mode 100644 (file)
index 0000000..e36e8c0
--- /dev/null
@@ -0,0 +1,7 @@
+package com.vaadin.data.util.converter;\r
+\r
+public interface ConverterFactory {\r
+    <SOURCE, TARGET> Converter<SOURCE, TARGET> createConverter(\r
+            Class<SOURCE> sourceType, Class<TARGET> targetType);\r
+\r
+}\r
diff --git a/src/com/vaadin/data/util/converter/DateToStringConverter.java b/src/com/vaadin/data/util/converter/DateToStringConverter.java
new file mode 100644 (file)
index 0000000..b46fa75
--- /dev/null
@@ -0,0 +1,50 @@
+package com.vaadin.data.util.converter;\r
+\r
+import java.text.DateFormat;\r
+import java.text.ParsePosition;\r
+import java.util.Date;\r
+import java.util.Locale;\r
+\r
+public class DateToStringConverter implements Converter<Date, String> {\r
+\r
+    public Date convertFromTargetToSource(String value, Locale locale)\r
+            throws com.vaadin.data.util.converter.Converter.ConversionException {\r
+        if (value == null) {\r
+            return null;\r
+        }\r
+\r
+        ParsePosition parsePosition = new ParsePosition(0);\r
+        Date parsedValue = getFormat(locale).parse(value, parsePosition);\r
+        if (parsePosition.getIndex() != value.length()) {\r
+            throw new ConversionException("Could not convert '" + value\r
+                    + "' to " + getTargetType().getName());\r
+        }\r
+\r
+        return parsedValue;\r
+    }\r
+\r
+    public String convertFromSourceToTarget(Date value, Locale locale)\r
+            throws com.vaadin.data.util.converter.Converter.ConversionException {\r
+        if (value == null) {\r
+            return null;\r
+        }\r
+\r
+        return getFormat(locale).format(value);\r
+    }\r
+\r
+    protected DateFormat getFormat(Locale locale) {\r
+        DateFormat f = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,\r
+                DateFormat.MEDIUM, locale);\r
+        f.setLenient(false);\r
+        return f;\r
+    }\r
+\r
+    public Class<Date> getSourceType() {\r
+        return Date.class;\r
+    }\r
+\r
+    public Class<String> getTargetType() {\r
+        return String.class;\r
+    }\r
+\r
+}\r
diff --git a/src/com/vaadin/data/util/converter/DefaultConverterFactory.java b/src/com/vaadin/data/util/converter/DefaultConverterFactory.java
new file mode 100644 (file)
index 0000000..8ddd35a
--- /dev/null
@@ -0,0 +1,77 @@
+package com.vaadin.data.util.converter;\r
+\r
+import java.util.Date;\r
+\r
+public class DefaultConverterFactory implements ConverterFactory {\r
+\r
+    public <SOURCE, TARGET> Converter<SOURCE, TARGET> createConverter(\r
+            Class<SOURCE> sourceType, Class<TARGET> targetType) {\r
+        Converter<SOURCE, TARGET> converter = findConverter(sourceType,\r
+                targetType);\r
+        if (converter != null) {\r
+            System.out.println(getClass().getName() + " created a "\r
+                    + converter.getClass());\r
+            return converter;\r
+        }\r
+\r
+        // Try to find a reverse converter\r
+        Converter<TARGET, SOURCE> reverseConverter = findConverter(targetType,\r
+                sourceType);\r
+        if (reverseConverter != null) {\r
+            System.out.println(getClass().getName() + " created a reverse "\r
+                    + reverseConverter.getClass());\r
+            return new ReverseConverter<SOURCE, TARGET>(reverseConverter);\r
+        }\r
+\r
+        System.out.println(getClass().getName()\r
+                + " could not find a converter for " + sourceType.getName()\r
+                + " to " + targetType.getName() + " conversion");\r
+        return null;\r
+\r
+    }\r
+\r
+    protected <SOURCE, TARGET> Converter<SOURCE, TARGET> findConverter(\r
+            Class<SOURCE> sourceType, Class<TARGET> targetType) {\r
+        if (targetType == String.class) {\r
+            // TextField converters and more\r
+            Converter<SOURCE, TARGET> converter = (Converter<SOURCE, TARGET>) createStringConverter(sourceType);\r
+            if (converter != null) {\r
+                return converter;\r
+            }\r
+        } else if (targetType == Date.class) {\r
+            // DateField converters and more\r
+            Converter<SOURCE, TARGET> converter = (Converter<SOURCE, TARGET>) createDateConverter(sourceType);\r
+            if (converter != null) {\r
+                return converter;\r
+            }\r
+        }\r
+\r
+        return null;\r
+\r
+    }\r
+\r
+    protected Converter<?, Date> createDateConverter(Class<?> sourceType) {\r
+        if (Long.class.isAssignableFrom(sourceType)) {\r
+            return new LongToDateConverter();\r
+        } else {\r
+            return null;\r
+        }\r
+    }\r
+\r
+    protected Converter<?, String> createStringConverter(Class<?> sourceType) {\r
+        if (Double.class.isAssignableFrom(sourceType)) {\r
+            return new DoubleToStringConverter();\r
+        } else if (Integer.class.isAssignableFrom(sourceType)) {\r
+            return new IntegerToStringConverter();\r
+        } else if (Boolean.class.isAssignableFrom(sourceType)) {\r
+            return new BooleanToStringConverter();\r
+        } else if (Number.class.isAssignableFrom(sourceType)) {\r
+            return new NumberToStringConverter();\r
+        } else if (Date.class.isAssignableFrom(sourceType)) {\r
+            return new DateToStringConverter();\r
+        } else {\r
+            return null;\r
+        }\r
+    }\r
+\r
+}\r
diff --git a/src/com/vaadin/data/util/converter/DoubleToStringConverter.java b/src/com/vaadin/data/util/converter/DoubleToStringConverter.java
new file mode 100644 (file)
index 0000000..299ed79
--- /dev/null
@@ -0,0 +1,38 @@
+package com.vaadin.data.util.converter;\r
+\r
+import java.text.NumberFormat;\r
+import java.text.ParsePosition;\r
+import java.util.Locale;\r
+\r
+public class DoubleToStringConverter implements Converter<Double, String> {\r
+\r
+    protected NumberFormat getFormatter(Locale locale) {\r
+        return NumberFormat.getNumberInstance(locale);\r
+    }\r
+\r
+    public Double convertFromTargetToSource(String value, Locale locale) {\r
+        ParsePosition parsePosition = new ParsePosition(0);\r
+        Number parsedValue = getFormatter(locale).parse(value, parsePosition);\r
+        if (parsePosition.getIndex() != value.length()) {\r
+            throw new ConversionException("Could not convert '" + value\r
+                    + "' to " + getTargetType().getName());\r
+        }\r
+        return parsedValue.doubleValue();\r
+    }\r
+\r
+    public String convertFromSourceToTarget(Double value, Locale locale) {\r
+        if (value == null) {\r
+            return null;\r
+        }\r
+\r
+        return getFormatter(locale).format(value);\r
+    }\r
+\r
+    public Class<Double> getSourceType() {\r
+        return Double.class;\r
+    }\r
+\r
+    public Class<String> getTargetType() {\r
+        return String.class;\r
+    }\r
+}\r
diff --git a/src/com/vaadin/data/util/converter/FieldValueConverterFactory.java b/src/com/vaadin/data/util/converter/FieldValueConverterFactory.java
new file mode 100644 (file)
index 0000000..1dc6f42
--- /dev/null
@@ -0,0 +1,14 @@
+package com.vaadin.data.util.converter;\r
+//package com.vaadin.data;\r
+//\r
+//import java.io.Serializable;\r
+//\r
+//import com.vaadin.ui.Field;\r
+//\r
+//public interface FieldValueConverterFactory extends Serializable {\r
+//\r
+//    <SOURCE, TARGET> Converter<SOURCE, TARGET> createConverter(\r
+//            Field<SOURCE> field, Item item, Object propertyId,\r
+//            Class<TARGET> targetType);\r
+//\r
+// }\r
diff --git a/src/com/vaadin/data/util/converter/IntegerToStringConverter.java b/src/com/vaadin/data/util/converter/IntegerToStringConverter.java
new file mode 100644 (file)
index 0000000..c6d48d5
--- /dev/null
@@ -0,0 +1,57 @@
+package com.vaadin.data.util.converter;\r
+\r
+import java.text.NumberFormat;\r
+import java.text.ParsePosition;\r
+import java.util.Locale;\r
+\r
+public class IntegerToStringConverter implements Converter<Integer, String> {\r
+\r
+    protected NumberFormat getFormatter(Locale locale) {\r
+        if (locale == null) {\r
+            return NumberFormat.getIntegerInstance();\r
+        } else {\r
+            return NumberFormat.getIntegerInstance(locale);\r
+        }\r
+    }\r
+\r
+    public Integer convertFromTargetToSource(String value, Locale locale) {\r
+        if (value == null) {\r
+            return null;\r
+        }\r
+\r
+        // Remove extra spaces\r
+        value = value.trim();\r
+\r
+        // Parse and detect errors. If the full string was not used, it is\r
+        // an error.\r
+        ParsePosition parsePosition = new ParsePosition(0);\r
+        Number parsedValue = getFormatter(locale).parse(value, parsePosition);\r
+        if (parsePosition.getIndex() != value.length()) {\r
+            throw new ConversionException("Could not convert '" + value\r
+                    + "' to " + getTargetType().getName());\r
+        }\r
+\r
+        if (parsedValue == null) {\r
+            // Convert "" to null\r
+            return null;\r
+        }\r
+        return parsedValue.intValue();\r
+    }\r
+\r
+    public String convertFromSourceToTarget(Integer value, Locale locale) {\r
+        if (value == null) {\r
+            return null;\r
+        }\r
+\r
+        return getFormatter(locale).format(value);\r
+    }\r
+\r
+    public Class<Integer> getSourceType() {\r
+        return Integer.class;\r
+    }\r
+\r
+    public Class<String> getTargetType() {\r
+        return String.class;\r
+    }\r
+\r
+}\r
diff --git a/src/com/vaadin/data/util/converter/LongToDateConverter.java b/src/com/vaadin/data/util/converter/LongToDateConverter.java
new file mode 100644 (file)
index 0000000..666c532
--- /dev/null
@@ -0,0 +1,32 @@
+package com.vaadin.data.util.converter;\r
+\r
+import java.util.Date;\r
+import java.util.Locale;\r
+\r
+public class LongToDateConverter implements Converter<Long, Date> {\r
+\r
+    public Long convertFromTargetToSource(Date value, Locale locale) {\r
+        if (value == null) {\r
+            return null;\r
+        }\r
+\r
+        return value.getTime();\r
+    }\r
+\r
+    public Date convertFromSourceToTarget(Long value, Locale locale) {\r
+        if (value == null) {\r
+            return null;\r
+        }\r
+\r
+        return new Date(value);\r
+    }\r
+\r
+    public Class<Long> getSourceType() {\r
+        return Long.class;\r
+    }\r
+\r
+    public Class<Date> getTargetType() {\r
+        return Date.class;\r
+    }\r
+\r
+}\r
diff --git a/src/com/vaadin/data/util/converter/NumberToStringConverter.java b/src/com/vaadin/data/util/converter/NumberToStringConverter.java
new file mode 100644 (file)
index 0000000..07c6af5
--- /dev/null
@@ -0,0 +1,57 @@
+package com.vaadin.data.util.converter;\r
+\r
+import java.text.NumberFormat;\r
+import java.text.ParsePosition;\r
+import java.util.Locale;\r
+\r
+public class NumberToStringConverter implements Converter<Number, String> {\r
+\r
+    protected NumberFormat getFormatter(Locale locale) {\r
+        if (locale == null) {\r
+            return NumberFormat.getNumberInstance();\r
+        } else {\r
+            return NumberFormat.getNumberInstance(locale);\r
+        }\r
+    }\r
+\r
+    public Number convertFromTargetToSource(String value, Locale locale) {\r
+        if (value == null) {\r
+            return null;\r
+        }\r
+\r
+        // Remove extra spaces\r
+        value = value.trim();\r
+\r
+        // Parse and detect errors. If the full string was not used, it is\r
+        // an error.\r
+        ParsePosition parsePosition = new ParsePosition(0);\r
+        Number parsedValue = getFormatter(locale).parse(value, parsePosition);\r
+        if (parsePosition.getIndex() != value.length()) {\r
+            throw new ConversionException("Could not convert '" + value\r
+                    + "' to " + getTargetType().getName());\r
+        }\r
+\r
+        if (parsedValue == null) {\r
+            // Convert "" to null\r
+            return null;\r
+        }\r
+        return parsedValue;\r
+    }\r
+\r
+    public String convertFromSourceToTarget(Number value, Locale locale) {\r
+        if (value == null) {\r
+            return null;\r
+        }\r
+\r
+        return getFormatter(locale).format(value);\r
+    }\r
+\r
+    public Class<Number> getSourceType() {\r
+        return Number.class;\r
+    }\r
+\r
+    public Class<String> getTargetType() {\r
+        return String.class;\r
+    }\r
+\r
+}\r
diff --git a/src/com/vaadin/data/util/converter/ReverseConverter.java b/src/com/vaadin/data/util/converter/ReverseConverter.java
new file mode 100644 (file)
index 0000000..590927a
--- /dev/null
@@ -0,0 +1,32 @@
+package com.vaadin.data.util.converter;\r
+\r
+import java.util.Locale;\r
+\r
+public class ReverseConverter<SOURCE, TARGET> implements\r
+        Converter<SOURCE, TARGET> {\r
+\r
+    private Converter<TARGET, SOURCE> realConverter;\r
+\r
+    public ReverseConverter(Converter<TARGET, SOURCE> realConverter) {\r
+        this.realConverter = realConverter;\r
+    }\r
+\r
+    public SOURCE convertFromTargetToSource(TARGET value, Locale locale)\r
+            throws com.vaadin.data.util.converter.Converter.ConversionException {\r
+        return realConverter.convertFromSourceToTarget(value, locale);\r
+    }\r
+\r
+    public TARGET convertFromSourceToTarget(SOURCE value, Locale locale)\r
+            throws com.vaadin.data.util.converter.Converter.ConversionException {\r
+        return realConverter.convertFromTargetToSource(value, locale);\r
+    }\r
+\r
+    public Class<SOURCE> getSourceType() {\r
+        return realConverter.getTargetType();\r
+    }\r
+\r
+    public Class<TARGET> getTargetType() {\r
+        return realConverter.getSourceType();\r
+    }\r
+\r
+}\r