1 /*
2 * @(#)FormatHelper.java
3 *
4 * JFoxMQ the open source JMS MOM.
5 *
6 * Corpyright 2002-2003 Huihoo Power, Inc. All Rights Reserved. This software
7 * is licensed under LGPL license.
8 *
9 * For more information, please visit: http://www.huihoo.org
10 */
11
12 package org.huihoo.jfox.ms.jms.message;
13
14 import javax.jms.MessageFormatException;
15
16 /***
17 * <p>
18 * A simple format converter helper class in order to help convert an Objec
19 * t type as per the table listed below.
20 * </p>
21 *
22 * <P>A value written as the row type can be read as the column type.
23 *
24 * | | boolean byte short char int long float double String byte[]
25 * |----------------------------------------------------------------------
26 * |boolean | X X
27 * |byte | X X X X X
28 * |short | X X X X
29 * |char | X X
30 * |int | X X X
31 * |long | X X
32 * |float | X X X
33 * |double | X X
34 * |String | X X X X X X X X
35 * |byte[] | X
36 * |----------------------------------------------------------------------
37 *
38 * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter.Cheng</a>
39 * @version Revision: 1.1 Date: 2002-12-08 20:22:11
40 */
41 public class FormatHelper {
42
43 /***
44 * Convert value to boolean
45 *
46 * @param value
47 * @return the converted boolean
48 * @exception MessageFormatException if the conversion is invalid
49 */
50 public static boolean getBoolean(Object value) throws MessageFormatException {
51 if (value instanceof Boolean) {
52 return ((Boolean) value).booleanValue();
53 } else if (value instanceof String) {
54 return Boolean.valueOf((String) value).booleanValue();
55 } else if (value == null) {
56 return Boolean.valueOf((String) value).booleanValue();
57 } else {
58 throw new MessageFormatException(
59 "Can't convert value of type "
60 + value.getClass().getName()
61 + " to Boolean");
62 }
63 }
64
65 /***
66 * Convert value to byte
67 *
68 * @param value
69 * @return the converted byte
70 * @exception MessageFormatException if the conversion is invalid
71 */
72 public static byte getByte(Object value) throws MessageFormatException {
73 if (value instanceof Byte) {
74 return ((Byte) value).byteValue();
75 } else if (value instanceof String) {
76 return Byte.parseByte((String) value);
77 } else if (value == null) {
78 return Byte.valueOf((String) value).byteValue();
79 } else {
80 throw new MessageFormatException(
81 "Can't convert value of type "
82 + value.getClass().getName()
83 + " to Byte");
84 }
85 }
86
87 /***
88 * Convert value to short
89 *
90 * @param value
91 * @return the converted short
92 * @exception MessageFormatException if the conversion is invalid
93 */
94 public static short getShort(Object value) throws MessageFormatException {
95 if (value instanceof Short) {
96 return ((Short) value).shortValue();
97 } else if (value instanceof Byte) {
98 return ((Byte) value).shortValue();
99 } else if (value instanceof String) {
100 return Short.parseShort((String) value);
101 } else if (value == null) {
102 return Short.valueOf((String) value).shortValue();
103 } else {
104 throw new MessageFormatException(
105 "Can't convert value of type "
106 + value.getClass().getName()
107 + " to Short");
108 }
109 }
110
111 /***
112 * Convert value to char
113 *
114 * @param value the object to convert from
115 * @return the converted char
116 * @throws MessageFormatException if the conversion is invalid
117 * @throws NullPointerException if value is null
118 */
119 public static char getChar(Object value) throws MessageFormatException {
120 if (value == null) {
121 return ((Character) null).charValue();
122 } else if (value instanceof Character) {
123 return ((Character) value).charValue();
124 } else {
125 throw new MessageFormatException(
126 "Can't convert value of type "
127 + value.getClass().getName()
128 + " to Char");
129 }
130 }
131
132 /***
133 * Convert value to int
134 *
135 * @param value
136 * @return the converted int
137 * @exception MessageFormatException if the conversion is invalid
138 * @exception NumberFormatException if value is a String and the conversion
139 * is invalid
140 */
141 public static int getInt(Object value) throws MessageFormatException {
142 if (value instanceof Integer) {
143 return ((Integer) value).intValue();
144 } else if (value instanceof Byte) {
145 return ((Byte) value).intValue();
146 } else if (value instanceof Short) {
147 return ((Short) value).intValue();
148 } else if (value instanceof String) {
149 return Integer.parseInt((String) value);
150 } else if (value == null) {
151 return Integer.valueOf((String) value).intValue();
152 } else {
153 throw new MessageFormatException(
154 "Can't convert value of type "
155 + value.getClass().getName()
156 + " to Int");
157 }
158 }
159
160 /***
161 * Convert value to long
162 *
163 * @param value
164 * @return the converted long
165 * @exception MessageFormatException if the conversion is invalid
166 * @exception NumberFormatException if value is a String and the conversion
167 * is invalid
168 */
169 public static long getLong(Object value) throws MessageFormatException {
170 if (value instanceof Long) {
171 return ((Long) value).longValue();
172 } else if (value instanceof Byte) {
173 return ((Byte) value).longValue();
174 } else if (value instanceof Short) {
175 return ((Short) value).longValue();
176 } else if (value instanceof Integer) {
177 return ((Integer) value).longValue();
178 } else if (value instanceof String) {
179 return Long.parseLong((String) value);
180 } else if (value == null) {
181 return Long.valueOf((String) value).longValue();
182 } else {
183 throw new MessageFormatException(
184 "Can't convert value of type "
185 + value.getClass().getName()
186 + " to Long");
187 }
188 }
189
190 /***
191 * Convert value to float
192 *
193 * @param value
194 * @return the converted float
195 * @exception MessageFormatException if the conversion is invalid
196 * @exception NumberFormatException if value is a String and the conversion
197 * is invalid
198 */
199 public static float getFloat(Object value) throws MessageFormatException {
200 if (value instanceof Float) {
201 return ((Float) value).floatValue();
202 } else if (value instanceof String) {
203 return Float.parseFloat((String) value);
204 } else if (value == null) {
205 return Float.valueOf((String) value).floatValue();
206 } else {
207 throw new MessageFormatException(
208 "Can't convert value of type "
209 + value.getClass().getName()
210 + " to Float");
211 }
212 }
213
214 /***
215 * Convert value to double
216 *
217 * @param value the object to convert from
218 * @return the converted double
219 * @exception MessageFormatException if the conversion is invalid
220 * @exception NumberFormatException if value is a String and the conversion
221 * is invalid
222 */
223 public static double getDouble(Object value) throws MessageFormatException {
224 if (value instanceof Double) {
225 return ((Double) value).doubleValue();
226 } else if (value instanceof Float) {
227 return ((Float) value).doubleValue();
228 } else if (value instanceof String) {
229 return Double.parseDouble((String) value);
230 } else if (value == null) {
231 return Double.valueOf((String) value).doubleValue();
232 } else {
233 throw new MessageFormatException(
234 "Can't convert value of type "
235 + value.getClass().getName()
236 + " to Double");
237 }
238 }
239
240 /***
241 * Convert value to String
242 *
243 * @param value the object to convert from
244 * @return the converted String
245 * @exception MessageFormatException if the conversion is invalid
246 */
247 public static String getString(Object value) throws MessageFormatException {
248 return (value == null) ? null : String.valueOf(value);
249 }
250
251 /***
252 * Convert value to Bytes
253 *
254 * @param value the object to convert from
255 * @return the converted bytes
256 * @exception MessageFormatException if the conversion invalid
257 */
258 public static byte[] getBytes(Object value) throws MessageFormatException {
259 if (value == null) {
260 return (byte[]) value;
261 } else if (value instanceof byte[]) {
262 return (byte[]) value;
263 } else {
264 throw new MessageFormatException(
265 "Can't convert value of type "
266 + value.getClass().getName()
267 + " to byte[].");
268 }
269 }
270
271 }
This page was automatically generated by Maven