1 16 package org.apache.commons.collections.functors; 17 18 import java.io.Serializable ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.apache.commons.collections.Predicate; 23 import org.apache.commons.collections.Transformer; 24 25 34 public class SwitchTransformer implements Transformer, Serializable { 35 36 37 static final long serialVersionUID = -6404460890903469332L; 38 39 40 private final Predicate[] iPredicates; 41 42 private final Transformer[] iTransformers; 43 44 private final Transformer iDefault; 45 46 56 public static Transformer getInstance(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) { 57 FunctorUtils.validate(predicates); 58 FunctorUtils.validate(transformers); 59 if (predicates.length != transformers.length) { 60 throw new IllegalArgumentException ("The predicate and transformer arrays must be the same size"); 61 } 62 if (predicates.length == 0) { 63 return (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer); 64 } 65 predicates = FunctorUtils.copy(predicates); 66 transformers = FunctorUtils.copy(transformers); 67 return new SwitchTransformer(predicates, transformers, defaultTransformer); 68 } 69 70 87 public static Transformer getInstance(Map predicatesAndTransformers) { 88 Transformer[] transformers = null; 89 Predicate[] preds = null; 90 if (predicatesAndTransformers == null) { 91 throw new IllegalArgumentException ("The predicate and transformer map must not be null"); 92 } 93 if (predicatesAndTransformers.size() == 0) { 94 return ConstantTransformer.NULL_INSTANCE; 95 } 96 Transformer defaultTransformer = (Transformer) predicatesAndTransformers.remove(null); 98 int size = predicatesAndTransformers.size(); 99 if (size == 0) { 100 return (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer); 101 } 102 transformers = new Transformer[size]; 103 preds = new Predicate[size]; 104 int i = 0; 105 for (Iterator it = predicatesAndTransformers.entrySet().iterator(); it.hasNext();) { 106 Map.Entry entry = (Map.Entry ) it.next(); 107 preds[i] = (Predicate) entry.getKey(); 108 transformers[i] = (Transformer) entry.getValue(); 109 i++; 110 } 111 return new SwitchTransformer(preds, transformers, defaultTransformer); 112 } 113 114 122 public SwitchTransformer(Predicate[] predicates, Transformer[] transformers, Transformer defaultTransformer) { 123 super(); 124 iPredicates = predicates; 125 iTransformers = transformers; 126 iDefault = (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer); 127 } 128 129 136 public Object transform(Object input) { 137 for (int i = 0; i < iPredicates.length; i++) { 138 if (iPredicates[i].evaluate(input) == true) { 139 return iTransformers[i].transform(input); 140 } 141 } 142 return iDefault.transform(input); 143 } 144 145 151 public Predicate[] getPredicates() { 152 return iPredicates; 153 } 154 155 161 public Transformer[] getTransformers() { 162 return iTransformers; 163 } 164 165 171 public Transformer getDefaultTransformer() { 172 return iDefault; 173 } 174 175 } 176 | Popular Tags |