You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by th...@apache.org on 2020/12/05 20:29:12 UTC
[tapestry-5] 01/03: TAP5-2650: Change TypeCoercer configuration to
be a mapped one
This is an automated email from the ASF dual-hosted git repository.
thiagohp pushed a commit to branch java9modules
in repository https://gitbox.apache.org/repos/asf/tapestry-5.git
commit ced575607bfac23c06751b15f7ed22c8aabc67d9
Author: Thiago H. de Paula Figueiredo <th...@arsmachina.com.br>
AuthorDate: Tue Dec 1 19:32:21 2020 -0300
TAP5-2650: Change TypeCoercer configuration to be a mapped one
---
.../beanmodel/BeanModelSourceBuilder.java | 34 ++++++++-----
.../commons/internal/BasicTypeCoercions.java | 30 ++++--------
.../commons/internal/services/TypeCoercerImpl.java | 4 +-
.../tapestry5/commons/services/CoercionTuple.java | 57 ++++++++++++++++++++++
.../tapestry5/commons/services/TypeCoercer.java | 4 +-
.../tapestry5/util/EnumValueEncoderTest.java | 8 ++-
.../tapestry5/ioc/modules/TapestryIOCModule.java | 2 +-
7 files changed, 101 insertions(+), 38 deletions(-)
diff --git a/beanmodel/src/main/java/org/apache/tapestry5/beanmodel/BeanModelSourceBuilder.java b/beanmodel/src/main/java/org/apache/tapestry5/beanmodel/BeanModelSourceBuilder.java
index 040439b..1691f99 100644
--- a/beanmodel/src/main/java/org/apache/tapestry5/beanmodel/BeanModelSourceBuilder.java
+++ b/beanmodel/src/main/java/org/apache/tapestry5/beanmodel/BeanModelSourceBuilder.java
@@ -11,6 +11,10 @@
// limitations under the License.
package org.apache.tapestry5.beanmodel;
+import java.lang.annotation.Annotation;
+import java.util.HashMap;
+import java.util.Map;
+
import org.apache.tapestry5.beanmodel.internal.services.BeanModelSourceImpl;
import org.apache.tapestry5.beanmodel.internal.services.PropertyAccessImpl;
import org.apache.tapestry5.beanmodel.internal.services.PropertyConduitSourceImpl;
@@ -18,7 +22,7 @@ import org.apache.tapestry5.beanmodel.services.BeanModelSource;
import org.apache.tapestry5.beanmodel.services.PlasticProxyFactoryImpl;
import org.apache.tapestry5.beanmodel.services.PropertyConduitSource;
import org.apache.tapestry5.commons.AnnotationProvider;
-import org.apache.tapestry5.commons.Configuration;
+import org.apache.tapestry5.commons.MappedConfiguration;
import org.apache.tapestry5.commons.ObjectLocator;
import org.apache.tapestry5.commons.internal.BasicDataTypeAnalyzers;
import org.apache.tapestry5.commons.internal.BasicTypeCoercions;
@@ -27,16 +31,13 @@ import org.apache.tapestry5.commons.internal.services.StringInternerImpl;
import org.apache.tapestry5.commons.internal.services.TypeCoercerImpl;
import org.apache.tapestry5.commons.internal.util.TapestryException;
import org.apache.tapestry5.commons.services.CoercionTuple;
+import org.apache.tapestry5.commons.services.CoercionTuple.Key;
import org.apache.tapestry5.commons.services.DataTypeAnalyzer;
import org.apache.tapestry5.commons.services.PlasticProxyFactory;
import org.apache.tapestry5.commons.services.PropertyAccess;
import org.apache.tapestry5.commons.services.TypeCoercer;
import org.slf4j.LoggerFactory;
-import java.lang.annotation.Annotation;
-import java.util.ArrayList;
-import java.util.Collection;
-
/**
* Utility class for creating {@link BeanModelSource} instances without
* Tapestry-IoC. Usage of Tapestry-IoC is still recommended.
@@ -172,28 +173,39 @@ public class BeanModelSourceBuilder {
typeCoercer = new TypeCoercerImpl(configuration.getTuples());
}
- final private static class CoercionTupleConfiguration implements Configuration<CoercionTuple>
+ final private static class CoercionTupleConfiguration implements MappedConfiguration<CoercionTuple.Key, CoercionTuple>
{
- final private Collection<CoercionTuple> tuples = new ArrayList<CoercionTuple>();
+ final private Map<CoercionTuple.Key, CoercionTuple> tuples = new HashMap<>();
@Override
- public void add(CoercionTuple tuble)
+ public void add(CoercionTuple.Key key, CoercionTuple tuple)
{
- tuples.add(tuble);
+ tuples.put(key, tuple);
}
@Override
- public void addInstance(Class<? extends CoercionTuple> clazz)
+ public void addInstance(CoercionTuple.Key key, Class<? extends CoercionTuple> clazz)
{
throw new RuntimeException("Not implemented");
}
- public Collection<CoercionTuple> getTuples()
+ public Map<CoercionTuple.Key, CoercionTuple> getTuples()
{
return tuples;
}
+ @Override
+ public void override(Key key, CoercionTuple value)
+ {
+ throw new RuntimeException("Not implemented");
+ }
+
+ @Override
+ public void overrideInstance(Key key, Class<? extends CoercionTuple> clazz) {
+ throw new RuntimeException("Not implemented");
+ }
+
}
final private static class AutobuildOnlyObjectLocator implements ObjectLocator {
diff --git a/commons/src/main/java/org/apache/tapestry5/commons/internal/BasicTypeCoercions.java b/commons/src/main/java/org/apache/tapestry5/commons/internal/BasicTypeCoercions.java
index 12e756c..efb72a8 100644
--- a/commons/src/main/java/org/apache/tapestry5/commons/internal/BasicTypeCoercions.java
+++ b/commons/src/main/java/org/apache/tapestry5/commons/internal/BasicTypeCoercions.java
@@ -22,7 +22,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
-import org.apache.tapestry5.commons.Configuration;
+import org.apache.tapestry5.commons.MappedConfiguration;
import org.apache.tapestry5.commons.services.Coercion;
import org.apache.tapestry5.commons.services.CoercionTuple;
import org.apache.tapestry5.commons.services.TypeCoercer;
@@ -39,7 +39,8 @@ public class BasicTypeCoercions
/**
* Provides the basic type coercions to a {@link Configuration} instance.
*/
- public static void provideBasicTypeCoercions(Configuration<CoercionTuple> configuration)
+ public static void provideBasicTypeCoercions(
+ MappedConfiguration<CoercionTuple.Key, CoercionTuple> configuration)
{
add(configuration, Object.class, String.class, new Coercion<Object, String>()
{
@@ -310,31 +311,20 @@ public class BasicTypeCoercions
}
});
- configuration.add(CoercionTuple.create(Flow.class, List.class, new Coercion<Flow, List>()
- {
- @Override
- public List coerce(Flow input)
- {
- return input.toList();
- }
- }));
+ CoercionTuple<Flow, List> flowToListCoercion = CoercionTuple.create(Flow.class, List.class, Flow::toList);
+ configuration.add(flowToListCoercion.getKey(), flowToListCoercion);
- configuration.add(CoercionTuple.create(Flow.class, Boolean.class, new Coercion<Flow, Boolean>()
- {
- @Override
- public Boolean coerce(Flow input)
- {
- return !input.isEmpty();
- }
- }));
+ CoercionTuple<Flow, Boolean> flowToBooleanCoercion = CoercionTuple.create(Flow.class, Boolean.class, (i) -> !i.isEmpty());
+ configuration.add(flowToBooleanCoercion.getKey(), flowToBooleanCoercion);
}
- private static <S, T> void add(Configuration<CoercionTuple> configuration, Class<S> sourceType,
+ private static <S, T> void add(MappedConfiguration<CoercionTuple.Key, CoercionTuple> configuration, Class<S> sourceType,
Class<T> targetType, Coercion<S, T> coercion)
{
- configuration.add(CoercionTuple.create(sourceType, targetType, coercion));
+ CoercionTuple<S, T> tuple = CoercionTuple.create(sourceType, targetType, coercion);
+ configuration.add(tuple.getKey(), tuple);
}
diff --git a/commons/src/main/java/org/apache/tapestry5/commons/internal/services/TypeCoercerImpl.java b/commons/src/main/java/org/apache/tapestry5/commons/internal/services/TypeCoercerImpl.java
index e0d3f1d..681fa17 100644
--- a/commons/src/main/java/org/apache/tapestry5/commons/internal/services/TypeCoercerImpl.java
+++ b/commons/src/main/java/org/apache/tapestry5/commons/internal/services/TypeCoercerImpl.java
@@ -127,9 +127,9 @@ public class TypeCoercerImpl extends LockSupport implements TypeCoercer
}
};
- public TypeCoercerImpl(Collection<CoercionTuple> tuples)
+ public TypeCoercerImpl(Map<CoercionTuple.Key, CoercionTuple> tuples)
{
- for (CoercionTuple tuple : tuples)
+ for (CoercionTuple tuple : tuples.values())
{
Class key = tuple.getSourceType();
diff --git a/commons/src/main/java/org/apache/tapestry5/commons/services/CoercionTuple.java b/commons/src/main/java/org/apache/tapestry5/commons/services/CoercionTuple.java
index c315dfe..e5d3d62 100644
--- a/commons/src/main/java/org/apache/tapestry5/commons/services/CoercionTuple.java
+++ b/commons/src/main/java/org/apache/tapestry5/commons/services/CoercionTuple.java
@@ -36,6 +36,8 @@ public final class CoercionTuple<S, T>
private final Class<T> targetType;
private final Coercion<S, T> coercion;
+
+ private final Key key;
/**
* Wraps an arbitrary coercion with an implementation of toString() that identifies the source and target types.
@@ -119,6 +121,7 @@ public final class CoercionTuple<S, T>
this.sourceType = PlasticUtils.toWrapperType(sourceType);
this.targetType = PlasticUtils.toWrapperType(targetType);
this.coercion = wrap ? new CoercionWrapper<S, T>(coercion) : coercion;
+ this.key = new Key();
}
@Override
@@ -141,5 +144,59 @@ public final class CoercionTuple<S, T>
{
return targetType;
}
+
+ public Key getKey()
+ {
+ return key;
+ }
+
+ /**
+ * Class that represents the key to be used to the mapped configuration of the
+ * {@link TypeCoercer} service.
+ */
+ public final class Key
+ {
+
+ @Override
+ public String toString() {
+ return String.format("%s -> %s", sourceType.getName(), targetType.getName());
+ }
+
+ @Override
+ public int hashCode()
+ {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((sourceType == null) ? 0 : sourceType.hashCode());
+ result = prime * result + ((targetType == null) ? 0 : targetType.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ CoercionTuple other = (CoercionTuple) obj;
+ if (sourceType == null)
+ {
+ if (other.sourceType != null)
+ return false;
+ } else if (!sourceType.equals(other.sourceType))
+ return false;
+ if (targetType == null)
+ {
+ if (other.targetType != null)
+ return false;
+ } else if (!targetType.equals(other.targetType))
+ return false;
+ return true;
+ }
+
+ }
}
diff --git a/commons/src/main/java/org/apache/tapestry5/commons/services/TypeCoercer.java b/commons/src/main/java/org/apache/tapestry5/commons/services/TypeCoercer.java
index bcb59cd..cab5e1b 100644
--- a/commons/src/main/java/org/apache/tapestry5/commons/services/TypeCoercer.java
+++ b/commons/src/main/java/org/apache/tapestry5/commons/services/TypeCoercer.java
@@ -12,14 +12,14 @@
package org.apache.tapestry5.commons.services;
-import org.apache.tapestry5.ioc.annotations.UsesConfiguration;
+import org.apache.tapestry5.ioc.annotations.UsesMappedConfiguration;
/**
* Makes use of {@link org.apache.tapestry5.commons.services.Coercion}s to convert between an input value (of some specific
* type) and a desired output type. Smart about coercing, even if it requires multiple coercion steps (i.e., via an
* intermediate type, such as String).
*/
-@UsesConfiguration(CoercionTuple.class)
+@UsesMappedConfiguration(key = CoercionTuple.Key.class, value = CoercionTuple.class)
public interface TypeCoercer
{
/**
diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/util/EnumValueEncoderTest.java b/tapestry-core/src/test/java/org/apache/tapestry5/util/EnumValueEncoderTest.java
index ab46b71..e9d5322 100644
--- a/tapestry-core/src/test/java/org/apache/tapestry5/util/EnumValueEncoderTest.java
+++ b/tapestry-core/src/test/java/org/apache/tapestry5/util/EnumValueEncoderTest.java
@@ -14,13 +14,14 @@
package org.apache.tapestry5.util;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import org.apache.tapestry5.commons.internal.services.TypeCoercerImpl;
import org.apache.tapestry5.commons.services.Coercion;
import org.apache.tapestry5.commons.services.CoercionTuple;
import org.apache.tapestry5.commons.services.TypeCoercer;
-import org.apache.tapestry5.commons.util.CollectionFactory;
import org.apache.tapestry5.commons.util.UnknownValueException;
import org.apache.tapestry5.internal.test.InternalBaseTestCase;
import org.testng.annotations.Test;
@@ -74,7 +75,10 @@ public class EnumValueEncoderTest extends InternalBaseTestCase
});
- TypeCoercer typeCoercer = new TypeCoercerImpl(CollectionFactory.<CoercionTuple, CoercionTuple>newList(stoogeToString, stringToStooge));
+ Map<CoercionTuple.Key, CoercionTuple> map = new HashMap<>();
+ map.put(stoogeToString.getKey(), stoogeToString);
+ map.put(stringToStooge.getKey(), stringToStooge);
+ TypeCoercer typeCoercer = new TypeCoercerImpl(map);
EnumValueEncoder<Stooge> encoder = new EnumValueEncoder<Stooge>(typeCoercer, Stooge.class);
diff --git a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/modules/TapestryIOCModule.java b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/modules/TapestryIOCModule.java
index 0945819..e843547 100644
--- a/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/modules/TapestryIOCModule.java
+++ b/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/modules/TapestryIOCModule.java
@@ -255,7 +255,7 @@ public final class TapestryIOCModule
* </ul>
*/
@Contribute(TypeCoercer.class)
- public static void provideBasicTypeCoercions(Configuration<CoercionTuple> configuration)
+ public static void provideBasicTypeCoercions(MappedConfiguration<CoercionTuple.Key, CoercionTuple> configuration)
{
BasicTypeCoercions.provideBasicTypeCoercions(configuration);
}