From 6a2d458d28cc99eb448870714f512d197d9cf2ab Mon Sep 17 00:00:00 2001 From: "denis.magdenkov" Date: Fri, 5 Sep 2014 19:35:40 +0400 Subject: [PATCH] Add new StringTo Converters(#14583) Fixing code review points. Moving converters to server/src Change-Id: I3f2140c7366d514c1c24531a420a1e4eb0dcd086 --- .../StringToBigIntegerConverter.java | 61 +++++++++++++ .../util/converter/StringToByteConverter.java | 89 ++++++++++++++++++ .../converter/StringToShortConverter.java | 90 +++++++++++++++++++ .../TestStringToBigIntegerConverter.java | 53 +++++++++++ .../converter/TestStringToByteConverter.java | 65 ++++++++++++++ .../converter/TestStringToShortConverter.java | 65 ++++++++++++++ 6 files changed, 423 insertions(+) create mode 100644 server/src/com/vaadin/data/util/converter/StringToBigIntegerConverter.java create mode 100644 server/src/com/vaadin/data/util/converter/StringToByteConverter.java create mode 100644 server/src/com/vaadin/data/util/converter/StringToShortConverter.java create mode 100644 server/tests/src/com/vaadin/tests/data/converter/TestStringToBigIntegerConverter.java create mode 100644 server/tests/src/com/vaadin/tests/data/converter/TestStringToByteConverter.java create mode 100644 server/tests/src/com/vaadin/tests/data/converter/TestStringToShortConverter.java 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 index 0000000000..1e830c1fd2 --- /dev/null +++ b/server/src/com/vaadin/data/util/converter/StringToBigIntegerConverter.java @@ -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. + *

+ * Leading and trailing white spaces are ignored when converting from a String. + *

+ *

+ * Override and overwrite {@link #getFormat(Locale)} to use a different format. + *

+ * + * @author Vaadin Ltd + * @since 7.3.1 + */ +public class StringToBigIntegerConverter extends + AbstractStringToNumberConverter { + + @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 targetType, Locale locale) + throws com.vaadin.data.util.converter.Converter.ConversionException { + return (BigInteger) convertToNumber(value, BigInteger.class, locale); + } + + @Override + public Class 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 index 0000000000..b14182dc3f --- /dev/null +++ b/server/src/com/vaadin/data/util/converter/StringToByteConverter.java @@ -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. + *

+ * Override and overwrite {@link #getFormat(Locale)} to use a different format. + *

+ * + * @author Vaadin Ltd + * @since 7.3.1 + */ +public class StringToByteConverter extends + AbstractStringToNumberConverter { + + /** + * 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 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 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 index 0000000000..3761d11227 --- /dev/null +++ b/server/src/com/vaadin/data/util/converter/StringToShortConverter.java @@ -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. + *

+ * Override and overwrite {@link #getFormat(Locale)} to use a different format. + *

+ * + * @author Vaadin Ltd + * @since 7.3.1 + */ +public class StringToShortConverter extends + AbstractStringToNumberConverter { + + /** + * 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 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 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 index 0000000000..641f18c865 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToBigIntegerConverter.java @@ -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 index 0000000000..440d056c06 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToByteConverter.java @@ -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 reverseConverter = new ReverseConverter( + 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 index 0000000000..35547d2570 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToShortConverter.java @@ -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 reverseConverter = new ReverseConverter( + 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); + } +} -- 2.39.5