1 24 package org.objectweb.joram.shared.messages; 25 26 import org.objectweb.joram.shared.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 return Boolean.valueOf(null).booleanValue(); 44 45 if (value instanceof Boolean ) 46 return ((Boolean ) value).booleanValue(); 47 else if (value instanceof String ) 48 return Boolean.valueOf((String ) value).booleanValue(); 49 else 50 throw new MessageValueException("Type " + value.getClass().getName() 51 + " can't be converted to Boolean."); 52 } 53 54 60 public static byte toByte(Object value) throws MessageValueException 61 { 62 if (value == null) 63 return Byte.valueOf(null).byteValue(); 64 65 if (value instanceof Byte ) 66 return ((Byte ) value).byteValue(); 67 else if (value instanceof String ) 68 return Byte.valueOf((String ) value).byteValue(); 69 else 70 throw new MessageValueException("Type " + value.getClass().getName() 71 + " can't be converted to Byte."); 72 } 73 74 80 public static short toShort(Object value) throws MessageValueException 81 { 82 if (value == null) 83 return Short.valueOf(null).shortValue(); 84 85 if (value instanceof Byte || value instanceof Short ) 86 return ((Number ) value).shortValue(); 87 else if (value instanceof String ) 88 return Short.valueOf((String ) value).shortValue(); 89 else 90 throw new MessageValueException("Type " + value.getClass().getName() 91 + " can't be converted to Short."); 92 } 93 94 100 public static int toInt(Object value) throws MessageValueException 101 { 102 if (value == null) 103 return Integer.valueOf(null).intValue(); 104 105 if (value instanceof Byte || value instanceof Short 106 || value instanceof Integer ) 107 return ((Number ) value).intValue(); 108 else if (value instanceof String ) 109 return Integer.valueOf((String ) value).intValue(); 110 else 111 throw new MessageValueException("Type " + value.getClass().getName() 112 + " can't be converted to Integer."); 113 } 114 115 121 public static long toLong(Object value) throws MessageValueException 122 { 123 if (value == null) 124 return Long.valueOf(null).longValue(); 125 126 if (value instanceof Byte || value instanceof Short 127 || value instanceof Integer || value instanceof Long ) 128 return ((Number ) value).longValue(); 129 else if (value instanceof String ) 130 return Long.valueOf((String ) value).longValue(); 131 else 132 throw new MessageValueException("Type " + value.getClass().getName() 133 + " can't be converted to Long."); 134 } 135 136 142 public static float toFloat(Object value) throws MessageValueException 143 { 144 if (value == null) 145 return Float.valueOf(null).floatValue(); 146 147 if (value instanceof Float ) 148 return ((Float ) value).floatValue(); 149 else if (value instanceof String ) 150 return Float.valueOf((String ) value).floatValue(); 151 else 152 throw new MessageValueException("Type " + value.getClass().getName() 153 + " can't be converted to Float."); 154 } 155 156 162 public static double toDouble(Object value) throws MessageValueException 163 { 164 if (value == null) 165 return Double.valueOf(null).doubleValue(); 166 167 if (value instanceof Float || value instanceof Double ) 168 return ((Number ) value).doubleValue(); 169 else if (value instanceof String ) 170 return Double.valueOf((String ) value).doubleValue(); 171 else 172 throw new MessageValueException("Type " + value.getClass().getName() 173 + " can't be converted to Double."); 174 } 175 176 177 public static String toString(Object value) 178 { 179 if (value == null) 180 return null; 181 182 if (value instanceof byte[]) 183 return new String ((byte[]) value); 184 else 185 return value.toString(); 186 } 187 188 194 public static char toChar(Object value) throws MessageValueException 195 { 196 if (value == null) 197 return ((Character ) null).charValue(); 198 else if (value instanceof Character ) 199 return ((Character ) value).charValue(); 200 else 201 throw new MessageValueException("Type " + value.getClass().getName() 202 + " can't be converted to Character."); 203 } 204 205 211 public static byte[] toBytes(Object value) throws MessageValueException 212 { 213 if (value == null) 214 return (byte[]) value; 215 else if (value instanceof byte[]) 216 return (byte[]) value; 217 else 218 throw new MessageValueException("Type " + value.getClass().getName() 219 + " can't be converted to byte[]."); 220 } 221 } 222 | Popular Tags |