1 18 package org.apache.activemq.util; 19 20 import java.util.Date ; 21 import java.util.HashMap ; 22 23 import org.apache.activemq.command.ActiveMQDestination; 24 25 public class TypeConversionSupport { 26 27 static class ConversionKey { 28 final Class from; 29 final Class to; 30 final int hashCode; 31 32 public ConversionKey(Class from, Class to) { 33 this.from = from; 34 this.to = to; 35 this.hashCode = from.hashCode() ^ (to.hashCode() << 1); 36 } 37 38 public boolean equals(Object o) { 39 ConversionKey x = (ConversionKey) o; 40 return x.from == from && x.to == to; 41 } 42 43 public int hashCode() { 44 return hashCode; 45 } 46 } 47 48 interface Converter { 49 Object convert(Object value); 50 } 51 52 static final private HashMap CONVERSION_MAP = new HashMap (); 53 static { 54 Converter toStringConverter = new Converter() { 55 public Object convert(Object value) { 56 return value.toString(); 57 } 58 }; 59 CONVERSION_MAP.put(new ConversionKey(Boolean .class, String .class), toStringConverter); 60 CONVERSION_MAP.put(new ConversionKey(Byte .class, String .class), toStringConverter); 61 CONVERSION_MAP.put(new ConversionKey(Short .class, String .class), toStringConverter); 62 CONVERSION_MAP.put(new ConversionKey(Integer .class, String .class), toStringConverter); 63 CONVERSION_MAP.put(new ConversionKey(Long .class, String .class), toStringConverter); 64 CONVERSION_MAP.put(new ConversionKey(Float .class, String .class), toStringConverter); 65 CONVERSION_MAP.put(new ConversionKey(Double .class, String .class), toStringConverter); 66 67 CONVERSION_MAP.put(new ConversionKey(String .class, Boolean .class), new Converter() { 68 public Object convert(Object value) { 69 return Boolean.valueOf((String ) value); 70 } 71 }); 72 CONVERSION_MAP.put(new ConversionKey(String .class, Byte .class), new Converter() { 73 public Object convert(Object value) { 74 return Byte.valueOf((String ) value); 75 } 76 }); 77 CONVERSION_MAP.put(new ConversionKey(String .class, Short .class), new Converter() { 78 public Object convert(Object value) { 79 return Short.valueOf((String ) value); 80 } 81 }); 82 CONVERSION_MAP.put(new ConversionKey(String .class, Integer .class), new Converter() { 83 public Object convert(Object value) { 84 return Integer.valueOf((String ) value); 85 } 86 }); 87 CONVERSION_MAP.put(new ConversionKey(String .class, Long .class), new Converter() { 88 public Object convert(Object value) { 89 return Long.valueOf((String ) value); 90 } 91 }); 92 CONVERSION_MAP.put(new ConversionKey(String .class, Float .class), new Converter() { 93 public Object convert(Object value) { 94 return Float.valueOf((String ) value); 95 } 96 }); 97 CONVERSION_MAP.put(new ConversionKey(String .class, Double .class), new Converter() { 98 public Object convert(Object value) { 99 return Double.valueOf((String ) value); 100 } 101 }); 102 103 Converter longConverter = new Converter() { 104 public Object convert(Object value) { 105 return new Long (((Number ) value).longValue()); 106 } 107 }; 108 CONVERSION_MAP.put(new ConversionKey(Byte .class, Long .class), longConverter); 109 CONVERSION_MAP.put(new ConversionKey(Short .class, Long .class), longConverter); 110 CONVERSION_MAP.put(new ConversionKey(Integer .class, Long .class), longConverter); 111 CONVERSION_MAP.put(new ConversionKey(Date .class, Long .class), new Converter() { 112 public Object convert(Object value) { 113 return new Long (((Date ) value).getTime()); 114 } 115 }); 116 117 Converter intConverter = new Converter() { 118 public Object convert(Object value) { 119 return new Integer (((Number ) value).intValue()); 120 } 121 }; 122 CONVERSION_MAP.put(new ConversionKey(Byte .class, Integer .class), intConverter); 123 CONVERSION_MAP.put(new ConversionKey(Short .class, Integer .class), intConverter); 124 125 CONVERSION_MAP.put(new ConversionKey(Byte .class, Short .class), new Converter() { 126 public Object convert(Object value) { 127 return new Short (((Number ) value).shortValue()); 128 } 129 }); 130 131 CONVERSION_MAP.put(new ConversionKey(Float .class, Double .class), new Converter() { 132 public Object convert(Object value) { 133 return new Double (((Number ) value).doubleValue()); 134 } 135 }); 136 CONVERSION_MAP.put(new ConversionKey(String .class, ActiveMQDestination.class), new Converter() { 137 public Object convert(Object value) { 138 return ActiveMQDestination.createDestination((String ) value, ActiveMQDestination.QUEUE_TYPE); 139 } 140 }); 141 } 142 143 static public Object convert(Object value, Class clazz) { 144 145 assert value != null && clazz != null; 146 147 if (value.getClass() == clazz) 148 return value; 149 150 Converter c = (Converter) CONVERSION_MAP.get(new ConversionKey(value.getClass(), clazz)); 151 if (c == null) 152 return null; 153 return c.convert(value); 154 155 } 156 157 } 158 | Popular Tags |