]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add new StringTo<Byte, Short, BigInteger> Converters(#14583)
authordenis.magdenkov <denis.magdenkov@arcadia.spb.ru>
Fri, 5 Sep 2014 15:35:40 +0000 (19:35 +0400)
committerVaadin Code Review <review@vaadin.com>
Fri, 19 Sep 2014 10:02:00 +0000 (10:02 +0000)
Fixing code review points. Moving converters to server/src

Change-Id: I3f2140c7366d514c1c24531a420a1e4eb0dcd086

server/src/com/vaadin/data/util/converter/StringToBigIntegerConverter.java [new file with mode: 0644]
server/src/com/vaadin/data/util/converter/StringToByteConverter.java [new file with mode: 0644]
server/src/com/vaadin/data/util/converter/StringToShortConverter.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/data/converter/TestStringToBigIntegerConverter.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/data/converter/TestStringToByteConverter.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/data/converter/TestStringToShortConverter.java [new file with mode: 0644]

diff --git a/server/src/com/vaadin/data/util/converter/StringToBigIntegerConverter.java b/server/src/com/vaadin/data/util/converter/StringToBigIntegerConverter.java
new file mode 100644 (file)
index 0000000..1e830c1
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2000-2014 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.math.BigInteger;
+import java.text.DecimalFormat;
+import java.text.NumberFormat;
+import java.util.Locale;
+
+/**
+ * A converter that converts from {@link String} to {@link BigInteger} and back.
+ * Uses the given locale and a {@link NumberFormat} instance for formatting and
+ * parsing.
+ * <p>
+ * Leading and trailing white spaces are ignored when converting from a String.
+ * </p>
+ * <p>
+ * Override and overwrite {@link #getFormat(Locale)} to use a different format.
+ * </p>
+ * 
+ * @author Vaadin Ltd
+ * @since 7.3.1
+ */
+public class StringToBigIntegerConverter extends
+        AbstractStringToNumberConverter<BigInteger> {
+
+    @Override
+    protected NumberFormat getFormat(Locale locale) {
+        NumberFormat numberFormat = super.getFormat(locale);
+        if (numberFormat instanceof DecimalFormat) {
+            ((DecimalFormat) numberFormat).setParseIntegerOnly(true);
+        }
+
+        return numberFormat;
+    }
+
+    @Override
+    public BigInteger convertToModel(String value,
+            Class<? extends BigInteger> targetType, Locale locale)
+            throws com.vaadin.data.util.converter.Converter.ConversionException {
+        return (BigInteger) convertToNumber(value, BigInteger.class, locale);
+    }
+
+    @Override
+    public Class<BigInteger> getModelType() {
+        return BigInteger.class;
+    }
+}
diff --git a/server/src/com/vaadin/data/util/converter/StringToByteConverter.java b/server/src/com/vaadin/data/util/converter/StringToByteConverter.java
new file mode 100644 (file)
index 0000000..b14182d
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2000-2014 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 Byte} 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.3.1
+ */
+public class StringToByteConverter extends
+        AbstractStringToNumberConverter<Byte> {
+
+    /**
+     * Returns the format used by
+     * {@link #convertToPresentation(Byte, 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 Byte convertToModel(String value, Class<? extends Byte> targetType,
+            Locale locale) throws ConversionException {
+        Number n = convertToNumber(value, targetType, locale);
+
+        if (n == null) {
+            return null;
+        }
+
+        byte byteValue = n.byteValue();
+        if (byteValue == n.longValue()) {
+            return byteValue;
+        }
+
+        throw new ConversionException("Could not convert '" + value + "' to "
+                + Byte.class.getName() + ": value out of range");
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see com.vaadin.data.util.converter.Converter#getModelType()
+     */
+    @Override
+    public Class<Byte> getModelType() {
+        return Byte.class;
+    }
+
+}
diff --git a/server/src/com/vaadin/data/util/converter/StringToShortConverter.java b/server/src/com/vaadin/data/util/converter/StringToShortConverter.java
new file mode 100644 (file)
index 0000000..3761d11
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2000-2014 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 Short} 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.3.1
+ */
+public class StringToShortConverter extends
+        AbstractStringToNumberConverter<Short> {
+
+    /**
+     * Returns the format used by
+     * {@link #convertToPresentation(Short, 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 Short convertToModel(String value,
+            Class<? extends Short> targetType, Locale locale)
+            throws ConversionException {
+        Number n = convertToNumber(value, targetType, locale);
+
+        if (n == null) {
+            return null;
+        }
+
+        short shortValue = n.shortValue();
+        if (shortValue == n.longValue()) {
+            return shortValue;
+        }
+
+        throw new ConversionException("Could not convert '" + value + "' to "
+                + Short.class.getName() + ": value out of range");
+
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see com.vaadin.data.util.converter.Converter#getModelType()
+     */
+    @Override
+    public Class<Short> getModelType() {
+        return Short.class;
+    }
+
+}
diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToBigIntegerConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToBigIntegerConverter.java
new file mode 100644 (file)
index 0000000..641f18c
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2000-2014 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.tests.data.converter;
+
+import java.math.BigDecimal;
+import java.util.Locale;
+
+import junit.framework.TestCase;
+
+import com.vaadin.data.util.converter.StringToBigDecimalConverter;
+
+public class TestStringToBigIntegerConverter extends TestCase {
+
+    StringToBigDecimalConverter converter = new StringToBigDecimalConverter();
+
+    public void testNullConversion() {
+        assertEquals(null,
+                converter.convertToModel(null, BigDecimal.class, null));
+    }
+
+    public void testEmptyStringConversion() {
+        assertEquals(null, converter.convertToModel("", BigDecimal.class, null));
+    }
+
+    public void testValueParsing() {
+        BigDecimal converted = converter.convertToModel("10", BigDecimal.class,
+                null);
+        BigDecimal expected = new BigDecimal(10);
+        assertEquals(expected, converted);
+    }
+
+    public void testValueFormatting() {
+        BigDecimal bd = new BigDecimal(1000);
+        String expected = "1.000";
+
+        String converted = converter.convertToPresentation(bd, String.class,
+                Locale.GERMAN);
+        assertEquals(expected, converted);
+    }
+}
diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToByteConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToByteConverter.java
new file mode 100644 (file)
index 0000000..440d056
--- /dev/null
@@ -0,0 +1,65 @@
+package com.vaadin.tests.data.converter;
+
+import junit.framework.TestCase;
+
+import org.junit.Assert;
+
+import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.Converter.ConversionException;
+import com.vaadin.data.util.converter.ReverseConverter;
+import com.vaadin.data.util.converter.StringToByteConverter;
+
+public class TestStringToByteConverter extends TestCase {
+
+    StringToByteConverter converter = new StringToByteConverter();
+    Converter<Byte, String> reverseConverter = new ReverseConverter<Byte, String>(
+            converter);
+
+    public void testNullConversion() {
+        assertEquals(null, converter.convertToModel(null, Byte.class, null));
+    }
+
+    public void testReverseNullConversion() {
+        assertEquals(null,
+                reverseConverter.convertToModel(null, String.class, null));
+    }
+
+    public void testEmptyStringConversion() {
+        assertEquals(null, converter.convertToModel("", Byte.class, null));
+    }
+
+    public void testValueConversion() {
+        assertEquals(Byte.valueOf((byte) 10),
+                converter.convertToModel("10", Byte.class, null));
+    }
+
+    public void testReverseValueConversion() {
+        assertEquals(
+                reverseConverter.convertToModel((byte) 10, String.class, null),
+                "10");
+    }
+
+    public void testExtremeByteValueConversion() {
+        byte b = converter.convertToModel("127", Byte.class, null);
+        Assert.assertEquals(Byte.MAX_VALUE, b);
+        b = converter.convertToModel("-128", Byte.class, null);
+        assertEquals(Byte.MIN_VALUE, b);
+    }
+
+    public void testValueOutOfRange() {
+        Double[] values = new Double[] { Byte.MAX_VALUE * 2.0,
+                Byte.MIN_VALUE * 2.0, Long.MAX_VALUE * 2.0,
+                Long.MIN_VALUE * 2.0 };
+
+        boolean accepted = false;
+        for (Number value : values) {
+            try {
+                converter.convertToModel(String.format("%.0f", value),
+                        Byte.class, null);
+                accepted = true;
+            } catch (ConversionException expected) {
+            }
+        }
+        assertFalse("Accepted value outside range of int", accepted);
+    }
+}
diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToShortConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToShortConverter.java
new file mode 100644 (file)
index 0000000..35547d2
--- /dev/null
@@ -0,0 +1,65 @@
+package com.vaadin.tests.data.converter;
+
+import junit.framework.TestCase;
+
+import org.junit.Assert;
+
+import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.Converter.ConversionException;
+import com.vaadin.data.util.converter.ReverseConverter;
+import com.vaadin.data.util.converter.StringToShortConverter;
+
+public class TestStringToShortConverter extends TestCase {
+
+    StringToShortConverter converter = new StringToShortConverter();
+    Converter<Short, String> reverseConverter = new ReverseConverter<Short, String>(
+            converter);
+
+    public void testNullConversion() {
+        assertEquals(null, converter.convertToModel(null, Short.class, null));
+    }
+
+    public void testReverseNullConversion() {
+        assertEquals(null,
+                reverseConverter.convertToModel(null, String.class, null));
+    }
+
+    public void testEmptyStringConversion() {
+        assertEquals(null, converter.convertToModel("", Short.class, null));
+    }
+
+    public void testValueConversion() {
+        assertEquals(Short.valueOf((short) 10),
+                converter.convertToModel("10", Short.class, null));
+    }
+
+    public void testReverseValueConversion() {
+        assertEquals(
+                reverseConverter.convertToModel((short) 10, String.class, null),
+                "10");
+    }
+
+    public void testExtremeShortValueConversion() {
+        short b = converter.convertToModel("32767", Short.class, null);
+        Assert.assertEquals(Short.MAX_VALUE, b);
+        b = converter.convertToModel("-32768", Short.class, null);
+        assertEquals(Short.MIN_VALUE, b);
+    }
+
+    public void testValueOutOfRange() {
+        Double[] values = new Double[] { Integer.MAX_VALUE * 2.0,
+                Integer.MIN_VALUE * 2.0, Long.MAX_VALUE * 2.0,
+                Long.MIN_VALUE * 2.0 };
+
+        boolean accepted = false;
+        for (Number value : values) {
+            try {
+                converter.convertToModel(String.format("%.0f", value),
+                        Short.class, null);
+                accepted = true;
+            } catch (ConversionException expected) {
+            }
+        }
+        assertFalse("Accepted value outside range of int", accepted);
+    }
+}