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.lang3.Validate;
22
23 /**
24 * Wrap another {@link Generator} such that {@link #run(UnaryProcedure)} continues
25 * as long as a condition is true (test after).
26 *
27 * @param <E> the type of elements held in this generator.
28 * @version $Revision: 1365330 $ $Date: 2012-07-24 18:40:04 -0400 (Tue, 24 Jul 2012) $
29 */
30 public class GenerateWhile<E> extends BaseGenerator<E> {
31
32 /**
33 * The condition has to verified in order to execute the generation.
34 */
35 private final UnaryPredicate<? super E> test;
36
37 /**
38 * Create a new GenerateWhile.
39 * @param wrapped {@link Generator}
40 * @param test {@link UnaryPredicate}
41 */
42 public GenerateWhile(Generator<? extends E> wrapped, UnaryPredicate<? super E> test) {
43 super(Validate.notNull(wrapped, "Generator argument was null"));
44 this.test = Validate.notNull(test, "UnaryPredicate argument was null");
45 }
46
47 /**
48 * {@inheritDoc}
49 */
50 public void run(final UnaryProcedure<? super E> proc) {
51 getWrappedGenerator().run(new UnaryProcedure<E>() {
52 public void run(E obj) {
53 proc.run(obj);
54 if (!test.test(obj)) {
55 getWrappedGenerator().stop();
56 }
57 }
58 });
59 }
60
61 /**
62 * {@inheritDoc}
63 */
64 @SuppressWarnings("unchecked")
65 @Override
66 protected Generator<? extends E> getWrappedGenerator() {
67 return (Generator<? extends E>) super.getWrappedGenerator();
68 }
69
70 /**
71 * {@inheritDoc}
72 */
73 @Override
74 public boolean equals(Object obj) {
75 if (obj == this) {
76 return true;
77 }
78 if (!(obj instanceof GenerateWhile<?>)) {
79 return false;
80 }
81 GenerateWhile<?> other = (GenerateWhile<?>) obj;
82 return other.getWrappedGenerator().equals(getWrappedGenerator()) && other.test.equals(test);
83 }
84
85 /**
86 * {@inheritDoc}
87 */
88 @Override
89 public int hashCode() {
90 int result = "GenerateWhile".hashCode();
91 result <<= 2;
92 Generator<?> gen = getWrappedGenerator();
93 result ^= gen.hashCode();
94 result <<= 2;
95 result ^= test.hashCode();
96 return result;
97 }
98 }