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    *      https://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.collections4.functors;
18  
19  import java.util.Objects;
20  
21  import org.apache.commons.collections4.Closure;
22  import org.apache.commons.collections4.Predicate;
23  
24  /**
25   * Closure implementation that executes a closure repeatedly until a condition is met,
26   * like a do-while or while loop.
27   * <p>
28   * <strong>WARNING:</strong> from v4.1 onwards this class will <strong>not</strong> be serializable anymore
29   * in order to prevent potential remote code execution exploits. Please refer to
30   * <a href="https://issues.apache.org/jira/browse/COLLECTIONS-580">COLLECTIONS-580</a>
31   * for more details.
32   * </p>
33   *
34   * @param <T> The type of the input to the operation.
35   * @since 3.0
36   */
37  public class WhileClosure<T> implements Closure<T> {
38  
39      /**
40       * Factory method that performs validation.
41       *
42       * @param <E> The type that the closure acts on
43       * @param predicate  The predicate used to evaluate when the loop terminates, not null
44       * @param closure  The closure to execute, not null
45       * @param doLoop  true to act as a do-while loop, always executing the closure once
46       * @return The {@code while} closure
47       * @throws NullPointerException if the predicate or closure is null
48       */
49      public static <E> Closure<E> whileClosure(final Predicate<? super E> predicate,
50                                                final Closure<? super E> closure, final boolean doLoop) {
51          return new WhileClosure<>(Objects.requireNonNull(predicate, "predicate"),
52                  Objects.requireNonNull(closure, "closure"), doLoop);
53      }
54  
55      /** The test condition */
56      private final Predicate<? super T> iPredicate;
57  
58      /** The closure to call */
59      private final Closure<? super T> iClosure;
60  
61      /** The flag, true is a do loop, false is a while */
62      private final boolean iDoLoop;
63  
64      /**
65       * Constructor that performs no validation.
66       * Use {@code whileClosure} if you want that.
67       *
68       * @param predicate  The predicate used to evaluate when the loop terminates, not null
69       * @param closure  The closure to execute, not null
70       * @param doLoop  true to act as a do-while loop, always executing the closure once
71       */
72      public WhileClosure(final Predicate<? super T> predicate, final Closure<? super T> closure, final boolean doLoop) {
73          iPredicate = predicate;
74          iClosure = closure;
75          iDoLoop = doLoop;
76      }
77  
78      /**
79       * Executes the closure until the predicate is false.
80       *
81       * @param input  The input object
82       */
83      @Override
84      public void execute(final T input) {
85          if (iDoLoop) {
86              iClosure.accept(input);
87          }
88          while (iPredicate.test(input)) {
89              iClosure.accept(input);
90          }
91      }
92  
93      /**
94       * Gets the closure.
95       *
96       * @return The closure
97       * @since 3.1
98       */
99      public Closure<? super T> getClosure() {
100         return iClosure;
101     }
102 
103     /**
104      * Gets the predicate in use.
105      *
106      * @return The predicate
107      * @since 3.1
108      */
109     public Predicate<? super T> getPredicate() {
110         return iPredicate;
111     }
112 
113     /**
114      * Is the loop a do-while loop.
115      *
116      * @return true is do-while, false if while
117      * @since 3.1
118      */
119     public boolean isDoLoop() {
120         return iDoLoop;
121     }
122 
123 }