WhileClosure.java

  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. import java.util.Objects;

  19. import org.apache.commons.collections4.Closure;
  20. import org.apache.commons.collections4.Predicate;

  21. /**
  22.  * Closure implementation that executes a closure repeatedly until a condition is met,
  23.  * like a do-while or while loop.
  24.  * <p>
  25.  * <strong>WARNING:</strong> from v4.1 onwards this class will <strong>not</strong> be serializable anymore
  26.  * in order to prevent potential remote code execution exploits. Please refer to
  27.  * <a href="https://issues.apache.org/jira/browse/COLLECTIONS-580">COLLECTIONS-580</a>
  28.  * for more details.
  29.  * </p>
  30.  *
  31.  * @param <T> the type of the input to the operation.
  32.  * @since 3.0
  33.  */
  34. public class WhileClosure<T> implements Closure<T> {

  35.     /**
  36.      * Factory method that performs validation.
  37.      *
  38.      * @param <E> the type that the closure acts on
  39.      * @param predicate  the predicate used to evaluate when the loop terminates, not null
  40.      * @param closure  the closure to execute, not null
  41.      * @param doLoop  true to act as a do-while loop, always executing the closure once
  42.      * @return the {@code while} closure
  43.      * @throws NullPointerException if the predicate or closure is null
  44.      */
  45.     public static <E> Closure<E> whileClosure(final Predicate<? super E> predicate,
  46.                                               final Closure<? super E> closure, final boolean doLoop) {
  47.         return new WhileClosure<>(Objects.requireNonNull(predicate, "predicate"),
  48.                 Objects.requireNonNull(closure, "closure"), doLoop);
  49.     }
  50.     /** The test condition */
  51.     private final Predicate<? super T> iPredicate;
  52.     /** The closure to call */
  53.     private final Closure<? super T> iClosure;

  54.     /** The flag, true is a do loop, false is a while */
  55.     private final boolean iDoLoop;

  56.     /**
  57.      * Constructor that performs no validation.
  58.      * Use {@code whileClosure} if you want that.
  59.      *
  60.      * @param predicate  the predicate used to evaluate when the loop terminates, not null
  61.      * @param closure  the closure to execute, not null
  62.      * @param doLoop  true to act as a do-while loop, always executing the closure once
  63.      */
  64.     public WhileClosure(final Predicate<? super T> predicate, final Closure<? super T> closure, final boolean doLoop) {
  65.         iPredicate = predicate;
  66.         iClosure = closure;
  67.         iDoLoop = doLoop;
  68.     }

  69.     /**
  70.      * Executes the closure until the predicate is false.
  71.      *
  72.      * @param input  the input object
  73.      */
  74.     @Override
  75.     public void execute(final T input) {
  76.         if (iDoLoop) {
  77.             iClosure.accept(input);
  78.         }
  79.         while (iPredicate.test(input)) {
  80.             iClosure.accept(input);
  81.         }
  82.     }

  83.     /**
  84.      * Gets the closure.
  85.      *
  86.      * @return the closure
  87.      * @since 3.1
  88.      */
  89.     public Closure<? super T> getClosure() {
  90.         return iClosure;
  91.     }

  92.     /**
  93.      * Gets the predicate in use.
  94.      *
  95.      * @return the predicate
  96.      * @since 3.1
  97.      */
  98.     public Predicate<? super T> getPredicate() {
  99.         return iPredicate;
  100.     }

  101.     /**
  102.      * Is the loop a do-while loop.
  103.      *
  104.      * @return true is do-while, false if while
  105.      * @since 3.1
  106.      */
  107.     public boolean isDoLoop() {
  108.         return iDoLoop;
  109.     }

  110. }