001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.util;
017:
018: import java.io.Serializable;
019: import java.util.Collection;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Set;
023:
024: import org.apache.commons.math.MathException;
025:
026: /**
027: * This TansformerMap automates the transformation of of mixed object types.
028: * It provides a means to set NumberTransformers that will be selected
029: * based on the Class of the object handed to the Maps
030: * <code>double transform(Object o)</code> method.
031: * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
032: */
033: public class TransformerMap implements NumberTransformer, Serializable {
034:
035: /** Serializable version identifier */
036: private static final long serialVersionUID = -942772950698439883L;
037:
038: /**
039: * A default Number Transformer for Numbers and numeric Strings.
040: */
041: private NumberTransformer defaultTransformer = null;
042:
043: /**
044: * The internal Map.
045: */
046: private Map map = null;
047:
048: /**
049: *
050: */
051: public TransformerMap() {
052: map = new HashMap();
053: defaultTransformer = new DefaultTransformer();
054: }
055:
056: /**
057: * Tests if a Class is present in the TransformerMap.
058: * @param key Class to check
059: * @return true|false
060: */
061: public boolean containsClass(Class key) {
062: return map.containsKey(key);
063: }
064:
065: /**
066: * Tests if a NumberTransformer is present in the TransformerMap.
067: * @param value NumberTransformer to check
068: * @return true|false
069: */
070: public boolean containsTransformer(NumberTransformer value) {
071: return map.containsValue(value);
072: }
073:
074: /**
075: * Returns the Transformer that is mapped to a class
076: * if mapping is not present, this returns null.
077: * @param key The Class of the object
078: * @return the mapped NumberTransformer or null.
079: */
080: public NumberTransformer getTransformer(Class key) {
081: return (NumberTransformer) map.get(key);
082: }
083:
084: /**
085: * Sets a Class to Transformer Mapping in the Map. If
086: * the Class is already present, this overwrites that
087: * mapping.
088: * @param key The Class
089: * @param transformer The NumberTransformer
090: * @return the replaced transformer if one is present
091: */
092: public Object putTransformer(Class key,
093: NumberTransformer transformer) {
094: return map.put(key, transformer);
095: }
096:
097: /**
098: * Removes a Class to Transformer Mapping in the Map.
099: * @param key The Class
100: * @return the removed transformer if one is present or
101: * null if none was present.
102: */
103: public Object removeTransformer(Class key) {
104: return map.remove(key);
105: }
106:
107: /**
108: * Clears all the Class to Transformer mappings.
109: */
110: public void clear() {
111: map.clear();
112: }
113:
114: /**
115: * Returns the Set of Classes used as keys in the map.
116: * @return Set of Classes
117: */
118: public Set classes() {
119: return map.keySet();
120: }
121:
122: /**
123: * Returns the Set of NumberTransformers used as values
124: * in the map.
125: * @return Set of NumberTransformers
126: */
127: public Collection transformers() {
128: return map.values();
129: }
130:
131: /**
132: * Attempts to transform the Object against the map of
133: * NumberTransformers. Otherwise it returns Double.NaN.
134: *
135: * @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
136: */
137: public double transform(Object o) throws MathException {
138: double value = Double.NaN;
139:
140: if (o instanceof Number || o instanceof String) {
141: value = defaultTransformer.transform(o);
142: } else {
143: NumberTransformer trans = getTransformer(o.getClass());
144: if (trans != null) {
145: value = trans.transform(o);
146: }
147: }
148:
149: return value;
150: }
151:
152: }
|