Skip to content

Commit

Permalink
Rename ModelConverter related classes (#3750)
Browse files Browse the repository at this point in the history
Fixes #3382
  • Loading branch information
Denis committed Mar 26, 2018
1 parent 56711c5 commit 9b7748f
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected BeanModelType(Class<T> proxyType,
}

private BeanModelType(Class<T> javaType, PropertyFilter propertyFilter,
PathLookup<ModelConverter<?, ?>> converterLookup,
PathLookup<ModelEncoder<?, ?>> converterLookup,
PathLookup<ClientUpdateMode> clientUpdateLookup) {
this(javaType,
new PropertyMapBuilder(javaType, propertyFilter,
Expand Down Expand Up @@ -154,7 +154,7 @@ protected BeanModelType(Class<T> javaType, PropertyFilter propertyFilter,
static ModelType getModelType(Type propertyType,
PropertyFilter propertyFilter, String propertyName,
Class<?> declaringClass,
PathLookup<ModelConverter<?, ?>> converterLookup,
PathLookup<ModelEncoder<?, ?>> converterLookup,
PathLookup<ClientUpdateMode> clientUpdateLookup) {
if (propertyType instanceof Class<?>) {
Class<?> propertyTypeClass = (Class<?>) propertyType;
Expand Down Expand Up @@ -183,7 +183,7 @@ static ModelType getModelType(Type propertyType,
static ModelType getConvertedModelType(Type propertyType,
PropertyFilter propertyFilter, String propertyName,
Class<?> declaringClass,
PathLookup<ModelConverter<?, ?>> converterLookup,
PathLookup<ModelEncoder<?, ?>> converterLookup,
PathLookup<ClientUpdateMode> clientUpdateLookup) {

if (!(propertyType instanceof Class<?>)) {
Expand All @@ -193,7 +193,7 @@ static ModelType getConvertedModelType(Type propertyType,
declaringClass.getSimpleName(), propertyName));
}

Optional<ModelConverter<?, ?>> converterOptional = converterLookup
Optional<ModelEncoder<?, ?>> converterOptional = converterLookup
.getItem(propertyFilter.getPrefix());
if (!converterOptional.isPresent()) {
throw new IllegalStateException(
Expand All @@ -202,21 +202,21 @@ static ModelType getConvertedModelType(Type propertyType,
+ "for the given PropertyFilter.");
}

ModelConverter<?, ?> converter = converterOptional.get();
if (!converter.getModelType().equals(propertyType)) {
ModelEncoder<?, ?> converter = converterOptional.get();
if (!converter.getDecodedType().equals(propertyType)) {
throw new InvalidTemplateModelException(String.format(
"Converter '%s' is incompatible with the type '%s'.",
converter.getClass().getName(),
propertyType.getTypeName()));
}

if (isBean(converter.getPresentationType())) {
if (isBean(converter.getEncodedType())) {
return new ConvertedModelType<>(new BeanModelType<>(
converter.getPresentationType(), propertyFilter,
converter.getEncodedType(), propertyFilter,
converterLookup, clientUpdateLookup), converter);
} else {
Optional<ModelType> maybeBasicModelType = BasicModelType
.get(converter.getPresentationType());
.get(converter.getEncodedType());
if (maybeBasicModelType.isPresent()) {
return new ConvertedModelType<>(maybeBasicModelType.get(),
converter);
Expand All @@ -233,7 +233,7 @@ static ModelType getConvertedModelType(Type propertyType,
private static ModelType getListModelType(Type propertyType,
PropertyFilter propertyFilter, String propertyName,
Class<?> declaringClass,
PathLookup<ModelConverter<?, ?>> converterLookup,
PathLookup<ModelEncoder<?, ?>> converterLookup,
PathLookup<ClientUpdateMode> clientUpdateLookup) {
assert ListModelType.isList(propertyType);
ParameterizedType pt = (ParameterizedType) propertyType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

/**
* A {@link ModelType} implementation that wraps a model type for performing
* type conversions on together with a {@link ModelConverter}.
* type conversions on together with a {@link ModelEncoder}.
*
* @author Vaadin Ltd
*
Expand All @@ -37,7 +37,7 @@ public class ConvertedModelType<A, M extends Serializable>
implements ModelType {

private final ModelType wrappedModelType;
private final ModelConverter<A, M> converter;
private final ModelEncoder<A, M> converter;

/**
* Creates a new ConvertedModelType from the given model type and converter.
Expand All @@ -47,7 +47,7 @@ public class ConvertedModelType<A, M extends Serializable>
* @param converter
* the converter to use
*/
ConvertedModelType(ModelType modelType, ModelConverter<A, M> converter) {
ConvertedModelType(ModelType modelType, ModelEncoder<A, M> converter) {
wrappedModelType = modelType;
this.converter = converter;
}
Expand All @@ -57,7 +57,7 @@ public Object modelToApplication(Serializable modelValue) {
@SuppressWarnings("unchecked")
M wrappedApplicationValue = (M) wrappedModelType
.modelToApplication(modelValue);
return converter.toModel(wrappedApplicationValue);
return converter.decode(wrappedApplicationValue);
}

@Override
Expand All @@ -69,19 +69,19 @@ public Object modelToNashorn(Serializable modelValue) {
public Serializable applicationToModel(Object applicationValue,
PropertyFilter filter) {
@SuppressWarnings("unchecked")
M convertedValue = converter.toPresentation((A) applicationValue);
M convertedValue = converter.encode((A) applicationValue);
return wrappedModelType.applicationToModel(convertedValue, filter);
}

@Override
public boolean accepts(Type applicationType) {
return converter.getModelType()
return converter.getDecodedType()
.isAssignableFrom((Class<?>) applicationType);
}

@Override
public Type getJavaType() {
return converter.getModelType();
return converter.getDecodedType();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,52 @@
import java.lang.annotation.Target;

/**
* Defines a ModelConverter on a template model property found through
* Defines a ModelEncoder on a template model property found through
* {@link #path()}.
* <p>
* Use this annotation on setters in your {@link TemplateModel} class to perform
* type conversions on properties.
*
* @see ModelConverter
* @see ModelEncoder
*
* @author Vaadin Ltd
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Repeatable(Convert.Container.class)
public @interface Convert {
@Repeatable(Encode.Container.class)
public @interface Encode {

/**
* The ModelConverter class to use for conversion of the property found
* through {{@link #path()}.
* The ModelEncoder class to use for encoding the property found through
* {{@link #path()}.
*
* @return the ModelConverter class
* @return the ModelEncoder class
*/
Class<? extends ModelConverter<?, ?>> value();
Class<? extends ModelEncoder<?, ?>> value();

/**
* The dot separated path from the TemplateModel property to the value to
* apply conversion to. Empty string by default, which will apply conversion
* apply encoding to. Empty string by default, which will apply encoding
* directly to the property.
*
* @return the dot separated path to the bean property to convert, empty
* @return the dot separated path to the bean property to encode, empty
* string by default
*/
String path() default "";

/**
* Internal annotation to enable use of multiple {@link Convert}
* annotations.
* Internal annotation to enable use of multiple {@link Encode} annotations.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface Container {

/**
* Internally used to enable use of multiple {@link Convert}
* annotations.
* Internally used to enable use of multiple {@link Encode} annotations.
*
* @return an array of the Convert annotations
* @return an array of the Encode annotations
*/
Convert[] value();
Encode[] value();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright 2000-2017 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.flow.templatemodel;

import java.io.Serializable;
import java.lang.reflect.Type;

import com.googlecode.gentyref.GenericTypeReflector;

/**
* Interface for implementing type encodings in template models together with
* the {@link Encode} annotation. Used for enabling the use of types in template
* model methods that are not natively supported by the framework.
*
* @see Encode
*
* @author Vaadin Ltd
*
* @param <D>
* the decoded type
* @param <E>
* the encoded type
*/
public interface ModelEncoder<D, E extends Serializable> extends Serializable {

/**
* Get the decoded type of this encoder.
*
* @return the application type
*/
@SuppressWarnings("unchecked")
default Class<D> getDecodedType() {
Type type = GenericTypeReflector.getTypeParameter(this.getClass(),
ModelEncoder.class.getTypeParameters()[0]);
if (type instanceof Class<?>) {
return (Class<D>) GenericTypeReflector.erase(type);
}
throw new InvalidTemplateModelException(String.format(
"Could not detect the model type of %s '%s'. "
+ "The method 'getDecodedType' needs to be overridden manually.",
ModelEncoder.class.getSimpleName(), this.getClass().getName()));
}

/**
* Get the encoded type of this encoder.
*
* @return the model type
*/
@SuppressWarnings("unchecked")
default Class<E> getEncodedType() {
Type type = GenericTypeReflector.getTypeParameter(this.getClass(),
ModelEncoder.class.getTypeParameters()[1]);
if (type instanceof Class<?>) {
return (Class<E>) GenericTypeReflector.erase(type);
}
throw new InvalidTemplateModelException(String.format(
"Could not detect the presentation type of %s '%s'. "
+ "The method 'getEncodedType' needs to be overridden manually.",
ModelEncoder.class.getSimpleName(), this.getClass().getName()));
}

/**
* Encodes the given value.
*
* @param value
* the value to encode
* @return the encoded model value
*/
E encode(D value);

/**
* Decodes the given value.
*
* @param value
* the value to decode
* @return the decoded value
*/
D decode(E value);
}

0 comments on commit 9b7748f

Please sign in to comment.