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.ChainedTransformer; 23 import org.apache.commons.collections.functors.CloneTransformer; 24 import org.apache.commons.collections.functors.ClosureTransformer; 25 import org.apache.commons.collections.functors.ConstantTransformer; 26 import org.apache.commons.collections.functors.EqualPredicate; 27 import org.apache.commons.collections.functors.ExceptionTransformer; 28 import org.apache.commons.collections.functors.FactoryTransformer; 29 import org.apache.commons.collections.functors.InstantiateTransformer; 30 import org.apache.commons.collections.functors.InvokerTransformer; 31 import org.apache.commons.collections.functors.MapTransformer; 32 import org.apache.commons.collections.functors.NOPTransformer; 33 import org.apache.commons.collections.functors.PredicateTransformer; 34 import org.apache.commons.collections.functors.StringValueTransformer; 35 import org.apache.commons.collections.functors.SwitchTransformer; 36 37 65 public class TransformerUtils { 66 67 70 public TransformerUtils() { 71 super(); 72 } 73 74 82 public static Transformer exceptionTransformer() { 83 return ExceptionTransformer.INSTANCE; 84 } 85 86 93 public static Transformer nullTransformer() { 94 return ConstantTransformer.NULL_INSTANCE; 95 } 96 97 106 public static Transformer nopTransformer() { 107 return NOPTransformer.INSTANCE; 108 } 109 110 124 public static Transformer cloneTransformer() { 125 return CloneTransformer.INSTANCE; 126 } 127 128 137 public static Transformer constantTransformer(Object constantToReturn) { 138 return ConstantTransformer.getInstance(constantToReturn); 139 } 140 141 151 public static Transformer asTransformer(Closure closure) { 152 return ClosureTransformer.getInstance(closure); 153 } 154 155 165 public static Transformer asTransformer(Predicate predicate) { 166 return PredicateTransformer.getInstance(predicate); 167 } 168 169 179 public static Transformer asTransformer(Factory factory) { 180 return FactoryTransformer.getInstance(factory); 181 } 182 183 194 public static Transformer chainedTransformer(Transformer transformer1, Transformer transformer2) { 195 return ChainedTransformer.getInstance(transformer1, transformer2); 196 } 197 198 209 public static Transformer chainedTransformer(Transformer[] transformers) { 210 return ChainedTransformer.getInstance(transformers); 211 } 212 213 225 public static Transformer chainedTransformer(Collection transformers) { 226 return ChainedTransformer.getInstance(transformers); 227 } 228 229 242 public static Transformer switchTransformer(Predicate predicate, Transformer trueTransformer, Transformer falseTransformer) { 243 return SwitchTransformer.getInstance(new Predicate[] { predicate }, new Transformer[] { trueTransformer }, falseTransformer); 244 } 245 246 262 public static Transformer switchTransformer(Predicate[] predicates, Transformer[] transformers) { 263 return SwitchTransformer.getInstance(predicates, transformers, null); 264 } 265 266 284 public static Transformer switchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) { 285 return SwitchTransformer.getInstance(predicates, transformers, defaultTransformer); 286 } 287 288 309 public static Transformer switchTransformer(Map predicatesAndTransformers) { 310 return SwitchTransformer.getInstance(predicatesAndTransformers); 311 } 312 313 330 public static Transformer switchMapTransformer(Map objectsAndTransformers) { 331 Transformer[] trs = null; 332 Predicate[] preds = null; 333 if (objectsAndTransformers == null) { 334 throw new IllegalArgumentException ("The object and transformer map must not be null"); 335 } 336 Transformer def = (Transformer) objectsAndTransformers.remove(null); 337 int size = objectsAndTransformers.size(); 338 trs = new Transformer[size]; 339 preds = new Predicate[size]; 340 int i = 0; 341 for (Iterator it = objectsAndTransformers.entrySet().iterator(); it.hasNext();) { 342 Map.Entry entry = (Map.Entry ) it.next(); 343 preds[i] = EqualPredicate.getInstance(entry.getKey()); 344 trs[i] = (Transformer) entry.getValue(); 345 i++; 346 } 347 return switchTransformer(preds, trs, def); 348 } 349 350 357 public static Transformer instantiateTransformer() { 358 return InstantiateTransformer.NO_ARG_INSTANCE; 359 } 360 361 373 public static Transformer instantiateTransformer(Class [] paramTypes, Object [] args) { 374 return InstantiateTransformer.getInstance(paramTypes, args); 375 } 376 377 387 public static Transformer mapTransformer(Map map) { 388 return MapTransformer.getInstance(map); 389 } 390 391 406 public static Transformer invokerTransformer(String methodName){ 407 return InvokerTransformer.getInstance(methodName, null, null); 408 } 409 410 424 public static Transformer invokerTransformer(String methodName, Class [] paramTypes, Object [] args){ 425 return InvokerTransformer.getInstance(methodName, paramTypes, args); 426 } 427 428 437 public static Transformer stringValueTransformer() { 438 return StringValueTransformer.INSTANCE; 439 } 440 441 } 442 | Popular Tags |