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.UnaryPredicate;
20  import org.apache.commons.functor.UnaryProcedure;
21  import org.apache.commons.functor.core.composite.ConditionalUnaryProcedure;
22  
23  /**
24   * Generator that filters another Generator by only passing through those elements
25   * that are matched by a specified UnaryPredicate.
26   *
27   * @version $Revision: 1156795 $ $Date: 2011-08-11 22:08:30 +0200 (Thu, 11 Aug 2011) $
28   */
29  public class FilteredGenerator<E> extends BaseGenerator<E> {
30      private final UnaryPredicate<? super E> pred;
31  
32      /**
33       * Create a new FilteredGenerator.
34       * @param wrapped Generator to wrap
35       * @param pred filtering UnaryPredicate
36       */
37      public FilteredGenerator(Generator<? extends E> wrapped, UnaryPredicate<? super E> pred) {
38          super(wrapped);
39          if (wrapped == null) {
40              throw new IllegalArgumentException("Generator argument was null");
41          }
42          if (pred == null) {
43              throw new IllegalArgumentException("UnaryPredicate argument was null");
44          }
45          this.pred = pred;
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      public void run(UnaryProcedure<? super E> proc) {
52          getWrappedGenerator().run(new ConditionalUnaryProcedure<E>(pred, proc));
53      }
54  
55      /**
56       * {@inheritDoc}
57       */
58      @SuppressWarnings("unchecked")
59      @Override
60      protected Generator<? extends E> getWrappedGenerator() {
61          return (Generator<? extends E>) super.getWrappedGenerator();
62      }
63  
64      /**
65       * {@inheritDoc}
66       */
67      public boolean equals(Object obj) {
68          if (obj == this) {
69              return true;
70          }
71          if (!(obj instanceof FilteredGenerator<?>)) {
72              return false;
73          }
74          FilteredGenerator<?> other = (FilteredGenerator<?>) obj;
75          return other.getWrappedGenerator().equals(getWrappedGenerator()) && other.pred.equals(pred);
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      public int hashCode() {
82          int result = "FilteredGenerator".hashCode();
83          result <<= 2;
84          Generator<?> gen = getWrappedGenerator();
85          result ^= gen == null ? 0 : gen.hashCode();
86          result <<= 2;
87          result ^= pred == null ? 0 : pred.hashCode();
88          return result;
89      }
90  }