diff options
author | Anna Miroshnik <anna.miroshnik@arcadia.spb.ru> | 2014-12-04 15:52:27 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2014-12-08 19:14:31 +0000 |
commit | a28d6c2098e738dfd78e1df929ce689a2ceb6c2a (patch) | |
tree | 9b9e725043d4e6ad3d6f25ee5a28e4e8bf533bab /server | |
parent | bf4e325ac6387cac0afc8096b3ca65b88bf9fdfd (diff) | |
download | vaadin-framework-a28d6c2098e738dfd78e1df929ce689a2ceb6c2a.tar.gz vaadin-framework-a28d6c2098e738dfd78e1df929ce689a2ceb6c2a.zip |
Add new StringTo<Byte, Short, BigInteger> Converters (#14583)
DefaultConverterFactory was updated, added missed types.
Change-Id: I65cffc7f91df6f40e132b45e325e15f029f9848c
Diffstat (limited to 'server')
8 files changed, 54 insertions, 28 deletions
diff --git a/server/src/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java b/server/src/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java index 9054f258c7..81f1a85f89 100644 --- a/server/src/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java +++ b/server/src/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java @@ -88,6 +88,7 @@ public abstract class AbstractStringToNumberConverter<T> implements // Convert "" to null return null; } + return parsedValue; } diff --git a/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java b/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java index 26613c5d02..3a1f1a4252 100644 --- a/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java +++ b/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java @@ -17,6 +17,7 @@ package com.vaadin.data.util.converter; import java.math.BigDecimal; +import java.math.BigInteger; import java.util.Date; import java.util.logging.Logger; @@ -112,6 +113,12 @@ public class DefaultConverterFactory implements ConverterFactory { return new StringToDateConverter(); } else if (Enum.class.isAssignableFrom(sourceType)) { return new StringToEnumConverter(); + } else if (BigInteger.class.isAssignableFrom(sourceType)) { + return new StringToBigIntegerConverter(); + } else if (Short.class.isAssignableFrom(sourceType)) { + return new StringToShortConverter(); + } else if (Byte.class.isAssignableFrom(sourceType)) { + return new StringToByteConverter(); } else { return null; } diff --git a/server/src/com/vaadin/data/util/converter/StringToBigIntegerConverter.java b/server/src/com/vaadin/data/util/converter/StringToBigIntegerConverter.java index 1e830c1fd2..d176ac2e0d 100644 --- a/server/src/com/vaadin/data/util/converter/StringToBigIntegerConverter.java +++ b/server/src/com/vaadin/data/util/converter/StringToBigIntegerConverter.java @@ -15,6 +15,7 @@ */ package com.vaadin.data.util.converter; +import java.math.BigDecimal; import java.math.BigInteger; import java.text.DecimalFormat; import java.text.NumberFormat; @@ -32,7 +33,7 @@ import java.util.Locale; * </p> * * @author Vaadin Ltd - * @since 7.3.1 + * @since */ public class StringToBigIntegerConverter extends AbstractStringToNumberConverter<BigInteger> { @@ -41,7 +42,7 @@ public class StringToBigIntegerConverter extends protected NumberFormat getFormat(Locale locale) { NumberFormat numberFormat = super.getFormat(locale); if (numberFormat instanceof DecimalFormat) { - ((DecimalFormat) numberFormat).setParseIntegerOnly(true); + ((DecimalFormat) numberFormat).setParseBigDecimal(true); } return numberFormat; @@ -51,7 +52,11 @@ public class StringToBigIntegerConverter extends 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); + + BigDecimal bigDecimalValue = (BigDecimal) convertToNumber(value, + BigDecimal.class, locale); + + return (bigDecimalValue != null) ? bigDecimalValue.toBigInteger() : null; } @Override diff --git a/server/src/com/vaadin/data/util/converter/StringToByteConverter.java b/server/src/com/vaadin/data/util/converter/StringToByteConverter.java index b14182dc3f..26f52d108a 100644 --- a/server/src/com/vaadin/data/util/converter/StringToByteConverter.java +++ b/server/src/com/vaadin/data/util/converter/StringToByteConverter.java @@ -28,7 +28,7 @@ import java.util.Locale; * </p> * * @author Vaadin Ltd - * @since 7.3.1 + * @since */ public class StringToByteConverter extends AbstractStringToNumberConverter<Byte> { diff --git a/server/src/com/vaadin/data/util/converter/StringToShortConverter.java b/server/src/com/vaadin/data/util/converter/StringToShortConverter.java index 3761d11227..4ee085286f 100644 --- a/server/src/com/vaadin/data/util/converter/StringToShortConverter.java +++ b/server/src/com/vaadin/data/util/converter/StringToShortConverter.java @@ -28,7 +28,7 @@ import java.util.Locale; * </p> * * @author Vaadin Ltd - * @since 7.3.1 + * @since */ public class StringToShortConverter extends AbstractStringToNumberConverter<Short> { diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToBigIntegerConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToBigIntegerConverter.java index 641f18c865..8d493609fe 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/TestStringToBigIntegerConverter.java +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToBigIntegerConverter.java @@ -15,39 +15,43 @@ */ package com.vaadin.tests.data.converter; -import java.math.BigDecimal; +import java.math.BigInteger; import java.util.Locale; import junit.framework.TestCase; -import com.vaadin.data.util.converter.StringToBigDecimalConverter; +import com.vaadin.data.util.converter.StringToBigIntegerConverter; public class TestStringToBigIntegerConverter extends TestCase { - StringToBigDecimalConverter converter = new StringToBigDecimalConverter(); + StringToBigIntegerConverter converter = new StringToBigIntegerConverter(); public void testNullConversion() { - assertEquals(null, - converter.convertToModel(null, BigDecimal.class, null)); + assertEquals("Null value was converted incorrectly", null, + converter.convertToModel(null, BigInteger.class, null)); } public void testEmptyStringConversion() { - assertEquals(null, converter.convertToModel("", BigDecimal.class, null)); + assertEquals("Empty value was converted incorrectly", null, + converter.convertToModel("", BigInteger.class, null)); } public void testValueParsing() { - BigDecimal converted = converter.convertToModel("10", BigDecimal.class, - null); - BigDecimal expected = new BigDecimal(10); - assertEquals(expected, converted); + String bigInt = "1180591620717411303424"; // 2^70 > 2^63 - 1 + BigInteger converted = converter.convertToModel(bigInt, + BigInteger.class, null); + BigInteger expected = new BigInteger(bigInt); + assertEquals("Value bigger than max long was converted incorrectly", + expected, converted); } public void testValueFormatting() { - BigDecimal bd = new BigDecimal(1000); + BigInteger bd = new BigInteger("1000"); String expected = "1.000"; String converted = converter.convertToPresentation(bd, String.class, Locale.GERMAN); - assertEquals(expected, converted); + assertEquals("Value with specific locale was converted incorrectly", + 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 index 440d056c06..19a68fbfdb 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/TestStringToByteConverter.java +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToByteConverter.java @@ -16,25 +16,28 @@ public class TestStringToByteConverter extends TestCase { converter); public void testNullConversion() { - assertEquals(null, converter.convertToModel(null, Byte.class, null)); + assertEquals("Null value was converted incorrectly", null, + converter.convertToModel(null, Byte.class, null)); } public void testReverseNullConversion() { - assertEquals(null, + assertEquals("Null value reversely was converted incorrectly", null, reverseConverter.convertToModel(null, String.class, null)); } public void testEmptyStringConversion() { - assertEquals(null, converter.convertToModel("", Byte.class, null)); + assertEquals("Empty value was converted incorrectly", null, + converter.convertToModel("", Byte.class, null)); } public void testValueConversion() { - assertEquals(Byte.valueOf((byte) 10), + assertEquals("Byte value was converted incorrectly", + Byte.valueOf((byte) 10), converter.convertToModel("10", Byte.class, null)); } public void testReverseValueConversion() { - assertEquals( + assertEquals("Byte value reversely was converted incorrectly", reverseConverter.convertToModel((byte) 10, String.class, null), "10"); } @@ -43,7 +46,8 @@ public class TestStringToByteConverter extends TestCase { 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); + assertEquals("Min byte value was converted incorrectly", + Byte.MIN_VALUE, b); } public void testValueOutOfRange() { diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToShortConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToShortConverter.java index 35547d2570..542c580025 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/TestStringToShortConverter.java +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToShortConverter.java @@ -16,25 +16,29 @@ public class TestStringToShortConverter extends TestCase { converter); public void testNullConversion() { - assertEquals(null, converter.convertToModel(null, Short.class, null)); + assertEquals("Null value was converted incorrectly", null, + converter.convertToModel(null, Short.class, null)); } public void testReverseNullConversion() { - assertEquals(null, + assertEquals("Null value reversely was converted incorrectly", null, reverseConverter.convertToModel(null, String.class, null)); } public void testEmptyStringConversion() { - assertEquals(null, converter.convertToModel("", Short.class, null)); + assertEquals("Empty value was converted incorrectly", null, + converter.convertToModel("", Short.class, null)); } public void testValueConversion() { - assertEquals(Short.valueOf((short) 10), + assertEquals("Short value was converted incorrectly", + Short.valueOf((short) 10), converter.convertToModel("10", Short.class, null)); } public void testReverseValueConversion() { assertEquals( + "Short value reversely was converted incorrectly", reverseConverter.convertToModel((short) 10, String.class, null), "10"); } @@ -43,7 +47,8 @@ public class TestStringToShortConverter extends TestCase { 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); + assertEquals("Min short value was converted incorrectly", + Short.MIN_VALUE, b); } public void testValueOutOfRange() { |