1 6 7 package org.jfox.jms.message; 8 9 import javax.jms.MessageFormatException ; 10 11 36 public class Conversions { 37 38 46 public static boolean getBoolean(Object value) throws MessageFormatException { 47 if (value instanceof Boolean ) { 48 return ((Boolean ) value).booleanValue(); 49 } else if (value instanceof String ) { 50 return Boolean.valueOf((String ) value).booleanValue(); 51 } else if (value == null) { 52 return Boolean.valueOf((String ) value).booleanValue(); 53 } else { 54 throw new MessageFormatException ("Can't convert value of type " 55 + value.getClass().getName() 56 + " to Boolean"); 57 } 58 } 59 60 68 public static byte getByte(Object value) throws MessageFormatException { 69 if (value instanceof Byte ) { 70 return ((Byte ) value).byteValue(); 71 } else if (value instanceof String ) { 72 return Byte.parseByte((String ) value); 73 } else if (value == null) { 74 return Byte.valueOf((String ) value).byteValue(); 75 } else { 76 throw new MessageFormatException ("Can't convert value of type " 77 + value.getClass().getName() 78 + " to Byte"); 79 } 80 } 81 82 90 public static short getShort(Object value) throws MessageFormatException { 91 if (value instanceof Short ) { 92 return ((Short ) value).shortValue(); 93 } else if (value instanceof Byte ) { 94 return ((Byte ) value).shortValue(); 95 } else if (value instanceof String ) { 96 return Short.parseShort((String ) value); 97 } else if (value == null) { 98 return Short.valueOf((String ) value).shortValue(); 99 } else { 100 throw new MessageFormatException ("Can't convert value of type " 101 + value.getClass().getName() 102 + " to Short"); 103 } 104 } 105 106 115 public static char getChar(Object value) throws MessageFormatException { 116 if (value == null) { 117 return ((Character ) null).charValue(); 118 } else if (value instanceof Character ) { 119 return ((Character ) value).charValue(); 120 } else { 121 throw new MessageFormatException ("Can't convert value of type " 122 + value.getClass().getName() 123 + " to Char"); 124 } 125 } 126 127 137 public static int getInt(Object value) throws MessageFormatException { 138 if (value instanceof Integer ) { 139 return ((Integer ) value).intValue(); 140 } else if (value instanceof Byte ) { 141 return ((Byte ) value).intValue(); 142 } else if (value instanceof Short ) { 143 return ((Short ) value).intValue(); 144 } else if (value instanceof String ) { 145 return Integer.parseInt((String ) value); 146 } else if (value == null) { 147 return Integer.valueOf((String ) value).intValue(); 148 } else { 149 throw new MessageFormatException ("Can't convert value of type " 150 + value.getClass().getName() 151 + " to Int"); 152 } 153 } 154 155 165 public static long getLong(Object value) throws MessageFormatException { 166 if (value instanceof Long ) { 167 return ((Long ) value).longValue(); 168 } else if (value instanceof Byte ) { 169 return ((Byte ) value).longValue(); 170 } else if (value instanceof Short ) { 171 return ((Short ) value).longValue(); 172 } else if (value instanceof Integer ) { 173 return ((Integer ) value).longValue(); 174 } else if (value instanceof String ) { 175 return Long.parseLong((String ) value); 176 } else if (value == null) { 177 return Long.valueOf((String ) value).longValue(); 178 } else { 179 throw new MessageFormatException ("Can't convert value of type " 180 + value.getClass().getName() 181 + " to Long"); 182 } 183 } 184 185 195 public static float getFloat(Object value) throws MessageFormatException { 196 if (value instanceof Float ) { 197 return ((Float ) value).floatValue(); 198 } else if (value instanceof String ) { 199 return Float.parseFloat((String ) value); 200 } else if (value == null) { 201 return Float.valueOf((String ) value).floatValue(); 202 } else { 203 throw new MessageFormatException ("Can't convert value of type " 204 + value.getClass().getName() 205 + " to Float"); 206 } 207 } 208 209 219 public static double getDouble(Object value) throws MessageFormatException { 220 if (value instanceof Double ) { 221 return ((Double ) value).doubleValue(); 222 } else if (value instanceof Float ) { 223 return ((Float ) value).doubleValue(); 224 } else if (value instanceof String ) { 225 return Double.parseDouble((String ) value); 226 } else if (value == null) { 227 return Double.valueOf((String ) value).doubleValue(); 228 } else { 229 throw new MessageFormatException ("Can't convert value of type " 230 + value.getClass().getName() 231 + " to Double"); 232 } 233 } 234 235 243 public static String getString(Object value) throws MessageFormatException { 244 return (value == null) ? null : String.valueOf(value); 245 } 246 247 255 public static byte[] getBytes(Object value) throws MessageFormatException { 256 if (value == null) { 257 return (byte[]) value; 258 } else if (value instanceof byte[]) { 259 return (byte[]) value; 260 } else { 261 throw new MessageFormatException ("Can't convert value of type " 262 + value.getClass().getName() 263 + " to byte[]."); 264 } 265 } 266 267 } 268 | Popular Tags |