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.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   * <b>WARNING:</b> from v4.1 onwards this class will <b>not</b> 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   * @since 3.0
35   */
36  public class WhileClosure<E> implements Closure<E> {
37  
38      /**
39       * Factory method that performs validation.
40       *
41       * @param <E> the type that the closure acts on
42       * @param predicate  the predicate used to evaluate when the loop terminates, not null
43       * @param closure  the closure to execute, not null
44       * @param doLoop  true to act as a do-while loop, always executing the closure once
45       * @return the {@code while} closure
46       * @throws NullPointerException if the predicate or closure is null
47       */
48      public static <E> Closure<E> whileClosure(final Predicate<? super E> predicate,
49                                                final Closure<? super E> closure, final boolean doLoop) {
50          return new WhileClosure<>(Objects.requireNonNull(predicate, "predicate"),
51                  Objects.requireNonNull(closure, "closure"), doLoop);
52      }
53      /** The test condition */
54      private final Predicate<? super E> iPredicate;
55      /** The closure to call */
56      private final Closure<? super E> iClosure;
57  
58      /** The flag, true is a do loop, false is a while */
59      private final boolean iDoLoop;
60  
61      /**
62       * Constructor that performs no validation.
63       * Use {@code whileClosure} if you want that.
64       *
65       * @param predicate  the predicate used to evaluate when the loop terminates, not null
66       * @param closure  the closure to execute, not null
67       * @param doLoop  true to act as a do-while loop, always executing the closure once
68       */
69      public WhileClosure(final Predicate<? super E> predicate, final Closure<? super E> closure, final boolean doLoop) {
70          iPredicate = predicate;
71          iClosure = closure;
72          iDoLoop = doLoop;
73      }
74  
75      /**
76       * Executes the closure until the predicate is false.
77       *
78       * @param input  the input object
79       */
80      @Override
81      public void execute(final E input) {
82          if (iDoLoop) {
83              iClosure.execute(input);
84          }
85          while (iPredicate.evaluate(input)) {
86              iClosure.execute(input);
87          }
88      }
89  
90      /**
91       * Gets the closure.
92       *
93       * @return the closure
94       * @since 3.1
95       */
96      public Closure<? super E> getClosure() {
97          return iClosure;
98      }
99  
100     /**
101      * Gets the predicate in use.
102      *
103      * @return the predicate
104      * @since 3.1
105      */
106     public Predicate<? super E> getPredicate() {
107         return iPredicate;
108     }
109 
110     /**
111      * Is the loop a do-while loop.
112      *
113      * @return true is do-while, false if while
114      * @since 3.1
115      */
116     public boolean isDoLoop() {
117         return iDoLoop;
118     }
119 
120 }