KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > TypeConversionSupport


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.util;
19
20 import java.util.Date JavaDoc;
21 import java.util.HashMap JavaDoc;
22
23 import org.apache.activemq.command.ActiveMQDestination;
24
25 public class TypeConversionSupport {
26
27     static class ConversionKey {
28         final Class JavaDoc from;
29         final Class JavaDoc to;
30         final int hashCode;
31
32         public ConversionKey(Class JavaDoc from, Class JavaDoc to) {
33             this.from = from;
34             this.to = to;
35             this.hashCode = from.hashCode() ^ (to.hashCode() << 1);
36         }
37
38         public boolean equals(Object JavaDoc 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 JavaDoc convert(Object JavaDoc value);
50     }
51
52     static final private HashMap JavaDoc CONVERSION_MAP = new HashMap JavaDoc();
53     static {
54         Converter toStringConverter = new Converter() {
55             public Object JavaDoc convert(Object JavaDoc value) {
56                 return value.toString();
57             }
58         };
59         CONVERSION_MAP.put(new ConversionKey(Boolean JavaDoc.class, String JavaDoc.class), toStringConverter);
60         CONVERSION_MAP.put(new ConversionKey(Byte JavaDoc.class, String JavaDoc.class), toStringConverter);
61         CONVERSION_MAP.put(new ConversionKey(Short JavaDoc.class, String JavaDoc.class), toStringConverter);
62         CONVERSION_MAP.put(new ConversionKey(Integer JavaDoc.class, String JavaDoc.class), toStringConverter);
63         CONVERSION_MAP.put(new ConversionKey(Long JavaDoc.class, String JavaDoc.class), toStringConverter);
64         CONVERSION_MAP.put(new ConversionKey(Float JavaDoc.class, String JavaDoc.class), toStringConverter);
65         CONVERSION_MAP.put(new ConversionKey(Double JavaDoc.class, String JavaDoc.class), toStringConverter);
66
67         CONVERSION_MAP.put(new ConversionKey(String JavaDoc.class, Boolean JavaDoc.class), new Converter() {
68             public Object JavaDoc convert(Object JavaDoc value) {
69                 return Boolean.valueOf((String JavaDoc) value);
70             }
71         });
72         CONVERSION_MAP.put(new ConversionKey(String JavaDoc.class, Byte JavaDoc.class), new Converter() {
73             public Object JavaDoc convert(Object JavaDoc value) {
74                 return Byte.valueOf((String JavaDoc) value);
75             }
76         });
77         CONVERSION_MAP.put(new ConversionKey(String JavaDoc.class, Short JavaDoc.class), new Converter() {
78             public Object JavaDoc convert(Object JavaDoc value) {
79                 return Short.valueOf((String JavaDoc) value);
80             }
81         });
82         CONVERSION_MAP.put(new ConversionKey(String JavaDoc.class, Integer JavaDoc.class), new Converter() {
83             public Object JavaDoc convert(Object JavaDoc value) {
84                 return Integer.valueOf((String JavaDoc) value);
85             }
86         });
87         CONVERSION_MAP.put(new ConversionKey(String JavaDoc.class, Long JavaDoc.class), new Converter() {
88             public Object JavaDoc convert(Object JavaDoc value) {
89                 return Long.valueOf((String JavaDoc) value);
90             }
91         });
92         CONVERSION_MAP.put(new ConversionKey(String JavaDoc.class, Float JavaDoc.class), new Converter() {
93             public Object JavaDoc convert(Object JavaDoc value) {
94                 return Float.valueOf((String JavaDoc) value);
95             }
96         });
97         CONVERSION_MAP.put(new ConversionKey(String JavaDoc.class, Double JavaDoc.class), new Converter() {
98             public Object JavaDoc convert(Object JavaDoc value) {
99                 return Double.valueOf((String JavaDoc) value);
100             }
101         });
102
103         Converter longConverter = new Converter() {
104             public Object JavaDoc convert(Object JavaDoc value) {
105                 return new Long JavaDoc(((Number JavaDoc) value).longValue());
106             }
107         };
108         CONVERSION_MAP.put(new ConversionKey(Byte JavaDoc.class, Long JavaDoc.class), longConverter);
109         CONVERSION_MAP.put(new ConversionKey(Short JavaDoc.class, Long JavaDoc.class), longConverter);
110         CONVERSION_MAP.put(new ConversionKey(Integer JavaDoc.class, Long JavaDoc.class), longConverter);
111         CONVERSION_MAP.put(new ConversionKey(Date JavaDoc.class, Long JavaDoc.class), new Converter() {
112             public Object JavaDoc convert(Object JavaDoc value) {
113                 return new Long JavaDoc(((Date JavaDoc) value).getTime());
114             }
115         });
116
117         Converter intConverter = new Converter() {
118             public Object JavaDoc convert(Object JavaDoc value) {
119                 return new Integer JavaDoc(((Number JavaDoc) value).intValue());
120             }
121         };
122         CONVERSION_MAP.put(new ConversionKey(Byte JavaDoc.class, Integer JavaDoc.class), intConverter);
123         CONVERSION_MAP.put(new ConversionKey(Short JavaDoc.class, Integer JavaDoc.class), intConverter);
124
125         CONVERSION_MAP.put(new ConversionKey(Byte JavaDoc.class, Short JavaDoc.class), new Converter() {
126             public Object JavaDoc convert(Object JavaDoc value) {
127                 return new Short JavaDoc(((Number JavaDoc) value).shortValue());
128             }
129         });
130         
131         CONVERSION_MAP.put(new ConversionKey(Float JavaDoc.class, Double JavaDoc.class), new Converter() {
132             public Object JavaDoc convert(Object JavaDoc value) {
133                 return new Double JavaDoc(((Number JavaDoc) value).doubleValue());
134             }
135         });
136         CONVERSION_MAP.put(new ConversionKey(String JavaDoc.class, ActiveMQDestination.class), new Converter() {
137             public Object JavaDoc convert(Object JavaDoc value) {
138                 return ActiveMQDestination.createDestination((String JavaDoc) value, ActiveMQDestination.QUEUE_TYPE);
139             }
140         });
141     }
142
143     static public Object JavaDoc convert(Object JavaDoc value, Class JavaDoc 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