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.loop;
18  
19  import org.apache.commons.functor.Predicate;
20  import org.apache.commons.functor.Procedure;
21  import org.apache.commons.functor.generator.Generator;
22  import org.apache.commons.lang3.Validate;
23  
24  /**
25   * Wrap another {@link Generator} such that {@link #run(Procedure)} terminates once
26   * a condition has been satisfied.
27   *
28   * @param <E> the type of elements held in this generator.
29   * @version $Revision: 1508677 $ $Date: 2013-07-30 19:48:02 -0300 (Tue, 30 Jul 2013) $
30   */
31  public class UntilGenerate<E> extends LoopGenerator<E> {
32  
33      /**
34       * The condition has to verified in order to execute the generation.
35       */
36      private final Predicate<? super E> test;
37  
38      /**
39       * Create a new UntilGenerate.
40       * @param wrapped {@link Generator}
41       * @param test {@link Predicate}
42       */
43      public UntilGenerate(Predicate<? super E> test, Generator<? extends E> wrapped) {
44          super(Validate.notNull(wrapped, "Generator argument was null"));
45          this.test = Validate.notNull(test, "Predicate argument was null");
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      public void run(final Procedure<? super E> proc) {
52          getWrappedGenerator().run(new Procedure<E>() {
53              public void run(E obj) {
54                  if (test.test(obj)) {
55                      UntilGenerate.this.stop();
56                  } else {
57                      proc.run(obj);
58                  }
59              }
60          });
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public boolean equals(Object obj) {
68          if (obj == this) {
69              return true;
70          }
71          if (!(obj instanceof UntilGenerate<?>)) {
72              return false;
73          }
74          UntilGenerate<?> other = (UntilGenerate<?>) obj;
75          return other.getWrappedGenerator().equals(getWrappedGenerator()) && other.test.equals(test);
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public int hashCode() {
83          int result = "UntilGenerate".hashCode();
84          result <<= 2;
85          Generator<?> gen = getWrappedGenerator();
86          result ^= gen.hashCode();
87          result <<= 2;
88          result ^= test.hashCode();
89          return result;
90      }
91  }