summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java2
-rw-r--r--server/src/com/vaadin/data/util/converter/StringToLongConverter.java78
-rw-r--r--server/tests/src/com/vaadin/tests/data/converter/TestStringToLongConverter.java68
-rw-r--r--server/tests/src/com/vaadin/tests/server/component/abstractfield/DefaultConverterFactory.java42
4 files changed, 190 insertions, 0 deletions
diff --git a/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java b/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java
index bbd3945a37..cadfdcc774 100644
--- a/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java
+++ b/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java
@@ -101,6 +101,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 (Boolean.class.isAssignableFrom(sourceType)) {
return new StringToBooleanConverter();
} else if (Number.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
index 0000000000..3532336787
--- /dev/null
+++ b/server/src/com/vaadin/data/util/converter/StringToLongConverter.java
@@ -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
index 0000000000..18e2ed06c0
--- /dev/null
+++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToLongConverter.java
@@ -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);
+
+ }
+}
diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractfield/DefaultConverterFactory.java b/server/tests/src/com/vaadin/tests/server/component/abstractfield/DefaultConverterFactory.java
index bac024725f..99397e9e8f 100644
--- a/server/tests/src/com/vaadin/tests/server/component/abstractfield/DefaultConverterFactory.java
+++ b/server/tests/src/com/vaadin/tests/server/component/abstractfield/DefaultConverterFactory.java
@@ -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);