1 48 49 package org.exolab.jms.message; 50 51 53 import javax.jms.JMSException ; 54 import javax.jms.MessageFormatException ; 55 56 57 89 class FormatConverter { 90 91 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 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 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 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 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 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 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 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 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 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 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 } | Popular Tags |