1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 /**
27 * This TansformerMap automates the transformation of of mixed object types.
28 * It provides a means to set NumberTransformers that will be selected
29 * based on the Class of the object handed to the Maps
30 * <code>double transform(Object o)</code> method.
31 * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
32 */
33 public class TransformerMap implements NumberTransformer, Serializable {
34
35 /** Serializable version identifier */
36 private static final long serialVersionUID = -942772950698439883L;
37
38 /**
39 * A default Number Transformer for Numbers and numeric Strings.
40 */
41 private NumberTransformer defaultTransformer = null;
42
43 /**
44 * The internal Map.
45 */
46 private Map map = null;
47
48 /**
49 *
50 */
51 public TransformerMap() {
52 map = new HashMap();
53 defaultTransformer = new DefaultTransformer();
54 }
55
56 /**
57 * Tests if a Class is present in the TransformerMap.
58 * @param key Class to check
59 * @return true|false
60 */
61 public boolean containsClass(Class key) {
62 return map.containsKey(key);
63 }
64
65 /**
66 * Tests if a NumberTransformer is present in the TransformerMap.
67 * @param value NumberTransformer to check
68 * @return true|false
69 */
70 public boolean containsTransformer(NumberTransformer value) {
71 return map.containsValue(value);
72 }
73
74 /**
75 * Returns the Transformer that is mapped to a class
76 * if mapping is not present, this returns null.
77 * @param key The Class of the object
78 * @return the mapped NumberTransformer or null.
79 */
80 public NumberTransformer getTransformer(Class key) {
81 return (NumberTransformer) map.get(key);
82 }
83
84 /**
85 * Sets a Class to Transformer Mapping in the Map. If
86 * the Class is already present, this overwrites that
87 * mapping.
88 * @param key The Class
89 * @param transformer The NumberTransformer
90 * @return the replaced transformer if one is present
91 */
92 public Object putTransformer(Class key, NumberTransformer transformer) {
93 return map.put(key, transformer);
94 }
95
96 /**
97 * Removes a Class to Transformer Mapping in the Map.
98 * @param key The Class
99 * @return the removed transformer if one is present or
100 * null if none was present.
101 */
102 public Object removeTransformer(Class key) {
103 return map.remove(key);
104 }
105
106 /**
107 * Clears all the Class to Transformer mappings.
108 */
109 public void clear() {
110 map.clear();
111 }
112
113 /**
114 * Returns the Set of Classes used as keys in the map.
115 * @return Set of Classes
116 */
117 public Set classes() {
118 return map.keySet();
119 }
120
121 /**
122 * Returns the Set of NumberTransformers used as values
123 * in the map.
124 * @return Set of NumberTransformers
125 */
126 public Collection transformers() {
127 return map.values();
128 }
129
130 /**
131 * Attempts to transform the Object against the map of
132 * NumberTransformers. Otherwise it returns Double.NaN.
133 *
134 * @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
135 */
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 }