KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > kjoram > messages > ConversionHelper


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Jeff Mesnil (INRIA)
22  * Contributor(s): Frederic Maistre (INRIA), Nicolas Tachker (ScalAgent)
23  */

24 package com.scalagent.kjoram.messages;
25
26 import com.scalagent.kjoram.excepts.MessageValueException;
27
28 /**
29  * The <code>ConversionHelper</code> class is used for converting values
30  * carried by messages into specified types, if possible.
31  */

32 public class ConversionHelper
33 {
34   /**
35    * Gets the boolean value of the given object.
36    *
37    * @exception MessageValueException If the given object can't be converted
38    * into a boolean value.
39    */

40   public static boolean toBoolean(Object JavaDoc value) throws MessageValueException
41   {
42     if (value == null)
43       throw new RuntimeException JavaDoc("toBoolean value = null");
44
45     if (value instanceof Boolean JavaDoc)
46       return ((Boolean JavaDoc) value).booleanValue();
47     else if (value instanceof String JavaDoc) {
48       String JavaDoc s = ((String JavaDoc) value).toUpperCase();
49       return s.equals("TRUE") || s.equals("YES");
50     } else
51       throw new MessageValueException("Type " + value.getClass().getName()
52                                       + " can't be converted to Boolean.");
53   }
54  
55   /**
56    * Gets the byte value of the given object.
57    *
58    * @exception MessageValueException If the given object can't be converted
59    * into a byte value.
60    */

61   public static byte toByte(Object JavaDoc value) throws MessageValueException
62   {
63     if (value == null)
64       throw new RuntimeException JavaDoc("toByte value = null");
65
66     if (value instanceof Byte JavaDoc )
67       return ((Byte JavaDoc) value).byteValue();
68     else if (value instanceof String JavaDoc)
69       return Byte.parseByte((String JavaDoc) value);
70     else
71       throw new MessageValueException("Type " + value.getClass().getName()
72                                       + " can't be converted to Byte.");
73   }
74  
75   /**
76    * Gets the short value of the given object.
77    *
78    * @exception MessageValueException If the given object can't be converted
79    * into a short value.
80    */

81   public static short toShort(Object JavaDoc value) throws MessageValueException
82   {
83     if (value == null)
84       throw new RuntimeException JavaDoc("toShort value = null");
85
86     if (value instanceof Byte JavaDoc)
87       return Short.parseShort(((Byte JavaDoc) value).toString());
88     else if (value instanceof Short JavaDoc)
89       return ((Short JavaDoc) value).shortValue();
90     else if (value instanceof String JavaDoc)
91       return Short.parseShort((String JavaDoc) value);
92     else
93       throw new MessageValueException("Type " + value.getClass().getName()
94                                       + " can't be converted to Short.");
95   }
96  
97   /**
98    * Gets the int value of the given object.
99    *
100    * @exception MessageValueException If the given object can't be converted
101    * into an int value.
102    */

103   public static int toInt(Object JavaDoc value) throws MessageValueException
104   {
105     if (value == null)
106       return Integer.valueOf(null).intValue();
107
108     if (value instanceof Byte JavaDoc)
109       return Integer.parseInt(((Byte JavaDoc) value).toString());
110     else if (value instanceof Short JavaDoc )
111       return Integer.parseInt(((Short JavaDoc) value).toString());
112     else if (value instanceof Integer JavaDoc)
113       return ((Integer JavaDoc) value).intValue();
114     else if (value instanceof String JavaDoc)
115       return Integer.valueOf((String JavaDoc) value).intValue();
116     else
117       throw new MessageValueException("Type " + value.getClass().getName()
118                                       + " can't be converted to Integer.");
119   }
120  
121   /**
122    * Gets the long value of the given object.
123    *
124    * @exception MessageValueException If the given object can't be converted
125    * into a long value.
126    */

127   public static long toLong(Object JavaDoc value) throws MessageValueException
128   {
129     if (value == null)
130       throw new RuntimeException JavaDoc("toLong value = null");
131
132     if (value instanceof Byte JavaDoc)
133       return Byte.parseByte(((Byte JavaDoc) value).toString());
134     else if (value instanceof Short JavaDoc)
135       return Long.parseLong(((Short JavaDoc) value).toString());
136     else if (value instanceof Integer JavaDoc)
137       return ((Integer JavaDoc) value).longValue();
138     else if (value instanceof Long JavaDoc)
139       return ((Long JavaDoc) value).longValue();
140     else if (value instanceof String JavaDoc)
141       return Long.parseLong((String JavaDoc) value);
142     else
143       throw new MessageValueException("Type " + value.getClass().getName()
144                                       + " can't be converted to Long.");
145   }
146
147   /**
148    * Gets the float value of the given object.
149    *
150    * @exception MessageValueException If the given object can't be converted
151    * into a float value.
152    */

153 // public static float toFloat(Object value) throws MessageValueException
154
// {
155
// if (value == null)
156
// return Float.valueOf(null).floatValue();
157

158 // if (value instanceof Float)
159
// return ((Float) value).floatValue();
160
// else if (value instanceof String)
161
// return Float.valueOf((String) value).floatValue();
162
// else
163
// throw new MessageValueException("Type " + value.getClass().getName()
164
// + " can't be converted to Float.");
165
// }
166

167   /**
168    * Gets the double value of the given object.
169    *
170    * @exception MessageValueException If the given object can't be converted
171    * into a double value.
172    */

173 // public static double toDouble(Object value) throws MessageValueException
174
// {
175
// if (value == null)
176
// return Double.valueOf(null).doubleValue();
177

178 // if (value instanceof Float)
179
// return ((Float) value).doubleValue();
180
// else if (value instanceof Double)
181
// return ((Double) value).doubleValue();
182
// else if (value instanceof String)
183
// return Double.valueOf((String) value).doubleValue();
184
// else
185
// throw new MessageValueException("Type " + value.getClass().getName()
186
// + " can't be converted to Double.");
187
// }
188

189   /** Gets the String value of the given object. */
190   public static String JavaDoc toString(Object JavaDoc value)
191   {
192     if (value == null)
193       return null;
194
195     if (value instanceof byte[])
196       return new String JavaDoc((byte[]) value);
197     else
198       return value.toString();
199   }
200
201   /**
202    * Gets the char value of the given object.
203    *
204    * @exception MessageValueException If the given object can't be converted
205    * into a char value.
206    */

207   public static char toChar(Object JavaDoc value) throws MessageValueException
208   {
209     if (value == null)
210       return ((Character JavaDoc) null).charValue();
211     else if (value instanceof Character JavaDoc)
212       return ((Character JavaDoc) value).charValue();
213     else
214       throw new MessageValueException("Type " + value.getClass().getName()
215                                       + " can't be converted to Character.");
216   }
217
218   /**
219    * Gets the bytes value of the given object.
220    *
221    * @exception MessageValueException If the given object can't be converted
222    * into a bytes array.
223    */

224   public static byte[] toBytes(Object JavaDoc value) throws MessageValueException
225   {
226     if (value == null)
227       return (byte[]) value;
228     else if (value instanceof byte[])
229       return (byte[]) value;
230     else
231       throw new MessageValueException("Type " + value.getClass().getName()
232                                       + " can't be converted to byte[].");
233   }
234 }
235
Popular Tags