KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > jms > message > Conversions


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6
7 package org.jfox.jms.message;
8
9 import javax.jms.MessageFormatException JavaDoc;
10
11 /**
12  * <p/>
13  * A simple format converter helper class in order to help convert an Objec
14  * t type as per the table listed below.
15  * </p>
16  * <p/>
17  * <P>A value written as the row type can be read as the column type.
18  * <p/>
19  * | | boolean byte short char int long float double String byte[]
20  * |----------------------------------------------------------------------
21  * |boolean | X X
22  * |byte | X X X X X
23  * |short | X X X X
24  * |char | X X
25  * |int | X X X
26  * |long | X X
27  * |float | X X X
28  * |double | X X
29  * |String | X X X X X X X X
30  * |byte[] | X
31  * |----------------------------------------------------------------------
32  *
33  * @author <a HREF="mailto:founder_chen@yahoo.com.cn">Peter.Cheng</a>
34  * @version Revision: 1.1 Date: 2002-12-08 20:22:11
35  */

36 public class Conversions {
37
38     /**
39      * Convert value to boolean
40      *
41      * @param value
42      * @return the converted boolean
43      * @throws javax.jms.MessageFormatException
44      * if the conversion is invalid
45      */

46     public static boolean getBoolean(Object JavaDoc value) throws MessageFormatException JavaDoc {
47         if (value instanceof Boolean JavaDoc) {
48             return ((Boolean JavaDoc) value).booleanValue();
49         } else if (value instanceof String JavaDoc) {
50             return Boolean.valueOf((String JavaDoc) value).booleanValue();
51         } else if (value == null) {
52             return Boolean.valueOf((String JavaDoc) value).booleanValue();
53         } else {
54             throw new MessageFormatException JavaDoc("Can't convert value of type "
55                     + value.getClass().getName()
56                     + " to Boolean");
57         }
58     }
59
60     /**
61      * Convert value to byte
62      *
63      * @param value
64      * @return the converted byte
65      * @throws javax.jms.MessageFormatException
66      * if the conversion is invalid
67      */

68     public static byte getByte(Object JavaDoc value) throws MessageFormatException JavaDoc {
69         if (value instanceof Byte JavaDoc) {
70             return ((Byte JavaDoc) value).byteValue();
71         } else if (value instanceof String JavaDoc) {
72             return Byte.parseByte((String JavaDoc) value);
73         } else if (value == null) {
74             return Byte.valueOf((String JavaDoc) value).byteValue();
75         } else {
76             throw new MessageFormatException JavaDoc("Can't convert value of type "
77                     + value.getClass().getName()
78                     + " to Byte");
79         }
80     }
81
82     /**
83      * Convert value to short
84      *
85      * @param value
86      * @return the converted short
87      * @throws javax.jms.MessageFormatException
88      * if the conversion is invalid
89      */

90     public static short getShort(Object JavaDoc value) throws MessageFormatException JavaDoc {
91         if (value instanceof Short JavaDoc) {
92             return ((Short JavaDoc) value).shortValue();
93         } else if (value instanceof Byte JavaDoc) {
94             return ((Byte JavaDoc) value).shortValue();
95         } else if (value instanceof String JavaDoc) {
96             return Short.parseShort((String JavaDoc) value);
97         } else if (value == null) {
98             return Short.valueOf((String JavaDoc) value).shortValue();
99         } else {
100             throw new MessageFormatException JavaDoc("Can't convert value of type "
101                     + value.getClass().getName()
102                     + " to Short");
103         }
104     }
105
106     /**
107      * Convert value to char
108      *
109      * @param value the object to convert from
110      * @return the converted char
111      * @throws javax.jms.MessageFormatException
112      * if the conversion is invalid
113      * @throws NullPointerException if value is null
114      */

115     public static char getChar(Object JavaDoc value) throws MessageFormatException JavaDoc {
116         if (value == null) {
117             return ((Character JavaDoc) null).charValue();
118         } else if (value instanceof Character JavaDoc) {
119             return ((Character JavaDoc) value).charValue();
120         } else {
121             throw new MessageFormatException JavaDoc("Can't convert value of type "
122                     + value.getClass().getName()
123                     + " to Char");
124         }
125     }
126
127     /**
128      * Convert value to int
129      *
130      * @param value
131      * @return the converted int
132      * @throws javax.jms.MessageFormatException
133      * if the conversion is invalid
134      * @throws NumberFormatException if value is a String and the conversion
135      * is invalid
136      */

137     public static int getInt(Object JavaDoc value) throws MessageFormatException JavaDoc {
138         if (value instanceof Integer JavaDoc) {
139             return ((Integer JavaDoc) value).intValue();
140         } else if (value instanceof Byte JavaDoc) {
141             return ((Byte JavaDoc) value).intValue();
142         } else if (value instanceof Short JavaDoc) {
143             return ((Short JavaDoc) value).intValue();
144         } else if (value instanceof String JavaDoc) {
145             return Integer.parseInt((String JavaDoc) value);
146         } else if (value == null) {
147             return Integer.valueOf((String JavaDoc) value).intValue();
148         } else {
149             throw new MessageFormatException JavaDoc("Can't convert value of type "
150                     + value.getClass().getName()
151                     + " to Int");
152         }
153     }
154
155     /**
156      * Convert value to long
157      *
158      * @param value
159      * @return the converted long
160      * @throws javax.jms.MessageFormatException
161      * if the conversion is invalid
162      * @throws NumberFormatException if value is a String and the conversion
163      * is invalid
164      */

165     public static long getLong(Object JavaDoc value) throws MessageFormatException JavaDoc {
166         if (value instanceof Long JavaDoc) {
167             return ((Long JavaDoc) value).longValue();
168         } else if (value instanceof Byte JavaDoc) {
169             return ((Byte JavaDoc) value).longValue();
170         } else if (value instanceof Short JavaDoc) {
171             return ((Short JavaDoc) value).longValue();
172         } else if (value instanceof Integer JavaDoc) {
173             return ((Integer JavaDoc) value).longValue();
174         } else if (value instanceof String JavaDoc) {
175             return Long.parseLong((String JavaDoc) value);
176         } else if (value == null) {
177             return Long.valueOf((String JavaDoc) value).longValue();
178         } else {
179             throw new MessageFormatException JavaDoc("Can't convert value of type "
180                     + value.getClass().getName()
181                     + " to Long");
182         }
183     }
184
185     /**
186      * Convert value to float
187      *
188      * @param value
189      * @return the converted float
190      * @throws javax.jms.MessageFormatException
191      * if the conversion is invalid
192      * @throws NumberFormatException if value is a String and the conversion
193      * is invalid
194      */

195     public static float getFloat(Object JavaDoc value) throws MessageFormatException JavaDoc {
196         if (value instanceof Float JavaDoc) {
197             return ((Float JavaDoc) value).floatValue();
198         } else if (value instanceof String JavaDoc) {
199             return Float.parseFloat((String JavaDoc) value);
200         } else if (value == null) {
201             return Float.valueOf((String JavaDoc) value).floatValue();
202         } else {
203             throw new MessageFormatException JavaDoc("Can't convert value of type "
204                     + value.getClass().getName()
205                     + " to Float");
206         }
207     }
208
209     /**
210      * Convert value to double
211      *
212      * @param value the object to convert from
213      * @return the converted double
214      * @throws javax.jms.MessageFormatException
215      * if the conversion is invalid
216      * @throws NumberFormatException if value is a String and the conversion
217      * is invalid
218      */

219     public static double getDouble(Object JavaDoc value) throws MessageFormatException JavaDoc {
220         if (value instanceof Double JavaDoc) {
221             return ((Double JavaDoc) value).doubleValue();
222         } else if (value instanceof Float JavaDoc) {
223             return ((Float JavaDoc) value).doubleValue();
224         } else if (value instanceof String JavaDoc) {
225             return Double.parseDouble((String JavaDoc) value);
226         } else if (value == null) {
227             return Double.valueOf((String JavaDoc) value).doubleValue();
228         } else {
229             throw new MessageFormatException JavaDoc("Can't convert value of type "
230                     + value.getClass().getName()
231                     + " to Double");
232         }
233     }
234
235     /**
236      * Convert value to String
237      *
238      * @param value the object to convert from
239      * @return the converted String
240      * @throws javax.jms.MessageFormatException
241      * if the conversion is invalid
242      */

243     public static String JavaDoc getString(Object JavaDoc value) throws MessageFormatException JavaDoc {
244         return (value == null) ? null : String.valueOf(value);
245     }
246
247     /**
248      * Convert value to Bytes
249      *
250      * @param value the object to convert from
251      * @return the converted bytes
252      * @throws javax.jms.MessageFormatException
253      * if the conversion invalid
254      */

255     public static byte[] getBytes(Object JavaDoc value) throws MessageFormatException JavaDoc {
256         if (value == null) {
257             return (byte[]) value;
258         } else if (value instanceof byte[]) {
259             return (byte[]) value;
260         } else {
261             throw new MessageFormatException JavaDoc("Can't convert value of type "
262                     + value.getClass().getName()
263                     + " to byte[].");
264         }
265     }
266
267 }
268
Popular Tags