1 16 package org.apache.commons.math.util; 17 18 import java.io.Serializable ; 19 import java.util.Collection ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import org.apache.commons.math.MathException; 25 26 33 public class TransformerMap implements NumberTransformer, Serializable { 34 35 36 static final long serialVersionUID = -942772950698439883L; 37 38 41 private NumberTransformer defaultTransformer = null; 42 43 46 private Map map = null; 47 48 51 public TransformerMap() { 52 map = new HashMap (); 53 defaultTransformer = new DefaultTransformer(); 54 } 55 56 61 public boolean containsClass(Class key) { 62 return map.containsKey(key); 63 } 64 65 70 public boolean containsTransformer(NumberTransformer value) { 71 return map.containsValue(value); 72 } 73 74 80 public NumberTransformer getTransformer(Class key) { 81 return (NumberTransformer) map.get(key); 82 } 83 84 92 public Object putTransformer(Class key, NumberTransformer transformer) { 93 return map.put(key, transformer); 94 } 95 96 102 public Object removeTransformer(Class key) { 103 return map.remove(key); 104 } 105 106 109 public void clear() { 110 map.clear(); 111 } 112 113 117 public Set classes() { 118 return map.keySet(); 119 } 120 121 126 public Collection transformers() { 127 return map.values(); 128 } 129 130 136 public double transform(Object o) throws MathException { 137 double value = Double.NaN; 138 139 if (o instanceof Number || o instanceof String ) { 140 value = defaultTransformer.transform(o); 141 } else { 142 NumberTransformer trans = getTransformer(o.getClass()); 143 if (trans != null) { 144 value = trans.transform(o); 145 } 146 } 147 148 return value; 149 } 150 151 } | Popular Tags |