diff options
author | denis.magdenkov <denis.magdenkov@arcadia.spb.ru> | 2014-09-05 19:35:40 +0400 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2014-09-19 10:02:00 +0000 |
commit | 6a2d458d28cc99eb448870714f512d197d9cf2ab (patch) | |
tree | b16c95c17e6f1a86ddb6fd99080842e6033c9035 /server/src/com/vaadin/data/util | |
parent | 93afaa3065040f32d4c439c58a0a1cd2a8608bf3 (diff) | |
download | vaadin-framework-6a2d458d28cc99eb448870714f512d197d9cf2ab.tar.gz vaadin-framework-6a2d458d28cc99eb448870714f512d197d9cf2ab.zip |
Add new StringTo<Byte, Short, BigInteger> Converters(#14583)
Fixing code review points. Moving converters to server/src
Change-Id: I3f2140c7366d514c1c24531a420a1e4eb0dcd086
Diffstat (limited to 'server/src/com/vaadin/data/util')
3 files changed, 240 insertions, 0 deletions
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. + * <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 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. + * <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 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. + * <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; + } + +} |