KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jms > util > JMSTypeConversions


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.jms.util;
8
9 import javax.jms.MessageFormatException JavaDoc;
10
11 /**
12  * Enforces JMS conversion rules.
13  *
14  * @author <a HREF="mailto:nathan@jboss.org">Nathan Phelps</a>
15  * @version $Revision: 1.1.1.1 $ $Date: 2003/06/07 02:18:28 $
16  */

17 public class JMSTypeConversions
18 {
19     private static MessageFormatException JavaDoc createMessageFormatException(
20             Class JavaDoc fromClass,
21             Class JavaDoc toClass)
22     {
23         return new MessageFormatException JavaDoc(
24                 "Unsupported conversion: Cannot convert '"
25                 + fromClass.getName()
26                 + "' to '"
27                 + toClass.getName()
28                 + ".' Please see section three of the JMS specification for more information.");
29
30     }
31
32     public static boolean getBoolean(Object JavaDoc value)
33             throws MessageFormatException JavaDoc
34     {
35         if (value == null)
36         {
37             return Boolean.valueOf(null).booleanValue();
38         }
39         else if (value instanceof Boolean JavaDoc)
40         {
41             return ((Boolean JavaDoc) value).booleanValue();
42         }
43         else if (value instanceof String JavaDoc)
44         {
45             return Boolean.valueOf((String JavaDoc) value).booleanValue();
46         }
47         else
48         {
49             throw createMessageFormatException(value.getClass(), Boolean.TYPE);
50         }
51     }
52
53     public static byte getByte(Object JavaDoc value) throws MessageFormatException JavaDoc
54     {
55         if (value == null)
56         {
57             return Byte.valueOf(null).byteValue();
58         }
59         else if (value instanceof Byte JavaDoc)
60         {
61             return ((Byte JavaDoc) value).byteValue();
62         }
63         else if (value instanceof String JavaDoc)
64         {
65             return Byte.valueOf((String JavaDoc) value).byteValue();
66         }
67         else
68         {
69             throw createMessageFormatException(value.getClass(), Byte.TYPE);
70         }
71     }
72
73     public static byte[] getBytes(Object JavaDoc value) throws MessageFormatException JavaDoc
74     {
75         if (value == null)
76         {
77             return null;
78         }
79         else if (value instanceof Byte JavaDoc[])
80         {
81             return (byte[]) value;
82         }
83         else
84         {
85             throw createMessageFormatException(value.getClass(), Byte JavaDoc[].class);
86         }
87     }
88
89     public static char getChar(Object JavaDoc value) throws MessageFormatException JavaDoc
90     {
91         if (value == null)
92         {
93             throw new NullPointerException JavaDoc("Item does not exist or is null.");
94         }
95         else if (value instanceof Character JavaDoc)
96         {
97             return ((Character JavaDoc) value).charValue();
98         }
99         else
100         {
101             throw createMessageFormatException(
102                     value.getClass(),
103                     Character.TYPE);
104         }
105     }
106
107     public static double getDouble(Object JavaDoc value) throws MessageFormatException JavaDoc
108     {
109         if (value == null)
110         {
111             return Double.valueOf(null).doubleValue();
112         }
113         else if (value instanceof Double JavaDoc)
114         {
115             return ((Double JavaDoc) value).doubleValue();
116         }
117         else if (value instanceof Float JavaDoc)
118         {
119             return ((Float JavaDoc) value).doubleValue();
120         }
121         else if (value instanceof String JavaDoc)
122         {
123             return Double.valueOf((String JavaDoc) value).doubleValue();
124         }
125         else
126         {
127             throw createMessageFormatException(value.getClass(), Double.TYPE);
128         }
129     }
130
131     public static float getFloat(Object JavaDoc value) throws MessageFormatException JavaDoc
132     {
133         if (value == null)
134         {
135             return Float.valueOf(null).floatValue();
136         }
137         else if (value instanceof Float JavaDoc)
138         {
139             return ((Float JavaDoc) value).floatValue();
140         }
141         else if (value instanceof String JavaDoc)
142         {
143             return Float.valueOf((String JavaDoc) value).floatValue();
144         }
145         else
146         {
147             throw createMessageFormatException(value.getClass(), Float.TYPE);
148         }
149     }
150
151     public static int getInt(Object JavaDoc value) throws MessageFormatException JavaDoc
152     {
153         if (value == null)
154         {
155             return Integer.valueOf(null).intValue();
156         }
157         else if (value instanceof Integer JavaDoc)
158         {
159             return ((Integer JavaDoc) value).intValue();
160         }
161         else if (value instanceof Byte JavaDoc)
162         {
163             return ((Byte JavaDoc) value).intValue();
164         }
165         else if (value instanceof Short JavaDoc)
166         {
167             return ((Short JavaDoc) value).intValue();
168         }
169         else if (value instanceof String JavaDoc)
170         {
171             return Short.valueOf((String JavaDoc) value).intValue();
172         }
173         else
174         {
175             throw createMessageFormatException(value.getClass(), Integer.TYPE);
176         }
177     }
178
179     public static long getLong(Object JavaDoc value) throws MessageFormatException JavaDoc
180     {
181         if (value == null)
182         {
183             return Long.valueOf(null).longValue();
184         }
185         else if (value instanceof Long JavaDoc)
186         {
187             return ((Long JavaDoc) value).longValue();
188         }
189         else if (value instanceof Byte JavaDoc)
190         {
191             return ((Byte JavaDoc) value).longValue();
192         }
193         else if (value instanceof Short JavaDoc)
194         {
195             return ((Short JavaDoc) value).longValue();
196         }
197         else if (value instanceof Integer JavaDoc)
198         {
199             return ((Integer JavaDoc) value).longValue();
200         }
201         else if (value instanceof String JavaDoc)
202         {
203             return Long.valueOf((String JavaDoc) value).longValue();
204         }
205         else
206         {
207             throw createMessageFormatException(value.getClass(), Long.TYPE);
208         }
209     }
210
211     public static Object JavaDoc getObject(Object JavaDoc value)
212     {
213         return value;
214     }
215
216     public static short getShort(Object JavaDoc value) throws MessageFormatException JavaDoc
217     {
218         if (value == null)
219         {
220             return Short.valueOf(null).shortValue();
221         }
222         else if (value instanceof Short JavaDoc)
223         {
224             return ((Short JavaDoc) value).shortValue();
225         }
226         else if (value instanceof Byte JavaDoc)
227         {
228             return ((Byte JavaDoc) value).shortValue();
229         }
230         else if (value instanceof String JavaDoc)
231         {
232             return Short.valueOf((String JavaDoc) value).shortValue();
233         }
234         else
235         {
236             throw createMessageFormatException(value.getClass(), Short.TYPE);
237         }
238     }
239
240     public static String JavaDoc getString(Object JavaDoc value) throws MessageFormatException JavaDoc
241     {
242         if (value == null)
243         {
244             return String.valueOf(null);
245         }
246         else if (value instanceof Byte JavaDoc[])
247         {
248             throw createMessageFormatException(Byte JavaDoc[].class, String JavaDoc.class);
249         }
250         else
251         {
252             return value.toString();
253         }
254     }
255 }
Popular Tags