1 22 23 24 package com.mchange.v2.lang; 25 26 import java.util.*; 27 28 public final class Coerce 29 { 30 final static Set CAN_COERCE; 31 32 static 33 { 34 Class [] classes = 35 { 36 byte.class, 37 boolean.class, 38 char.class, 39 short.class, 40 int.class, 41 long.class, 42 float.class, 43 double.class, 44 String .class, 45 Byte .class, 46 Boolean .class, 47 Character .class, 48 Short .class, 49 Integer .class, 50 Long .class, 51 Float .class, 52 Double .class 53 }; 54 Set tmp = new HashSet(); 55 tmp.addAll( Arrays.asList( classes ) ); 56 CAN_COERCE = Collections.unmodifiableSet( tmp ); 57 } 58 59 public static boolean canCoerce( Class cl ) 60 { return CAN_COERCE.contains( cl ); } 61 62 public static boolean canCoerce( Object o ) 63 { return canCoerce( o.getClass() ); } 64 65 public static int toInt( String s ) 66 { 67 try { return Integer.parseInt( s ); } 68 catch ( NumberFormatException e ) 69 { return (int) Double.parseDouble( s ); } 70 } 71 72 public static long toLong( String s ) 73 { 74 try { return Long.parseLong( s ); } 75 catch ( NumberFormatException e ) 76 { return (long) Double.parseDouble( s ); } 77 } 78 79 public static float toFloat( String s ) 80 { return Float.parseFloat( s ); } 81 82 public static double toDouble( String s ) 83 { return Double.parseDouble( s ); } 84 85 public static byte toByte( String s ) 86 { return (byte) toInt(s); } 87 88 public static short toShort( String s ) 89 { return (short) toInt(s); } 90 91 public static boolean toBoolean( String s ) 92 { return Boolean.valueOf( s ).booleanValue(); } 93 94 public static char toChar( String s ) 95 { 96 s = s.trim(); 97 if (s.length() == 1) 98 return s.charAt( 0 ); 99 else 100 return (char) toInt(s); 101 } 102 103 public static Object toObject( String s, Class type ) 104 { 105 if ( type == byte.class) type = Byte .class; 106 else if ( type == boolean.class) type = Boolean .class; 107 else if ( type == char.class) type = Character .class; 108 else if ( type == short.class) type = Short .class; 109 else if ( type == int.class) type = Integer .class; 110 else if ( type == long.class) type = Long .class; 111 else if ( type == float.class) type = Float .class; 112 else if ( type == double.class) type = Double .class; 113 114 if ( type == String .class ) 115 return s; 116 else if ( type == Byte .class ) 117 return new Byte ( toByte( s ) ); 118 else if ( type == Boolean .class ) 119 return Boolean.valueOf( s ); 120 else if ( type == Character .class ) 121 return new Character ( toChar( s ) ); 122 else if ( type == Short .class ) 123 return new Short ( toShort( s ) ); 124 else if ( type == Integer .class ) 125 return new Integer ( s ); 126 else if ( type == Long .class ) 127 return new Long ( s ); 128 else if ( type == Float .class ) 129 return new Float ( s ); 130 else if ( type == Double .class ) 131 return new Double ( s ); 132 else 133 throw new IllegalArgumentException ("Cannot coerce to type: " + type.getName()); 134 } 135 136 private Coerce() 137 {} 138 } 139 | Popular Tags |