View Javadoc

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.UnaryFunction;
20  import org.apache.commons.functor.generator.util.CollectionTransformer;
21  
22  /**
23   * Base class for generators. Adds support for all of the {@link Algorithms} to
24   * each subclass.
25   *
26   * @since 1.0
27   * @version $Revision: 1156778 $ $Date: 2011-08-11 21:50:51 +0200 (Thu, 11 Aug 2011) $
28   * @author  Jason Horman (jason@jhorman.org)
29   */
30  public abstract class BaseGenerator<E> implements Generator<E> {
31  
32      /** A generator can wrap another generator. */
33      private final Generator<?> wrappedGenerator;
34  
35      /** Set to true when the generator is {@link #stop stopped}. */
36      private boolean stopped = false;
37  
38      /** Create a new generator. */
39      public BaseGenerator() {
40          this(null);
41      }
42  
43      /**
44       * A generator can wrap another generator. When wrapping generators you
45       * should use probably this constructor since doing so will cause the
46       * {@link #stop} method to stop the wrapped generator as well.
47       * @param generator Generator to wrap
48       */
49      public BaseGenerator(Generator<?> generator) {
50          this.wrappedGenerator = generator;
51      }
52  
53      /**
54       * Get the generator that is being wrapped.
55       * @return Generator
56       */
57      protected Generator<?> getWrappedGenerator() {
58          return wrappedGenerator;
59      }
60  
61      /**
62       * {@inheritDoc}
63       * Stop the generator. Will stop the wrapped generator if one was set.
64       */
65      public void stop() {
66          if (wrappedGenerator != null) {
67              wrappedGenerator.stop();
68          }
69          stopped = true;
70      }
71  
72      /**
73       * {@inheritDoc}
74       * Check if the generator is stopped.
75       */
76      public boolean isStopped() {
77          return stopped;
78      }
79  
80      /**
81       * {@inheritDoc}
82       * Transforms this generator using the passed in
83       * UnaryFunction. An example function might turn the contents of the
84       * generator into a {@link Collection} of elements.
85       */
86      public final <T> T to(UnaryFunction<Generator<? extends E>, ? extends T> transformer) {
87          return transformer.evaluate(this);
88      }
89  
90      /**
91       * {@inheritDoc}
92       * Same as to(new CollectionTransformer(collection)).
93       */
94      public final Collection<? super E> to(Collection<? super E> collection) {
95          return to(new CollectionTransformer<E>(collection));
96      }
97  
98      /**
99       * {@inheritDoc}
100      * Same as to(new CollectionTransformer()).
101      */
102     @SuppressWarnings("unchecked")
103     public final Collection<E> toCollection() {
104         return (Collection<E>) to(new CollectionTransformer<E>());
105     }
106 }