1 /* 2 * Licensed under the Apache License, Version 2.0 (the "License"); 3 * you may not use this file except in compliance with the License. 4 * You may obtain a copy of the License at 5 * 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * 8 * Unless required by applicable law or agreed to in writing, software 9 * distributed under the License is distributed on an "AS IS" BASIS, 10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 * See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 15 package org.apache.commons.functor.generator; 16 17 import java.util.Collection; 18 19 import org.apache.commons.functor.Function; 20 import org.apache.commons.functor.generator.util.CollectionTransformer; 21 22 /** 23 * Base class for generators. Adds support for all of the Algorithms to 24 * each subclass. 25 * 26 * @param <E> the type of elements held in this generator. 27 * @since 1.0 28 * @version $Revision: 1522355 $ $Date: 2013-09-12 06:29:46 +0200 (Do, 12 Sep 2013) $ 29 */ 30 public abstract class BaseGenerator<E> implements Generator<E> { 31 32 /** Create a new generator. */ 33 public BaseGenerator() { 34 super(); 35 } 36 37 /** 38 * {@inheritDoc} 39 * Transforms this generator using the passed in 40 * Function. An example function might turn the contents of the 41 * generator into a {@link Collection} of elements. 42 */ 43 public final <T> T to(Function<Generator<? extends E>, ? extends T> transformer) { 44 return transformer.evaluate(this); 45 } 46 47 /** 48 * {@inheritDoc} 49 * Same as to(new CollectionTransformer(collection)). 50 */ 51 public final <C extends Collection<? super E>> C to(C collection) { 52 return to(new CollectionTransformer<E, C>(collection)); 53 } 54 55 /** 56 * {@inheritDoc} 57 * Same as to(new CollectionTransformer()). 58 */ 59 public final Collection<E> toCollection() { 60 return to(CollectionTransformer.<E> toCollection()); 61 } 62 }