001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.util;
017:
018: import java.math.BigDecimal;
019: import java.math.BigInteger;
020:
021: import org.geotools.factory.Hints;
022:
023: /**
024: * ConverterFactory which converts between the "standard" numeric types.
025: * <p>
026: * Supported types:
027: * <ul>
028: * <li>{@link java.lang.Long}
029: * <li>{@link java.lang.Integer}
030: * <li>{@link java.lang.Short}
031: * <li>{@link java.lang.Byte}
032: * <li>{@link java.lang.BigInteger}
033: * <li>{@link java.lang.Double}
034: * <li>{@link java.lang.Float}
035: * <li>{@link java.lang.BigDecimal}
036: * </ul>
037: * </p>
038: *
039: * @author Justin Deoliveira, The Open Planning Project
040: * @since 2.4
041: */
042: public class NumericConverterFactory implements ConverterFactory {
043:
044: public Converter createConverter(Class source, Class target,
045: Hints hints) {
046:
047: //check if source is a number or a string. We can't convert to a number
048: // from anything else.
049: if (!(Number.class.isAssignableFrom(source))
050: && !(String.class.isAssignableFrom(source)))
051: return null;
052:
053: //check if target is one of supported
054: if (Long.class.equals(target) || Integer.class.equals(target)
055: || Short.class.equals(target)
056: || Byte.class.equals(target)
057: || BigInteger.class.equals(target)
058: || BigDecimal.class.equals(target)
059: || Double.class.equals(target)
060: || Float.class.equals(target)
061: || Number.class.equals(target)) {
062: return new NumericConverter();
063: }
064:
065: return null;
066: }
067:
068: class NumericConverter implements Converter {
069:
070: public Object convert(Object source, Class target)
071: throws Exception {
072: if (source instanceof Number) {
073: Number s = (Number) source;
074:
075: //integral
076: if (Long.class.equals(target)) {
077: return new Long(s.longValue());
078: }
079: if (Integer.class.equals(target)) {
080: return new Integer(s.intValue());
081: }
082: if (Short.class.equals(target)) {
083: return new Short(s.shortValue());
084: }
085: if (Byte.class.equals(target)) {
086: return new Byte(s.byteValue());
087: }
088: if (BigInteger.class.equals(target)) {
089: return BigInteger.valueOf(s.longValue());
090: }
091:
092: //floating point
093: if (Double.class.equals(target)) {
094: return new Double(s.doubleValue());
095: }
096: if (Float.class.equals(target)) {
097: return new Float(s.floatValue());
098: }
099: if (BigDecimal.class.equals(target)) {
100: return new BigDecimal(s.doubleValue());
101: }
102:
103: if (Number.class.equals(target)) {
104: try {
105: return new Integer(s.toString());
106: } catch (Exception e) {
107: }
108:
109: try {
110: return new BigInteger(s.toString());
111: } catch (Exception e) {
112: }
113:
114: try {
115: return new Double(s.toString());
116: } catch (Exception e) {
117: }
118:
119: try {
120: return new BigDecimal(s.toString());
121: } catch (Exception e) {
122: }
123: }
124: } else if (source instanceof String) {
125: String s = (String) source;
126:
127: //ensure we trim any space off the string
128: s = s.trim();
129:
130: //textual
131: if (Long.class.equals(target)) {
132: return new Long(s);
133: }
134: if (Integer.class.equals(target)) {
135: return new Integer(s);
136: }
137: if (Short.class.equals(target)) {
138: return new Short(s);
139: }
140: if (Byte.class.equals(target)) {
141: return new Byte(s);
142: }
143: if (BigInteger.class.equals(target)) {
144: return new BigInteger(s);
145: }
146:
147: //floating point
148: if (Double.class.equals(target)) {
149: return new Double(s);
150: }
151: if (Float.class.equals(target)) {
152: return new Float(s);
153: }
154: if (BigDecimal.class.equals(target)) {
155: return new BigDecimal(s);
156: }
157:
158: //fallback. If you ask for Number, you get our 'best guess'
159: if (Number.class.equals(target)) {
160: try {
161: return new Integer(s);
162: } catch (Exception e) {
163: }
164:
165: try {
166: return new BigInteger(s);
167: } catch (Exception e) {
168: }
169:
170: try {
171: return new Double(s);
172: } catch (Exception e) {
173: }
174:
175: try {
176: return new BigDecimal(s);
177: } catch (Exception e) {
178: }
179: }
180: }
181: //nothing matched. Return null.
182: return null;
183: }
184: }
185:
186: }
|