KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > lang > Coerce


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

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 JavaDoc[] 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 JavaDoc.class,
45         Byte JavaDoc.class,
46         Boolean JavaDoc.class,
47         Character JavaDoc.class,
48         Short JavaDoc.class,
49         Integer JavaDoc.class,
50         Long JavaDoc.class,
51         Float JavaDoc.class,
52         Double JavaDoc.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 JavaDoc cl )
60     { return CAN_COERCE.contains( cl ); }
61
62     public static boolean canCoerce( Object JavaDoc o )
63     { return canCoerce( o.getClass() ); }
64
65     public static int toInt( String JavaDoc s )
66     {
67     try { return Integer.parseInt( s ); }
68     catch ( NumberFormatException JavaDoc e )
69         { return (int) Double.parseDouble( s ); }
70     }
71
72     public static long toLong( String JavaDoc s )
73     {
74     try { return Long.parseLong( s ); }
75     catch ( NumberFormatException JavaDoc e )
76         { return (long) Double.parseDouble( s ); }
77     }
78
79     public static float toFloat( String JavaDoc s )
80     { return Float.parseFloat( s ); }
81
82     public static double toDouble( String JavaDoc s )
83     { return Double.parseDouble( s ); }
84
85     public static byte toByte( String JavaDoc s )
86     { return (byte) toInt(s); }
87
88     public static short toShort( String JavaDoc s )
89     { return (short) toInt(s); }
90
91     public static boolean toBoolean( String JavaDoc s )
92     { return Boolean.valueOf( s ).booleanValue(); }
93
94     public static char toChar( String JavaDoc 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 JavaDoc toObject( String JavaDoc s, Class JavaDoc type )
104     {
105     if ( type == byte.class) type = Byte JavaDoc.class;
106     else if ( type == boolean.class) type = Boolean JavaDoc.class;
107     else if ( type == char.class) type = Character JavaDoc.class;
108     else if ( type == short.class) type = Short JavaDoc.class;
109     else if ( type == int.class) type = Integer JavaDoc.class;
110     else if ( type == long.class) type = Long JavaDoc.class;
111     else if ( type == float.class) type = Float JavaDoc.class;
112     else if ( type == double.class) type = Double JavaDoc.class;
113
114     if ( type == String JavaDoc.class )
115         return s;
116     else if ( type == Byte JavaDoc.class )
117         return new Byte JavaDoc( toByte( s ) );
118     else if ( type == Boolean JavaDoc.class )
119         return Boolean.valueOf( s );
120     else if ( type == Character JavaDoc.class )
121         return new Character JavaDoc( toChar( s ) );
122     else if ( type == Short JavaDoc.class )
123         return new Short JavaDoc( toShort( s ) );
124     else if ( type == Integer JavaDoc.class )
125         return new Integer JavaDoc( s );
126     else if ( type == Long JavaDoc.class )
127         return new Long JavaDoc( s );
128     else if ( type == Float JavaDoc.class )
129         return new Float JavaDoc( s );
130     else if ( type == Double JavaDoc.class )
131         return new Double JavaDoc( s );
132     else
133         throw new IllegalArgumentException JavaDoc("Cannot coerce to type: " + type.getName());
134     }
135
136     private Coerce()
137     {}
138 }
139
Popular Tags