KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > util > TransformerMap


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 JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
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 JavaDoc {
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     private NumberTransformer defaultTransformer = null;
42
43     /**
44      * The internal Map.
45      */

46     private Map JavaDoc map = null;
47
48     /**
49      *
50      */

51     public TransformerMap() {
52         map = new HashMap JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc putTransformer(Class JavaDoc 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 JavaDoc removeTransformer(Class JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc o) throws MathException {
137         double value = Double.NaN;
138
139         if (o instanceof Number JavaDoc || o instanceof String JavaDoc) {
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