001: package org.obe.convert;
002:
003: import java.math.BigDecimal;
004: import java.math.BigInteger;
005: import java.util.Date;
006: import java.util.HashMap;
007: import java.util.Map;
008:
009: /**
010: * @author Adrian Price
011: */
012: public final class ConverterRegistry {
013: private static final ConverterRegistry _theInstance = new ConverterRegistry();
014: private Map _converters = new HashMap();
015: private Map _calculators = new HashMap();
016:
017: private static class StringConverter extends AbstractCalculator
018: implements Converter {
019:
020: public Class getTargetClass() {
021: return String.class;
022: }
023:
024: public Object convert(Object value) {
025: return value == null ? null : value.toString();
026: }
027:
028: public Object add(Object lhs, Object rhs) {
029: StringBuffer sb = new StringBuffer();
030: if (lhs != null)
031: sb.append(lhs);
032: if (rhs != null)
033: sb.append(rhs);
034: return sb.toString();
035: }
036: }
037:
038: private static abstract class NumberCalculator implements
039: Calculator {
040: public Object add(Object lhs, Object rhs) {
041: return new Integer(((Number) lhs).intValue()
042: + ((Number) rhs).intValue());
043: }
044:
045: public Object subtract(Object lhs, Object rhs) {
046: return new Integer(((Number) lhs).intValue()
047: - ((Number) rhs).intValue());
048: }
049:
050: public Object multiply(Object lhs, Object rhs) {
051: return new Integer(((Number) lhs).intValue()
052: * ((Number) rhs).intValue());
053: }
054:
055: public Object divide(Object lhs, Object rhs) {
056: return new Integer(((Number) lhs).intValue()
057: / ((Number) rhs).intValue());
058: }
059:
060: public Object negate(Object rhs) {
061: return rhs == null ? null : new Integer(-((Number) rhs)
062: .intValue());
063: }
064:
065: public int compare(Object lhs, Object rhs) {
066: return ((Comparable) lhs).compareTo(rhs);
067: }
068: }
069:
070: private static class ByteConverter extends NumberCalculator
071: implements Converter {
072:
073: public Class getTargetClass() {
074: return Byte.class;
075: }
076:
077: public Object convert(Object value) {
078: if (value == null || value instanceof Byte)
079: return value;
080: if (value instanceof Number)
081: return new Byte(((Number) value).byteValue());
082: return Byte.valueOf(value.toString());
083: }
084: }
085:
086: private static class ShortConverter extends NumberCalculator
087: implements Converter {
088:
089: public Class getTargetClass() {
090: return Short.class;
091: }
092:
093: public Object convert(Object value) {
094: if (value == null || value instanceof Short)
095: return value;
096: if (value instanceof Number)
097: return new Short(((Number) value).shortValue());
098: return Short.valueOf(value.toString());
099: }
100: }
101:
102: private static class IntegerConverter extends NumberCalculator
103: implements Converter {
104:
105: public Class getTargetClass() {
106: return Integer.class;
107: }
108:
109: public Object convert(Object value) {
110: if (value == null || value instanceof Integer)
111: return value;
112: if (value instanceof Number)
113: return new Integer(((Number) value).intValue());
114: return Integer.valueOf(value.toString());
115: }
116: }
117:
118: private static class LongConverter implements Calculator, Converter {
119: public Class getTargetClass() {
120: return Long.class;
121: }
122:
123: public Object convert(Object value) {
124: if (value == null || value instanceof Long)
125: return value;
126: if (value instanceof Number)
127: return new Long(((Number) value).longValue());
128: return Long.valueOf(value.toString());
129: }
130:
131: public Object add(Object lhs, Object rhs) {
132: return new Long(((Number) lhs).longValue()
133: + ((Number) rhs).longValue());
134: }
135:
136: public Object subtract(Object lhs, Object rhs) {
137: return new Long(((Number) lhs).longValue()
138: - ((Number) rhs).longValue());
139: }
140:
141: public Object multiply(Object lhs, Object rhs) {
142: return new Long(((Number) lhs).longValue()
143: * ((Number) rhs).longValue());
144: }
145:
146: public Object divide(Object lhs, Object rhs) {
147: return new Long(((Number) lhs).longValue()
148: / ((Number) rhs).longValue());
149: }
150:
151: public Object negate(Object rhs) {
152: return rhs == null ? null : new Long(-((Number) rhs)
153: .longValue());
154: }
155:
156: public int compare(Object lhs, Object rhs) {
157: return ((Long) lhs).compareTo((Long) rhs);
158: }
159: }
160:
161: private static class FloatConverter implements Calculator,
162: Converter {
163: public Class getTargetClass() {
164: return Float.class;
165: }
166:
167: public Object convert(Object value) {
168: if (value == null || value instanceof Float)
169: return value;
170: if (value instanceof Number)
171: return new Float(((Number) value).floatValue());
172: return Float.valueOf(value.toString());
173: }
174:
175: public Object add(Object lhs, Object rhs) {
176: return new Float(((Number) lhs).floatValue()
177: + ((Number) rhs).floatValue());
178: }
179:
180: public Object subtract(Object lhs, Object rhs) {
181: return new Float(((Number) lhs).floatValue()
182: - ((Number) rhs).floatValue());
183: }
184:
185: public Object multiply(Object lhs, Object rhs) {
186: return new Float(((Number) lhs).floatValue()
187: * ((Number) rhs).floatValue());
188: }
189:
190: public Object divide(Object lhs, Object rhs) {
191: return new Float(((Number) lhs).floatValue()
192: / ((Number) rhs).floatValue());
193: }
194:
195: public Object negate(Object rhs) {
196: return rhs == null ? null : new Float(-((Number) rhs)
197: .floatValue());
198: }
199:
200: public int compare(Object lhs, Object rhs) {
201: return ((Float) lhs).compareTo((Float) rhs);
202: }
203: }
204:
205: private static class DoubleConverter implements Calculator,
206: Converter {
207: public Class getTargetClass() {
208: return Double.class;
209: }
210:
211: public Object convert(Object value) {
212: if (value == null || value instanceof Double)
213: return value;
214: if (value instanceof Number)
215: return new Double(((Number) value).doubleValue());
216: return Double.valueOf(value.toString());
217: }
218:
219: public Object add(Object lhs, Object rhs) {
220: return new Double(((Number) lhs).doubleValue()
221: + ((Number) rhs).doubleValue());
222: }
223:
224: public Object subtract(Object lhs, Object rhs) {
225: return new Double(((Number) lhs).doubleValue()
226: - ((Number) rhs).doubleValue());
227: }
228:
229: public Object multiply(Object lhs, Object rhs) {
230: return new Double(((Number) lhs).doubleValue()
231: * ((Number) rhs).doubleValue());
232: }
233:
234: public Object divide(Object lhs, Object rhs) {
235: return new Double(((Number) lhs).doubleValue()
236: / ((Number) rhs).doubleValue());
237: }
238:
239: public Object negate(Object rhs) {
240: return rhs == null ? null : new Double(-((Number) rhs)
241: .doubleValue());
242: }
243:
244: public int compare(Object lhs, Object rhs) {
245: return ((Float) lhs).compareTo((Float) rhs);
246: }
247: }
248:
249: private static class BigIntegerConverter implements Calculator,
250: Converter {
251: public Class getTargetClass() {
252: return BigInteger.class;
253: }
254:
255: public Object convert(Object value) {
256: if (value == null || value instanceof BigInteger)
257: return value;
258: return new BigInteger(value.toString());
259: }
260:
261: public Object add(Object lhs, Object rhs) {
262: return ((BigInteger) lhs).add((BigInteger) rhs);
263: }
264:
265: public Object subtract(Object lhs, Object rhs) {
266: return ((BigInteger) lhs).subtract((BigInteger) rhs);
267: }
268:
269: public Object multiply(Object lhs, Object rhs) {
270: return ((BigInteger) lhs).multiply((BigInteger) rhs);
271: }
272:
273: public Object divide(Object lhs, Object rhs) {
274: return ((BigInteger) lhs).divide((BigInteger) rhs);
275: }
276:
277: public Object negate(Object rhs) {
278: return rhs == null ? null : ((BigInteger) rhs).negate();
279: }
280:
281: public int compare(Object lhs, Object rhs) {
282: return ((BigInteger) lhs).compareTo((BigInteger) rhs);
283: }
284: }
285:
286: private static class BigDecimalConverter implements Calculator,
287: Converter {
288: public Class getTargetClass() {
289: return BigDecimal.class;
290: }
291:
292: public Object convert(Object value) {
293: if (value == null || value instanceof BigDecimal)
294: return value;
295: return new BigDecimal(value.toString());
296: }
297:
298: public Object add(Object lhs, Object rhs) {
299: return ((BigDecimal) lhs).add((BigDecimal) rhs);
300: }
301:
302: public Object subtract(Object lhs, Object rhs) {
303: return ((BigDecimal) lhs).subtract((BigDecimal) rhs);
304: }
305:
306: public Object multiply(Object lhs, Object rhs) {
307: return ((BigDecimal) lhs).multiply((BigDecimal) rhs);
308: }
309:
310: public Object divide(Object lhs, Object rhs) {
311: return ((BigDecimal) lhs).divide((BigDecimal) rhs,
312: BigDecimal.ROUND_HALF_EVEN);
313: }
314:
315: public Object negate(Object rhs) {
316: return rhs == null ? null : ((BigDecimal) rhs).negate();
317: }
318:
319: public int compare(Object lhs, Object rhs) {
320: return ((BigDecimal) lhs).compareTo((BigDecimal) rhs);
321: }
322: }
323:
324: private static class DateConverter implements Converter {
325: public Class getTargetClass() {
326: return Date.class;
327: }
328:
329: public Object convert(Object value) {
330: if (value == null || value instanceof Date)
331: return value;
332: // TODO: Use DateFormat instead.
333: return new Date(value.toString());
334: }
335: }
336:
337: public static ConverterRegistry getInstance() {
338: return _theInstance;
339: }
340:
341: private ConverterRegistry() {
342: register(new StringConverter());
343: register(new ByteConverter());
344: register(new ShortConverter());
345: register(new IntegerConverter());
346: register(new LongConverter());
347: register(new FloatConverter());
348: register(new DoubleConverter());
349: register(new BigIntegerConverter());
350: register(new BigDecimalConverter());
351: register(new DateConverter());
352: }
353:
354: public Object convert(Class targetClass, Object value) {
355: return findConverter(targetClass).convert(value);
356: }
357:
358: public void register(ClassUtility utility) {
359: Class targetClass = utility.getTargetClass();
360: if (utility instanceof Converter)
361: _converters.put(targetClass, utility);
362: if (utility instanceof Calculator)
363: _calculators.put(targetClass, utility);
364: }
365:
366: public Calculator findCalculator(Class targetClass) {
367: Object converter = _calculators.get(targetClass);
368: if (converter == null) {
369: throw new IllegalArgumentException(
370: "No calculator registered for " + targetClass);
371: }
372: return (Calculator) converter;
373: }
374:
375: public Converter findConverter(Class targetClass) {
376: Object converter = _converters.get(targetClass);
377: if (converter == null) {
378: throw new IllegalArgumentException(
379: "No converter registered for " + targetClass);
380: }
381: return (Converter) converter;
382: }
383: }
|