1 28 29 package com.caucho.jms.message; 30 31 import com.caucho.log.Log; 32 import com.caucho.util.L10N; 33 34 import javax.jms.JMSException ; 35 import javax.jms.MessageFormatException ; 36 import java.util.logging.Logger ; 37 38 41 public class ObjectConverter { 42 static final Logger log = Log.open(ObjectConverter.class); 43 static final L10N L = new L10N(ObjectConverter.class); 44 45 48 public static boolean toBoolean(Object obj) 49 throws JMSException 50 { 51 if (obj instanceof Boolean ) 52 return ((Boolean ) obj).booleanValue(); 53 else if (obj == null || obj instanceof String ) 54 return Boolean.valueOf((String ) obj).booleanValue(); 55 else 56 throw new MessageFormatException (L.l("can't convert '{0}' to boolean", 57 obj.getClass().getName())); 58 } 59 60 63 public static byte toByte(Object obj) 64 throws JMSException 65 { 66 if (obj instanceof Byte ) 67 return ((Number ) obj).byteValue(); 68 else if (obj == null || obj instanceof String ) 69 return (byte) Long.parseLong((String ) obj); 70 else 71 throw new MessageFormatException (L.l("can't convert '{0}' to byte", 72 obj.getClass().getName())); 73 } 74 75 78 public static short toShort(Object obj) 79 throws JMSException 80 { 81 if (obj instanceof Short || obj instanceof Byte ) 82 return ((Number ) obj).shortValue(); 83 else if (obj == null || obj instanceof String ) 84 return (short) Long.parseLong((String ) obj); 85 else 86 throw new MessageFormatException (L.l("can't convert '{0}' to short", 87 obj.getClass().getName())); 88 } 89 90 93 public static int toInt(Object obj) 94 throws JMSException 95 { 96 if (obj instanceof Integer || 97 obj instanceof Short || 98 obj instanceof Byte ) 99 return ((Number ) obj).intValue(); 100 else if (obj == null || obj instanceof String ) 101 return (int) Long.parseLong((String ) obj); 102 else 103 throw new MessageFormatException (L.l("can't convert '{0}' to int", 104 obj.getClass().getName())); 105 } 106 107 110 public static long toLong(Object obj) 111 throws JMSException 112 { 113 if (obj instanceof Long || 114 obj instanceof Integer || 115 obj instanceof Short || 116 obj instanceof Byte ) 117 return ((Number ) obj).longValue(); 118 else if (obj == null || obj instanceof String ) 119 return Long.parseLong((String ) obj); 120 else 121 throw new MessageFormatException (L.l("can't convert '{0}' to long", 122 obj.getClass().getName())); 123 } 124 125 128 public static float toFloat(Object obj) 129 throws JMSException 130 { 131 if (obj == null || obj instanceof Float ) 132 return ((Number ) obj).floatValue(); 133 else if (obj == null || obj instanceof String ) 134 return (float) Double.parseDouble((String ) obj); 135 else 136 throw new MessageFormatException (L.l("can't convert '{0}' to float", 137 obj.getClass().getName())); 138 } 139 140 143 public static double toDouble(Object obj) 144 throws JMSException 145 { 146 if (obj == null || obj instanceof Float || obj instanceof Double ) 147 return ((Number ) obj).doubleValue(); 148 else if (obj == null || obj instanceof String ) 149 return Double.parseDouble((String ) obj); 150 else 151 throw new MessageFormatException (L.l("can't convert '{0}' to double", 152 obj.getClass().getName())); 153 } 154 155 158 public static String toString(Object obj) 159 throws JMSException 160 { 161 if (obj == null) 162 return null; 163 else 164 return obj.toString(); 165 } 166 167 170 public static char toChar(Object obj) 171 throws JMSException 172 { 173 if (obj == null) 174 throw new NullPointerException (); 175 else if (obj instanceof Character ) 176 return ((Character ) obj).charValue(); 177 else if (obj instanceof String ) 178 return ((String ) obj).charAt(0); 179 else 180 throw new MessageFormatException (L.l("bad property {0}", obj)); 181 182 } 183 184 187 public static byte []toBytes(Object obj) 188 throws JMSException 189 { 190 if (obj == null) 191 return null; 192 else if (obj instanceof byte[]) { 193 byte []bytes = (byte []) obj; 194 byte []newBytes = new byte[bytes.length]; 195 System.arraycopy(bytes, 0, newBytes, 0, bytes.length); 196 197 return newBytes; 198 } 199 else if (obj instanceof String ) { 200 String string = toString(obj); 201 try { 202 return string.getBytes("UTF-8"); 203 } catch (Exception e) { 204 throw new MessageFormatException (e.toString()); 205 } 206 } 207 else 208 throw new MessageFormatException (L.l("can't convert {0} to byte[]", 209 obj.getClass().getName())); 210 } 211 } 212 | Popular Tags |