1 7 package org.jboss.jms.util; 8 9 import javax.jms.MessageFormatException ; 10 11 17 public class JMSTypeConversions 18 { 19 private static MessageFormatException createMessageFormatException( 20 Class fromClass, 21 Class toClass) 22 { 23 return new MessageFormatException ( 24 "Unsupported conversion: Cannot convert '" 25 + fromClass.getName() 26 + "' to '" 27 + toClass.getName() 28 + ".' Please see section three of the JMS specification for more information."); 29 30 } 31 32 public static boolean getBoolean(Object value) 33 throws MessageFormatException 34 { 35 if (value == null) 36 { 37 return Boolean.valueOf(null).booleanValue(); 38 } 39 else if (value instanceof Boolean ) 40 { 41 return ((Boolean ) value).booleanValue(); 42 } 43 else if (value instanceof String ) 44 { 45 return Boolean.valueOf((String ) value).booleanValue(); 46 } 47 else 48 { 49 throw createMessageFormatException(value.getClass(), Boolean.TYPE); 50 } 51 } 52 53 public static byte getByte(Object value) throws MessageFormatException 54 { 55 if (value == null) 56 { 57 return Byte.valueOf(null).byteValue(); 58 } 59 else if (value instanceof Byte ) 60 { 61 return ((Byte ) value).byteValue(); 62 } 63 else if (value instanceof String ) 64 { 65 return Byte.valueOf((String ) value).byteValue(); 66 } 67 else 68 { 69 throw createMessageFormatException(value.getClass(), Byte.TYPE); 70 } 71 } 72 73 public static byte[] getBytes(Object value) throws MessageFormatException 74 { 75 if (value == null) 76 { 77 return null; 78 } 79 else if (value instanceof Byte []) 80 { 81 return (byte[]) value; 82 } 83 else 84 { 85 throw createMessageFormatException(value.getClass(), Byte [].class); 86 } 87 } 88 89 public static char getChar(Object value) throws MessageFormatException 90 { 91 if (value == null) 92 { 93 throw new NullPointerException ("Item does not exist or is null."); 94 } 95 else if (value instanceof Character ) 96 { 97 return ((Character ) value).charValue(); 98 } 99 else 100 { 101 throw createMessageFormatException( 102 value.getClass(), 103 Character.TYPE); 104 } 105 } 106 107 public static double getDouble(Object value) throws MessageFormatException 108 { 109 if (value == null) 110 { 111 return Double.valueOf(null).doubleValue(); 112 } 113 else if (value instanceof Double ) 114 { 115 return ((Double ) value).doubleValue(); 116 } 117 else if (value instanceof Float ) 118 { 119 return ((Float ) value).doubleValue(); 120 } 121 else if (value instanceof String ) 122 { 123 return Double.valueOf((String ) value).doubleValue(); 124 } 125 else 126 { 127 throw createMessageFormatException(value.getClass(), Double.TYPE); 128 } 129 } 130 131 public static float getFloat(Object value) throws MessageFormatException 132 { 133 if (value == null) 134 { 135 return Float.valueOf(null).floatValue(); 136 } 137 else if (value instanceof Float ) 138 { 139 return ((Float ) value).floatValue(); 140 } 141 else if (value instanceof String ) 142 { 143 return Float.valueOf((String ) value).floatValue(); 144 } 145 else 146 { 147 throw createMessageFormatException(value.getClass(), Float.TYPE); 148 } 149 } 150 151 public static int getInt(Object value) throws MessageFormatException 152 { 153 if (value == null) 154 { 155 return Integer.valueOf(null).intValue(); 156 } 157 else if (value instanceof Integer ) 158 { 159 return ((Integer ) value).intValue(); 160 } 161 else if (value instanceof Byte ) 162 { 163 return ((Byte ) value).intValue(); 164 } 165 else if (value instanceof Short ) 166 { 167 return ((Short ) value).intValue(); 168 } 169 else if (value instanceof String ) 170 { 171 return Short.valueOf((String ) value).intValue(); 172 } 173 else 174 { 175 throw createMessageFormatException(value.getClass(), Integer.TYPE); 176 } 177 } 178 179 public static long getLong(Object value) throws MessageFormatException 180 { 181 if (value == null) 182 { 183 return Long.valueOf(null).longValue(); 184 } 185 else if (value instanceof Long ) 186 { 187 return ((Long ) value).longValue(); 188 } 189 else if (value instanceof Byte ) 190 { 191 return ((Byte ) value).longValue(); 192 } 193 else if (value instanceof Short ) 194 { 195 return ((Short ) value).longValue(); 196 } 197 else if (value instanceof Integer ) 198 { 199 return ((Integer ) value).longValue(); 200 } 201 else if (value instanceof String ) 202 { 203 return Long.valueOf((String ) value).longValue(); 204 } 205 else 206 { 207 throw createMessageFormatException(value.getClass(), Long.TYPE); 208 } 209 } 210 211 public static Object getObject(Object value) 212 { 213 return value; 214 } 215 216 public static short getShort(Object value) throws MessageFormatException 217 { 218 if (value == null) 219 { 220 return Short.valueOf(null).shortValue(); 221 } 222 else if (value instanceof Short ) 223 { 224 return ((Short ) value).shortValue(); 225 } 226 else if (value instanceof Byte ) 227 { 228 return ((Byte ) value).shortValue(); 229 } 230 else if (value instanceof String ) 231 { 232 return Short.valueOf((String ) value).shortValue(); 233 } 234 else 235 { 236 throw createMessageFormatException(value.getClass(), Short.TYPE); 237 } 238 } 239 240 public static String getString(Object value) throws MessageFormatException 241 { 242 if (value == null) 243 { 244 return String.valueOf(null); 245 } 246 else if (value instanceof Byte []) 247 { 248 throw createMessageFormatException(Byte [].class, String .class); 249 } 250 else 251 { 252 return value.toString(); 253 } 254 } 255 } | Popular Tags |