View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.functor.generator;
18  
19  import org.apache.commons.functor.UnaryFunction;
20  import org.apache.commons.functor.UnaryProcedure;
21  
22  /**
23   * Generator that transforms the elements of another Generator.
24   *
25   * @version $Revision: 1156799 $ $Date: 2011-08-11 22:11:54 +0200 (Thu, 11 Aug 2011) $
26   */
27  public class TransformedGenerator<I, E> extends BaseGenerator<E> {
28      private final UnaryFunction<? super I, ? extends E> func;
29  
30      /**
31       * Create a new TransformedGenerator.
32       * @param wrapped Generator to transform
33       * @param func UnaryFunction to apply to each element
34       */
35      public TransformedGenerator(Generator<? extends I> wrapped, UnaryFunction<? super I, ? extends E> func) {
36          super(wrapped);
37          if (wrapped == null) {
38              throw new IllegalArgumentException("Generator argument was null");
39          }
40          if (func == null) {
41              throw new IllegalArgumentException("UnaryFunction argument was null");
42          }
43          this.func = func;
44      }
45  
46      /**
47       * {@inheritDoc}
48       */
49      public void run(final UnaryProcedure<? super E> proc) {
50          getWrappedGenerator().run(new UnaryProcedure<I>() {
51              public void run(I obj) {
52                  proc.run(func.evaluate(obj));
53              }
54          });
55      }
56  
57      /**
58       * {@inheritDoc}
59       */
60      @SuppressWarnings("unchecked")
61      @Override
62      protected Generator<? extends I> getWrappedGenerator() {
63          return (Generator<? extends I>) super.getWrappedGenerator();
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      public boolean equals(Object obj) {
70          if (obj == this) {
71              return true;
72          }
73          if (!(obj instanceof TransformedGenerator<?, ?>)) {
74              return false;
75          }
76          TransformedGenerator<?, ?> other = (TransformedGenerator<?, ?>) obj;
77          return other.getWrappedGenerator().equals(getWrappedGenerator()) && other.func == func;
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      public int hashCode() {
84          int result = "TransformedGenerator".hashCode();
85          result <<= 2;
86          result ^= getWrappedGenerator().hashCode();
87          result <<= 2;
88          result ^= func.hashCode();
89          return result;
90      }
91  }