001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014 015package org.apache.commons.functor.generator; 016 017import java.util.Collection; 018 019import org.apache.commons.functor.Function; 020import org.apache.commons.functor.generator.util.CollectionTransformer; 021 022/** 023 * Base class for generators. Adds support for all of the Algorithms to 024 * each subclass. 025 * 026 * @param <E> the type of elements held in this generator. 027 * @since 1.0 028 * @version $Revision: 1522355 $ $Date: 2013-09-12 06:29:46 +0200 (Do, 12 Sep 2013) $ 029 */ 030public abstract class BaseGenerator<E> implements Generator<E> { 031 032 /** Create a new generator. */ 033 public BaseGenerator() { 034 super(); 035 } 036 037 /** 038 * {@inheritDoc} 039 * Transforms this generator using the passed in 040 * Function. An example function might turn the contents of the 041 * generator into a {@link Collection} of elements. 042 */ 043 public final <T> T to(Function<Generator<? extends E>, ? extends T> transformer) { 044 return transformer.evaluate(this); 045 } 046 047 /** 048 * {@inheritDoc} 049 * Same as to(new CollectionTransformer(collection)). 050 */ 051 public final <C extends Collection<? super E>> C to(C collection) { 052 return to(new CollectionTransformer<E, C>(collection)); 053 } 054 055 /** 056 * {@inheritDoc} 057 * Same as to(new CollectionTransformer()). 058 */ 059 public final Collection<E> toCollection() { 060 return to(CollectionTransformer.<E> toCollection()); 061 } 062}