--- /dev/null
+/*
+ * 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;
+ }
+}
--- /dev/null
+/*
+ * 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;
+ }
+
+}
--- /dev/null
+/*
+ * 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;
+ }
+
+}
--- /dev/null
+/*
+ * 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);
+ }
+}
--- /dev/null
+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);
+ }
+}
--- /dev/null
+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);
+ }
+}