    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.collections4.functors;
    018
    019import java.util.Objects;
    020
    021import org.apache.commons.collections4.Closure;
    022import org.apache.commons.collections4.Predicate;
    023
    024/**
    025 * Closure implementation that executes a closure repeatedly until a condition is met,
    026 * like a do-while or while loop.
    027 * <p>
    028 * <strong>WARNING:</strong> from v4.1 onwards this class will <strong>not</strong> be serializable anymore
    029 * in order to prevent potential remote code execution exploits. Please refer to
    030 * <a href="https://issues.apache.org/jira/browse/COLLECTIONS-580">COLLECTIONS-580</a>
    031 * for more details.
    032 * </p>
    033 *
    034 * @param <T> the type of the input to the operation.
    035 * @since 3.0
    036 */
    037public class WhileClosure<T> implements Closure<T> {
    038
    039    /**
    040     * Factory method that performs validation.
    041     *
    042     * @param <E> the type that the closure acts on
    043     * @param predicate  the predicate used to evaluate when the loop terminates, not null
    044     * @param closure  the closure to execute, not null
    045     * @param doLoop  true to act as a do-while loop, always executing the closure once
    046     * @return the {@code while} closure
    047     * @throws NullPointerException if the predicate or closure is null
    048     */
    049    public static <E> Closure<E> whileClosure(final Predicate<? super E> predicate,
    050                                              final Closure<? super E> closure, final boolean doLoop) {
    051        return new WhileClosure<>(Objects.requireNonNull(predicate, "predicate"),
    052                Objects.requireNonNull(closure, "closure"), doLoop);
    053    }
    054    /** The test condition */
    055    private final Predicate<? super T> iPredicate;
    056    /** The closure to call */
    057    private final Closure<? super T> iClosure;
    058
    059    /** The flag, true is a do loop, false is a while */
    060    private final boolean iDoLoop;
    061
    062    /**
    063     * Constructor that performs no validation.
    064     * Use {@code whileClosure} if you want that.
    065     *
    066     * @param predicate  the predicate used to evaluate when the loop terminates, not null
    067     * @param closure  the closure to execute, not null
    068     * @param doLoop  true to act as a do-while loop, always executing the closure once
    069     */
    070    public WhileClosure(final Predicate<? super T> predicate, final Closure<? super T> closure, final boolean doLoop) {
    071        iPredicate = predicate;
    072        iClosure = closure;
    073        iDoLoop = doLoop;
    074    }
    075
    076    /**
    077     * Executes the closure until the predicate is false.
    078     *
    079     * @param input  the input object
    080     */
    081    @Override
    082    public void execute(final T input) {
    083        if (iDoLoop) {
    084            iClosure.accept(input);
    085        }
    086        while (iPredicate.test(input)) {
    087            iClosure.accept(input);
    088        }
    089    }
    090
    091    /**
    092     * Gets the closure.
    093     *
    094     * @return the closure
    095     * @since 3.1
    096     */
    097    public Closure<? super T> getClosure() {
    098        return iClosure;
    099    }
    100
    101    /**
    102     * Gets the predicate in use.
    103     *
    104     * @return the predicate
    105     * @since 3.1
    106     */
    107    public Predicate<? super T> getPredicate() {
    108        return iPredicate;
    109    }
    110
    111    /**
    112     * Is the loop a do-while loop.
    113     *
    114     * @return true is do-while, false if while
    115     * @since 3.1
    116     */
    117    public boolean isDoLoop() {
    118        return iDoLoop;
    119    }
    120
    121}