Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
TransformerMap |
|
| 1.2;1.2 |
1 | /* |
|
2 | * Copyright 2003-2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
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$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $ |
|
32 | */ |
|
33 | public class TransformerMap implements NumberTransformer, Serializable { |
|
34 | ||
35 | /** Serializable version identifier */ |
|
36 | static final long serialVersionUID = -942772950698439883L; |
|
37 | ||
38 | /** |
|
39 | * A default Number Transformer for Numbers and numeric Strings. |
|
40 | */ |
|
41 | 30 | private NumberTransformer defaultTransformer = null; |
42 | ||
43 | /** |
|
44 | * The internal Map. |
|
45 | */ |
|
46 | 30 | private Map map = null; |
47 | ||
48 | /** |
|
49 | * |
|
50 | */ |
|
51 | 30 | public TransformerMap() { |
52 | 30 | map = new HashMap(); |
53 | 30 | defaultTransformer = new DefaultTransformer(); |
54 | 30 | } |
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 | 10 | 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 | 6 | 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 | 18 | 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 | 30 | 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 | 2 | return map.remove(key); |
104 | } |
|
105 | ||
106 | /** |
|
107 | * Clears all the Class to Transformer mappings. |
|
108 | */ |
|
109 | public void clear() { |
|
110 | 2 | map.clear(); |
111 | 2 | } |
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 | 2 | 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 | 2 | 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 | 266 | double value = Double.NaN; |
138 | ||
139 | 266 | if (o instanceof Number || o instanceof String) { |
140 | 250 | value = defaultTransformer.transform(o); |
141 | } else { |
|
142 | 16 | NumberTransformer trans = getTransformer(o.getClass()); |
143 | 16 | if (trans != null) { |
144 | 16 | value = trans.transform(o); |
145 | } |
|
146 | } |
|
147 | ||
148 | 266 | return value; |
149 | } |
|
150 | ||
151 | } |