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  package org.apache.commons.functor.generator;
15  
16  import java.util.Collection;
17  
18  import org.apache.commons.functor.UnaryFunction;
19  import org.apache.commons.functor.UnaryProcedure;
20  
21  /**
22   * The Generator interface defines a number of useful actions applying UnaryFunctors
23   * to each in a series of argument Objects.
24   *
25   * @param <E> the type of elements held in this generator.
26   * @version $Revision: 1370922 $ $Date: 2012-08-08 15:53:37 -0400 (Wed, 08 Aug 2012) $
27   */
28  public interface Generator<E> {
29      /**
30       * Generators must implement this method.
31       * @param proc UnaryProcedure to run
32       */
33      void run(UnaryProcedure<? super E> proc);
34  
35      /**
36       * Stop the generator. Will stop the wrapped generator if one was set.
37       */
38      void stop();
39  
40      /**
41       * Check if the generator is stopped.
42       * @return true if stopped
43       */
44      boolean isStopped();
45  
46      /**
47       * Transforms this generator using the passed in
48       * transformer. An example transformer might turn the contents of the
49       * generator into a {@link Collection} of elements.
50       * @param <Z> the returned value type of the input {@link UnaryFunction}.
51       * @param transformer UnaryFunction to apply to this
52       * @return transformation result
53       */
54      <Z> Z to(UnaryFunction<Generator<? extends E>, ? extends Z> transformer);
55  
56      /**
57       * Same as to(new CollectionTransformer(collection)).
58       * @param collection Collection to which my elements should be added
59       * @return <code>collection</code>
60       */
61      <C extends Collection<? super E>> C to(C collection);
62  
63      /**
64       * Same as to(new CollectionTransformer()).
65       * @return Collection
66       */
67      Collection<? super E> toCollection();
68  }