From 192e93a814f05dc8c5cc7a01b7264bc0a8a9dec7 Mon Sep 17 00:00:00 2001
From: Artur Signell
Date: Wed, 10 Aug 2016 10:02:21 +0300
Subject: Move and rename converters into
com.vaadin.legacy.data.util.converter.Legacy*
Change-Id: I75fd33e66c8c5c265bc60cce58ff639a5d5642ab
---
.../com/vaadin/data/util/PropertyFormatter.java | 4 +-
.../converter/AbstractStringToNumberConverter.java | 123 ----------
.../com/vaadin/data/util/converter/Converter.java | 176 --------------
.../data/util/converter/ConverterFactory.java | 33 ---
.../vaadin/data/util/converter/ConverterUtil.java | 261 ---------------------
.../data/util/converter/DateToLongConverter.java | 89 -------
.../util/converter/DateToSqlDateConverter.java | 81 -------
.../util/converter/DefaultConverterFactory.java | 127 ----------
.../data/util/converter/ReverseConverter.java | 96 --------
.../converter/StringToBigDecimalConverter.java | 60 -----
.../converter/StringToBigIntegerConverter.java | 67 ------
.../util/converter/StringToBooleanConverter.java | 167 -------------
.../data/util/converter/StringToByteConverter.java | 89 -------
.../converter/StringToCollectionConverter.java | 243 -------------------
.../data/util/converter/StringToDateConverter.java | 131 -----------
.../util/converter/StringToDoubleConverter.java | 64 -----
.../data/util/converter/StringToEnumConverter.java | 160 -------------
.../util/converter/StringToFloatConverter.java | 64 -----
.../util/converter/StringToIntegerConverter.java | 94 --------
.../data/util/converter/StringToLongConverter.java | 78 ------
.../util/converter/StringToShortConverter.java | 90 -------
.../data/util/sqlcontainer/ColumnProperty.java | 2 +-
.../LegacyAbstractStringToNumberConverter.java | 123 ++++++++++
.../data/util/converter/LegacyConverter.java | 176 ++++++++++++++
.../util/converter/LegacyConverterFactory.java | 33 +++
.../data/util/converter/LegacyConverterUtil.java | 261 +++++++++++++++++++++
.../util/converter/LegacyDateToLongConverter.java | 89 +++++++
.../converter/LegacyDateToSqlDateConverter.java | 81 +++++++
.../converter/LegacyDefaultConverterFactory.java | 127 ++++++++++
.../util/converter/LegacyReverseConverter.java | 96 ++++++++
.../LegacyStringToBigDecimalConverter.java | 60 +++++
.../LegacyStringToBigIntegerConverter.java | 67 ++++++
.../converter/LegacyStringToBooleanConverter.java | 167 +++++++++++++
.../converter/LegacyStringToByteConverter.java | 89 +++++++
.../LegacyStringToCollectionConverter.java | 243 +++++++++++++++++++
.../converter/LegacyStringToDateConverter.java | 131 +++++++++++
.../converter/LegacyStringToDoubleConverter.java | 64 +++++
.../converter/LegacyStringToEnumConverter.java | 160 +++++++++++++
.../converter/LegacyStringToFloatConverter.java | 64 +++++
.../converter/LegacyStringToIntegerConverter.java | 94 ++++++++
.../converter/LegacyStringToLongConverter.java | 78 ++++++
.../converter/LegacyStringToShortConverter.java | 90 +++++++
.../data/validator/LegacyDoubleValidator.java | 4 +-
.../data/validator/LegacyIntegerValidator.java | 4 +-
.../com/vaadin/legacy/ui/LegacyAbstractField.java | 42 ++--
.../main/java/com/vaadin/server/VaadinSession.java | 30 +--
.../main/java/com/vaadin/ui/AbstractSelect.java | 10 +-
server/src/main/java/com/vaadin/ui/DateField.java | 10 +-
server/src/main/java/com/vaadin/ui/Grid.java | 38 +--
server/src/main/java/com/vaadin/ui/Label.java | 18 +-
server/src/main/java/com/vaadin/ui/Table.java | 18 +-
.../ui/declarative/DesignAttributeHandler.java | 4 +-
.../com/vaadin/ui/declarative/DesignFormatter.java | 56 ++---
.../converters/DesignDateConverter.java | 8 +-
.../converters/DesignEnumConverter.java | 8 +-
.../converters/DesignObjectConverter.java | 8 +-
.../converters/DesignResourceConverter.java | 20 +-
.../converters/DesignShortcutActionConverter.java | 8 +-
.../converters/DesignTimeZoneConverter.java | 8 +-
.../converters/DesignToStringConverter.java | 20 +-
60 files changed, 2453 insertions(+), 2453 deletions(-)
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/Converter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/ConverterFactory.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/ConverterUtil.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/DefaultConverterFactory.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/ReverseConverter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToByteConverter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToCollectionConverter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToEnumConverter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java
delete mode 100644 server/src/main/java/com/vaadin/data/util/converter/StringToShortConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyAbstractStringToNumberConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterFactory.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterUtil.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToLongConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToSqlDateConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDefaultConverterFactory.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyReverseConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigDecimalConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigIntegerConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBooleanConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToByteConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToCollectionConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDateConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDoubleConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToEnumConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToFloatConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToIntegerConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToLongConverter.java
create mode 100644 server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToShortConverter.java
(limited to 'server/src/main')
diff --git a/server/src/main/java/com/vaadin/data/util/PropertyFormatter.java b/server/src/main/java/com/vaadin/data/util/PropertyFormatter.java
index e188ce8bc3..b3df70534b 100644
--- a/server/src/main/java/com/vaadin/data/util/PropertyFormatter.java
+++ b/server/src/main/java/com/vaadin/data/util/PropertyFormatter.java
@@ -16,7 +16,7 @@
package com.vaadin.data.util;
import com.vaadin.data.Property;
-import com.vaadin.data.util.converter.Converter;
+import com.vaadin.legacy.data.util.converter.LegacyConverter;
/**
* Formatting proxy for a {@link Property}.
@@ -46,7 +46,7 @@ import com.vaadin.data.util.converter.Converter;
* type of the underlying property (a PropertyFormatter is always a
* Property<String>)
*
- * @deprecated As of 7.0, replaced by {@link Converter}
+ * @deprecated As of 7.0, replaced by {@link LegacyConverter}
* @author Vaadin Ltd.
* @since 5.3.0
*/
diff --git a/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java b/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java
deleted file mode 100644
index 81f1a85f89..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/AbstractStringToNumberConverter.java
+++ /dev/null
@@ -1,123 +0,0 @@
-/*
- * 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.text.ParsePosition;
-import java.util.Locale;
-
-/**
- * A converter that converts from the number type T to {@link String} and back.
- * Uses the given locale and {@link NumberFormat} for formatting and parsing.
- * Automatically trims the input string, removing any leading and trailing white
- * space.
- *
- * Override and overwrite {@link #getFormat(Locale)} to use a different format.
- *
- *
- * @author Vaadin Ltd
- * @since 7.1
- */
-public abstract class AbstractStringToNumberConverter implements
- Converter {
-
- /**
- * Returns the format used by {@link #convertToPresentation(Object, Locale)}
- * and {@link #convertToModel(Object, Locale)}.
- *
- * @param locale
- * The locale to use
- * @return A NumberFormat instance
- * @since 7.1
- */
- protected NumberFormat getFormat(Locale locale) {
- if (locale == null) {
- locale = Locale.getDefault();
- }
-
- return NumberFormat.getNumberInstance(locale);
- }
-
- /**
- * Convert the value to a Number using the given locale and
- * {@link #getFormat(Locale)}.
- *
- * @param value
- * The value to convert
- * @param locale
- * The locale to use for conversion
- * @return The converted value
- * @throws ConversionException
- * If there was a problem converting the value
- * @since 7.1
- */
- protected Number convertToNumber(String value,
- Class extends Number> targetType, Locale locale)
- throws ConversionException {
- if (value == null) {
- return null;
- }
-
- // Remove leading and trailing white space
- value = value.trim();
-
- // Parse and detect errors. If the full string was not used, it is
- // an error.
- ParsePosition parsePosition = new ParsePosition(0);
- Number parsedValue = getFormat(locale).parse(value, parsePosition);
- if (parsePosition.getIndex() != value.length()) {
- throw new ConversionException("Could not convert '" + value
- + "' to " + getModelType().getName());
- }
-
- if (parsedValue == null) {
- // Convert "" to null
- return null;
- }
-
- return parsedValue;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
- * .Object, java.util.Locale)
- */
- @Override
- public String convertToPresentation(T value,
- Class extends String> targetType, Locale locale)
- throws ConversionException {
- if (value == null) {
- return null;
- }
-
- return getFormat(locale).format(value);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.data.util.converter.Converter#getPresentationType()
- */
- @Override
- public Class getPresentationType() {
- return String.class;
- }
-
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/Converter.java b/server/src/main/java/com/vaadin/data/util/converter/Converter.java
deleted file mode 100644
index a7188ebb33..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/Converter.java
+++ /dev/null
@@ -1,176 +0,0 @@
-/*
- * 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.io.Serializable;
-import java.util.Locale;
-
-/**
- * Interface that implements conversion between a model and a presentation type.
- *
- * Typically {@link #convertToPresentation(Object, Class, Locale)} and
- * {@link #convertToModel(Object, Class, Locale)} should be symmetric so that
- * chaining these together returns the original result for all input but this is
- * not a requirement.
- *
- *
- * Converters must not have any side effects (never update UI from inside a
- * converter).
- *
- *
- * All Converters must be stateless and thread safe.
- *
- *
- * If conversion of a value fails, a {@link ConversionException} is thrown.
- *
- *
- * @param
- * The presentation type. Must be compatible with what
- * {@link #getPresentationType()} returns.
- * @param
- * The model type. Must be compatible with what
- * {@link #getModelType()} returns.
- * @author Vaadin Ltd.
- * @since 7.0
- */
-public interface Converter extends Serializable {
-
- /**
- * Converts the given value from target type to source type.
- *
- * A converter can optionally use locale to do the conversion.
- *
- * A converter should in most cases be symmetric so chaining
- * {@link #convertToPresentation(Object, Class, Locale)} and
- * {@link #convertToModel(Object, Class, Locale)} should return the original
- * value.
- *
- * @param value
- * The value to convert, compatible with the target type. Can be
- * null
- * @param targetType
- * The requested type of the return value
- * @param locale
- * The locale to use for conversion. Can be null.
- * @return The converted value compatible with the source type
- * @throws ConversionException
- * If the value could not be converted
- */
- public MODEL convertToModel(PRESENTATION value,
- Class extends MODEL> targetType, Locale locale)
- throws ConversionException;
-
- /**
- * Converts the given value from source type to target type.
- *
- * A converter can optionally use locale to do the conversion.
- *
- * A converter should in most cases be symmetric so chaining
- * {@link #convertToPresentation(Object, Class, Locale)} and
- * {@link #convertToModel(Object, Class, Locale)} should return the original
- * value.
- *
- * @param value
- * The value to convert, compatible with the target type. Can be
- * null
- * @param targetType
- * The requested type of the return value
- * @param locale
- * The locale to use for conversion. Can be null.
- * @return The converted value compatible with the source type
- * @throws ConversionException
- * If the value could not be converted
- */
- public PRESENTATION convertToPresentation(MODEL value,
- Class extends PRESENTATION> targetType, Locale locale)
- throws ConversionException;
-
- /**
- * The source type of the converter.
- *
- * Values of this type can be passed to
- * {@link #convertToPresentation(Object, Class, Locale)}.
- *
- * @return The source type
- */
- public Class getModelType();
-
- /**
- * The target type of the converter.
- *
- * Values of this type can be passed to
- * {@link #convertToModel(Object, Class, Locale)}.
- *
- * @return The target type
- */
- public Class getPresentationType();
-
- /**
- * An exception that signals that the value passed to
- * {@link Converter#convertToPresentation(Object, Class, Locale)} or
- * {@link Converter#convertToModel(Object, Class, Locale)} could not be
- * converted.
- *
- * @author Vaadin Ltd
- * @since 7.0
- */
- public static class ConversionException extends RuntimeException {
-
- /**
- * Constructs a new ConversionException without a detail
- * message.
- */
- public ConversionException() {
- }
-
- /**
- * Constructs a new ConversionException with the specified
- * detail message.
- *
- * @param msg
- * the detail message
- */
- public ConversionException(String msg) {
- super(msg);
- }
-
- /**
- * Constructs a new {@code ConversionException} with the specified
- * cause.
- *
- * @param cause
- * The cause of the the exception
- */
- public ConversionException(Throwable cause) {
- super(cause);
- }
-
- /**
- * Constructs a new ConversionException with the specified
- * detail message and cause.
- *
- * @param message
- * the detail message
- * @param cause
- * The cause of the the exception
- */
- public ConversionException(String message, Throwable cause) {
- super(message, cause);
- }
- }
-
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/ConverterFactory.java b/server/src/main/java/com/vaadin/data/util/converter/ConverterFactory.java
deleted file mode 100644
index 0280be4a73..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/ConverterFactory.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * 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.io.Serializable;
-
-/**
- * Factory interface for providing Converters based on a presentation type and a
- * model type.
- *
- * @author Vaadin Ltd.
- * @since 7.0
- *
- */
-public interface ConverterFactory extends Serializable {
- public Converter createConverter(
- Class presentationType, Class modelType);
-
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/ConverterUtil.java b/server/src/main/java/com/vaadin/data/util/converter/ConverterUtil.java
deleted file mode 100644
index 91e11a6222..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/ConverterUtil.java
+++ /dev/null
@@ -1,261 +0,0 @@
-/*
- * 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.io.Serializable;
-import java.util.Locale;
-
-import com.vaadin.server.VaadinSession;
-
-public class ConverterUtil implements Serializable {
-
- /**
- * Finds a converter that can convert from the given presentation type to
- * the given model type and back. Uses the given application to find a
- * {@link ConverterFactory} or, if application is null, uses the
- * {@link VaadinSession#getCurrent()}.
- *
- * @param
- * the presentation type
- * @param
- * the model type
- * @param presentationType
- * the presentation type
- * @param modelType
- * the model type
- * @param session
- * the session to use to find a ConverterFactory or null to use
- * the current session
- * @return a Converter capable of converting between the given types or null
- * if no converter was found
- */
- public static Converter getConverter(
- Class presentationType,
- Class modelType, VaadinSession session) {
- Converter converter = null;
- if (session == null) {
- session = VaadinSession.getCurrent();
- }
-
- if (session != null) {
- ConverterFactory factory = session.getConverterFactory();
- converter = factory.createConverter(presentationType, modelType);
- }
- return converter;
-
- }
-
- /**
- * Convert the given value from the data source type to the UI type.
- *
- * @param modelValue
- * the model value to convert
- * @param presentationType
- * the type of the presentation value
- * @param converter
- * the converter to use
- * @param locale
- * the locale to use for conversion
- * @param
- * the presentation type
- * @param
- * the model type
- *
- * @return the converted value, compatible with the presentation type, or
- * the original value if its type is compatible and no converter is
- * set.
- * @throws Converter.ConversionException
- * if there was a problem converting the value
- */
- @SuppressWarnings("unchecked")
- public static PRESENTATIONTYPE convertFromModel(
- MODELTYPE modelValue,
- Class extends PRESENTATIONTYPE> presentationType,
- Converter converter, Locale locale)
- throws Converter.ConversionException {
- if (converter != null) {
- /*
- * If there is a converter, always use it. It must convert or throw
- * an exception.
- */
- PRESENTATIONTYPE presentation = converter.convertToPresentation(
- modelValue, presentationType, locale);
- if (presentation != null
- && !presentationType.isInstance(presentation)) {
- throw new Converter.ConversionException(
- "Converter returned an object of type "
- + presentation.getClass().getName()
- + " when expecting "
- + presentationType.getName());
- }
-
- return presentation;
- }
- if (modelValue == null) {
- // Null should always be passed through the converter but if there
- // is no converter we can safely return null
- return null;
- }
-
- if (presentationType.isAssignableFrom(modelValue.getClass())) {
- return (PRESENTATIONTYPE) modelValue;
- } else {
- throw new Converter.ConversionException(
- "Unable to convert value of type "
- + modelValue.getClass().getName()
- + " to presentation type "
- + presentationType
- + ". No converter is set and the types are not compatible.");
- }
- }
-
- /**
- * Convert the given value from the presentation (UI) type to model (data
- * source) type.
- *
- * @param presentationValue
- * the presentation value to convert
- * @param modelType
- * the type of the model
- * @param converter
- * the converter to use
- * @param locale
- * the locale to use for conversion
- * @param
- * the presentation type
- * @param
- * the model type
- *
- * @return the converted value, compatible with the model type, or the
- * original value if its type is compatible and no converter is set.
- * @throws Converter.ConversionException
- * if there was a problem converting the value
- */
- public static MODELTYPE convertToModel(
- PRESENTATIONTYPE presentationValue, Class modelType,
- Converter converter, Locale locale)
- throws Converter.ConversionException {
- if (converter != null) {
- /*
- * If there is a converter, always use it. It must convert or throw
- * an exception.
- */
- MODELTYPE model = converter.convertToModel(presentationValue,
- modelType, locale);
- if (model != null && !modelType.isInstance(model)) {
- throw new Converter.ConversionException(
- "Converter returned an object of type "
- + model.getClass().getName()
- + " when expecting " + modelType.getName());
- }
-
- return model;
- }
-
- if (presentationValue == null) {
- // Null should always be passed through the converter but if there
- // is no converter we can safely return null
- return null;
- }
-
- if (modelType == null) {
- // No model type, return original value
- return (MODELTYPE) presentationValue;
- } else if (modelType.isAssignableFrom(presentationValue.getClass())) {
- // presentation type directly compatible with model type
- return modelType.cast(presentationValue);
- } else {
- throw new Converter.ConversionException(
- "Unable to convert value of type "
- + presentationValue.getClass().getName()
- + " to model type "
- + modelType
- + ". No converter is set and the types are not compatible.");
- }
-
- }
-
- /**
- * Checks if the given converter can handle conversion between the given
- * presentation and model type. Does strict type checking and only returns
- * true if the converter claims it can handle exactly the given types.
- *
- * @see #canConverterPossiblyHandle(Converter, Class, Class)
- *
- * @param converter
- * The converter to check. If this is null the result is always
- * false.
- * @param presentationType
- * The presentation type
- * @param modelType
- * The model type
- * @return true if the converter supports conversion between the given
- * presentation and model type, false otherwise
- */
- public static boolean canConverterHandle(Converter, ?> converter,
- Class> presentationType, Class> modelType) {
- if (converter == null) {
- return false;
- }
-
- if (modelType != converter.getModelType()) {
- return false;
- }
- if (presentationType != converter.getPresentationType()) {
- return false;
- }
-
- return true;
- }
-
- /**
- * Checks if it possible that the given converter can handle conversion
- * between the given presentation and model type somehow.
- *
- * @param converter
- * The converter to check. If this is null the result is always
- * false.
- * @param presentationType
- * The presentation type
- * @param modelType
- * The model type
- * @return true if the converter possibly support conversion between the
- * given presentation and model type, false otherwise
- */
- public static boolean canConverterPossiblyHandle(Converter, ?> converter,
- Class> presentationType, Class> modelType) {
- if (converter == null) {
- return false;
- }
- Class> converterModelType = converter.getModelType();
-
- if (!modelType.isAssignableFrom(converterModelType)
- && !converterModelType.isAssignableFrom(modelType)) {
- // model types are not compatible in any way
- return false;
- }
-
- Class> converterPresentationType = converter.getPresentationType();
- if (!presentationType.isAssignableFrom(converterPresentationType)
- && !converterPresentationType
- .isAssignableFrom(presentationType)) {
- // presentation types are not compatible in any way
- return false;
- }
-
- return true;
- }
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java b/server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java
deleted file mode 100644
index fffe80352e..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/DateToLongConverter.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * 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.util.Date;
-import java.util.Locale;
-
-/**
- * A converter that converts from {@link Long} to {@link Date} and back.
- *
- * @author Vaadin Ltd
- * @since 7.0
- */
-public class DateToLongConverter implements Converter {
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
- * java.lang.Class, java.util.Locale)
- */
- @Override
- public Long convertToModel(Date value, Class extends Long> targetType,
- Locale locale) {
- if (value == null) {
- return null;
- }
-
- return value.getTime();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
- * .Object, java.lang.Class, java.util.Locale)
- */
- @Override
- public Date convertToPresentation(Long value,
- Class extends Date> targetType, Locale locale) {
- if (targetType != getPresentationType()) {
- throw new ConversionException("Converter only supports "
- + getPresentationType().getName() + " (targetType was "
- + targetType.getName() + ")");
- }
- if (value == null) {
- return null;
- }
-
- return new Date(value);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.data.util.converter.Converter#getModelType()
- */
- @Override
- public Class getModelType() {
- return Long.class;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.data.util.converter.Converter#getPresentationType()
- */
- @Override
- public Class getPresentationType() {
- return Date.class;
- }
-
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java b/server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java
deleted file mode 100644
index 6a7e8327a1..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/DateToSqlDateConverter.java
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * 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.util.Date;
-import java.util.Locale;
-
-/**
- * Converter for handling conversion between {@link java.util.Date} and
- * {@link java.sql.Date}. This is used when a PopupDateField or InlineDateField
- * is connected to a java.sql.Date property, typically through a JPAContainer or
- * SQLContainer. Note that information (time information) is lost when
- * converting from {@link java.util.Date} to {@link java.sql.Date}.
- *
- * @since 7.1
- * @author Vaadin Ltd
- */
-public class DateToSqlDateConverter implements Converter {
-
- @Override
- public java.sql.Date convertToModel(Date value,
- Class extends java.sql.Date> targetType, Locale locale)
- throws ConversionException {
- if (targetType != getModelType()) {
- throw new ConversionException("Converter only supports "
- + getModelType().getName() + " (targetType was "
- + targetType.getName() + ")");
- }
-
- if (value == null) {
- return null;
- }
-
- return new java.sql.Date(value.getTime());
- }
-
- @Override
- public Date convertToPresentation(java.sql.Date value,
- Class extends Date> targetType, Locale locale)
- throws ConversionException {
- if (targetType != getPresentationType()) {
- throw new ConversionException("Converter only supports "
- + getPresentationType().getName() + " (targetType was "
- + targetType.getName() + ")");
- }
-
- if (value == null) {
- return null;
- }
-
- return new Date(value.getTime());
- }
-
- @Override
- public Class getModelType() {
- return java.sql.Date.class;
- }
-
- @Override
- public Class getPresentationType() {
- return Date.class;
- }
-
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/DefaultConverterFactory.java b/server/src/main/java/com/vaadin/data/util/converter/DefaultConverterFactory.java
deleted file mode 100644
index 3a1f1a4252..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/DefaultConverterFactory.java
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * 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.BigDecimal;
-import java.math.BigInteger;
-import java.util.Date;
-import java.util.logging.Logger;
-
-import com.vaadin.server.VaadinSession;
-
-/**
- * Default implementation of {@link ConverterFactory}. Provides converters for
- * standard types like {@link String}, {@link Double} and {@link Date}.
- *
- * Custom converters can be provided by extending this class and using
- * {@link VaadinSession#setConverterFactory(ConverterFactory)}.
- *
- *
- * @author Vaadin Ltd
- * @since 7.0
- */
-public class DefaultConverterFactory implements ConverterFactory {
-
- private final static Logger log = Logger
- .getLogger(DefaultConverterFactory.class.getName());
-
- @Override
- public Converter createConverter(
- Class presentationType, Class modelType) {
- Converter converter = findConverter(
- presentationType, modelType);
- if (converter != null) {
- log.finest(getClass().getName() + " created a "
- + converter.getClass());
- return converter;
- }
-
- // Try to find a reverse converter
- Converter reverseConverter = findConverter(
- modelType, presentationType);
- if (reverseConverter != null) {
- log.finest(getClass().getName() + " created a reverse "
- + reverseConverter.getClass());
- return new ReverseConverter(reverseConverter);
- }
-
- log.finest(getClass().getName() + " could not find a converter for "
- + presentationType.getName() + " to " + modelType.getName()
- + " conversion");
- return null;
-
- }
-
- protected Converter findConverter(
- Class presentationType, Class modelType) {
- if (presentationType == String.class) {
- // TextField converters and more
- Converter converter = (Converter) createStringConverter(modelType);
- if (converter != null) {
- return converter;
- }
- } else if (presentationType == Date.class) {
- // DateField converters and more
- Converter converter = (Converter) createDateConverter(modelType);
- if (converter != null) {
- return converter;
- }
- }
-
- return null;
-
- }
-
- protected Converter createDateConverter(Class> sourceType) {
- if (Long.class.isAssignableFrom(sourceType)) {
- return new DateToLongConverter();
- } else if (java.sql.Date.class.isAssignableFrom(sourceType)) {
- return new DateToSqlDateConverter();
- } else {
- return null;
- }
- }
-
- protected Converter createStringConverter(Class> sourceType) {
- if (Double.class.isAssignableFrom(sourceType)) {
- return new StringToDoubleConverter();
- } else if (Float.class.isAssignableFrom(sourceType)) {
- return new StringToFloatConverter();
- } else if (Integer.class.isAssignableFrom(sourceType)) {
- return new StringToIntegerConverter();
- } else if (Long.class.isAssignableFrom(sourceType)) {
- return new StringToLongConverter();
- } else if (BigDecimal.class.isAssignableFrom(sourceType)) {
- return new StringToBigDecimalConverter();
- } else if (Boolean.class.isAssignableFrom(sourceType)) {
- return new StringToBooleanConverter();
- } else if (Date.class.isAssignableFrom(sourceType)) {
- 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/main/java/com/vaadin/data/util/converter/ReverseConverter.java b/server/src/main/java/com/vaadin/data/util/converter/ReverseConverter.java
deleted file mode 100644
index 2495dc631a..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/ReverseConverter.java
+++ /dev/null
@@ -1,96 +0,0 @@
-/*
- * 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.util.Locale;
-
-/**
- * A converter that wraps another {@link Converter} and reverses source and
- * target types.
- *
- * @param
- * The source type
- * @param
- * The target type
- *
- * @author Vaadin Ltd
- * @since 7.0
- */
-public class ReverseConverter implements
- Converter {
-
- private Converter realConverter;
-
- /**
- * Creates a converter from source to target based on a converter that
- * converts from target to source.
- *
- * @param converter
- * The converter to use in a reverse fashion
- */
- public ReverseConverter(Converter converter) {
- this.realConverter = converter;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.data.util.converter.Converter#convertToModel(java
- * .lang.Object, java.util.Locale)
- */
- @Override
- public MODEL convertToModel(PRESENTATION value,
- Class extends MODEL> targetType, Locale locale)
- throws com.vaadin.data.util.converter.Converter.ConversionException {
- return realConverter.convertToPresentation(value, targetType, locale);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
- * .Object, java.util.Locale)
- */
- @Override
- public PRESENTATION convertToPresentation(MODEL value,
- Class extends PRESENTATION> targetType, Locale locale)
- throws com.vaadin.data.util.converter.Converter.ConversionException {
- return realConverter.convertToModel(value, targetType, locale);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.data.util.converter.Converter#getSourceType()
- */
- @Override
- public Class getModelType() {
- return realConverter.getPresentationType();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.data.util.converter.Converter#getTargetType()
- */
- @Override
- public Class getPresentationType() {
- return realConverter.getModelType();
- }
-
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java
deleted file mode 100644
index 549f156f61..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToBigDecimalConverter.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * 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.BigDecimal;
-import java.text.DecimalFormat;
-import java.text.NumberFormat;
-import java.util.Locale;
-
-/**
- * A converter that converts from {@link String} to {@link BigDecimal} 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.2
- */
-public class StringToBigDecimalConverter extends
- AbstractStringToNumberConverter {
- @Override
- protected NumberFormat getFormat(Locale locale) {
- NumberFormat numberFormat = super.getFormat(locale);
- if (numberFormat instanceof DecimalFormat) {
- ((DecimalFormat) numberFormat).setParseBigDecimal(true);
- }
-
- return numberFormat;
- }
-
- @Override
- public BigDecimal convertToModel(String value,
- Class extends BigDecimal> targetType, Locale locale)
- throws com.vaadin.data.util.converter.Converter.ConversionException {
- return (BigDecimal) convertToNumber(value, BigDecimal.class, locale);
- }
-
- @Override
- public Class getModelType() {
- return BigDecimal.class;
- }
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java
deleted file mode 100644
index adaa8c6111..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToBigIntegerConverter.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * 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.BigDecimal;
-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.4
- */
-public class StringToBigIntegerConverter extends
- AbstractStringToNumberConverter {
-
- @Override
- protected NumberFormat getFormat(Locale locale) {
- NumberFormat numberFormat = super.getFormat(locale);
- if (numberFormat instanceof DecimalFormat) {
- ((DecimalFormat) numberFormat).setParseBigDecimal(true);
- }
-
- return numberFormat;
- }
-
- @Override
- public BigInteger convertToModel(String value,
- Class extends BigInteger> targetType, Locale locale)
- throws com.vaadin.data.util.converter.Converter.ConversionException {
-
- BigDecimal bigDecimalValue = (BigDecimal) convertToNumber(value,
- BigDecimal.class, locale);
-
- return (bigDecimalValue != null) ? bigDecimalValue.toBigInteger()
- : null;
- }
-
- @Override
- public Class getModelType() {
- return BigInteger.class;
- }
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java
deleted file mode 100644
index f965cfcc6a..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToBooleanConverter.java
+++ /dev/null
@@ -1,167 +0,0 @@
-/*
- * 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.util.Locale;
-
-/**
- * A converter that converts from {@link String} to {@link Boolean} and back. The String representation is given by
- * {@link Boolean#toString()} or provided in constructor {@link #StringToBooleanConverter(String, String)}.
- *
Leading and trailing white spaces are ignored when converting from a String.
- *
For language-dependent representation, subclasses should overwrite {@link #getFalseString(Locale)} and {@link #getTrueString(Locale)}
- *
- * @author Vaadin Ltd
- * @since 7.0
- */
-public class StringToBooleanConverter implements Converter {
-
- private final String trueString;
-
- private final String falseString;
-
- /**
- * Creates converter with default string representations - "true" and "false"
- *
- */
- public StringToBooleanConverter() {
- this(Boolean.TRUE.toString(), Boolean.FALSE.toString());
- }
-
- /**
- * Creates converter with custom string representation.
- *
- * @since 7.5.4
- * @param falseString string representation for false
- * @param trueString string representation for true
- */
- public StringToBooleanConverter(String trueString, String falseString) {
- this.trueString = trueString;
- this.falseString = falseString;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
- * java.lang.Class, java.util.Locale)
- */
- @Override
- public Boolean convertToModel(String value,
- Class extends Boolean> targetType, Locale locale)
- throws ConversionException {
- if (value == null || value.isEmpty()) {
- return null;
- }
-
- // Remove leading and trailing white space
- value = value.trim();
-
- if (getTrueString().equals(value)) {
- return true;
- } else if (getFalseString().equals(value)) {
- return false;
- } else {
- throw new ConversionException("Cannot convert " + value + " to "
- + getModelType().getName());
- }
- }
-
- /**
- * Gets the string representation for true. Default is "true", if not set in constructor.
- *
- * @return the string representation for true
- */
- protected String getTrueString() {
- return trueString;
- }
-
- /**
- * Gets the string representation for false. Default is "false", if not set in constructor.
- *
- * @return the string representation for false
- */
- protected String getFalseString() {
- return falseString;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
- * .Object, java.lang.Class, java.util.Locale)
- */
- @Override
- public String convertToPresentation(Boolean value,
- Class extends String> targetType, Locale locale)
- throws ConversionException {
- if (value == null) {
- return null;
- }
- if (value) {
- return getTrueString(locale);
- } else {
- return getFalseString(locale);
- }
- }
-
- /**
- * Gets the locale-depended string representation for false.
- * Default is locale-independent value provided by {@link #getFalseString()}
- *
- * @since 7.5.4
- * @param locale to be used
- * @return the string representation for false
- */
- protected String getFalseString(Locale locale) {
- return getFalseString();
- }
-
- /**
- * Gets the locale-depended string representation for true.
- * Default is locale-independent value provided by {@link #getTrueString()}
- *
- * @since 7.5.4
- * @param locale to be used
- * @return the string representation for true
- */
- protected String getTrueString(Locale locale) {
- return getTrueString();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.data.util.converter.Converter#getModelType()
- */
- @Override
- public Class getModelType() {
- return Boolean.class;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.data.util.converter.Converter#getPresentationType()
- */
- @Override
- public Class getPresentationType() {
- return String.class;
- }
-
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToByteConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToByteConverter.java
deleted file mode 100644
index 719fced0ed..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToByteConverter.java
+++ /dev/null
@@ -1,89 +0,0 @@
-/*
- * 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.4
- */
-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 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 getModelType() {
- return Byte.class;
- }
-
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToCollectionConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToCollectionConverter.java
deleted file mode 100644
index 6930b6da06..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToCollectionConverter.java
+++ /dev/null
@@ -1,243 +0,0 @@
-/*
- * 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.io.Serializable;
-import java.lang.reflect.Modifier;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Locale;
-
-/**
- * A converter that converts from {@link String} to {@link Collection} of tokens
- * and back.
- *
- * Allows to break a string into tokens using delimiter. Each token can be
- * converted to its own model using provided converter.
- *
- * Default constructor uses ", " as delimiter string and
- * {@link String} for token types. Other constructors allow to configure
- * delimiter and token types.
- *
- * @since 7.5.0
- *
- * @author Vaadin Ltd
- */
-public class StringToCollectionConverter implements
- Converter {
-
- private final String delimiter;
- private final Converter tokenConverter;
- private final Class> tokenType;
- private final CollectionFactory factory;
-
- /**
- * Creates converter with ", " as delimiter and {@link String}
- * as token model type in collection.
- */
- public StringToCollectionConverter() {
- this(", ", null, String.class);
- }
-
- /**
- * Creates converter with given {@code delimiter} and {@link String} as
- * token model type in collection.
- *
- * @param delimiter
- * custom delimiter
- */
- public StringToCollectionConverter(String delimiter) {
- this(delimiter, null, String.class);
- }
-
- /**
- * Creates converter with given {@code tokenConverter} for convert tokens
- * and expected {@code tokenType}.
- *
- * If {@code tokenConverter} is null then no conversation is done and
- * {@link String} is used as token type in resulting model collection.
- *
- * @param tokenConverter
- * converter for token
- * @param tokenType
- * expected token model type
- */
- public StringToCollectionConverter(Converter tokenConverter,
- Class> tokenType) {
- this(", ", tokenConverter, tokenType);
- }
-
- /**
- * Creates converter with given {@code tokenConverter} for convert tokens
- * and expected {@code tokenType}.
- *
- * If {@code tokenConverter} is null then no conversation is done and
- * {@link String} is used as token type in resulting model collection.
- *
- * @param tokenConverter
- * converter for token
- * @param tokenType
- * expected token model type
- * @param delimiter
- * delimiter in presentation string
- */
- public StringToCollectionConverter(String delimiter,
- Converter tokenConverter, Class> tokenClass) {
- this(delimiter, tokenConverter, tokenClass,
- new DefaultCollectionFactory());
- }
-
- /**
- * Creates converter with given {@code tokenConverter} for convert tokens
- * and expected {@code tokenType}.
- *
- * If {@code tokenConverter} is null then no conversation is done and
- * {@link String} is used as token type in resulting model collection.
- *
- * @param tokenConverter
- * converter for token
- * @param tokenType
- * expected token model type
- * @param delimiter
- * delimiter in presentation string
- * @param factory
- * factory to create resulting collection
- */
- public StringToCollectionConverter(String delimiter,
- Converter tokenConverter, Class> tokenClass,
- CollectionFactory factory) {
- if (delimiter == null || delimiter.isEmpty()) {
- throw new IllegalArgumentException(
- "Delimiter should be non-empty string");
- }
- this.delimiter = delimiter;
- this.tokenConverter = tokenConverter;
- tokenType = tokenClass;
- this.factory = factory;
- }
-
- @Override
- public Class getModelType() {
- return Collection.class;
- }
-
- @Override
- public Class getPresentationType() {
- return String.class;
- }
-
- @Override
- public Collection convertToModel(String value,
- Class extends Collection> targetType, Locale locale)
- throws Converter.ConversionException {
- if (value == null) {
- return null;
- }
-
- int index = value.indexOf(delimiter);
- int previous = 0;
- Collection result = factory.createCollection(targetType);
- Converter converter = tokenConverter;
- while (index != -1) {
- collectToken(value.substring(previous, index), result, converter,
- locale);
- previous = index + delimiter.length();
- index = value.indexOf(delimiter, previous);
- }
- collectToken(value.substring(previous), result, converter, locale);
- return result;
- }
-
- @Override
- public String convertToPresentation(Collection value,
- Class extends String> targetType, Locale locale)
- throws Converter.ConversionException {
- if (value == null) {
- return null;
- }
- StringBuilder builder = new StringBuilder();
- Converter converter = tokenConverter;
- for (Iterator> iterator = value.iterator(); iterator.hasNext();) {
- if (converter == null) {
- builder.append(iterator.next());
- } else {
- builder.append(converter.convertToPresentation(iterator.next(),
- targetType, locale));
- }
- builder.append(delimiter);
- }
- if (builder.length() > 0) {
- return builder.substring(0, builder.length() - delimiter.length());
- } else {
- return builder.toString();
- }
- }
-
- private void collectToken(String token, Collection collection,
- Converter converter, Locale locale) {
- if (converter == null) {
- collection.add(token);
- } else {
- collection.add(converter.convertToModel(token, tokenType, locale));
- }
- }
-
- /**
- * Default collection factory implementation.
- *
- * @author Vaadin Ltd
- */
- public static class DefaultCollectionFactory implements CollectionFactory {
-
- @Override
- public Collection> createCollection(Class extends Collection> type) {
- if (type.isAssignableFrom(ArrayList.class)) {
- return new ArrayList();
- } else if (type.isAssignableFrom(HashSet.class)) {
- return new HashSet();
- } else if (!type.isInterface()
- && !Modifier.isAbstract(type.getModifiers())) {
- try {
- return type.newInstance();
- } catch (InstantiationException ignore) {
- } catch (IllegalAccessException ignore) {
- }
- }
- return new ArrayList();
- }
-
- }
-
- /**
- * Collection factory. Defines a strategy to create collection by collection
- * class.
- *
- * @author Vaadin Ltd
- */
- public interface CollectionFactory extends Serializable {
-
- /**
- * Create collection by its {@code type}.
- *
- * @param type
- * collection type
- * @return instantiated collection with given {@code type}
- */
- Collection> createCollection(Class extends Collection> type);
- }
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java
deleted file mode 100644
index 8f0db24caf..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToDateConverter.java
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- * 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.DateFormat;
-import java.text.ParsePosition;
-import java.util.Date;
-import java.util.Locale;
-
-/**
- * A converter that converts from {@link Date} to {@link String} and back. Uses
- * the given locale and {@link DateFormat} 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.0
- */
-public class StringToDateConverter implements Converter {
-
- /**
- * Returns the format used by
- * {@link #convertToPresentation(Date, Class,Locale)} and
- * {@link #convertToModel(String, Class, Locale)}.
- *
- * @param locale
- * The locale to use
- * @return A DateFormat instance
- */
- protected DateFormat getFormat(Locale locale) {
- if (locale == null) {
- locale = Locale.getDefault();
- }
-
- DateFormat f = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
- DateFormat.MEDIUM, locale);
- f.setLenient(false);
- return f;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
- * java.lang.Class, java.util.Locale)
- */
- @Override
- public Date convertToModel(String value, Class extends Date> targetType,
- Locale locale)
- throws com.vaadin.data.util.converter.Converter.ConversionException {
- if (targetType != getModelType()) {
- throw new ConversionException("Converter only supports "
- + getModelType().getName() + " (targetType was "
- + targetType.getName() + ")");
- }
-
- if (value == null) {
- return null;
- }
-
- // Remove leading and trailing white space
- value = value.trim();
-
- ParsePosition parsePosition = new ParsePosition(0);
- Date parsedValue = getFormat(locale).parse(value, parsePosition);
- if (parsePosition.getIndex() != value.length()) {
- throw new ConversionException("Could not convert '" + value
- + "' to " + getModelType().getName());
- }
-
- return parsedValue;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
- * .Object, java.lang.Class, java.util.Locale)
- */
- @Override
- public String convertToPresentation(Date value,
- Class extends String> targetType, Locale locale)
- throws com.vaadin.data.util.converter.Converter.ConversionException {
- if (value == null) {
- return null;
- }
-
- return getFormat(locale).format(value);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.data.util.converter.Converter#getModelType()
- */
- @Override
- public Class getModelType() {
- return Date.class;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.data.util.converter.Converter#getPresentationType()
- */
- @Override
- public Class getPresentationType() {
- return String.class;
- }
-
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java
deleted file mode 100644
index f514eac648..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToDoubleConverter.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * 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 Double} 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.0
- */
-public class StringToDoubleConverter extends
- AbstractStringToNumberConverter {
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
- * java.util.Locale)
- */
- @Override
- public Double convertToModel(String value,
- Class extends Double> targetType, Locale locale)
- throws ConversionException {
- Number n = convertToNumber(value, targetType, locale);
- return n == null ? null : n.doubleValue();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.data.util.converter.Converter#getModelType()
- */
- @Override
- public Class getModelType() {
- return Double.class;
- }
-
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToEnumConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToEnumConverter.java
deleted file mode 100644
index 7b2f43f972..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToEnumConverter.java
+++ /dev/null
@@ -1,160 +0,0 @@
-/*
- * 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.util.EnumSet;
-import java.util.Locale;
-
-/**
- * A converter that converts from {@link String} to an {@link Enum} and back.
- *
- * Designed to provide nice human readable strings for {@link Enum} classes
- * conforming to one of these patterns:
- *
- *
The constants are named SOME_UPPERCASE_WORDS and there's no toString
- * implementation.
- *
toString() always returns the same human readable string that is not the
- * same as its name() value. Each constant in the enum type returns a distinct
- * toString() value.
- *
- * Will not necessarily work correctly for other cases.
- *
- *
- * @author Vaadin Ltd
- * @since 7.4
- */
-public class StringToEnumConverter implements Converter {
-
- @Override
- public Enum convertToModel(String value, Class extends Enum> targetType,
- Locale locale) throws ConversionException {
- if (value == null || value.trim().equals("")) {
- return null;
- }
-
- return stringToEnum(value, targetType, locale);
- }
-
- /**
- * Converts the given string to the given enum type using the given locale
- *
- * Compatible with {@link #enumToString(Enum, Locale)}
- *
- * @param value
- * The string value to convert
- * @param enumType
- * The type of enum to create
- * @param locale
- * The locale to use for conversion. If null, the JVM default
- * locale will be used
- * @return The enum which matches the given string
- * @throws ConversionException
- * if the conversion fails
- */
- public static > T stringToEnum(String value,
- Class enumType, Locale locale) throws ConversionException {
- if (locale == null) {
- locale = Locale.getDefault();
- }
-
- if (!enumType.isEnum()) {
- throw new ConversionException(enumType.getName()
- + " is not an enum type");
- }
-
- // First test for the human-readable value since that's the more likely
- // input
- String upperCaseValue = value.toUpperCase(locale);
- T match = null;
- for (T e : EnumSet.allOf(enumType)) {
- String upperCase = enumToString(e, locale).toUpperCase(locale);
- if (upperCase.equals(upperCaseValue)) {
- if (match != null) {
- throw new ConversionException("Both " + match.name()
- + " and " + e.name()
- + " are matching the input string " + value);
- }
- match = e;
- }
- }
-
- if (match != null) {
- return match;
- }
-
- // Then fall back to using a strict match based on name()
- try {
- return Enum.valueOf(enumType, upperCaseValue);
- } catch (Exception ee) {
- throw new ConversionException(ee);
- }
- }
-
- /**
- * Converts the given enum to a human readable string using the given locale
- *
- * Compatible with {@link #stringToEnum(String, Class, Locale)}
- *
- * @param value
- * The enum value to convert
- * @param locale
- * The locale to use for conversion. If null, the JVM default
- * locale will be used
- * @return A human readable string based on the enum
- * @throws ConversionException
- * if the conversion fails
- */
- public static String enumToString(Enum> value, Locale locale) {
- if (locale == null) {
- locale = Locale.getDefault();
- }
-
- String enumString = value.toString();
- if (enumString.equals(value.name())) {
- // FOO -> Foo
- // FOO_BAR -> Foo bar
- // _FOO -> _foo
- String result = enumString.substring(0, 1).toUpperCase(locale);
- result += enumString.substring(1).toLowerCase(locale)
- .replace('_', ' ');
- return result;
- } else {
- return enumString;
- }
- }
-
- @Override
- public String convertToPresentation(Enum value,
- Class extends String> targetType, Locale locale)
- throws ConversionException {
- if (value == null) {
- return null;
- }
-
- return enumToString(value, locale);
- }
-
- @Override
- public Class getModelType() {
- return Enum.class;
- }
-
- @Override
- public Class getPresentationType() {
- return String.class;
- }
-
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java
deleted file mode 100644
index f4d7f97853..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToFloatConverter.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * 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 Float} 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.0
- */
-public class StringToFloatConverter extends
- AbstractStringToNumberConverter {
-
- /*
- * (non-Javadoc)
- *
- * @see
- * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
- * java.util.Locale)
- */
- @Override
- public Float convertToModel(String value,
- Class extends Float> targetType, Locale locale)
- throws ConversionException {
- Number n = convertToNumber(value, targetType, locale);
- return n == null ? null : n.floatValue();
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.data.util.converter.Converter#getModelType()
- */
- @Override
- public Class getModelType() {
- return Float.class;
- }
-
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java
deleted file mode 100644
index 59a46babe1..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToIntegerConverter.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * 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 Integer} 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.0
- */
-public class StringToIntegerConverter extends
- AbstractStringToNumberConverter {
-
- /**
- * Returns the format used by
- * {@link #convertToPresentation(Integer, 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 Integer convertToModel(String value,
- Class extends Integer> targetType, Locale locale)
- throws ConversionException {
- Number n = convertToNumber(value, targetType, locale);
-
- if (n == null) {
- return null;
- }
-
- int intValue = n.intValue();
- if (intValue == n.longValue()) {
- // If the value of n is outside the range of long, the return value
- // of longValue() is either Long.MIN_VALUE or Long.MAX_VALUE. The
- // above comparison promotes int to long and thus does not need to
- // consider wrap-around.
- return intValue;
- }
-
- throw new ConversionException("Could not convert '" + value + "' to "
- + Integer.class.getName() + ": value out of range");
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.data.util.converter.Converter#getModelType()
- */
- @Override
- public Class getModelType() {
- return Integer.class;
- }
-
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java
deleted file mode 100644
index cd7cf49042..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToLongConverter.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * 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 Long} 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.2
- */
-public class StringToLongConverter extends
- AbstractStringToNumberConverter {
-
- /**
- * Returns the format used by
- * {@link #convertToPresentation(Long, 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 Long convertToModel(String value, Class extends Long> targetType,
- Locale locale) throws ConversionException {
- Number n = convertToNumber(value, targetType, locale);
- return n == null ? null : n.longValue();
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see com.vaadin.data.util.converter.Converter#getModelType()
- */
- @Override
- public Class getModelType() {
- return Long.class;
- }
-
-}
diff --git a/server/src/main/java/com/vaadin/data/util/converter/StringToShortConverter.java b/server/src/main/java/com/vaadin/data/util/converter/StringToShortConverter.java
deleted file mode 100644
index 554b958ee0..0000000000
--- a/server/src/main/java/com/vaadin/data/util/converter/StringToShortConverter.java
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * 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.4
- */
-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 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 getModelType() {
- return Short.class;
- }
-
-}
diff --git a/server/src/main/java/com/vaadin/data/util/sqlcontainer/ColumnProperty.java b/server/src/main/java/com/vaadin/data/util/sqlcontainer/ColumnProperty.java
index e89b8c02a8..0f778ba2ab 100644
--- a/server/src/main/java/com/vaadin/data/util/sqlcontainer/ColumnProperty.java
+++ b/server/src/main/java/com/vaadin/data/util/sqlcontainer/ColumnProperty.java
@@ -22,7 +22,7 @@ import java.util.logging.Logger;
import com.vaadin.data.Property;
import com.vaadin.data.util.LegacyPropertyHelper;
-import com.vaadin.data.util.converter.Converter.ConversionException;
+import com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException;
/**
* ColumnProperty represents the value of one column in a RowItem. In addition
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyAbstractStringToNumberConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyAbstractStringToNumberConverter.java
new file mode 100644
index 0000000000..4b14f66df9
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyAbstractStringToNumberConverter.java
@@ -0,0 +1,123 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.text.NumberFormat;
+import java.text.ParsePosition;
+import java.util.Locale;
+
+/**
+ * A converter that converts from the number type T to {@link String} and back.
+ * Uses the given locale and {@link NumberFormat} for formatting and parsing.
+ * Automatically trims the input string, removing any leading and trailing white
+ * space.
+ *
+ * Override and overwrite {@link #getFormat(Locale)} to use a different format.
+ *
+ *
+ * @author Vaadin Ltd
+ * @since 7.1
+ */
+public abstract class LegacyAbstractStringToNumberConverter implements
+ LegacyConverter {
+
+ /**
+ * Returns the format used by {@link #convertToPresentation(Object, Locale)}
+ * and {@link #convertToModel(Object, Locale)}.
+ *
+ * @param locale
+ * The locale to use
+ * @return A NumberFormat instance
+ * @since 7.1
+ */
+ protected NumberFormat getFormat(Locale locale) {
+ if (locale == null) {
+ locale = Locale.getDefault();
+ }
+
+ return NumberFormat.getNumberInstance(locale);
+ }
+
+ /**
+ * Convert the value to a Number using the given locale and
+ * {@link #getFormat(Locale)}.
+ *
+ * @param value
+ * The value to convert
+ * @param locale
+ * The locale to use for conversion
+ * @return The converted value
+ * @throws ConversionException
+ * If there was a problem converting the value
+ * @since 7.1
+ */
+ protected Number convertToNumber(String value,
+ Class extends Number> targetType, Locale locale)
+ throws ConversionException {
+ if (value == null) {
+ return null;
+ }
+
+ // Remove leading and trailing white space
+ value = value.trim();
+
+ // Parse and detect errors. If the full string was not used, it is
+ // an error.
+ ParsePosition parsePosition = new ParsePosition(0);
+ Number parsedValue = getFormat(locale).parse(value, parsePosition);
+ if (parsePosition.getIndex() != value.length()) {
+ throw new ConversionException("Could not convert '" + value
+ + "' to " + getModelType().getName());
+ }
+
+ if (parsedValue == null) {
+ // Convert "" to null
+ return null;
+ }
+
+ return parsedValue;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
+ * .Object, java.util.Locale)
+ */
+ @Override
+ public String convertToPresentation(T value,
+ Class extends String> targetType, Locale locale)
+ throws ConversionException {
+ if (value == null) {
+ return null;
+ }
+
+ return getFormat(locale).format(value);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#getPresentationType()
+ */
+ @Override
+ public Class getPresentationType() {
+ return String.class;
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverter.java
new file mode 100644
index 0000000000..1fdd0755b9
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverter.java
@@ -0,0 +1,176 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.io.Serializable;
+import java.util.Locale;
+
+/**
+ * Interface that implements conversion between a model and a presentation type.
+ *
+ * Typically {@link #convertToPresentation(Object, Class, Locale)} and
+ * {@link #convertToModel(Object, Class, Locale)} should be symmetric so that
+ * chaining these together returns the original result for all input but this is
+ * not a requirement.
+ *
+ *
+ * Converters must not have any side effects (never update UI from inside a
+ * converter).
+ *
+ *
+ * All Converters must be stateless and thread safe.
+ *
+ *
+ * If conversion of a value fails, a {@link ConversionException} is thrown.
+ *
+ *
+ * @param
+ * The presentation type. Must be compatible with what
+ * {@link #getPresentationType()} returns.
+ * @param
+ * The model type. Must be compatible with what
+ * {@link #getModelType()} returns.
+ * @author Vaadin Ltd.
+ * @since 7.0
+ */
+public interface LegacyConverter extends Serializable {
+
+ /**
+ * Converts the given value from target type to source type.
+ *
+ * A converter can optionally use locale to do the conversion.
+ *
+ * A converter should in most cases be symmetric so chaining
+ * {@link #convertToPresentation(Object, Class, Locale)} and
+ * {@link #convertToModel(Object, Class, Locale)} should return the original
+ * value.
+ *
+ * @param value
+ * The value to convert, compatible with the target type. Can be
+ * null
+ * @param targetType
+ * The requested type of the return value
+ * @param locale
+ * The locale to use for conversion. Can be null.
+ * @return The converted value compatible with the source type
+ * @throws ConversionException
+ * If the value could not be converted
+ */
+ public MODEL convertToModel(PRESENTATION value,
+ Class extends MODEL> targetType, Locale locale)
+ throws ConversionException;
+
+ /**
+ * Converts the given value from source type to target type.
+ *
+ * A converter can optionally use locale to do the conversion.
+ *
+ * A converter should in most cases be symmetric so chaining
+ * {@link #convertToPresentation(Object, Class, Locale)} and
+ * {@link #convertToModel(Object, Class, Locale)} should return the original
+ * value.
+ *
+ * @param value
+ * The value to convert, compatible with the target type. Can be
+ * null
+ * @param targetType
+ * The requested type of the return value
+ * @param locale
+ * The locale to use for conversion. Can be null.
+ * @return The converted value compatible with the source type
+ * @throws ConversionException
+ * If the value could not be converted
+ */
+ public PRESENTATION convertToPresentation(MODEL value,
+ Class extends PRESENTATION> targetType, Locale locale)
+ throws ConversionException;
+
+ /**
+ * The source type of the converter.
+ *
+ * Values of this type can be passed to
+ * {@link #convertToPresentation(Object, Class, Locale)}.
+ *
+ * @return The source type
+ */
+ public Class getModelType();
+
+ /**
+ * The target type of the converter.
+ *
+ * Values of this type can be passed to
+ * {@link #convertToModel(Object, Class, Locale)}.
+ *
+ * @return The target type
+ */
+ public Class getPresentationType();
+
+ /**
+ * An exception that signals that the value passed to
+ * {@link LegacyConverter#convertToPresentation(Object, Class, Locale)} or
+ * {@link LegacyConverter#convertToModel(Object, Class, Locale)} could not be
+ * converted.
+ *
+ * @author Vaadin Ltd
+ * @since 7.0
+ */
+ public static class ConversionException extends RuntimeException {
+
+ /**
+ * Constructs a new ConversionException without a detail
+ * message.
+ */
+ public ConversionException() {
+ }
+
+ /**
+ * Constructs a new ConversionException with the specified
+ * detail message.
+ *
+ * @param msg
+ * the detail message
+ */
+ public ConversionException(String msg) {
+ super(msg);
+ }
+
+ /**
+ * Constructs a new {@code ConversionException} with the specified
+ * cause.
+ *
+ * @param cause
+ * The cause of the the exception
+ */
+ public ConversionException(Throwable cause) {
+ super(cause);
+ }
+
+ /**
+ * Constructs a new ConversionException with the specified
+ * detail message and cause.
+ *
+ * @param message
+ * the detail message
+ * @param cause
+ * The cause of the the exception
+ */
+ public ConversionException(String message, Throwable cause) {
+ super(message, cause);
+ }
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterFactory.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterFactory.java
new file mode 100644
index 0000000000..8c5cbacf04
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterFactory.java
@@ -0,0 +1,33 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.io.Serializable;
+
+/**
+ * Factory interface for providing Converters based on a presentation type and a
+ * model type.
+ *
+ * @author Vaadin Ltd.
+ * @since 7.0
+ *
+ */
+public interface LegacyConverterFactory extends Serializable {
+ public LegacyConverter createConverter(
+ Class presentationType, Class modelType);
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterUtil.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterUtil.java
new file mode 100644
index 0000000000..42d268f781
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyConverterUtil.java
@@ -0,0 +1,261 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.io.Serializable;
+import java.util.Locale;
+
+import com.vaadin.server.VaadinSession;
+
+public class LegacyConverterUtil implements Serializable {
+
+ /**
+ * Finds a converter that can convert from the given presentation type to
+ * the given model type and back. Uses the given application to find a
+ * {@link LegacyConverterFactory} or, if application is null, uses the
+ * {@link VaadinSession#getCurrent()}.
+ *
+ * @param
+ * the presentation type
+ * @param
+ * the model type
+ * @param presentationType
+ * the presentation type
+ * @param modelType
+ * the model type
+ * @param session
+ * the session to use to find a ConverterFactory or null to use
+ * the current session
+ * @return a Converter capable of converting between the given types or null
+ * if no converter was found
+ */
+ public static LegacyConverter getConverter(
+ Class presentationType,
+ Class modelType, VaadinSession session) {
+ LegacyConverter converter = null;
+ if (session == null) {
+ session = VaadinSession.getCurrent();
+ }
+
+ if (session != null) {
+ LegacyConverterFactory factory = session.getConverterFactory();
+ converter = factory.createConverter(presentationType, modelType);
+ }
+ return converter;
+
+ }
+
+ /**
+ * Convert the given value from the data source type to the UI type.
+ *
+ * @param modelValue
+ * the model value to convert
+ * @param presentationType
+ * the type of the presentation value
+ * @param converter
+ * the converter to use
+ * @param locale
+ * the locale to use for conversion
+ * @param
+ * the presentation type
+ * @param
+ * the model type
+ *
+ * @return the converted value, compatible with the presentation type, or
+ * the original value if its type is compatible and no converter is
+ * set.
+ * @throws LegacyConverter.ConversionException
+ * if there was a problem converting the value
+ */
+ @SuppressWarnings("unchecked")
+ public static PRESENTATIONTYPE convertFromModel(
+ MODELTYPE modelValue,
+ Class extends PRESENTATIONTYPE> presentationType,
+ LegacyConverter converter, Locale locale)
+ throws LegacyConverter.ConversionException {
+ if (converter != null) {
+ /*
+ * If there is a converter, always use it. It must convert or throw
+ * an exception.
+ */
+ PRESENTATIONTYPE presentation = converter.convertToPresentation(
+ modelValue, presentationType, locale);
+ if (presentation != null
+ && !presentationType.isInstance(presentation)) {
+ throw new LegacyConverter.ConversionException(
+ "Converter returned an object of type "
+ + presentation.getClass().getName()
+ + " when expecting "
+ + presentationType.getName());
+ }
+
+ return presentation;
+ }
+ if (modelValue == null) {
+ // Null should always be passed through the converter but if there
+ // is no converter we can safely return null
+ return null;
+ }
+
+ if (presentationType.isAssignableFrom(modelValue.getClass())) {
+ return (PRESENTATIONTYPE) modelValue;
+ } else {
+ throw new LegacyConverter.ConversionException(
+ "Unable to convert value of type "
+ + modelValue.getClass().getName()
+ + " to presentation type "
+ + presentationType
+ + ". No converter is set and the types are not compatible.");
+ }
+ }
+
+ /**
+ * Convert the given value from the presentation (UI) type to model (data
+ * source) type.
+ *
+ * @param presentationValue
+ * the presentation value to convert
+ * @param modelType
+ * the type of the model
+ * @param converter
+ * the converter to use
+ * @param locale
+ * the locale to use for conversion
+ * @param
+ * the presentation type
+ * @param
+ * the model type
+ *
+ * @return the converted value, compatible with the model type, or the
+ * original value if its type is compatible and no converter is set.
+ * @throws LegacyConverter.ConversionException
+ * if there was a problem converting the value
+ */
+ public static MODELTYPE convertToModel(
+ PRESENTATIONTYPE presentationValue, Class modelType,
+ LegacyConverter converter, Locale locale)
+ throws LegacyConverter.ConversionException {
+ if (converter != null) {
+ /*
+ * If there is a converter, always use it. It must convert or throw
+ * an exception.
+ */
+ MODELTYPE model = converter.convertToModel(presentationValue,
+ modelType, locale);
+ if (model != null && !modelType.isInstance(model)) {
+ throw new LegacyConverter.ConversionException(
+ "Converter returned an object of type "
+ + model.getClass().getName()
+ + " when expecting " + modelType.getName());
+ }
+
+ return model;
+ }
+
+ if (presentationValue == null) {
+ // Null should always be passed through the converter but if there
+ // is no converter we can safely return null
+ return null;
+ }
+
+ if (modelType == null) {
+ // No model type, return original value
+ return (MODELTYPE) presentationValue;
+ } else if (modelType.isAssignableFrom(presentationValue.getClass())) {
+ // presentation type directly compatible with model type
+ return modelType.cast(presentationValue);
+ } else {
+ throw new LegacyConverter.ConversionException(
+ "Unable to convert value of type "
+ + presentationValue.getClass().getName()
+ + " to model type "
+ + modelType
+ + ". No converter is set and the types are not compatible.");
+ }
+
+ }
+
+ /**
+ * Checks if the given converter can handle conversion between the given
+ * presentation and model type. Does strict type checking and only returns
+ * true if the converter claims it can handle exactly the given types.
+ *
+ * @see #canConverterPossiblyHandle(LegacyConverter, Class, Class)
+ *
+ * @param converter
+ * The converter to check. If this is null the result is always
+ * false.
+ * @param presentationType
+ * The presentation type
+ * @param modelType
+ * The model type
+ * @return true if the converter supports conversion between the given
+ * presentation and model type, false otherwise
+ */
+ public static boolean canConverterHandle(LegacyConverter, ?> converter,
+ Class> presentationType, Class> modelType) {
+ if (converter == null) {
+ return false;
+ }
+
+ if (modelType != converter.getModelType()) {
+ return false;
+ }
+ if (presentationType != converter.getPresentationType()) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Checks if it possible that the given converter can handle conversion
+ * between the given presentation and model type somehow.
+ *
+ * @param converter
+ * The converter to check. If this is null the result is always
+ * false.
+ * @param presentationType
+ * The presentation type
+ * @param modelType
+ * The model type
+ * @return true if the converter possibly support conversion between the
+ * given presentation and model type, false otherwise
+ */
+ public static boolean canConverterPossiblyHandle(LegacyConverter, ?> converter,
+ Class> presentationType, Class> modelType) {
+ if (converter == null) {
+ return false;
+ }
+ Class> converterModelType = converter.getModelType();
+
+ if (!modelType.isAssignableFrom(converterModelType)
+ && !converterModelType.isAssignableFrom(modelType)) {
+ // model types are not compatible in any way
+ return false;
+ }
+
+ Class> converterPresentationType = converter.getPresentationType();
+ if (!presentationType.isAssignableFrom(converterPresentationType)
+ && !converterPresentationType
+ .isAssignableFrom(presentationType)) {
+ // presentation types are not compatible in any way
+ return false;
+ }
+
+ return true;
+ }
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToLongConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToLongConverter.java
new file mode 100644
index 0000000000..04d037b12f
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToLongConverter.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.legacy.data.util.converter;
+
+import java.util.Date;
+import java.util.Locale;
+
+/**
+ * A converter that converts from {@link Long} to {@link Date} and back.
+ *
+ * @author Vaadin Ltd
+ * @since 7.0
+ */
+public class LegacyDateToLongConverter implements LegacyConverter {
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
+ * java.lang.Class, java.util.Locale)
+ */
+ @Override
+ public Long convertToModel(Date value, Class extends Long> targetType,
+ Locale locale) {
+ if (value == null) {
+ return null;
+ }
+
+ return value.getTime();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
+ * .Object, java.lang.Class, java.util.Locale)
+ */
+ @Override
+ public Date convertToPresentation(Long value,
+ Class extends Date> targetType, Locale locale) {
+ if (targetType != getPresentationType()) {
+ throw new ConversionException("Converter only supports "
+ + getPresentationType().getName() + " (targetType was "
+ + targetType.getName() + ")");
+ }
+ if (value == null) {
+ return null;
+ }
+
+ return new Date(value);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#getModelType()
+ */
+ @Override
+ public Class getModelType() {
+ return Long.class;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#getPresentationType()
+ */
+ @Override
+ public Class getPresentationType() {
+ return Date.class;
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToSqlDateConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToSqlDateConverter.java
new file mode 100644
index 0000000000..1ea4eaca8c
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDateToSqlDateConverter.java
@@ -0,0 +1,81 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.util.Date;
+import java.util.Locale;
+
+/**
+ * Converter for handling conversion between {@link java.util.Date} and
+ * {@link java.sql.Date}. This is used when a PopupDateField or InlineDateField
+ * is connected to a java.sql.Date property, typically through a JPAContainer or
+ * SQLContainer. Note that information (time information) is lost when
+ * converting from {@link java.util.Date} to {@link java.sql.Date}.
+ *
+ * @since 7.1
+ * @author Vaadin Ltd
+ */
+public class LegacyDateToSqlDateConverter implements LegacyConverter {
+
+ @Override
+ public java.sql.Date convertToModel(Date value,
+ Class extends java.sql.Date> targetType, Locale locale)
+ throws ConversionException {
+ if (targetType != getModelType()) {
+ throw new ConversionException("Converter only supports "
+ + getModelType().getName() + " (targetType was "
+ + targetType.getName() + ")");
+ }
+
+ if (value == null) {
+ return null;
+ }
+
+ return new java.sql.Date(value.getTime());
+ }
+
+ @Override
+ public Date convertToPresentation(java.sql.Date value,
+ Class extends Date> targetType, Locale locale)
+ throws ConversionException {
+ if (targetType != getPresentationType()) {
+ throw new ConversionException("Converter only supports "
+ + getPresentationType().getName() + " (targetType was "
+ + targetType.getName() + ")");
+ }
+
+ if (value == null) {
+ return null;
+ }
+
+ return new Date(value.getTime());
+ }
+
+ @Override
+ public Class getModelType() {
+ return java.sql.Date.class;
+ }
+
+ @Override
+ public Class getPresentationType() {
+ return Date.class;
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDefaultConverterFactory.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDefaultConverterFactory.java
new file mode 100644
index 0000000000..c647981650
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyDefaultConverterFactory.java
@@ -0,0 +1,127 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.Date;
+import java.util.logging.Logger;
+
+import com.vaadin.server.VaadinSession;
+
+/**
+ * Default implementation of {@link LegacyConverterFactory}. Provides converters for
+ * standard types like {@link String}, {@link Double} and {@link Date}.
+ *
+ * Custom converters can be provided by extending this class and using
+ * {@link VaadinSession#setConverterFactory(LegacyConverterFactory)}.
+ *
+ *
+ * @author Vaadin Ltd
+ * @since 7.0
+ */
+public class LegacyDefaultConverterFactory implements LegacyConverterFactory {
+
+ private final static Logger log = Logger
+ .getLogger(LegacyDefaultConverterFactory.class.getName());
+
+ @Override
+ public LegacyConverter createConverter(
+ Class presentationType, Class modelType) {
+ LegacyConverter converter = findConverter(
+ presentationType, modelType);
+ if (converter != null) {
+ log.finest(getClass().getName() + " created a "
+ + converter.getClass());
+ return converter;
+ }
+
+ // Try to find a reverse converter
+ LegacyConverter reverseConverter = findConverter(
+ modelType, presentationType);
+ if (reverseConverter != null) {
+ log.finest(getClass().getName() + " created a reverse "
+ + reverseConverter.getClass());
+ return new LegacyReverseConverter(reverseConverter);
+ }
+
+ log.finest(getClass().getName() + " could not find a converter for "
+ + presentationType.getName() + " to " + modelType.getName()
+ + " conversion");
+ return null;
+
+ }
+
+ protected LegacyConverter findConverter(
+ Class presentationType, Class modelType) {
+ if (presentationType == String.class) {
+ // TextField converters and more
+ LegacyConverter converter = (LegacyConverter) createStringConverter(modelType);
+ if (converter != null) {
+ return converter;
+ }
+ } else if (presentationType == Date.class) {
+ // DateField converters and more
+ LegacyConverter converter = (LegacyConverter) createDateConverter(modelType);
+ if (converter != null) {
+ return converter;
+ }
+ }
+
+ return null;
+
+ }
+
+ protected LegacyConverter createDateConverter(Class> sourceType) {
+ if (Long.class.isAssignableFrom(sourceType)) {
+ return new LegacyDateToLongConverter();
+ } else if (java.sql.Date.class.isAssignableFrom(sourceType)) {
+ return new LegacyDateToSqlDateConverter();
+ } else {
+ return null;
+ }
+ }
+
+ protected LegacyConverter createStringConverter(Class> sourceType) {
+ if (Double.class.isAssignableFrom(sourceType)) {
+ return new LegacyStringToDoubleConverter();
+ } else if (Float.class.isAssignableFrom(sourceType)) {
+ return new LegacyStringToFloatConverter();
+ } else if (Integer.class.isAssignableFrom(sourceType)) {
+ return new LegacyStringToIntegerConverter();
+ } else if (Long.class.isAssignableFrom(sourceType)) {
+ return new LegacyStringToLongConverter();
+ } else if (BigDecimal.class.isAssignableFrom(sourceType)) {
+ return new LegacyStringToBigDecimalConverter();
+ } else if (Boolean.class.isAssignableFrom(sourceType)) {
+ return new LegacyStringToBooleanConverter();
+ } else if (Date.class.isAssignableFrom(sourceType)) {
+ return new LegacyStringToDateConverter();
+ } else if (Enum.class.isAssignableFrom(sourceType)) {
+ return new LegacyStringToEnumConverter();
+ } else if (BigInteger.class.isAssignableFrom(sourceType)) {
+ return new LegacyStringToBigIntegerConverter();
+ } else if (Short.class.isAssignableFrom(sourceType)) {
+ return new LegacyStringToShortConverter();
+ } else if (Byte.class.isAssignableFrom(sourceType)) {
+ return new LegacyStringToByteConverter();
+ } else {
+ return null;
+ }
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyReverseConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyReverseConverter.java
new file mode 100644
index 0000000000..6841e99b6e
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyReverseConverter.java
@@ -0,0 +1,96 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.util.Locale;
+
+/**
+ * A converter that wraps another {@link LegacyConverter} and reverses source and
+ * target types.
+ *
+ * @param
+ * The source type
+ * @param
+ * The target type
+ *
+ * @author Vaadin Ltd
+ * @since 7.0
+ */
+public class LegacyReverseConverter implements
+ LegacyConverter {
+
+ private LegacyConverter realConverter;
+
+ /**
+ * Creates a converter from source to target based on a converter that
+ * converts from target to source.
+ *
+ * @param converter
+ * The converter to use in a reverse fashion
+ */
+ public LegacyReverseConverter(LegacyConverter converter) {
+ this.realConverter = converter;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#convertToModel(java
+ * .lang.Object, java.util.Locale)
+ */
+ @Override
+ public MODEL convertToModel(PRESENTATION value,
+ Class extends MODEL> targetType, Locale locale)
+ throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException {
+ return realConverter.convertToPresentation(value, targetType, locale);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
+ * .Object, java.util.Locale)
+ */
+ @Override
+ public PRESENTATION convertToPresentation(MODEL value,
+ Class extends PRESENTATION> targetType, Locale locale)
+ throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException {
+ return realConverter.convertToModel(value, targetType, locale);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#getSourceType()
+ */
+ @Override
+ public Class getModelType() {
+ return realConverter.getPresentationType();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#getTargetType()
+ */
+ @Override
+ public Class getPresentationType() {
+ return realConverter.getModelType();
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigDecimalConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigDecimalConverter.java
new file mode 100644
index 0000000000..4b6c2151a0
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigDecimalConverter.java
@@ -0,0 +1,60 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.math.BigDecimal;
+import java.text.DecimalFormat;
+import java.text.NumberFormat;
+import java.util.Locale;
+
+/**
+ * A converter that converts from {@link String} to {@link BigDecimal} 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.2
+ */
+public class LegacyStringToBigDecimalConverter extends
+ LegacyAbstractStringToNumberConverter {
+ @Override
+ protected NumberFormat getFormat(Locale locale) {
+ NumberFormat numberFormat = super.getFormat(locale);
+ if (numberFormat instanceof DecimalFormat) {
+ ((DecimalFormat) numberFormat).setParseBigDecimal(true);
+ }
+
+ return numberFormat;
+ }
+
+ @Override
+ public BigDecimal convertToModel(String value,
+ Class extends BigDecimal> targetType, Locale locale)
+ throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException {
+ return (BigDecimal) convertToNumber(value, BigDecimal.class, locale);
+ }
+
+ @Override
+ public Class getModelType() {
+ return BigDecimal.class;
+ }
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigIntegerConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigIntegerConverter.java
new file mode 100644
index 0000000000..f2d3754dd2
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBigIntegerConverter.java
@@ -0,0 +1,67 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.math.BigDecimal;
+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.4
+ */
+public class LegacyStringToBigIntegerConverter extends
+ LegacyAbstractStringToNumberConverter {
+
+ @Override
+ protected NumberFormat getFormat(Locale locale) {
+ NumberFormat numberFormat = super.getFormat(locale);
+ if (numberFormat instanceof DecimalFormat) {
+ ((DecimalFormat) numberFormat).setParseBigDecimal(true);
+ }
+
+ return numberFormat;
+ }
+
+ @Override
+ public BigInteger convertToModel(String value,
+ Class extends BigInteger> targetType, Locale locale)
+ throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException {
+
+ BigDecimal bigDecimalValue = (BigDecimal) convertToNumber(value,
+ BigDecimal.class, locale);
+
+ return (bigDecimalValue != null) ? bigDecimalValue.toBigInteger()
+ : null;
+ }
+
+ @Override
+ public Class getModelType() {
+ return BigInteger.class;
+ }
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBooleanConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBooleanConverter.java
new file mode 100644
index 0000000000..7d36a9d248
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToBooleanConverter.java
@@ -0,0 +1,167 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.util.Locale;
+
+/**
+ * A converter that converts from {@link String} to {@link Boolean} and back. The String representation is given by
+ * {@link Boolean#toString()} or provided in constructor {@link #StringToBooleanConverter(String, String)}.
+ *
Leading and trailing white spaces are ignored when converting from a String.
+ *
For language-dependent representation, subclasses should overwrite {@link #getFalseString(Locale)} and {@link #getTrueString(Locale)}
+ *
+ * @author Vaadin Ltd
+ * @since 7.0
+ */
+public class LegacyStringToBooleanConverter implements LegacyConverter {
+
+ private final String trueString;
+
+ private final String falseString;
+
+ /**
+ * Creates converter with default string representations - "true" and "false"
+ *
+ */
+ public LegacyStringToBooleanConverter() {
+ this(Boolean.TRUE.toString(), Boolean.FALSE.toString());
+ }
+
+ /**
+ * Creates converter with custom string representation.
+ *
+ * @since 7.5.4
+ * @param falseString string representation for false
+ * @param trueString string representation for true
+ */
+ public LegacyStringToBooleanConverter(String trueString, String falseString) {
+ this.trueString = trueString;
+ this.falseString = falseString;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
+ * java.lang.Class, java.util.Locale)
+ */
+ @Override
+ public Boolean convertToModel(String value,
+ Class extends Boolean> targetType, Locale locale)
+ throws ConversionException {
+ if (value == null || value.isEmpty()) {
+ return null;
+ }
+
+ // Remove leading and trailing white space
+ value = value.trim();
+
+ if (getTrueString().equals(value)) {
+ return true;
+ } else if (getFalseString().equals(value)) {
+ return false;
+ } else {
+ throw new ConversionException("Cannot convert " + value + " to "
+ + getModelType().getName());
+ }
+ }
+
+ /**
+ * Gets the string representation for true. Default is "true", if not set in constructor.
+ *
+ * @return the string representation for true
+ */
+ protected String getTrueString() {
+ return trueString;
+ }
+
+ /**
+ * Gets the string representation for false. Default is "false", if not set in constructor.
+ *
+ * @return the string representation for false
+ */
+ protected String getFalseString() {
+ return falseString;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
+ * .Object, java.lang.Class, java.util.Locale)
+ */
+ @Override
+ public String convertToPresentation(Boolean value,
+ Class extends String> targetType, Locale locale)
+ throws ConversionException {
+ if (value == null) {
+ return null;
+ }
+ if (value) {
+ return getTrueString(locale);
+ } else {
+ return getFalseString(locale);
+ }
+ }
+
+ /**
+ * Gets the locale-depended string representation for false.
+ * Default is locale-independent value provided by {@link #getFalseString()}
+ *
+ * @since 7.5.4
+ * @param locale to be used
+ * @return the string representation for false
+ */
+ protected String getFalseString(Locale locale) {
+ return getFalseString();
+ }
+
+ /**
+ * Gets the locale-depended string representation for true.
+ * Default is locale-independent value provided by {@link #getTrueString()}
+ *
+ * @since 7.5.4
+ * @param locale to be used
+ * @return the string representation for true
+ */
+ protected String getTrueString(Locale locale) {
+ return getTrueString();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#getModelType()
+ */
+ @Override
+ public Class getModelType() {
+ return Boolean.class;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#getPresentationType()
+ */
+ @Override
+ public Class getPresentationType() {
+ return String.class;
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToByteConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToByteConverter.java
new file mode 100644
index 0000000000..0531322663
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToByteConverter.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.legacy.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.4
+ */
+public class LegacyStringToByteConverter extends
+ LegacyAbstractStringToNumberConverter {
+
+ /**
+ * 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 getModelType() {
+ return Byte.class;
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToCollectionConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToCollectionConverter.java
new file mode 100644
index 0000000000..59321d5fb3
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToCollectionConverter.java
@@ -0,0 +1,243 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.io.Serializable;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Locale;
+
+/**
+ * A converter that converts from {@link String} to {@link Collection} of tokens
+ * and back.
+ *
+ * Allows to break a string into tokens using delimiter. Each token can be
+ * converted to its own model using provided converter.
+ *
+ * Default constructor uses ", " as delimiter string and
+ * {@link String} for token types. Other constructors allow to configure
+ * delimiter and token types.
+ *
+ * @since 7.5.0
+ *
+ * @author Vaadin Ltd
+ */
+public class LegacyStringToCollectionConverter implements
+ LegacyConverter {
+
+ private final String delimiter;
+ private final LegacyConverter tokenConverter;
+ private final Class> tokenType;
+ private final CollectionFactory factory;
+
+ /**
+ * Creates converter with ", " as delimiter and {@link String}
+ * as token model type in collection.
+ */
+ public LegacyStringToCollectionConverter() {
+ this(", ", null, String.class);
+ }
+
+ /**
+ * Creates converter with given {@code delimiter} and {@link String} as
+ * token model type in collection.
+ *
+ * @param delimiter
+ * custom delimiter
+ */
+ public LegacyStringToCollectionConverter(String delimiter) {
+ this(delimiter, null, String.class);
+ }
+
+ /**
+ * Creates converter with given {@code tokenConverter} for convert tokens
+ * and expected {@code tokenType}.
+ *
+ * If {@code tokenConverter} is null then no conversation is done and
+ * {@link String} is used as token type in resulting model collection.
+ *
+ * @param tokenConverter
+ * converter for token
+ * @param tokenType
+ * expected token model type
+ */
+ public LegacyStringToCollectionConverter(LegacyConverter tokenConverter,
+ Class> tokenType) {
+ this(", ", tokenConverter, tokenType);
+ }
+
+ /**
+ * Creates converter with given {@code tokenConverter} for convert tokens
+ * and expected {@code tokenType}.
+ *
+ * If {@code tokenConverter} is null then no conversation is done and
+ * {@link String} is used as token type in resulting model collection.
+ *
+ * @param tokenConverter
+ * converter for token
+ * @param tokenType
+ * expected token model type
+ * @param delimiter
+ * delimiter in presentation string
+ */
+ public LegacyStringToCollectionConverter(String delimiter,
+ LegacyConverter tokenConverter, Class> tokenClass) {
+ this(delimiter, tokenConverter, tokenClass,
+ new DefaultCollectionFactory());
+ }
+
+ /**
+ * Creates converter with given {@code tokenConverter} for convert tokens
+ * and expected {@code tokenType}.
+ *
+ * If {@code tokenConverter} is null then no conversation is done and
+ * {@link String} is used as token type in resulting model collection.
+ *
+ * @param tokenConverter
+ * converter for token
+ * @param tokenType
+ * expected token model type
+ * @param delimiter
+ * delimiter in presentation string
+ * @param factory
+ * factory to create resulting collection
+ */
+ public LegacyStringToCollectionConverter(String delimiter,
+ LegacyConverter tokenConverter, Class> tokenClass,
+ CollectionFactory factory) {
+ if (delimiter == null || delimiter.isEmpty()) {
+ throw new IllegalArgumentException(
+ "Delimiter should be non-empty string");
+ }
+ this.delimiter = delimiter;
+ this.tokenConverter = tokenConverter;
+ tokenType = tokenClass;
+ this.factory = factory;
+ }
+
+ @Override
+ public Class getModelType() {
+ return Collection.class;
+ }
+
+ @Override
+ public Class getPresentationType() {
+ return String.class;
+ }
+
+ @Override
+ public Collection convertToModel(String value,
+ Class extends Collection> targetType, Locale locale)
+ throws LegacyConverter.ConversionException {
+ if (value == null) {
+ return null;
+ }
+
+ int index = value.indexOf(delimiter);
+ int previous = 0;
+ Collection result = factory.createCollection(targetType);
+ LegacyConverter converter = tokenConverter;
+ while (index != -1) {
+ collectToken(value.substring(previous, index), result, converter,
+ locale);
+ previous = index + delimiter.length();
+ index = value.indexOf(delimiter, previous);
+ }
+ collectToken(value.substring(previous), result, converter, locale);
+ return result;
+ }
+
+ @Override
+ public String convertToPresentation(Collection value,
+ Class extends String> targetType, Locale locale)
+ throws LegacyConverter.ConversionException {
+ if (value == null) {
+ return null;
+ }
+ StringBuilder builder = new StringBuilder();
+ LegacyConverter converter = tokenConverter;
+ for (Iterator> iterator = value.iterator(); iterator.hasNext();) {
+ if (converter == null) {
+ builder.append(iterator.next());
+ } else {
+ builder.append(converter.convertToPresentation(iterator.next(),
+ targetType, locale));
+ }
+ builder.append(delimiter);
+ }
+ if (builder.length() > 0) {
+ return builder.substring(0, builder.length() - delimiter.length());
+ } else {
+ return builder.toString();
+ }
+ }
+
+ private void collectToken(String token, Collection collection,
+ LegacyConverter converter, Locale locale) {
+ if (converter == null) {
+ collection.add(token);
+ } else {
+ collection.add(converter.convertToModel(token, tokenType, locale));
+ }
+ }
+
+ /**
+ * Default collection factory implementation.
+ *
+ * @author Vaadin Ltd
+ */
+ public static class DefaultCollectionFactory implements CollectionFactory {
+
+ @Override
+ public Collection> createCollection(Class extends Collection> type) {
+ if (type.isAssignableFrom(ArrayList.class)) {
+ return new ArrayList();
+ } else if (type.isAssignableFrom(HashSet.class)) {
+ return new HashSet();
+ } else if (!type.isInterface()
+ && !Modifier.isAbstract(type.getModifiers())) {
+ try {
+ return type.newInstance();
+ } catch (InstantiationException ignore) {
+ } catch (IllegalAccessException ignore) {
+ }
+ }
+ return new ArrayList();
+ }
+
+ }
+
+ /**
+ * Collection factory. Defines a strategy to create collection by collection
+ * class.
+ *
+ * @author Vaadin Ltd
+ */
+ public interface CollectionFactory extends Serializable {
+
+ /**
+ * Create collection by its {@code type}.
+ *
+ * @param type
+ * collection type
+ * @return instantiated collection with given {@code type}
+ */
+ Collection> createCollection(Class extends Collection> type);
+ }
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDateConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDateConverter.java
new file mode 100644
index 0000000000..40780bfdd0
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDateConverter.java
@@ -0,0 +1,131 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.text.DateFormat;
+import java.text.ParsePosition;
+import java.util.Date;
+import java.util.Locale;
+
+/**
+ * A converter that converts from {@link Date} to {@link String} and back. Uses
+ * the given locale and {@link DateFormat} 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.0
+ */
+public class LegacyStringToDateConverter implements LegacyConverter {
+
+ /**
+ * Returns the format used by
+ * {@link #convertToPresentation(Date, Class,Locale)} and
+ * {@link #convertToModel(String, Class, Locale)}.
+ *
+ * @param locale
+ * The locale to use
+ * @return A DateFormat instance
+ */
+ protected DateFormat getFormat(Locale locale) {
+ if (locale == null) {
+ locale = Locale.getDefault();
+ }
+
+ DateFormat f = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
+ DateFormat.MEDIUM, locale);
+ f.setLenient(false);
+ return f;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
+ * java.lang.Class, java.util.Locale)
+ */
+ @Override
+ public Date convertToModel(String value, Class extends Date> targetType,
+ Locale locale)
+ throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException {
+ if (targetType != getModelType()) {
+ throw new ConversionException("Converter only supports "
+ + getModelType().getName() + " (targetType was "
+ + targetType.getName() + ")");
+ }
+
+ if (value == null) {
+ return null;
+ }
+
+ // Remove leading and trailing white space
+ value = value.trim();
+
+ ParsePosition parsePosition = new ParsePosition(0);
+ Date parsedValue = getFormat(locale).parse(value, parsePosition);
+ if (parsePosition.getIndex() != value.length()) {
+ throw new ConversionException("Could not convert '" + value
+ + "' to " + getModelType().getName());
+ }
+
+ return parsedValue;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.vaadin.data.util.converter.Converter#convertToPresentation(java.lang
+ * .Object, java.lang.Class, java.util.Locale)
+ */
+ @Override
+ public String convertToPresentation(Date value,
+ Class extends String> targetType, Locale locale)
+ throws com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException {
+ if (value == null) {
+ return null;
+ }
+
+ return getFormat(locale).format(value);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#getModelType()
+ */
+ @Override
+ public Class getModelType() {
+ return Date.class;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#getPresentationType()
+ */
+ @Override
+ public Class getPresentationType() {
+ return String.class;
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDoubleConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDoubleConverter.java
new file mode 100644
index 0000000000..191648fba9
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToDoubleConverter.java
@@ -0,0 +1,64 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+/**
+ * A converter that converts from {@link String} to {@link Double} 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.0
+ */
+public class LegacyStringToDoubleConverter extends
+ LegacyAbstractStringToNumberConverter {
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
+ * java.util.Locale)
+ */
+ @Override
+ public Double convertToModel(String value,
+ Class extends Double> targetType, Locale locale)
+ throws ConversionException {
+ Number n = convertToNumber(value, targetType, locale);
+ return n == null ? null : n.doubleValue();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#getModelType()
+ */
+ @Override
+ public Class getModelType() {
+ return Double.class;
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToEnumConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToEnumConverter.java
new file mode 100644
index 0000000000..ef1dec64eb
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToEnumConverter.java
@@ -0,0 +1,160 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.util.EnumSet;
+import java.util.Locale;
+
+/**
+ * A converter that converts from {@link String} to an {@link Enum} and back.
+ *
+ * Designed to provide nice human readable strings for {@link Enum} classes
+ * conforming to one of these patterns:
+ *
+ *
The constants are named SOME_UPPERCASE_WORDS and there's no toString
+ * implementation.
+ *
toString() always returns the same human readable string that is not the
+ * same as its name() value. Each constant in the enum type returns a distinct
+ * toString() value.
+ *
+ * Will not necessarily work correctly for other cases.
+ *
+ *
+ * @author Vaadin Ltd
+ * @since 7.4
+ */
+public class LegacyStringToEnumConverter implements LegacyConverter {
+
+ @Override
+ public Enum convertToModel(String value, Class extends Enum> targetType,
+ Locale locale) throws ConversionException {
+ if (value == null || value.trim().equals("")) {
+ return null;
+ }
+
+ return stringToEnum(value, targetType, locale);
+ }
+
+ /**
+ * Converts the given string to the given enum type using the given locale
+ *
+ * Compatible with {@link #enumToString(Enum, Locale)}
+ *
+ * @param value
+ * The string value to convert
+ * @param enumType
+ * The type of enum to create
+ * @param locale
+ * The locale to use for conversion. If null, the JVM default
+ * locale will be used
+ * @return The enum which matches the given string
+ * @throws ConversionException
+ * if the conversion fails
+ */
+ public static > T stringToEnum(String value,
+ Class enumType, Locale locale) throws ConversionException {
+ if (locale == null) {
+ locale = Locale.getDefault();
+ }
+
+ if (!enumType.isEnum()) {
+ throw new ConversionException(enumType.getName()
+ + " is not an enum type");
+ }
+
+ // First test for the human-readable value since that's the more likely
+ // input
+ String upperCaseValue = value.toUpperCase(locale);
+ T match = null;
+ for (T e : EnumSet.allOf(enumType)) {
+ String upperCase = enumToString(e, locale).toUpperCase(locale);
+ if (upperCase.equals(upperCaseValue)) {
+ if (match != null) {
+ throw new ConversionException("Both " + match.name()
+ + " and " + e.name()
+ + " are matching the input string " + value);
+ }
+ match = e;
+ }
+ }
+
+ if (match != null) {
+ return match;
+ }
+
+ // Then fall back to using a strict match based on name()
+ try {
+ return Enum.valueOf(enumType, upperCaseValue);
+ } catch (Exception ee) {
+ throw new ConversionException(ee);
+ }
+ }
+
+ /**
+ * Converts the given enum to a human readable string using the given locale
+ *
+ * Compatible with {@link #stringToEnum(String, Class, Locale)}
+ *
+ * @param value
+ * The enum value to convert
+ * @param locale
+ * The locale to use for conversion. If null, the JVM default
+ * locale will be used
+ * @return A human readable string based on the enum
+ * @throws ConversionException
+ * if the conversion fails
+ */
+ public static String enumToString(Enum> value, Locale locale) {
+ if (locale == null) {
+ locale = Locale.getDefault();
+ }
+
+ String enumString = value.toString();
+ if (enumString.equals(value.name())) {
+ // FOO -> Foo
+ // FOO_BAR -> Foo bar
+ // _FOO -> _foo
+ String result = enumString.substring(0, 1).toUpperCase(locale);
+ result += enumString.substring(1).toLowerCase(locale)
+ .replace('_', ' ');
+ return result;
+ } else {
+ return enumString;
+ }
+ }
+
+ @Override
+ public String convertToPresentation(Enum value,
+ Class extends String> targetType, Locale locale)
+ throws ConversionException {
+ if (value == null) {
+ return null;
+ }
+
+ return enumToString(value, locale);
+ }
+
+ @Override
+ public Class getModelType() {
+ return Enum.class;
+ }
+
+ @Override
+ public Class getPresentationType() {
+ return String.class;
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToFloatConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToFloatConverter.java
new file mode 100644
index 0000000000..0d24e16a35
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToFloatConverter.java
@@ -0,0 +1,64 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+/**
+ * A converter that converts from {@link String} to {@link Float} 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.0
+ */
+public class LegacyStringToFloatConverter extends
+ LegacyAbstractStringToNumberConverter {
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.vaadin.data.util.converter.Converter#convertToModel(java.lang.Object,
+ * java.util.Locale)
+ */
+ @Override
+ public Float convertToModel(String value,
+ Class extends Float> targetType, Locale locale)
+ throws ConversionException {
+ Number n = convertToNumber(value, targetType, locale);
+ return n == null ? null : n.floatValue();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#getModelType()
+ */
+ @Override
+ public Class getModelType() {
+ return Float.class;
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToIntegerConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToIntegerConverter.java
new file mode 100644
index 0000000000..37c00b7c3e
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToIntegerConverter.java
@@ -0,0 +1,94 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+/**
+ * A converter that converts from {@link String} to {@link Integer} 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.0
+ */
+public class LegacyStringToIntegerConverter extends
+ LegacyAbstractStringToNumberConverter {
+
+ /**
+ * Returns the format used by
+ * {@link #convertToPresentation(Integer, 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 Integer convertToModel(String value,
+ Class extends Integer> targetType, Locale locale)
+ throws ConversionException {
+ Number n = convertToNumber(value, targetType, locale);
+
+ if (n == null) {
+ return null;
+ }
+
+ int intValue = n.intValue();
+ if (intValue == n.longValue()) {
+ // If the value of n is outside the range of long, the return value
+ // of longValue() is either Long.MIN_VALUE or Long.MAX_VALUE. The
+ // above comparison promotes int to long and thus does not need to
+ // consider wrap-around.
+ return intValue;
+ }
+
+ throw new ConversionException("Could not convert '" + value + "' to "
+ + Integer.class.getName() + ": value out of range");
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#getModelType()
+ */
+ @Override
+ public Class getModelType() {
+ return Integer.class;
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToLongConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToLongConverter.java
new file mode 100644
index 0000000000..0de27bc3f3
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToLongConverter.java
@@ -0,0 +1,78 @@
+/*
+ * 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.legacy.data.util.converter;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+/**
+ * A converter that converts from {@link String} to {@link Long} 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.2
+ */
+public class LegacyStringToLongConverter extends
+ LegacyAbstractStringToNumberConverter {
+
+ /**
+ * Returns the format used by
+ * {@link #convertToPresentation(Long, 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 Long convertToModel(String value, Class extends Long> targetType,
+ Locale locale) throws ConversionException {
+ Number n = convertToNumber(value, targetType, locale);
+ return n == null ? null : n.longValue();
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.data.util.converter.Converter#getModelType()
+ */
+ @Override
+ public Class getModelType() {
+ return Long.class;
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToShortConverter.java b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToShortConverter.java
new file mode 100644
index 0000000000..8b1646542f
--- /dev/null
+++ b/server/src/main/java/com/vaadin/legacy/data/util/converter/LegacyStringToShortConverter.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.legacy.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.4
+ */
+public class LegacyStringToShortConverter extends
+ LegacyAbstractStringToNumberConverter {
+
+ /**
+ * 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 getModelType() {
+ return Short.class;
+ }
+
+}
diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDoubleValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDoubleValidator.java
index 0c804e7247..dd45f365d0 100644
--- a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDoubleValidator.java
+++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyDoubleValidator.java
@@ -16,7 +16,7 @@
package com.vaadin.legacy.data.validator;
import com.vaadin.data.Property;
-import com.vaadin.data.util.converter.StringToDoubleConverter;
+import com.vaadin.legacy.data.util.converter.LegacyStringToDoubleConverter;
/**
* String validator for a double precision floating point number. See
@@ -25,7 +25,7 @@ import com.vaadin.data.util.converter.StringToDoubleConverter;
*
* @author Vaadin Ltd.
* @since 5.4
- * @deprecated As of 7.0. Use a {@link StringToDoubleConverter} converter on the
+ * @deprecated As of 7.0. Use a {@link LegacyStringToDoubleConverter} converter on the
* field instead or bind the field to a {@link Property} of type
* {@link Double}.
*/
diff --git a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyIntegerValidator.java b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyIntegerValidator.java
index e6c2bae815..dc34215680 100644
--- a/server/src/main/java/com/vaadin/legacy/data/validator/LegacyIntegerValidator.java
+++ b/server/src/main/java/com/vaadin/legacy/data/validator/LegacyIntegerValidator.java
@@ -16,7 +16,7 @@
package com.vaadin.legacy.data.validator;
import com.vaadin.data.Property;
-import com.vaadin.data.util.converter.StringToIntegerConverter;
+import com.vaadin.legacy.data.util.converter.LegacyStringToIntegerConverter;
/**
* String validator for integers. See
@@ -25,7 +25,7 @@ import com.vaadin.data.util.converter.StringToIntegerConverter;
*
* @author Vaadin Ltd.
* @since 5.4
- * @deprecated As of 7.0. Use a {@link StringToIntegerConverter} converter on
+ * @deprecated As of 7.0. Use a {@link LegacyStringToIntegerConverter} converter on
* the field instead or bind the field to a {@link Property} of type
* {@link Integer}.
*/
diff --git a/server/src/main/java/com/vaadin/legacy/ui/LegacyAbstractField.java b/server/src/main/java/com/vaadin/legacy/ui/LegacyAbstractField.java
index 5beada527f..402f1fb257 100644
--- a/server/src/main/java/com/vaadin/legacy/ui/LegacyAbstractField.java
+++ b/server/src/main/java/com/vaadin/legacy/ui/LegacyAbstractField.java
@@ -33,15 +33,15 @@ import org.jsoup.nodes.Element;
import com.vaadin.data.Buffered;
import com.vaadin.data.Property;
import com.vaadin.data.util.LegacyPropertyHelper;
-import com.vaadin.data.util.converter.Converter;
-import com.vaadin.data.util.converter.Converter.ConversionException;
-import com.vaadin.data.util.converter.ConverterUtil;
import com.vaadin.event.Action;
import com.vaadin.event.ShortcutAction;
import com.vaadin.event.ShortcutListener;
import com.vaadin.legacy.data.Validatable;
import com.vaadin.legacy.data.Validator;
import com.vaadin.legacy.data.Validator.InvalidValueException;
+import com.vaadin.legacy.data.util.converter.LegacyConverter;
+import com.vaadin.legacy.data.util.converter.LegacyConverterUtil;
+import com.vaadin.legacy.data.util.converter.LegacyConverter.ConversionException;
import com.vaadin.server.AbstractErrorMessage;
import com.vaadin.server.CompositeErrorMessage;
import com.vaadin.server.ErrorMessage;
@@ -99,7 +99,7 @@ public abstract class LegacyAbstractField extends AbstractComponent
* A converter used to convert from the data model type to the field type
* and vice versa.
*/
- private Converter converter = null;
+ private LegacyConverter converter = null;
/**
* Connected data-source.
*/
@@ -459,7 +459,7 @@ public abstract class LegacyAbstractField extends AbstractComponent
*/
@Override
public void setValue(T newFieldValue) throws Property.ReadOnlyException,
- Converter.ConversionException {
+ LegacyConverter.ConversionException {
setValue(newFieldValue, false);
}
@@ -471,7 +471,7 @@ public abstract class LegacyAbstractField extends AbstractComponent
* @param repaintIsNotNeeded
* True iff caller is sure that repaint is not needed.
* @throws Property.ReadOnlyException
- * @throws Converter.ConversionException
+ * @throws LegacyConverter.ConversionException
* @throws InvalidValueException
*/
protected void setValue(T newFieldValue, boolean repaintIsNotNeeded) {
@@ -489,12 +489,12 @@ public abstract class LegacyAbstractField extends AbstractComponent
* @param ignoreReadOnly
* True iff if the read-only check should be ignored
* @throws Property.ReadOnlyException
- * @throws Converter.ConversionException
+ * @throws LegacyConverter.ConversionException
* @throws InvalidValueException
*/
protected void setValue(T newFieldValue, boolean repaintIsNotNeeded,
boolean ignoreReadOnly) throws Property.ReadOnlyException,
- Converter.ConversionException, InvalidValueException {
+ LegacyConverter.ConversionException, InvalidValueException {
if (!SharedUtil.equals(newFieldValue, getInternalValue())) {
@@ -656,7 +656,7 @@ public abstract class LegacyAbstractField extends AbstractComponent
// Check if the current converter is compatible.
if (newDataSource != null
- && !ConverterUtil.canConverterPossiblyHandle(getConverter(),
+ && !LegacyConverterUtil.canConverterPossiblyHandle(getConverter(),
getType(), newDataSource.getType())) {
// There is no converter set or there is no way the current
// converter can be compatible.
@@ -714,7 +714,7 @@ public abstract class LegacyAbstractField extends AbstractComponent
* from
*/
public void setConverter(Class> datamodelType) {
- Converter c = (Converter) ConverterUtil.getConverter(
+ LegacyConverter c = (LegacyConverter) LegacyConverterUtil.getConverter(
getType(), datamodelType, getSession());
setConverter(c);
}
@@ -726,7 +726,7 @@ public abstract class LegacyAbstractField extends AbstractComponent
* The data source value to convert.
* @return The converted value that is compatible with the UI type or the
* original value if its type is compatible and no converter is set.
- * @throws Converter.ConversionException
+ * @throws LegacyConverter.ConversionException
* if there is no converter and the type is not compatible with
* the data source type.
*/
@@ -741,12 +741,12 @@ public abstract class LegacyAbstractField extends AbstractComponent
* The data source value to convert.
* @return The converted value that is compatible with the UI type or the
* original value if its type is compatible and no converter is set.
- * @throws Converter.ConversionException
+ * @throws LegacyConverter.ConversionException
* if there is no converter and the type is not compatible with
* the data source type.
*/
private T convertFromModel(Object newValue, Locale locale) {
- return ConverterUtil.convertFromModel(newValue, getType(),
+ return LegacyConverterUtil.convertFromModel(newValue, getType(),
getConverter(), locale);
}
@@ -757,12 +757,12 @@ public abstract class LegacyAbstractField extends AbstractComponent
* The value to convert. Typically returned by
* {@link #getFieldValue()}
* @return The converted value that is compatible with the data source type.
- * @throws Converter.ConversionException
+ * @throws LegacyConverter.ConversionException
* if there is no converter and the type is not compatible with
* the data source type.
*/
private Object convertToModel(T fieldValue)
- throws Converter.ConversionException {
+ throws LegacyConverter.ConversionException {
return convertToModel(fieldValue, getLocale());
}
@@ -775,15 +775,15 @@ public abstract class LegacyAbstractField extends AbstractComponent
* @param locale
* The locale to use for the conversion
* @return The converted value that is compatible with the data source type.
- * @throws Converter.ConversionException
+ * @throws LegacyConverter.ConversionException
* if there is no converter and the type is not compatible with
* the data source type.
*/
private Object convertToModel(T fieldValue, Locale locale)
- throws Converter.ConversionException {
+ throws LegacyConverter.ConversionException {
Class> modelType = getModelType();
try {
- return ConverterUtil.convertToModel(fieldValue,
+ return LegacyConverterUtil.convertToModel(fieldValue,
(Class