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 *      https://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
055    /** The test condition */
056    private final Predicate<? super T> iPredicate;
057
058    /** The closure to call */
059    private final Closure<? super T> iClosure;
060
061    /** The flag, true is a do loop, false is a while */
062    private final boolean iDoLoop;
063
064    /**
065     * Constructor that performs no validation.
066     * Use {@code whileClosure} if you want that.
067     *
068     * @param predicate  The predicate used to evaluate when the loop terminates, not null
069     * @param closure  The closure to execute, not null
070     * @param doLoop  true to act as a do-while loop, always executing the closure once
071     */
072    public WhileClosure(final Predicate<? super T> predicate, final Closure<? super T> closure, final boolean doLoop) {
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 T input) {
085        if (iDoLoop) {
086            iClosure.accept(input);
087        }
088        while (iPredicate.test(input)) {
089            iClosure.accept(input);
090        }
091    }
092
093    /**
094     * Gets the closure.
095     *
096     * @return The closure
097     * @since 3.1
098     */
099    public Closure<? super T> getClosure() {
100        return iClosure;
101    }
102
103    /**
104     * Gets the predicate in use.
105     *
106     * @return The predicate
107     * @since 3.1
108     */
109    public Predicate<? super T> getPredicate() {
110        return iPredicate;
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}