]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add StringToLongConverter for automatic handling of long values (#12291)
authorArtur Signell <artur@vaadin.com>
Fri, 9 Aug 2013 17:35:54 +0000 (20:35 +0300)
committerVaadin Code Review <review@vaadin.com>
Tue, 3 Sep 2013 07:39:51 +0000 (07:39 +0000)
Change-Id: Ia2865b199303c0e35b562120f1c916f288aa458a

server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java
server/src/com/vaadin/data/util/converter/StringToLongConverter.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/data/converter/TestStringToLongConverter.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/server/component/abstractfield/DefaultConverterFactory.java

index 0b3cfcd1b0a18fbd57972d9601ad506552e301f9..4d3717e9babaf6b82aa1114b4ee5288ce73ab915 100644 (file)
@@ -102,6 +102,8 @@ public class DefaultConverterFactory implements ConverterFactory {
             return new StringToFloatConverter();
         } else if (Integer.class.isAssignableFrom(sourceType)) {
             return new StringToIntegerConverter();
+        } else if (Long.class.isAssignableFrom(sourceType)) {
+            return new StringToLongConverter();
         } else if (BigDecimal.class.isAssignableFrom(sourceType)) {
             return new StringToBigDecimalConverter();
         } else if (Boolean.class.isAssignableFrom(sourceType)) {
diff --git a/server/src/com/vaadin/data/util/converter/StringToLongConverter.java b/server/src/com/vaadin/data/util/converter/StringToLongConverter.java
new file mode 100644 (file)
index 0000000..3532336
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2000-2013 Vaadin Ltd.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.vaadin.data.util.converter;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+/**
+ * A converter that converts from {@link String} to {@link Long} and back. Uses
+ * the given locale and a {@link NumberFormat} instance for formatting and
+ * parsing.
+ * <p>
+ * Override and overwrite {@link #getFormat(Locale)} to use a different format.
+ * </p>
+ * 
+ * @author Vaadin Ltd
+ * @since 7.2
+ */
+public class StringToLongConverter extends
+        AbstractStringToNumberConverter<Long> {
+
+    /**
+     * Returns the format used by
+     * {@link #convertToPresentation(Long, Class, Locale)} and
+     * {@link #convertToModel(String, Class, Locale)}
+     * 
+     * @param locale
+     *            The locale to use
+     * @return A NumberFormat instance
+     */
+    @Override
+    protected NumberFormat getFormat(Locale locale) {
+        if (locale == null) {
+            locale = Locale.getDefault();
+        }
+        return NumberFormat.getIntegerInstance(locale);
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see
+     * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
+     * java.lang.Class, java.util.Locale)
+     */
+    @Override
+    public Long convertToModel(String value, Class<? extends Long> targetType,
+            Locale locale) throws ConversionException {
+        Number n = convertToNumber(value, targetType, locale);
+        return n == null ? null : n.longValue();
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see com.vaadin.data.util.converter.Converter#getModelType()
+     */
+    @Override
+    public Class<Long> getModelType() {
+        return Long.class;
+    }
+
+}
diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToLongConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToLongConverter.java
new file mode 100644 (file)
index 0000000..18e2ed0
--- /dev/null
@@ -0,0 +1,68 @@
+package com.vaadin.tests.data.converter;
+
+import java.util.Locale;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ReverseConverter;
+import com.vaadin.data.util.converter.StringToLongConverter;
+
+public class TestStringToLongConverter extends TestCase {
+
+    StringToLongConverter converter = new StringToLongConverter();
+    Converter<Long, String> reverseConverter = new ReverseConverter<Long, String>(
+            converter);
+
+    public void testNullConversion() {
+        assertEquals(null, converter.convertToModel(null, Long.class, null));
+    }
+
+    public void testReverseNullConversion() {
+        assertEquals(null,
+                reverseConverter.convertToModel(null, String.class, null));
+    }
+
+    public void testEmptyStringConversion() {
+        assertEquals(null, converter.convertToModel("", Long.class, null));
+    }
+
+    public void testValueConversion() {
+        assertEquals(Long.valueOf(10),
+                converter.convertToModel("10", Long.class, null));
+    }
+
+    public void testReverseValueConversion() {
+        assertEquals(reverseConverter.convertToModel(10L, String.class, null),
+                "10");
+    }
+
+    public void testExtremeLongValueConversion() {
+        long l = converter.convertToModel("9223372036854775807", Long.class,
+                null);
+        Assert.assertEquals(Long.MAX_VALUE, l);
+        l = converter.convertToModel("-9223372036854775808", Long.class, null);
+        assertEquals(Long.MIN_VALUE, l);
+    }
+
+    public void testExtremeReverseLongValueConversion() {
+        String str = reverseConverter.convertToModel(Long.MAX_VALUE,
+                String.class, Locale.ENGLISH);
+        Assert.assertEquals("9,223,372,036,854,775,807", str);
+        str = reverseConverter.convertToModel(Long.MIN_VALUE, String.class,
+                null);
+        Assert.assertEquals("-9,223,372,036,854,775,808", str);
+    }
+
+    public void testOutOfBoundsValueConversion() {
+        // Long.MAX_VALUE+1 is converted to Long.MAX_VALUE
+        long l = converter.convertToModel("9223372036854775808", Long.class,
+                null);
+        Assert.assertEquals(Long.MAX_VALUE, l);
+        // Long.MIN_VALUE-1 is converted to Long.MIN_VALUE
+        l = converter.convertToModel("-9223372036854775809", Long.class, null);
+        assertEquals(Long.MIN_VALUE, l);
+
+    }
+}
index bac024725f611fd68e3a57e630ee4c0523bcb6ba..99397e9e8ff223b46914864737a80f38cd7d47e1 100644 (file)
@@ -43,6 +43,33 @@ public class DefaultConverterFactory extends TestCase {
 
     }
 
+    public static class LongBean {
+        long l1;
+        Long l2;
+
+        public LongBean(long l1, Long l2) {
+            this.l1 = l1;
+            this.l2 = l2;
+        }
+
+        public long getL1() {
+            return l1;
+        }
+
+        public void setL1(long l1) {
+            this.l1 = l1;
+        }
+
+        public Long getL2() {
+            return l2;
+        }
+
+        public void setL2(Long l2) {
+            this.l2 = l2;
+        }
+
+    }
+
     Person paulaBean = new Person("Paula", "Brilliant", "paula@brilliant.com",
             34, Sex.FEMALE, new Address("Paula street 1", 12345, "P-town",
                     Country.FINLAND));
@@ -68,6 +95,21 @@ public class DefaultConverterFactory extends TestCase {
         assertEquals(24f, tf.getPropertyDataSource().getValue());
     }
 
+    public void testLongConversion() {
+        VaadinSession sess = new AlwaysLockedVaadinSession(null);
+        VaadinSession.setCurrent(sess);
+
+        TextField tf = new TextField();
+        tf.setLocale(new Locale("en", "US"));
+        tf.setPropertyDataSource(new MethodProperty<Integer>(new LongBean(12,
+                1982739187238L), "l2"));
+        assertEquals("1,982,739,187,238", tf.getValue());
+        tf.setValue("1982739187239");
+        assertEquals("1,982,739,187,239", tf.getValue());
+        assertEquals(1982739187239L, tf.getConvertedValue());
+        assertEquals(1982739187239L, tf.getPropertyDataSource().getValue());
+    }
+
     public void testDefaultNumberConversion() {
         VaadinSession app = new AlwaysLockedVaadinSession(null);
         VaadinSession.setCurrent(app);