/*
 * 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 org.apache.commons.functor.generator.util;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.functor.Function;
import org.apache.commons.functor.Procedure;
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
 * the constructor an ArrayList will be returned from the transform method.
 *
 * @param <E> the type of elements held in the adapted collection.
 * @param <C> the type of the adapted collection
 * @since 1.0
 * @version $Revision$ $Date$
 */
public class CollectionTransformer<E, C extends Collection<? super E>> implements Function<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,
     * #evaluate() should return a new ArrayList for each call, or continue adding to
     * a single ArrayList instance (the current behavior).
     * Perhaps this is more a documentation issue than anything.
     */

    // instance methods
    //---------------------------------------------------
    /**
     * The adapted collection has to be filled.
     */
    private final C toFill;

    // constructors
    //---------------------------------------------------
    /**
     * Create a new CollectionTransformer.
     * @param toFill Collection to fill
     */
    public CollectionTransformer(C toFill) {
        this.toFill = Validate.notNull(toFill, "toFill");
    }

    // instance methods
    //---------------------------------------------------
    /**
     * {@inheritDoc}
     */
    public C evaluate(Generator<? extends E> generator) {
        generator.run(new Procedure<E>() {
            public void run(E obj) {
                toFill.add(obj);
            }
        });
        return toFill;
    }

    /**
     * Get a {@link CollectionTransformer} instance that simply returns any {@link Collection}.
     * @param <E> the collection type
     * @return {@link CollectionTransformer}
     */
    public static <E> CollectionTransformer<E, Collection<E>> toCollection() {
        return new CollectionTransformer<E, Collection<E>>(new ArrayList<E>());
    }
}

@@ -906,120 +906,9 @@
 ion.
- If a collection is not passed into%0A * the constructor an ArrayList will be returned from the transform method.
 %0A
+
  *%0A 
@@ -1193,422 +1193,8 @@
 C%3E %7B
-%0A    /*%0A     * TODO revisit this class... it could stand a more-descriptive name.  Also, it's a little%0A     * hard to say whether, for an instance constructed without a specific target collection,%0A     * #evaluate() should return a new ArrayList for each call, or continue adding to%0A     * a single ArrayList instance (the current behavior).%0A     * Perhaps this is more a documentation issue than anything.%0A     */
 %0A%0A  
@@ -1535,16 +1535,86 @@
 to fill%0A
+     * @throws NullPointerException if the collection is %7B@code null%7D%0A
      */%0A
@@ -1823,22 +1823,212 @@
    *
+ Run the
  %7B@
+l
 in
-heritDoc%7D
+k Generator%7D adding each element produced into the%0A     * collection.%0A     * @param generator the generator%0A     * @return the C collection filled with the elements produced by the generator
 %0A   

Update Javadoc for CollectionTransformer