001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.lang;
024:
025: import java.util.*;
026:
027: public final class Coerce {
028: final static Set CAN_COERCE;
029:
030: static {
031: Class[] classes = { byte.class, boolean.class, char.class,
032: short.class, int.class, long.class, float.class,
033: double.class, String.class, Byte.class, Boolean.class,
034: Character.class, Short.class, Integer.class,
035: Long.class, Float.class, Double.class };
036: Set tmp = new HashSet();
037: tmp.addAll(Arrays.asList(classes));
038: CAN_COERCE = Collections.unmodifiableSet(tmp);
039: }
040:
041: public static boolean canCoerce(Class cl) {
042: return CAN_COERCE.contains(cl);
043: }
044:
045: public static boolean canCoerce(Object o) {
046: return canCoerce(o.getClass());
047: }
048:
049: public static int toInt(String s) {
050: try {
051: return Integer.parseInt(s);
052: } catch (NumberFormatException e) {
053: return (int) Double.parseDouble(s);
054: }
055: }
056:
057: public static long toLong(String s) {
058: try {
059: return Long.parseLong(s);
060: } catch (NumberFormatException e) {
061: return (long) Double.parseDouble(s);
062: }
063: }
064:
065: public static float toFloat(String s) {
066: return Float.parseFloat(s);
067: }
068:
069: public static double toDouble(String s) {
070: return Double.parseDouble(s);
071: }
072:
073: public static byte toByte(String s) {
074: return (byte) toInt(s);
075: }
076:
077: public static short toShort(String s) {
078: return (short) toInt(s);
079: }
080:
081: public static boolean toBoolean(String s) {
082: return Boolean.valueOf(s).booleanValue();
083: }
084:
085: public static char toChar(String s) {
086: s = s.trim();
087: if (s.length() == 1)
088: return s.charAt(0);
089: else
090: return (char) toInt(s);
091: }
092:
093: public static Object toObject(String s, Class type) {
094: if (type == byte.class)
095: type = Byte.class;
096: else if (type == boolean.class)
097: type = Boolean.class;
098: else if (type == char.class)
099: type = Character.class;
100: else if (type == short.class)
101: type = Short.class;
102: else if (type == int.class)
103: type = Integer.class;
104: else if (type == long.class)
105: type = Long.class;
106: else if (type == float.class)
107: type = Float.class;
108: else if (type == double.class)
109: type = Double.class;
110:
111: if (type == String.class)
112: return s;
113: else if (type == Byte.class)
114: return new Byte(toByte(s));
115: else if (type == Boolean.class)
116: return Boolean.valueOf(s);
117: else if (type == Character.class)
118: return new Character(toChar(s));
119: else if (type == Short.class)
120: return new Short(toShort(s));
121: else if (type == Integer.class)
122: return new Integer(s);
123: else if (type == Long.class)
124: return new Long(s);
125: else if (type == Float.class)
126: return new Float(s);
127: else if (type == Double.class)
128: return new Double(s);
129: else
130: throw new IllegalArgumentException(
131: "Cannot coerce to type: " + type.getName());
132: }
133:
134: private Coerce() {
135: }
136: }
|