Class WhileClosure<T>

  • Type Parameters:
    T - the type of the input to the operation.
    All Implemented Interfaces:
    java.util.function.Consumer<T>, Closure<T>

    public class WhileClosure<T>
    extends java.lang.Object
    implements Closure<T>
    Closure implementation that executes a closure repeatedly until a condition is met, like a do-while or while loop.

    WARNING: from v4.1 onwards this class will not be serializable anymore in order to prevent potential remote code execution exploits. Please refer to COLLECTIONS-580 for more details.

    Since:
    3.0
    • Constructor Summary

      Constructors 
      Constructor Description
      WhileClosure​(Predicate<? super T> predicate, Closure<? super T> closure, boolean doLoop)
      Constructor that performs no validation.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void execute​(T input)
      Executes the closure until the predicate is false.
      Closure<? super T> getClosure()
      Gets the closure.
      Predicate<? super T> getPredicate()
      Gets the predicate in use.
      boolean isDoLoop()
      Is the loop a do-while loop.
      static <E> Closure<E> whileClosure​(Predicate<? super E> predicate, Closure<? super E> closure, boolean doLoop)
      Factory method that performs validation.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface org.apache.commons.collections4.Closure

        accept
      • Methods inherited from interface java.util.function.Consumer

        andThen
    • Constructor Detail

      • WhileClosure

        public WhileClosure​(Predicate<? super T> predicate,
                            Closure<? super T> closure,
                            boolean doLoop)
        Constructor that performs no validation. Use whileClosure if you want that.
        Parameters:
        predicate - the predicate used to evaluate when the loop terminates, not null
        closure - the closure to execute, not null
        doLoop - true to act as a do-while loop, always executing the closure once
    • Method Detail

      • whileClosure

        public static <E> Closure<E> whileClosure​(Predicate<? super E> predicate,
                                                  Closure<? super E> closure,
                                                  boolean doLoop)
        Factory method that performs validation.
        Type Parameters:
        E - the type that the closure acts on
        Parameters:
        predicate - the predicate used to evaluate when the loop terminates, not null
        closure - the closure to execute, not null
        doLoop - true to act as a do-while loop, always executing the closure once
        Returns:
        the while closure
        Throws:
        java.lang.NullPointerException - if the predicate or closure is null
      • execute

        public void execute​(T input)
        Executes the closure until the predicate is false.
        Specified by:
        execute in interface Closure<T>
        Parameters:
        input - the input object
      • getClosure

        public Closure<? super TgetClosure()
        Gets the closure.
        Returns:
        the closure
        Since:
        3.1
      • getPredicate

        public Predicate<? super TgetPredicate()
        Gets the predicate in use.
        Returns:
        the predicate
        Since:
        3.1
      • isDoLoop

        public boolean isDoLoop()
        Is the loop a do-while loop.
        Returns:
        true is do-while, false if while
        Since:
        3.1