1 24 package com.scalagent.kjoram.messages; 25 26 import com.scalagent.kjoram.excepts.MessageValueException; 27 28 32 public class ConversionHelper 33 { 34 40 public static boolean toBoolean(Object value) throws MessageValueException 41 { 42 if (value == null) 43 throw new RuntimeException ("toBoolean value = null"); 44 45 if (value instanceof Boolean ) 46 return ((Boolean ) value).booleanValue(); 47 else if (value instanceof String ) { 48 String s = ((String ) value).toUpperCase(); 49 return s.equals("TRUE") || s.equals("YES"); 50 } else 51 throw new MessageValueException("Type " + value.getClass().getName() 52 + " can't be converted to Boolean."); 53 } 54 55 61 public static byte toByte(Object value) throws MessageValueException 62 { 63 if (value == null) 64 throw new RuntimeException ("toByte value = null"); 65 66 if (value instanceof Byte ) 67 return ((Byte ) value).byteValue(); 68 else if (value instanceof String ) 69 return Byte.parseByte((String ) value); 70 else 71 throw new MessageValueException("Type " + value.getClass().getName() 72 + " can't be converted to Byte."); 73 } 74 75 81 public static short toShort(Object value) throws MessageValueException 82 { 83 if (value == null) 84 throw new RuntimeException ("toShort value = null"); 85 86 if (value instanceof Byte ) 87 return Short.parseShort(((Byte ) value).toString()); 88 else if (value instanceof Short ) 89 return ((Short ) value).shortValue(); 90 else if (value instanceof String ) 91 return Short.parseShort((String ) value); 92 else 93 throw new MessageValueException("Type " + value.getClass().getName() 94 + " can't be converted to Short."); 95 } 96 97 103 public static int toInt(Object value) throws MessageValueException 104 { 105 if (value == null) 106 return Integer.valueOf(null).intValue(); 107 108 if (value instanceof Byte ) 109 return Integer.parseInt(((Byte ) value).toString()); 110 else if (value instanceof Short ) 111 return Integer.parseInt(((Short ) value).toString()); 112 else if (value instanceof Integer ) 113 return ((Integer ) value).intValue(); 114 else if (value instanceof String ) 115 return Integer.valueOf((String ) value).intValue(); 116 else 117 throw new MessageValueException("Type " + value.getClass().getName() 118 + " can't be converted to Integer."); 119 } 120 121 127 public static long toLong(Object value) throws MessageValueException 128 { 129 if (value == null) 130 throw new RuntimeException ("toLong value = null"); 131 132 if (value instanceof Byte ) 133 return Byte.parseByte(((Byte ) value).toString()); 134 else if (value instanceof Short ) 135 return Long.parseLong(((Short ) value).toString()); 136 else if (value instanceof Integer ) 137 return ((Integer ) value).longValue(); 138 else if (value instanceof Long ) 139 return ((Long ) value).longValue(); 140 else if (value instanceof String ) 141 return Long.parseLong((String ) value); 142 else 143 throw new MessageValueException("Type " + value.getClass().getName() 144 + " can't be converted to Long."); 145 } 146 147 153 158 167 173 178 189 190 public static String toString(Object value) 191 { 192 if (value == null) 193 return null; 194 195 if (value instanceof byte[]) 196 return new String ((byte[]) value); 197 else 198 return value.toString(); 199 } 200 201 207 public static char toChar(Object value) throws MessageValueException 208 { 209 if (value == null) 210 return ((Character ) null).charValue(); 211 else if (value instanceof Character ) 212 return ((Character ) value).charValue(); 213 else 214 throw new MessageValueException("Type " + value.getClass().getName() 215 + " can't be converted to Character."); 216 } 217 218 224 public static byte[] toBytes(Object value) throws MessageValueException 225 { 226 if (value == null) 227 return (byte[]) value; 228 else if (value instanceof byte[]) 229 return (byte[]) value; 230 else 231 throw new MessageValueException("Type " + value.getClass().getName() 232 + " can't be converted to byte[]."); 233 } 234 } 235 | Popular Tags |