Author: britter
Date: Sun Nov 8 17:34:17 2015
New Revision: 1713272
URL: http://svn.apache.org/viewvc?rev=1713272&view=rev
Log:
SANDBOX-502: Add Builder API for defining type conversion
Added:
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TypeConversion.java
(with props)
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TypeConversionTest.java
(with props)
Modified:
commons/sandbox/beanutils2/trunk/src/changes/changes.xml
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/ConverterRegistry.java
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/ConverterRegistryTestCase.java
Modified: commons/sandbox/beanutils2/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/changes/changes.xml?rev=1713272&r1=1713271&r2=1713272&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/changes/changes.xml (original)
+++ commons/sandbox/beanutils2/trunk/src/changes/changes.xml Sun Nov 8
17:34:17 2015
@@ -23,6 +23,9 @@
</properties>
<body>
<release version="2.0" date="TBA" description="Redesign of beanutils with a
fluent API">
+ <action dev="britter" type="update" issue="SANDBOX-502">
+ Add Builder API for defining type conversion
+ </action>
<action dev="britter" type="update" issue="SANDBOX-506">
Use 'Converter' instead of 'Transformer'
</action>
Modified:
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/ConverterRegistry.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/ConverterRegistry.java?rev=1713272&r1=1713271&r2=1713272&view=diff
==============================================================================
---
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/ConverterRegistry.java
(original)
+++
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/ConverterRegistry.java
Sun Nov 8 17:34:17 2015
@@ -20,7 +20,6 @@ package org.apache.commons.beanutils2;
import static org.apache.commons.beanutils2.Assertions.checkNotNull;
-import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
@@ -49,12 +48,12 @@ public class ConverterRegistry {
*
*/
private static class ConverterMapKey {
- private Type source = null;
- private Type target = null;
+ private Class<?> source = null;
+ private Class<?> target = null;
- private ConverterMapKey(Type sourceType, Type targetType) {
- source = checkNotNull(sourceType, "Source type must not be null!");
- target = checkNotNull(targetType, "Target type must not be null!");
+ private ConverterMapKey(Class<?> sourceClass, Class<?> targetClass) {
+ source = checkNotNull(sourceClass, "Source type must not be
null!");
+ target = checkNotNull(targetClass, "Target type must not be
null!");
}
/**
@@ -62,7 +61,7 @@ public class ConverterRegistry {
*
* @return type identifying the source
*/
- public Type getSource() {
+ public Class<?> getSource() {
return this.source;
}
@@ -71,7 +70,7 @@ public class ConverterRegistry {
*
* @return type identifying target
*/
- public Type getTarget() {
+ public Class<?> getTarget() {
return this.target;
}
@@ -107,17 +106,19 @@ public class ConverterRegistry {
}
/**
- * Registers the converter with the registry
+ * Registers a type conversion within this registry.
*
- * @param converter
- * instance of the Transformer implementation
+ * @param typeConversion
+ * a conversion specification describing the type conversion to
register.
+ * @param <S> the source type of the TypeConversion
+ * @param <T> the target type of the TypeConversion
*/
- public <S, T> void register(Class<S> sourceClass, Class<T> targetClass,
Function<S, T> converter) {
- checkNotNull(converter, "Converter must not be null!");
+ public <S, T> void register(TypeConversion<S, T> typeConversion) {
+ checkNotNull(typeConversion, "conversionSpecification must not be
null!");
// TODO if transformer is already registered overwrite ?? or do not
// allow overwriting
- converters.put(new ConverterMapKey(sourceClass,
- targetClass), converter);
+ converters.put(new ConverterMapKey(typeConversion.getSourceClass(),
+ typeConversion.getTargetClass()),
typeConversion.getConversionFunction());
}
/**
@@ -127,6 +128,8 @@ public class ConverterRegistry {
* Type implementation defining source
* @param targetClass
* Type implementation defining target
+ * @param <S> the type of the sourceClass
+ * @param <T> the type of the targetClass
* @return true if transformer successfully removed or returns false
* otherwise
*/
@@ -154,8 +157,10 @@ public class ConverterRegistry {
*
* @param sourceType
* Class instance defining source
- * @param targetType
+ * @param targetType
* Class instance defining target
+ * @param <S> the type of the sourceClass
+ * @param <T> the type of the targetClass
* @return instance of converter Implementation if found or null otherwise
*/
@SuppressWarnings("unchecked")
Added:
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TypeConversion.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TypeConversion.java?rev=1713272&view=auto
==============================================================================
---
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TypeConversion.java
(added)
+++
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TypeConversion.java
Sun Nov 8 17:34:17 2015
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.beanutils2;
+
+import static org.apache.commons.beanutils2.Assertions.checkNotNull;
+
+import java.util.function.Function;
+
+/**
+ * A TypeConversion describes how to convert from one class to another using a
conversion function.
+ *
+ * <p>
+ * This class defines a fluent API to define new type conversions:
+ * </p>
+ *
+ * <pre>
+ * import static org.apache.commons.beanutils2.ConversionSpecification.from;
+ *
+ * from(String.class).to(Integer.class).using(Integer::valueOf);
+ * </pre>
+ *
+ * @param <S> the source type of the conversion
+ * @param <T> the target type of the conversion
+ */
+public final class TypeConversion<S, T> {
+
+ private final Class<S> sourceClass;
+ private final Class<T> targetClass;
+ private final Function<S, T> conversionFunction;
+
+ /**
+ * Defines the source class of the type conversion being created.
+ *
+ * @param sourceClass the source class of the type conversion being created
+ * @param <S> the type of the source class
+ *
+ * @return an intermediate SourceClassHolder object
+ */
+ public static <S> SourceClassHolder<S> from(Class<S> sourceClass) {
+ checkNotNull(sourceClass, "Parameter 'sourceClass' must not be null!");
+ return new DefaultSourceClassHolder<>(sourceClass);
+ }
+
+ /**
+ * Intermediate type for the fluent API for creating new TypeConversion
instances.
+ *
+ * @param <S> the type of the source class
+ */
+ public interface SourceClassHolder<S> {
+ /**
+ * Defines the target class of the type conversion being created.
+ *
+ * @param targetClass the target class of the type conversion being
created
+ * @param <T> the type of the target class
+ * @return an intermediate TargetClassHolder object
+ */
+ <T> TargetClassHolder<S, T> to(Class<T> targetClass);
+ }
+
+ private static class DefaultSourceClassHolder<S> implements
SourceClassHolder<S> {
+
+ private Class<S> sourceClass;
+
+ public DefaultSourceClassHolder(Class<S> sourceClass) {
+ this.sourceClass = sourceClass;
+ }
+
+ @Override
+ public <T> TargetClassHolder<S, T> to(Class<T> targetClass) {
+ checkNotNull(targetClass, "Parameter 'targetClass' must not be
null!");
+ return new DefaultTargetClassHolder<>(sourceClass, targetClass);
+ }
+ }
+
+ /**
+ * Intermediate type for the fluent API for creating new TypeConversion
instances.
+ *
+ * @param <S> the type of the source class
+ * @param <T> the type of the target class
+ */
+ public interface TargetClassHolder<S, T> {
+ /**
+ * Defines the conversion function for converting the source class to
the target class.
+ *
+ * @param conversionFunction the conversion function to use for
converting from the source class to the target
+ * Class.
+ * @return a new TypeConversion instance that describes how to convert
from the source class to the target class
+ * using the conversion function.
+ */
+ TypeConversion<S, T> using(Function<S, T> conversionFunction);
+ }
+
+ private static class DefaultTargetClassHolder<S, T> implements
TargetClassHolder<S, T> {
+ private final Class<S> sourceClass;
+ private final Class<T> targetClass;
+
+ public DefaultTargetClassHolder(Class<S> sourceClass, Class<T>
targetClass) {
+ this.sourceClass = sourceClass;
+ this.targetClass = targetClass;
+ }
+
+ @Override
+ public TypeConversion<S, T> using(Function<S, T> conversionFunction) {
+ checkNotNull(conversionFunction, "Parameter 'conversionFunction'
must not be null!");
+ return new TypeConversion<>(sourceClass, targetClass,
conversionFunction);
+ }
+ }
+
+ private TypeConversion(Class<S> sourceClass, Class<T> targetClass,
Function<S, T> conversionFunction) {
+ this.sourceClass = sourceClass;
+ this.targetClass = targetClass;
+ this.conversionFunction = conversionFunction;
+ }
+
+ /**
+ * Source class of this type conversion.
+ *
+ * @return the source class
+ */
+ public Class<S> getSourceClass() {
+ return sourceClass;
+ }
+
+ /**
+ * Target class of this type conversion.
+ *
+ * @return the target class
+ */
+ public Class<T> getTargetClass() {
+ return targetClass;
+ }
+
+ /**
+ * Conversion function to convert from {@link #getSourceClass() source
class} to
+ * {@link #getTargetClass() target class}.
+ *
+ * @return the conversion function
+ */
+ public Function<S, T> getConversionFunction() {
+ return conversionFunction;
+ }
+}
Propchange:
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TypeConversion.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/ConverterRegistryTestCase.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/ConverterRegistryTestCase.java?rev=1713272&r1=1713271&r2=1713272&view=diff
==============================================================================
---
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/ConverterRegistryTestCase.java
(original)
+++
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/ConverterRegistryTestCase.java
Sun Nov 8 17:34:17 2015
@@ -16,45 +16,36 @@
*/
package org.apache.commons.beanutils2;
+import static org.apache.commons.beanutils2.TypeConversion.from;
import static org.junit.Assert.assertEquals;
import java.util.Date;
import java.util.function.Function;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ConverterRegistryTestCase {
- Function<String, Integer> transformerStringIntegerImpl = Integer::parseInt;
- Function<String, Float> transformerStringFloatImpl = Float::parseFloat;
ConverterRegistry registry = null;
@Before
public void setUp() throws Exception {
registry = new ConverterRegistry();
- registry.register(String.class, Integer.class,
transformerStringIntegerImpl);
- registry.register(String.class, Float.class,
transformerStringFloatImpl);
- }
-
- @After
- public void tearDown() throws Exception {
- transformerStringIntegerImpl = null;
- registry = null;
+
registry.register(from(String.class).to(Integer.class).using(Integer::valueOf));
+
registry.register(from(String.class).to(Float.class).using(Float::valueOf));
}
@Test(expected = NullPointerException.class)
public void testRegisterNull() throws Exception {
- registry.register(null, null, null);
+ registry.register(null);
}
- /**
- * Tests for the lookup() method in TransformerRegistry
- */
@Test
public void testLookup() {
- assertEquals(transformerStringIntegerImpl,
registry.lookup(String.class, Integer.class));
+ Function<String, Integer> conversion = registry.lookup(String.class,
Integer.class);
+
+ assertEquals(Integer.valueOf(5), conversion.apply("5"));
}
@Test(expected = NullPointerException.class)
@@ -72,9 +63,6 @@ public class ConverterRegistryTestCase {
registry.lookup(Date.class, String.class);
}
- /**
- * Tests for the deregister() method in TransformerRegistry
- */
@Test(expected = ConversionException.class)
public void testDeregister() {
registry.deregister(String.class, Integer.class);
@@ -97,9 +85,6 @@ public class ConverterRegistryTestCase {
registry.deregister(null, String.class);
}
- /**
- * Tests for the deregisterAll() method in TransformerRegistry
- */
@Test(expected = ConversionException.class)
public void testDeregisterAll() {
registry.deregisterAll();
Added:
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TypeConversionTest.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TypeConversionTest.java?rev=1713272&view=auto
==============================================================================
---
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TypeConversionTest.java
(added)
+++
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TypeConversionTest.java
Sun Nov 8 17:34:17 2015
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.beanutils2;
+
+import static org.apache.commons.beanutils2.TypeConversion.from;
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class TypeConversionTest {
+
+ @Test(expected = NullPointerException.class)
+ public void fromNullClass() throws Exception {
+ from(null);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void toNullClass() throws Exception {
+ from(String.class).to(null);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void usingNullFunction() throws Exception {
+ from(String.class).to(Integer.class).using(null);
+ }
+
+ @Test
+ public void fromStringToIntegerUsingIntegerParseInt() throws Exception {
+ TypeConversion<String, Integer> spec =
from(String.class).to(Integer.class).using(Integer::valueOf);
+
+ assertEquals(String.class, spec.getSourceClass());
+ assertEquals(Integer.class, spec.getTargetClass());
+ assertEquals(Integer.valueOf(5),
spec.getConversionFunction().apply("5"));
+ }
+}
\ No newline at end of file
Propchange:
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TypeConversionTest.java
------------------------------------------------------------------------------
svn:eol-style = native