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.Predicate;
20  import org.apache.commons.functor.Procedure;
21  import org.apache.commons.functor.core.composite.ConditionalProcedure;
22  import org.apache.commons.lang3.Validate;
23  
24  /**
25   * Generator that filters another Generator by only passing through those elements
26   * that are matched by a specified Predicate.
27   *
28   * @param <E> the type of elements held in this generator.
29   * @version $Revision: 1522355 $ $Date: 2013-09-12 06:29:46 +0200 (Do, 12 Sep 2013) $
30   */
31  public class FilteredGenerator<E> extends BaseGenerator<E> {
32  
33      /**
34       * A generator can wrap another generator.
35       * */
36      private Generator<? extends E> wrappedGenerator;
37  
38      /**
39       * The predicate used to filter.
40       */
41      private final Predicate<? super E> pred;
42  
43      /**
44       * Create a new FilteredGenerator.
45       * @param wrapped Generator to wrap
46       * @param pred filtering Predicate
47       */
48      public FilteredGenerator(Generator<? extends E> wrapped, Predicate<? super E> pred) {
49          this.wrappedGenerator = Validate.notNull(wrapped, "Generator argument was null");
50          this.pred = Validate.notNull(pred, "Predicate argument was null");
51      }
52  
53      /**
54       * {@inheritDoc}
55       */
56      public void run(Procedure<? super E> proc) {
57          getWrappedGenerator().run(new ConditionalProcedure<E>(pred, proc));
58      }
59  
60      /**
61       * Get the generator that is being wrapped.
62       * @return Generator
63       */
64      protected Generator<? extends E> getWrappedGenerator() {
65          return this.wrappedGenerator;
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public boolean equals(Object obj) {
73          if (obj == this) {
74              return true;
75          }
76          if (!(obj instanceof FilteredGenerator<?>)) {
77              return false;
78          }
79          FilteredGenerator<?> other = (FilteredGenerator<?>) obj;
80          return other.getWrappedGenerator().equals(getWrappedGenerator()) && other.pred.equals(pred);
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public int hashCode() {
88          int result = "FilteredGenerator".hashCode();
89          result <<= 2;
90          Generator<?> gen = getWrappedGenerator();
91          result ^= gen.hashCode();
92          result <<= 2;
93          result ^= pred.hashCode();
94          return result;
95      }
96  }