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 org.apache.commons.collections4.Closure;
020import org.apache.commons.collections4.Predicate;
021
022/**
023 * Closure implementation that executes a closure repeatedly until a condition is met,
024 * like a do-while or while loop.
025 * <p>
026 * <b>WARNING:</b> from v4.1 onwards this class will <b>not</b> be serializable anymore
027 * in order to prevent potential remote code execution exploits. Please refer to
028 * <a href="https://issues.apache.org/jira/browse/COLLECTIONS-580">COLLECTIONS-580</a>
029 * for more details.
030 *
031 * @since 3.0
032 */
033public class WhileClosure<E> implements Closure<E> {
034
035    /** The test condition */
036    private final Predicate<? super E> iPredicate;
037    /** The closure to call */
038    private final Closure<? super E> iClosure;
039    /** The flag, true is a do loop, false is a while */
040    private final boolean iDoLoop;
041
042    /**
043     * Factory method that performs validation.
044     *
045     * @param <E> the type that the closure acts on
046     * @param predicate  the predicate used to evaluate when the loop terminates, not null
047     * @param closure  the closure the execute, not null
048     * @param doLoop  true to act as a do-while loop, always executing the closure once
049     * @return the <code>while</code> closure
050     * @throws NullPointerException if the predicate or closure is null
051     */
052    public static <E> Closure<E> whileClosure(final Predicate<? super E> predicate,
053                                              final Closure<? super E> closure, final boolean doLoop) {
054        if (predicate == null) {
055            throw new NullPointerException("Predicate must not be null");
056        }
057        if (closure == null) {
058            throw new NullPointerException("Closure must not be null");
059        }
060        return new WhileClosure<>(predicate, closure, doLoop);
061    }
062
063    /**
064     * Constructor that performs no validation.
065     * Use <code>whileClosure</code> if you want that.
066     *
067     * @param predicate  the predicate used to evaluate when the loop terminates, not null
068     * @param closure  the closure the execute, not null
069     * @param doLoop  true to act as a do-while loop, always executing the closure once
070     */
071    public WhileClosure(final Predicate<? super E> predicate, final Closure<? super E> closure, final boolean doLoop) {
072        super();
073        iPredicate = predicate;
074        iClosure = closure;
075        iDoLoop = doLoop;
076    }
077
078    /**
079     * Executes the closure until the predicate is false.
080     *
081     * @param input  the input object
082     */
083    @Override
084    public void execute(final E input) {
085        if (iDoLoop) {
086            iClosure.execute(input);
087        }
088        while (iPredicate.evaluate(input)) {
089            iClosure.execute(input);
090        }
091    }
092
093    /**
094     * Gets the predicate in use.
095     *
096     * @return the predicate
097     * @since 3.1
098     */
099    public Predicate<? super E> getPredicate() {
100        return iPredicate;
101    }
102
103    /**
104     * Gets the closure.
105     *
106     * @return the closure
107     * @since 3.1
108     */
109    public Closure<? super E> getClosure() {
110        return iClosure;
111    }
112
113    /**
114     * Is the loop a do-while loop.
115     *
116     * @return true is do-while, false if while
117     * @since 3.1
118     */
119    public boolean isDoLoop() {
120        return iDoLoop;
121    }
122
123}