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 IfClosure implements Closure, Serializable { 33 34 35 static final long serialVersionUID = 3518477308466486130L; 36 37 38 private final Predicate iPredicate; 39 40 private final Closure iTrueClosure; 41 42 private final Closure iFalseClosure; 43 44 53 public static Closure getInstance(Predicate predicate, Closure trueClosure, Closure falseClosure) { 54 if (predicate == null) { 55 throw new IllegalArgumentException ("Predicate must not be null"); 56 } 57 if (trueClosure == null || falseClosure == null) { 58 throw new IllegalArgumentException ("Closures must not be null"); 59 } 60 return new IfClosure(predicate, trueClosure, falseClosure); 61 } 62 63 71 public IfClosure(Predicate predicate, Closure trueClosure, Closure falseClosure) { 72 super(); 73 iPredicate = predicate; 74 iTrueClosure = trueClosure; 75 iFalseClosure = falseClosure; 76 } 77 78 83 public void execute(Object input) { 84 if (iPredicate.evaluate(input) == true) { 85 iTrueClosure.execute(input); 86 } else { 87 iFalseClosure.execute(input); 88 } 89 } 90 91 97 public Predicate getPredicate() { 98 return iPredicate; 99 } 100 101 107 public Closure getTrueClosure() { 108 return iTrueClosure; 109 } 110 111 117 public Closure getFalseClosure() { 118 return iFalseClosure; 119 } 120 121 } 122 | Popular Tags |