1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.util;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.math.MathException;
22
23 /**
24 * A Default NumberTransformer for java.lang.Numbers and Numeric Strings. This
25 * provides some simple conversion capabilities to turn any java/lang.Number
26 * into a primitive double or to turn a String representation of a Number into
27 * a double.
28 *
29 * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
30 */
31 public class DefaultTransformer implements NumberTransformer, Serializable {
32
33 /** Serializable version identifier */
34 private static final long serialVersionUID = 4019938025047800455L;
35
36 /**
37 * @param o the object that gets transformed.
38 * @return a double primitive representation of the Object o.
39 * @throws org.apache.commons.math.MathException If it cannot successfully
40 * be transformed or is null.
41 * @see org.apache.commons.collections.Transformer#transform(java.lang.Object)
42 */
43 public double transform(Object o) throws MathException{
44
45 if (o == null) {
46 throw new MathException("Conversion Exception in Transformation, Object is null");
47 }
48
49 if (o instanceof Number) {
50 return ((Number)o).doubleValue();
51 }
52
53 try {
54 return new Double(o.toString()).doubleValue();
55 } catch (Exception e) {
56 throw new MathException("Conversion Exception in Transformation: " + e.getMessage(), e);
57 }
58 }
59 }