commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mben...@apache.org
Subject svn commit: r1370922 - in /commons/proper/functor/trunk/src: main/java/org/apache/commons/functor/generator/ main/java/org/apache/commons/functor/generator/util/ test/java/org/apache/commons/functor/generator/
Date Wed, 08 Aug 2012 19:53:38 GMT
Author: mbenson
Date: Wed Aug  8 19:53:37 2012
New Revision: 1370922

URL: http://svn.apache.org/viewvc?rev=1370922&view=rev
Log:
make Generator.to(Collection) return strongly typed results matching parameter

Modified:
    commons/proper/functor/trunk/src/main/java/org/apache/commons/functor/generator/BaseGenerator.java
    commons/proper/functor/trunk/src/main/java/org/apache/commons/functor/generator/Generator.java
    commons/proper/functor/trunk/src/main/java/org/apache/commons/functor/generator/util/CollectionTransformer.java
    commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestBaseGenerator.java

Modified: commons/proper/functor/trunk/src/main/java/org/apache/commons/functor/generator/BaseGenerator.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/main/java/org/apache/commons/functor/generator/BaseGenerator.java?rev=1370922&r1=1370921&r2=1370922&view=diff
==============================================================================
--- commons/proper/functor/trunk/src/main/java/org/apache/commons/functor/generator/BaseGenerator.java
(original)
+++ commons/proper/functor/trunk/src/main/java/org/apache/commons/functor/generator/BaseGenerator.java
Wed Aug  8 19:53:37 2012
@@ -91,16 +91,15 @@ public abstract class BaseGenerator<E> i
      * {@inheritDoc}
      * Same as to(new CollectionTransformer(collection)).
      */
-    public final Collection<? super E> to(Collection<? super E> collection) {
-        return to(new CollectionTransformer<E>(collection));
+    public final <C extends Collection<? super E>> C to(C collection) {
+        return to(new CollectionTransformer<E, C>(collection));
     }
 
     /**
      * {@inheritDoc}
      * Same as to(new CollectionTransformer()).
      */
-    @SuppressWarnings("unchecked")
     public final Collection<E> toCollection() {
-        return (Collection<E>) to(new CollectionTransformer<E>());
+        return to(CollectionTransformer.<E> toCollection());
     }
 }

Modified: commons/proper/functor/trunk/src/main/java/org/apache/commons/functor/generator/Generator.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/main/java/org/apache/commons/functor/generator/Generator.java?rev=1370922&r1=1370921&r2=1370922&view=diff
==============================================================================
--- commons/proper/functor/trunk/src/main/java/org/apache/commons/functor/generator/Generator.java
(original)
+++ commons/proper/functor/trunk/src/main/java/org/apache/commons/functor/generator/Generator.java
Wed Aug  8 19:53:37 2012
@@ -58,7 +58,7 @@ public interface Generator<E> {
      * @param collection Collection to which my elements should be added
      * @return <code>collection</code>
      */
-    Collection<? super E> to(Collection<? super E> collection);
+    <C extends Collection<? super E>> C to(C collection);
 
     /**
      * Same as to(new CollectionTransformer()).

Modified: commons/proper/functor/trunk/src/main/java/org/apache/commons/functor/generator/util/CollectionTransformer.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/main/java/org/apache/commons/functor/generator/util/CollectionTransformer.java?rev=1370922&r1=1370921&r2=1370922&view=diff
==============================================================================
--- commons/proper/functor/trunk/src/main/java/org/apache/commons/functor/generator/util/CollectionTransformer.java
(original)
+++ commons/proper/functor/trunk/src/main/java/org/apache/commons/functor/generator/util/CollectionTransformer.java
Wed Aug  8 19:53:37 2012
@@ -20,6 +20,7 @@ import java.util.Collection;
 import org.apache.commons.functor.UnaryFunction;
 import org.apache.commons.functor.UnaryProcedure;
 import org.apache.commons.functor.generator.Generator;
+import org.apache.commons.lang3.Validate;
 
 /**
  * Transforms a generator into a collection. If a collection is not passed into
@@ -29,7 +30,7 @@ import org.apache.commons.functor.genera
  * @since 1.0
  * @version $Revision$ $Date$
  */
-public class CollectionTransformer<E> implements UnaryFunction<Generator<? extends
E>, Collection<? super E>> {
+public class CollectionTransformer<E, C extends Collection<? super E>> implements
UnaryFunction<Generator<? extends E>, C> {
     /*
      * TODO revisit this class... it could stand a more-descriptive name.  Also, it's a little
      * hard to say whether, for an instance constructed without a specific target collection,
@@ -43,29 +44,16 @@ public class CollectionTransformer<E> im
     /**
      * The adapted collection has to be filled.
      */
-    private final Collection<? super E> toFill;
+    private final C toFill;
 
     // constructors
     //---------------------------------------------------
     /**
      * Create a new CollectionTransformer.
-     */
-    public CollectionTransformer() {
-        this(null);
-    }
-
-    /**
-     * Create a new CollectionTransformer.
      * @param toFill Collection to fill
      */
-    public CollectionTransformer(Collection<? super E> toFill) {
-        Collection<? super E> coll;
-        if (toFill == null) {
-            coll = new ArrayList<E>();
-        } else {
-            coll = toFill;
-        }
-        this.toFill = coll;
+    public CollectionTransformer(C toFill) {
+        this.toFill = Validate.notNull(toFill, "toFill");
     }
 
     // instance methods
@@ -73,13 +61,20 @@ public class CollectionTransformer<E> im
     /**
      * {@inheritDoc}
      */
-    @SuppressWarnings("unchecked")
-    public Collection<E> evaluate(Generator<? extends E> generator) {
+    public C evaluate(Generator<? extends E> generator) {
         generator.run(new UnaryProcedure<E>() {
             public void run(E obj) {
                 toFill.add(obj);
             }
         });
-        return (Collection<E>) toFill;
+        return toFill;
+    }
+
+    /**
+     * Get a {@link CollectionTransformer} instance that simply returns any {@link Collection}.
+     * @return {@link CollectionTransformer}
+     */
+    public static <E> CollectionTransformer<E, Collection<E>> toCollection()
{
+        return new CollectionTransformer<E, Collection<E>>(new ArrayList<E>());
     }
 }

Modified: commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestBaseGenerator.java
URL: http://svn.apache.org/viewvc/commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestBaseGenerator.java?rev=1370922&r1=1370921&r2=1370922&view=diff
==============================================================================
--- commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestBaseGenerator.java
(original)
+++ commons/proper/functor/trunk/src/test/java/org/apache/commons/functor/generator/TestBaseGenerator.java
Wed Aug  8 19:53:37 2012
@@ -154,11 +154,11 @@ public class TestBaseGenerator {
 
     @Test
     public void testTo() {
-        Collection<Integer> col = (Collection<Integer>)simpleGenerator.to(new
CollectionTransformer<Integer>());
+        Collection<Integer> col = simpleGenerator.to(CollectionTransformer.<Integer>
toCollection());
         assertEquals("[0, 1, 2, 3, 4]", col.toString());
 
         Collection<Integer> fillThis = new LinkedList<Integer>();
-        col = (Collection<Integer>) simpleGenerator.to(new CollectionTransformer<Integer>(fillThis));
+        col = simpleGenerator.to(new CollectionTransformer<Integer, Collection<Integer>>(fillThis));
         assertSame(fillThis, col);
         assertEquals("[0, 1, 2, 3, 4]", col.toString());
 



Mime
View raw message