1 16 package org.apache.commons.collections.functors; 17 18 import java.io.Serializable ; 19 20 import org.apache.commons.collections.Closure; 21 import org.apache.commons.collections.Predicate; 22 23 32 public class WhileClosure implements Closure, Serializable { 33 34 35 static final long serialVersionUID = -3110538116913760108L; 36 37 38 private final Predicate iPredicate; 39 40 private final Closure iClosure; 41 42 private final boolean iDoLoop; 43 44 53 public static Closure getInstance(Predicate predicate, Closure closure, boolean doLoop) { 54 if (predicate == null) { 55 throw new IllegalArgumentException ("Predicate must not be null"); 56 } 57 if (closure == null) { 58 throw new IllegalArgumentException ("Closure must not be null"); 59 } 60 return new WhileClosure(predicate, closure, doLoop); 61 } 62 63 71 public WhileClosure(Predicate predicate, Closure closure, boolean doLoop) { 72 super(); 73 iPredicate = predicate; 74 iClosure = closure; 75 iDoLoop = doLoop; 76 } 77 78 83 public void execute(Object input) { 84 if (iDoLoop) { 85 iClosure.execute(input); 86 } 87 while (iPredicate.evaluate(input)) { 88 iClosure.execute(input); 89 } 90 } 91 92 98 public Predicate getPredicate() { 99 return iPredicate; 100 } 101 102 108 public Closure getClosure() { 109 return iClosure; 110 } 111 112 118 public boolean isDoLoop() { 119 return iDoLoop; 120 } 121 122 } 123 | Popular Tags |