1 16 package org.apache.commons.collections.functors; 17 18 import java.io.Serializable ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 22 import org.apache.commons.collections.Closure; 23 24 32 public class ChainedClosure implements Closure, Serializable { 33 34 35 static final long serialVersionUID = -3520677225766901240L; 36 37 38 private final Closure[] iClosures; 39 40 48 public static Closure getInstance(Closure[] closures) { 49 FunctorUtils.validate(closures); 50 if (closures.length == 0) { 51 return NOPClosure.INSTANCE; 52 } 53 closures = FunctorUtils.copy(closures); 54 return new ChainedClosure(closures); 55 } 56 57 67 public static Closure getInstance(Collection closures) { 68 if (closures == null) { 69 throw new IllegalArgumentException ("Closure collection must not be null"); 70 } 71 if (closures.size() == 0) { 72 return NOPClosure.INSTANCE; 73 } 74 Closure[] cmds = new Closure[closures.size()]; 76 int i = 0; 77 for (Iterator it = closures.iterator(); it.hasNext();) { 78 cmds[i++] = (Closure) it.next(); 79 } 80 FunctorUtils.validate(cmds); 81 return new ChainedClosure(cmds); 82 } 83 84 92 public static Closure getInstance(Closure closure1, Closure closure2) { 93 if (closure1 == null || closure2 == null) { 94 throw new IllegalArgumentException ("Closures must not be null"); 95 } 96 Closure[] closures = new Closure[] { closure1, closure2 }; 97 return new ChainedClosure(closures); 98 } 99 100 106 public ChainedClosure(Closure[] closures) { 107 super(); 108 iClosures = closures; 109 } 110 111 116 public void execute(Object input) { 117 for (int i = 0; i < iClosures.length; i++) { 118 iClosures[i].execute(input); 119 } 120 } 121 122 127 public Closure[] getClosures() { 128 return iClosures; 129 } 130 131 } 132 | Popular Tags |