1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.collections4.functors; 18 19 import java.util.Objects; 20 21 import org.apache.commons.collections4.Closure; 22 import org.apache.commons.collections4.Predicate; 23 24 /** 25 * Closure implementation that executes a closure repeatedly until a condition is met, 26 * like a do-while or while loop. 27 * <p> 28 * <strong>WARNING:</strong> from v4.1 onwards this class will <strong>not</strong> be serializable anymore 29 * in order to prevent potential remote code execution exploits. Please refer to 30 * <a href="https://issues.apache.org/jira/browse/COLLECTIONS-580">COLLECTIONS-580</a> 31 * for more details. 32 * </p> 33 * 34 * @param <T> the type of the input to the operation. 35 * @since 3.0 36 */ 37 public class WhileClosure<T> implements Closure<T> { 38 39 /** 40 * Factory method that performs validation. 41 * 42 * @param <E> the type that the closure acts on 43 * @param predicate the predicate used to evaluate when the loop terminates, not null 44 * @param closure the closure to execute, not null 45 * @param doLoop true to act as a do-while loop, always executing the closure once 46 * @return the {@code while} closure 47 * @throws NullPointerException if the predicate or closure is null 48 */ 49 public static <E> Closure<E> whileClosure(final Predicate<? super E> predicate, 50 final Closure<? super E> closure, final boolean doLoop) { 51 return new WhileClosure<>(Objects.requireNonNull(predicate, "predicate"), 52 Objects.requireNonNull(closure, "closure"), doLoop); 53 } 54 /** The test condition */ 55 private final Predicate<? super T> iPredicate; 56 /** The closure to call */ 57 private final Closure<? super T> iClosure; 58 59 /** The flag, true is a do loop, false is a while */ 60 private final boolean iDoLoop; 61 62 /** 63 * Constructor that performs no validation. 64 * Use {@code whileClosure} if you want that. 65 * 66 * @param predicate the predicate used to evaluate when the loop terminates, not null 67 * @param closure the closure to execute, not null 68 * @param doLoop true to act as a do-while loop, always executing the closure once 69 */ 70 public WhileClosure(final Predicate<? super T> predicate, final Closure<? super T> closure, final boolean doLoop) { 71 iPredicate = predicate; 72 iClosure = closure; 73 iDoLoop = doLoop; 74 } 75 76 /** 77 * Executes the closure until the predicate is false. 78 * 79 * @param input the input object 80 */ 81 @Override 82 public void execute(final T input) { 83 if (iDoLoop) { 84 iClosure.accept(input); 85 } 86 while (iPredicate.test(input)) { 87 iClosure.accept(input); 88 } 89 } 90 91 /** 92 * Gets the closure. 93 * 94 * @return the closure 95 * @since 3.1 96 */ 97 public Closure<? super T> getClosure() { 98 return iClosure; 99 } 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 }