    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 * <b>WARNING:</b> from v4.1 onwards this class will <b>not</b> 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 * @since 3.0
    035 */
    036public class WhileClosure<E> implements Closure<E> {
    037
    038    /**
    039     * Factory method that performs validation.
    040     *
    041     * @param <E> the type that the closure acts on
    042     * @param predicate  the predicate used to evaluate when the loop terminates, not null
    043     * @param closure  the closure to execute, not null
    044     * @param doLoop  true to act as a do-while loop, always executing the closure once
    045     * @return the {@code while} closure
    046     * @throws NullPointerException if the predicate or closure is null
    047     */
    048    public static <E> Closure<E> whileClosure(final Predicate<? super E> predicate,
    049                                              final Closure<? super E> closure, final boolean doLoop) {
    050        return new WhileClosure<>(Objects.requireNonNull(predicate, "predicate"),
    051                Objects.requireNonNull(closure, "closure"), doLoop);
    052    }
    053    /** The test condition */
    054    private final Predicate<? super E> iPredicate;
    055    /** The closure to call */
    056    private final Closure<? super E> iClosure;
    057
    058    /** The flag, true is a do loop, false is a while */
    059    private final boolean iDoLoop;
    060
    061    /**
    062     * Constructor that performs no validation.
    063     * Use {@code whileClosure} if you want that.
    064     *
    065     * @param predicate  the predicate used to evaluate when the loop terminates, not null
    066     * @param closure  the closure to execute, not null
    067     * @param doLoop  true to act as a do-while loop, always executing the closure once
    068     */
    069    public WhileClosure(final Predicate<? super E> predicate, final Closure<? super E> closure, final boolean doLoop) {
    070        iPredicate = predicate;
    071        iClosure = closure;
    072        iDoLoop = doLoop;
    073    }
    074
    075    /**
    076     * Executes the closure until the predicate is false.
    077     *
    078     * @param input  the input object
    079     */
    080    @Override
    081    public void execute(final E input) {
    082        if (iDoLoop) {
    083            iClosure.execute(input);
    084        }
    085        while (iPredicate.evaluate(input)) {
    086            iClosure.execute(input);
    087        }
    088    }
    089
    090    /**
    091     * Gets the closure.
    092     *
    093     * @return the closure
    094     * @since 3.1
    095     */
    096    public Closure<? super E> getClosure() {
    097        return iClosure;
    098    }
    099
    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}