KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > shared > 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)
23  */

24 package org.objectweb.joram.shared.messages;
25
26 import org.objectweb.joram.shared.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       return Boolean.valueOf(null).booleanValue();
44
45     if (value instanceof Boolean JavaDoc)
46       return ((Boolean JavaDoc) value).booleanValue();
47     else if (value instanceof String JavaDoc)
48       return Boolean.valueOf((String JavaDoc) value).booleanValue();
49     else
50       throw new MessageValueException("Type " + value.getClass().getName()
51                                       + " can't be converted to Boolean.");
52   }
53  
54   /**
55    * Gets the byte value of the given object.
56    *
57    * @exception MessageValueException If the given object can't be converted
58    * into a byte value.
59    */

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

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

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

121   public static long toLong(Object JavaDoc value) throws MessageValueException
122   {
123     if (value == null)
124       return Long.valueOf(null).longValue();
125
126     if (value instanceof Byte JavaDoc || value instanceof Short JavaDoc
127         || value instanceof Integer JavaDoc || value instanceof Long JavaDoc)
128       return ((Number JavaDoc) value).longValue();
129     else if (value instanceof String JavaDoc)
130       return Long.valueOf((String JavaDoc) value).longValue();
131     else
132       throw new MessageValueException("Type " + value.getClass().getName()
133                                       + " can't be converted to Long.");
134   }
135
136   /**
137    * Gets the float value of the given object.
138    *
139    * @exception MessageValueException If the given object can't be converted
140    * into a float value.
141    */

142   public static float toFloat(Object JavaDoc value) throws MessageValueException
143   {
144     if (value == null)
145       return Float.valueOf(null).floatValue();
146
147     if (value instanceof Float JavaDoc)
148       return ((Float JavaDoc) value).floatValue();
149     else if (value instanceof String JavaDoc)
150       return Float.valueOf((String JavaDoc) value).floatValue();
151     else
152       throw new MessageValueException("Type " + value.getClass().getName()
153                                       + " can't be converted to Float.");
154   }
155  
156   /**
157    * Gets the double value of the given object.
158    *
159    * @exception MessageValueException If the given object can't be converted
160    * into a double value.
161    */

162   public static double toDouble(Object JavaDoc value) throws MessageValueException
163   {
164     if (value == null)
165       return Double.valueOf(null).doubleValue();
166
167     if (value instanceof Float JavaDoc || value instanceof Double JavaDoc)
168       return ((Number JavaDoc) value).doubleValue();
169     else if (value instanceof String JavaDoc)
170       return Double.valueOf((String JavaDoc) value).doubleValue();
171     else
172       throw new MessageValueException("Type " + value.getClass().getName()
173                                       + " can't be converted to Double.");
174   }
175
176   /** Gets the String value of the given object. */
177   public static String JavaDoc toString(Object JavaDoc value)
178   {
179     if (value == null)
180       return null;
181
182     if (value instanceof byte[])
183       return new String JavaDoc((byte[]) value);
184     else
185       return value.toString();
186   }
187
188   /**
189    * Gets the char value of the given object.
190    *
191    * @exception MessageValueException If the given object can't be converted
192    * into a char value.
193    */

194   public static char toChar(Object JavaDoc value) throws MessageValueException
195   {
196     if (value == null)
197       return ((Character JavaDoc) null).charValue();
198     else if (value instanceof Character JavaDoc)
199       return ((Character JavaDoc) value).charValue();
200     else
201       throw new MessageValueException("Type " + value.getClass().getName()
202                                       + " can't be converted to Character.");
203   }
204
205   /**
206    * Gets the bytes value of the given object.
207    *
208    * @exception MessageValueException If the given object can't be converted
209    * into a bytes array.
210    */

211   public static byte[] toBytes(Object JavaDoc value) throws MessageValueException
212   {
213     if (value == null)
214       return (byte[]) value;
215     else if (value instanceof byte[])
216       return (byte[]) value;
217     else
218       throw new MessageValueException("Type " + value.getClass().getName()
219                                       + " can't be converted to byte[].");
220   }
221 }
222
Popular Tags