aboutsummaryrefslogtreecommitdiffstats
path: root/compatibility-server/src
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2016-08-29 14:59:28 +0300
committerVaadin Code Review <review@vaadin.com>2016-08-29 13:33:07 +0000
commit9fce56f6a4b81b69bbdf70a5434a51d6f85aebd8 (patch)
treec7217ab9b41480d56805d7feedc0497c83af829a /compatibility-server/src
parentae2fc6890a04e677bfc4cbe2a4e6636f8f5ee20c (diff)
downloadvaadin-framework-9fce56f6a4b81b69bbdf70a5434a51d6f85aebd8.tar.gz
vaadin-framework-9fce56f6a4b81b69bbdf70a5434a51d6f85aebd8.zip
Move V7 Converter and ConverterFactory to compatibility package
Change-Id: I48d1ea501a621f653bde840d646ae01e6edc3eea
Diffstat (limited to 'compatibility-server/src')
-rw-r--r--compatibility-server/src/main/java/com/vaadin/v7/data/util/converter/Converter.java177
-rw-r--r--compatibility-server/src/main/java/com/vaadin/v7/data/util/converter/ConverterFactory.java34
-rw-r--r--compatibility-server/src/main/java/com/vaadin/v7/data/util/converter/ConverterUtil.java21
3 files changed, 224 insertions, 8 deletions
diff --git a/compatibility-server/src/main/java/com/vaadin/v7/data/util/converter/Converter.java b/compatibility-server/src/main/java/com/vaadin/v7/data/util/converter/Converter.java
new file mode 100644
index 0000000000..cad8897c15
--- /dev/null
+++ b/compatibility-server/src/main/java/com/vaadin/v7/data/util/converter/Converter.java
@@ -0,0 +1,177 @@
+/*
+ * Copyright 2000-2016 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.v7.data.util.converter;
+
+import java.io.Serializable;
+import java.util.Locale;
+
+/**
+ * Interface that implements conversion between a model and a presentation type.
+ * <p>
+ * 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.
+ * </p>
+ * <p>
+ * Converters must not have any side effects (never update UI from inside a
+ * converter).
+ * </p>
+ * <p>
+ * All Converters must be stateless and thread safe.
+ * </p>
+ * <p>
+ * If conversion of a value fails, a {@link ConversionException} is thrown.
+ * </p>
+ *
+ * @param <PRESENTATION>
+ * The presentation type. Must be compatible with what
+ * {@link #getPresentationType()} returns.
+ * @param <MODEL>
+ * The model type. Must be compatible with what
+ * {@link #getModelType()} returns.
+ * @author Vaadin Ltd.
+ * @since 7.0
+ */
+@Deprecated
+public interface Converter<PRESENTATION, MODEL> extends Serializable {
+
+ /**
+ * Converts the given value from target type to source type.
+ * <p>
+ * A converter can optionally use locale to do the conversion.
+ * </p>
+ * 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.
+ * <p>
+ * A converter can optionally use locale to do the conversion.
+ * </p>
+ * 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<MODEL> 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<PRESENTATION> 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 <code>ConversionException</code> without a detail
+ * message.
+ */
+ public ConversionException() {
+ }
+
+ /**
+ * Constructs a new <code>ConversionException</code> 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 <code>ConversionException</code> 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/compatibility-server/src/main/java/com/vaadin/v7/data/util/converter/ConverterFactory.java b/compatibility-server/src/main/java/com/vaadin/v7/data/util/converter/ConverterFactory.java
new file mode 100644
index 0000000000..1730a15e10
--- /dev/null
+++ b/compatibility-server/src/main/java/com/vaadin/v7/data/util/converter/ConverterFactory.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2000-2016 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.v7.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
+ *
+ */
+@Deprecated
+public interface ConverterFactory extends Serializable {
+ public <PRESENTATION, MODEL> Converter<PRESENTATION, MODEL> createConverter(
+ Class<PRESENTATION> presentationType, Class<MODEL> modelType);
+
+}
diff --git a/compatibility-server/src/main/java/com/vaadin/v7/data/util/converter/ConverterUtil.java b/compatibility-server/src/main/java/com/vaadin/v7/data/util/converter/ConverterUtil.java
index b0679e9caf..0a874f465f 100644
--- a/compatibility-server/src/main/java/com/vaadin/v7/data/util/converter/ConverterUtil.java
+++ b/compatibility-server/src/main/java/com/vaadin/v7/data/util/converter/ConverterUtil.java
@@ -51,7 +51,13 @@ public class ConverterUtil implements Serializable {
}
if (session != null) {
- ConverterFactory factory = session.getConverterFactory();
+ ConverterFactory factory = (ConverterFactory) session
+ .getConverterFactory();
+ if (factory == null) {
+ // Assume DefaultConverterFactory should be in session
+ factory = new DefaultConverterFactory();
+ session.setConverterFactory(factory);
+ }
converter = factory.createConverter(presentationType, modelType);
}
return converter;
@@ -84,8 +90,8 @@ public class ConverterUtil implements Serializable {
public static <PRESENTATIONTYPE, MODELTYPE> PRESENTATIONTYPE convertFromModel(
MODELTYPE modelValue,
Class<? extends PRESENTATIONTYPE> presentationType,
- Converter<PRESENTATIONTYPE, MODELTYPE> converter,
- Locale locale) throws Converter.ConversionException {
+ Converter<PRESENTATIONTYPE, MODELTYPE> converter, Locale locale)
+ throws Converter.ConversionException {
if (converter != null) {
/*
* If there is a converter, always use it. It must convert or throw
@@ -145,8 +151,8 @@ public class ConverterUtil implements Serializable {
*/
public static <MODELTYPE, PRESENTATIONTYPE> MODELTYPE convertToModel(
PRESENTATIONTYPE presentationValue, Class<MODELTYPE> modelType,
- Converter<PRESENTATIONTYPE, MODELTYPE> converter,
- Locale locale) throws Converter.ConversionException {
+ Converter<PRESENTATIONTYPE, MODELTYPE> converter, Locale locale)
+ throws Converter.ConversionException {
if (converter != null) {
/*
* If there is a converter, always use it. It must convert or throw
@@ -233,9 +239,8 @@ public class ConverterUtil implements Serializable {
* @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) {
+ public static boolean canConverterPossiblyHandle(Converter<?, ?> converter,
+ Class<?> presentationType, Class<?> modelType) {
if (converter == null) {
return false;
}