View Javadoc
1 /*** 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Exoffice Technologies. For written permission, 18 * please contact info@exolab.org. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Exoffice Technologies. Exolab is a registered 23 * trademark of Exoffice Technologies. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 2000,2001 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: FormatConverter.java,v 1.6 2003/08/07 13:33:01 tanderson Exp $ 44 * 45 * Date Author Changes 46 * 02/26/2000 jimm Created 47 */ 48 49 package org.exolab.jms.message; 50 51 // jms 52 53 import javax.jms.JMSException; 54 import javax.jms.MessageFormatException; 55 56 57 /*** 58 * A simple format converter to help convert an Object type as per the 59 * table listed below. 60 * 61 * <P>A value written as the row type can be read as the column type. 62 * 63 * <PRE> 64 * | | boolean byte short char int long float double String byte[] 65 * |---------------------------------------------------------------------- 66 * |boolean | X X 67 * |byte | X X X X X 68 * |short | X X X X 69 * |char | X X 70 * |int | X X X 71 * |long | X X 72 * |float | X X X 73 * |double | X X 74 * |String | X X X X X X X X 75 * |byte[] | X 76 * |---------------------------------------------------------------------- 77 * </PRE> 78 * 79 * <P>Attempting to read a null value as a Java primitive type must be treated 80 * as calling the primitive's corresponding <code>valueOf(String)</code> 81 * conversion method with a null value. Since char does not support a String 82 * conversion, attempting to read a null value as a char must throw 83 * NullPointerException. 84 * 85 * @version $Revision: 1.6 $ $Date: 2003/08/07 13:33:01 $ 86 * @author <a href="mailto:mourikis@intalio.com">Jim Mourikis</a> 87 * @author <a href="mailto:tima@intalio.com">Tim Anderson</a> 88 */ 89 class FormatConverter { 90 91 /*** 92 * Convert value to boolean 93 * 94 * @param value the object to convert from 95 * @return the converted boolean 96 * @throws MessageFormatException if the conversion is invalid 97 */ 98 public static boolean getBoolean(Object value) 99 throws MessageFormatException { 100 boolean result = false; 101 102 if (value instanceof Boolean) { 103 result = ((Boolean) value).booleanValue(); 104 } else if (value instanceof String) { 105 result = Boolean.valueOf((String) value).booleanValue(); 106 } else if (value == null) { 107 result = Boolean.valueOf((String) value).booleanValue(); 108 } else { 109 raise(value, boolean.class); 110 } 111 return result; 112 } 113 114 /*** 115 * Convert value to byte 116 * 117 * @param value the object to convert from 118 * @return the converted byte 119 * @throws MessageFormatException if the conversion is invalid 120 * @throws NumberFormatException if value is a String and the conversion 121 * is invalid 122 */ 123 public static byte getByte(Object value) throws MessageFormatException { 124 byte result = 0; 125 126 if (value instanceof Byte) { 127 result = ((Byte) value).byteValue(); 128 } else if (value instanceof String) { 129 result = Byte.parseByte((String) value); 130 } else if (value == null) { 131 result = Byte.valueOf((String) value).byteValue(); 132 } else { 133 raise(value, byte.class); 134 } 135 return result; 136 } 137 138 139 /*** 140 * Convert value to short 141 * 142 * @param value the object to convert from 143 * @return the converted short 144 * @throws MessageFormatException if the conversion is invalid 145 * @throws NumberFormatException if value is a String and the conversion 146 * is invalid 147 */ 148 public static short getShort(Object value) throws MessageFormatException { 149 short result = 0; 150 151 if (value instanceof Short) { 152 result = ((Short) value).shortValue(); 153 } else if (value instanceof Byte) { 154 result = ((Byte) value).shortValue(); 155 } else if (value instanceof String) { 156 result = Short.parseShort((String) value); 157 } else if (value == null) { 158 result = Short.valueOf((String) value).shortValue(); 159 } else { 160 raise(value, short.class); 161 } 162 return result; 163 } 164 165 /*** 166 * Convert value to char 167 * 168 * @param value the object to convert from 169 * @return the converted char 170 * @throws MessageFormatException if the conversion is invalid 171 * @throws NullPointerException if value is null 172 */ 173 public static char getChar(Object value) throws MessageFormatException { 174 char result = '\0'; 175 if (value instanceof Character) { 176 result = ((Character) value).charValue(); 177 } else if (value == null) { 178 throw new NullPointerException( 179 "Cannot convert null value to char"); 180 } else { 181 raise(value, char.class); 182 } 183 return result; 184 } 185 186 /*** 187 * Convert value to int 188 * 189 * @param value the object to convert from 190 * @return the converted int 191 * @throws MessageFormatException if the conversion is invalid 192 * @throws NumberFormatException if value is a String and the conversion 193 * is invalid 194 */ 195 public static int getInt(Object value) throws MessageFormatException { 196 int result = 0; 197 198 if (value instanceof Integer) { 199 result = ((Integer) value).intValue(); 200 } else if (value instanceof Short) { 201 result = ((Short) value).intValue(); 202 } else if (value instanceof Byte) { 203 result = ((Byte) value).intValue(); 204 } else if (value instanceof String) { 205 result = Integer.parseInt((String) value); 206 } else if (value == null) { 207 result = Integer.valueOf((String) value).intValue(); 208 } else { 209 raise(value, int.class); 210 } 211 return result; 212 } 213 214 215 /*** 216 * Convert value to long 217 * 218 * @param value the object to convert from 219 * @return the converted long 220 * @throws MessageFormatException if the conversion is invalid 221 * @throws NumberFormatException if value is a String and the conversion 222 * is invalid 223 */ 224 public static long getLong(Object value) throws MessageFormatException { 225 long result = 0; 226 227 if (value instanceof Long) { 228 result = ((Long) value).longValue(); 229 } else if (value instanceof Integer) { 230 result = ((Integer) value).longValue(); 231 } else if (value instanceof Short) { 232 result = ((Short) value).longValue(); 233 } else if (value instanceof Byte) { 234 result = ((Byte) value).longValue(); 235 } else if (value instanceof String) { 236 result = Long.parseLong((String) value); 237 } else if (value == null) { 238 result = Long.valueOf((String) value).longValue(); 239 } else { 240 raise(value, long.class); 241 } 242 return result; 243 } 244 245 /*** 246 * Convert value to float 247 * 248 * @param value the object to convert from 249 * @return the converted float 250 * @throws MessageFormatException if the conversion is invalid 251 * @throws NumberFormatException if value is a String and the conversion 252 * is invalid 253 */ 254 public static float getFloat(Object value) throws MessageFormatException { 255 float result = 0; 256 257 if (value instanceof Float) { 258 result = ((Float) value).floatValue(); 259 } else if (value instanceof String) { 260 result = Float.parseFloat((String) value); 261 } else if (value == null) { 262 result = Float.valueOf((String) value).floatValue(); 263 } else { 264 raise(value, float.class); 265 } 266 return result; 267 } 268 269 /*** 270 * Convert value to double 271 * 272 * @param value the object to convert from 273 * @return the converted double 274 * @throws MessageFormatException if the conversion is invalid 275 * @throws NumberFormatException if value is a String and the conversion 276 * is invalid 277 */ 278 public static double getDouble(Object value) 279 throws MessageFormatException { 280 double result = 0; 281 282 if (value instanceof Double) { 283 result = ((Double) value).doubleValue(); 284 } else if (value instanceof Float) { 285 result = ((Float) value).doubleValue(); 286 } else if (value instanceof String) { 287 result = Double.parseDouble((String) value); 288 } else if (value == null) { 289 result = Double.valueOf((String) value).doubleValue(); 290 } else { 291 raise(value, double.class); 292 } 293 return result; 294 } 295 296 /*** 297 * Convert value to String 298 * 299 * @param value the object to convert from 300 * @return the converted String 301 * @throws MessageFormatException if the conversion is invalid 302 */ 303 public static String getString(Object value) 304 throws MessageFormatException { 305 if (value instanceof byte[]) { 306 raise(value, String.class); 307 } 308 return (value == null) ? null : String.valueOf(value); 309 } 310 311 /*** 312 * Convert value to byte[] 313 * 314 * @param value the object to convert from. This must be a byte array, or 315 * null 316 * @return a copy of the supplied array, or null 317 * @throws MessageFormatException if value is not a byte array or is not 318 * null 319 */ 320 public static byte[] getBytes(Object value) 321 throws MessageFormatException { 322 byte[] result = null; 323 324 if (value instanceof byte[]) { 325 byte[] bytes = (byte[]) value; 326 result = new byte[bytes.length]; 327 System.arraycopy(bytes, 0, result, 0, bytes.length); 328 } else if (value != null) { 329 raise(value, byte[].class); 330 } 331 return result; 332 } 333 334 /*** 335 * Helper to raise a MessageFormatException when a conversion cannot be 336 * performed 337 * 338 * @param value the value that cannot be converted 339 * @param type the type that the value cannot be converted to 340 * @throws MessageFormatException when invoked 341 */ 342 private static void raise(Object value, Class type) 343 throws MessageFormatException { 344 345 throw new MessageFormatException( 346 "Cannot convert values of type " + value.getClass().getName() + 347 " to " + type.getName()); 348 } 349 350 } //-- FormatConverter

This page was automatically generated by Maven