You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by br...@apache.org on 2015/11/08 17:23:35 UTC
svn commit: r1713244 - in /commons/sandbox/beanutils2/trunk/src: changes/
main/java/org/apache/commons/beanutils2/
main/java/org/apache/commons/beanutils2/transformers/
test/java/org/apache/commons/beanutils2/
test/java/org/apache/commons/beanutils2/te...
Author: britter
Date: Sun Nov 8 16:23:35 2015
New Revision: 1713244
URL: http://svn.apache.org/viewvc?rev=1713244&view=rev
Log:
SANDBOX-501: Remove Transformer<S, T> in favor of java.util.function.Function<S, T>
Removed:
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/Transformer.java
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/transformers/
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/testtransformers/
Modified:
commons/sandbox/beanutils2/trunk/src/changes/changes.xml
commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TransformerRegistry.java
commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TransformerRegistryTestCase.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=1713244&r1=1713243&r2=1713244&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/changes/changes.xml (original)
+++ commons/sandbox/beanutils2/trunk/src/changes/changes.xml Sun Nov 8 16:23:35 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="remove" issue="SANDBOX-501">
+ Remove Transformer<S, T> in favor of java.util.function.Function<S, T>
+ </action>
<action dev="britter" type="add" issue="SANDBOX-482">
Add short cut methods for accessing property values
</action>
Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TransformerRegistry.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TransformerRegistry.java?rev=1713244&r1=1713243&r2=1713244&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TransformerRegistry.java (original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/TransformerRegistry.java Sun Nov 8 16:23:35 2015
@@ -25,6 +25,7 @@ import java.lang.reflect.Type;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
+import java.util.function.Function;
/**
* <p>
@@ -43,7 +44,7 @@ public class TransformerRegistry {
/**
* Registry to hold all the transformers
*/
- private Map<TransformerMapKey, Transformer<?, ?>> transformers = null;
+ private Map<TransformerMapKey, Function<?, ?>> transformers = null;
/**
* <p>
@@ -115,75 +116,27 @@ public class TransformerRegistry {
* @param transformer
* instance of the Transformer implementation
*/
- public <S, T> void register(Transformer<S, T> transformer) {
+ public <S, T> void register(Class<S> sourceClass, Class<T> targetClass, Function<S, T> transformer) {
checkNotNull(transformer, "Transformer must not be null!");
// TODO if transformer is already registered overwrite ?? or do not
// allow overwriting
- transformers.put(new TransformerMapKey(transformer.getSourceType(),
- transformer.getTargetType()), transformer);
- }
-
- /**
- * Registers all the transformers contained within the list with the
- * registry
- *
- * @param transformerList
- * list implementation containing transformers.
- */
- public void registerAll(
- Collection<Transformer<?, ?>> transformerList) {
- checkNotNull(transformerList, "Transformer list must not be null!");
- checkArgument(!transformerList.isEmpty(), "Transformer list must not be empty!");
-
- // TODO if transformer is already registered overwrite ?? or do not
- // allow overwriting
- for (Transformer<?, ?> transformer : transformerList) {
- register(transformer);
- }
- }
-
- /**
- * Deregister's or removes a transformer from the registry
- *
- * @param transformer
- * instance of the transformer to be removed
- * @return true if transformer successfully removed or returns false
- * otherwise
- */
- public <S, T> boolean deregister(Transformer<S, T> transformer) {
- checkNotNull(transformer, "Transformer must not be null!");
-
- return deregister(transformer.getSourceType(),
- transformer.getTargetType());
+ transformers.put(new TransformerMapKey(sourceClass,
+ targetClass), transformer);
}
/**
* Deregister's or removes a transformer from the registry
*
- * @param sourceType
- * Class instance defining source
- * @param targetType
- * Class instance defining target
- * @return true if transformer successfully removed or returns false
- * otherwise
- */
- public <S, T> boolean deregister(Class<S> sourceType, Class<T> targetType) {
- return deregister((Type) sourceType, (Type) targetType);
- }
-
- /**
- * Deregister's or removes a transformer from the registry
- *
- * @param sourceType
+ * @param sourceClass
* Type implementation defining source
- * @param targetType
+ * @param targetClass
* Type implementation defining target
* @return true if transformer successfully removed or returns false
* otherwise
*/
- private boolean deregister(Type sourceType, Type targetType) {
- Transformer<?, ?> previousValue = transformers
- .remove(new TransformerMapKey(sourceType, targetType));
+ public <S, T> boolean deregister(Class<S> sourceClass, Class<T> targetClass) {
+ Function<?, ?> previousValue = transformers
+ .remove(new TransformerMapKey(sourceClass, targetClass));
boolean returnVal = true;
if (previousValue == null) {
@@ -210,12 +163,12 @@ public class TransformerRegistry {
* @return instance of Transformer Implementation if found or null otherwise
*/
@SuppressWarnings("unchecked")
- public <S, T> Transformer<S, T> lookup(Class<S> sourceType,
+ public <S, T> Function<S, T> lookup(Class<S> sourceType,
Class<T> targetType) {
TransformerMapKey key = new TransformerMapKey(sourceType, targetType);
if (transformers.containsKey(key)) {
- return (Transformer<S, T>) transformers.get(key);
+ return (Function<S, T>) transformers.get(key);
} else {
throw new TransformationException(String.format("Unable to transform from %s to %s.",
sourceType.getName(), targetType.getName()));
Modified: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TransformerRegistryTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TransformerRegistryTestCase.java?rev=1713244&r1=1713243&r2=1713244&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TransformerRegistryTestCase.java (original)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/TransformerRegistryTestCase.java Sun Nov 8 16:23:35 2015
@@ -17,33 +17,25 @@
package org.apache.commons.beanutils2;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
-import java.lang.reflect.Type;
-import java.util.ArrayList;
-import java.util.Collections;
import java.util.Date;
-import java.util.List;
+import java.util.function.Function;
-import org.apache.commons.beanutils2.testtransformers.TestStringFloatTransformer;
-import org.apache.commons.beanutils2.testtransformers.TestStringIntegerTransformer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TransformerRegistryTestCase {
- Transformer<String, Integer> transformerStringIntegerImpl = null;
- Transformer<String, Float> transformerStringFloatImpl = null;
+ Function<String, Integer> transformerStringIntegerImpl = Integer::parseInt;
+ Function<String, Float> transformerStringFloatImpl = Float::parseFloat;
TransformerRegistry registry = null;
@Before
public void setUp() throws Exception {
- transformerStringIntegerImpl = new TestStringIntegerTransformer();
- transformerStringFloatImpl = new TestStringFloatTransformer();
registry = new TransformerRegistry();
- registry.register(transformerStringIntegerImpl);
- registry.register(transformerStringFloatImpl);
+ registry.register(String.class, Integer.class, transformerStringIntegerImpl);
+ registry.register(String.class, Float.class, transformerStringFloatImpl);
}
@After
@@ -52,52 +44,9 @@ public class TransformerRegistryTestCase
registry = null;
}
- /**
- * Tests for the register() method in TransformerRegistry
- */
- @Test
- public void testRegister() {
- registry.register(transformerStringIntegerImpl);
- assertEquals(transformerStringIntegerImpl, registry.lookup(
- getClass(transformerStringIntegerImpl.getSourceType()),
- getClass(transformerStringIntegerImpl.getTargetType())));
- }
-
@Test(expected = NullPointerException.class)
public void testRegisterNull() throws Exception {
- registry.register(null);
- }
-
- /**
- * Tests for the registerAll() method in TransformerRegistry
- */
- @Test
- public void testRegisterAll() {
-
- List<Transformer<?, ?>> transformerList = new ArrayList<>(
- 2);
- transformerList.add(transformerStringIntegerImpl);
- transformerList.add(transformerStringFloatImpl);
-
- registry.deregisterAll();
- registry.registerAll(transformerList);
-
- assertEquals(transformerStringIntegerImpl, registry.lookup(
- getClass(transformerStringIntegerImpl.getSourceType()),
- getClass(transformerStringIntegerImpl.getTargetType())));
- assertEquals(transformerStringFloatImpl, registry.lookup(
- getClass(transformerStringFloatImpl.getSourceType()),
- getClass(transformerStringFloatImpl.getTargetType())));
- }
-
- @Test(expected = NullPointerException.class)
- public void testRegisterAllNull() throws Exception {
- registry.registerAll(null);
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testRegisterAllEmpty() throws Exception {
- registry.registerAll(Collections.<Transformer<?, ?>> emptyList());
+ registry.register(null, null, null);
}
/**
@@ -105,9 +54,7 @@ public class TransformerRegistryTestCase
*/
@Test
public void testLookup() {
- assertEquals(transformerStringIntegerImpl, registry.lookup(
- getClass(transformerStringIntegerImpl.getSourceType()),
- getClass(transformerStringIntegerImpl.getTargetType())));
+ assertEquals(transformerStringIntegerImpl, registry.lookup(String.class, Integer.class));
}
@Test(expected = NullPointerException.class)
@@ -128,22 +75,16 @@ public class TransformerRegistryTestCase
/**
* Tests for the deregister() method in TransformerRegistry
*/
- @Test
+ @Test(expected = TransformationException.class)
public void testDeregister() {
- registry.deregister(transformerStringIntegerImpl);
- try {
- registry.lookup(
- getClass(transformerStringIntegerImpl.getSourceType()),
- getClass(transformerStringIntegerImpl.getTargetType()));
- fail("Expected TransformationException to be thrown!");
- } catch (TransformationException expected) {
- // expected
- }
+ registry.deregister(String.class, Integer.class);
+
+ registry.lookup(String.class, Integer.class);
}
@Test(expected = NullPointerException.class)
public void testDeregisterNull() throws Exception {
- registry.deregister(null);
+ registry.deregister(null, null);
}
@Test(expected = NullPointerException.class)
@@ -159,46 +100,11 @@ public class TransformerRegistryTestCase
/**
* Tests for the deregisterAll() method in TransformerRegistry
*/
- @Test
+ @Test(expected = TransformationException.class)
public void testDeregisterAll() {
registry.deregisterAll();
- try {
- registry.lookup(
- getClass(transformerStringIntegerImpl.getSourceType()),
- getClass(transformerStringIntegerImpl.getTargetType()));
- fail("Expected TransformationException to be thrown!");
- } catch (TransformationException expected) {
- // expected
- }
- }
-
- /**
- * Utility method to return Class instance defining the Type
- *
- * @param type
- * - instance of java.lang.reflect.Type
- * @return
- */
- private static Class<?> getClass(Type type) {
- String className = type.toString();
- Class<?> clazz = null;
- if (className.startsWith("class ")) {
- className = className.substring("class ".length());
- }
-
- if (className.isEmpty()) {
- throw new IllegalArgumentException(
- "Invalid sourceType in Transformer instance");
- }
- try {
- clazz = Class.forName(className);
- } catch (ClassNotFoundException cnfe) {
- // can safely assume the class would be available in JVM as type
- // exists
- }
-
- return clazz;
+ registry.lookup(String.class, Integer.class);
}
}