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.Transformer; 23 24 35 public class ChainedTransformer implements Transformer, Serializable { 36 37 38 static final long serialVersionUID = 3514945074733160196L; 39 40 41 private final Transformer[] iTransformers; 42 43 51 public static Transformer getInstance(Transformer[] transformers) { 52 FunctorUtils.validate(transformers); 53 if (transformers.length == 0) { 54 return NOPTransformer.INSTANCE; 55 } 56 transformers = FunctorUtils.copy(transformers); 57 return new ChainedTransformer(transformers); 58 } 59 60 70 public static Transformer getInstance(Collection transformers) { 71 if (transformers == null) { 72 throw new IllegalArgumentException ("Transformer collection must not be null"); 73 } 74 if (transformers.size() == 0) { 75 return NOPTransformer.INSTANCE; 76 } 77 Transformer[] cmds = new Transformer[transformers.size()]; 79 int i = 0; 80 for (Iterator it = transformers.iterator(); it.hasNext();) { 81 cmds[i++] = (Transformer) it.next(); 82 } 83 FunctorUtils.validate(cmds); 84 return new ChainedTransformer(cmds); 85 } 86 87 95 public static Transformer getInstance(Transformer transformer1, Transformer transformer2) { 96 if (transformer1 == null || transformer2 == null) { 97 throw new IllegalArgumentException ("Transformers must not be null"); 98 } 99 Transformer[] transformers = new Transformer[] { transformer1, transformer2 }; 100 return new ChainedTransformer(transformers); 101 } 102 103 109 public ChainedTransformer(Transformer[] transformers) { 110 super(); 111 iTransformers = transformers; 112 } 113 114 120 public Object transform(Object object) { 121 for (int i = 0; i < iTransformers.length; i++) { 122 object = iTransformers[i].transform(object); 123 } 124 return object; 125 } 126 127 132 public Transformer[] getTransformers() { 133 return iTransformers; 134 } 135 136 } 137 | Popular Tags |