1 16 package org.apache.commons.collections; 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.apache.commons.collections.functors.ChainedClosure; 23 import org.apache.commons.collections.functors.EqualPredicate; 24 import org.apache.commons.collections.functors.ExceptionClosure; 25 import org.apache.commons.collections.functors.ForClosure; 26 import org.apache.commons.collections.functors.IfClosure; 27 import org.apache.commons.collections.functors.InvokerTransformer; 28 import org.apache.commons.collections.functors.NOPClosure; 29 import org.apache.commons.collections.functors.SwitchClosure; 30 import org.apache.commons.collections.functors.TransformerClosure; 31 import org.apache.commons.collections.functors.WhileClosure; 32 33 55 public class ClosureUtils { 56 57 60 public ClosureUtils() { 61 super(); 62 } 63 64 72 public static Closure exceptionClosure() { 73 return ExceptionClosure.INSTANCE; 74 } 75 76 84 public static Closure nopClosure() { 85 return NOPClosure.INSTANCE; 86 } 87 88 98 public static Closure asClosure(Transformer transformer) { 99 return TransformerClosure.getInstance(transformer); 100 } 101 102 113 public static Closure forClosure(int count, Closure closure) { 114 return ForClosure.getInstance(count, closure); 115 } 116 117 128 public static Closure whileClosure(Predicate predicate, Closure closure) { 129 return WhileClosure.getInstance(predicate, closure, false); 130 } 131 132 143 public static Closure doWhileClosure(Closure closure, Predicate predicate) { 144 return WhileClosure.getInstance(predicate, closure, true); 145 } 146 147 158 public static Closure invokerClosure(String methodName) { 159 return asClosure(InvokerTransformer.getInstance(methodName)); 161 } 162 163 177 public static Closure invokerClosure(String methodName, Class [] paramTypes, Object [] args) { 178 return asClosure(InvokerTransformer.getInstance(methodName, paramTypes, args)); 180 } 181 182 193 public static Closure chainedClosure(Closure closure1, Closure closure2) { 194 return ChainedClosure.getInstance(closure1, closure2); 195 } 196 197 208 public static Closure chainedClosure(Closure[] closures) { 209 return ChainedClosure.getInstance(closures); 210 } 211 212 225 public static Closure chainedClosure(Collection closures) { 226 return ChainedClosure.getInstance(closures); 227 } 228 229 242 public static Closure ifClosure(Predicate predicate, Closure trueClosure, Closure falseClosure) { 243 return IfClosure.getInstance(predicate, trueClosure, falseClosure); 244 } 245 246 263 public static Closure switchClosure(Predicate[] predicates, Closure[] closures) { 264 return SwitchClosure.getInstance(predicates, closures, null); 265 } 266 267 286 public static Closure switchClosure(Predicate[] predicates, Closure[] closures, Closure defaultClosure) { 287 return SwitchClosure.getInstance(predicates, closures, defaultClosure); 288 } 289 290 310 public static Closure switchClosure(Map predicatesAndClosures) { 311 return SwitchClosure.getInstance(predicatesAndClosures); 312 } 313 314 331 public static Closure switchMapClosure(Map objectsAndClosures) { 332 Closure[] trs = null; 333 Predicate[] preds = null; 334 if (objectsAndClosures == null) { 335 throw new IllegalArgumentException ("The object and closure map must not be null"); 336 } 337 Closure def = (Closure) objectsAndClosures.remove(null); 338 int size = objectsAndClosures.size(); 339 trs = new Closure[size]; 340 preds = new Predicate[size]; 341 int i = 0; 342 for (Iterator it = objectsAndClosures.entrySet().iterator(); it.hasNext();) { 343 Map.Entry entry = (Map.Entry ) it.next(); 344 preds[i] = EqualPredicate.getInstance(entry.getKey()); 345 trs[i] = (Closure) entry.getValue(); 346 i++; 347 } 348 return switchClosure(preds, trs, def); 349 } 350 351 } 352 | Popular Tags |