##### Coverage Report - org.webmacro.util.CastUtil

Classes in this File | Line Coverage | Branch Coverage | Complexity
CastUtil |
2% | 3/102
0% | 0/51
| 4.928571428571429;4.929

1 |  |
     /*
2 |  |
      * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
3 |  |
      *
4 |  |
      * Redistribution and use in source and binary forms, with or without
5 |  |
      * modification, are permitted under the terms of either of the following
6 |  |
      * Open Source licenses:
7 |  |
      *
8 |  |
      * The GNU General Public License, version 2, or any later version, as
9 |  |
      * published by the Free Software Foundation
10 |  |
      * (http://www.fsf.org/copyleft/gpl.html);
11 |  |
      *
12 |  |
      *  or
13 |  |
      *
14 |  |
      * The Semiotek Public License (http://webmacro.org/LICENSE.)
15 |  |
      *
16 |  |
      * This software is provided "as is", with NO WARRANTY, not even the
17 |  |
      * implied warranties of fitness to purpose, or merchantability. You
18 |  |
      * assume all risks and liabilities associated with its use.
19 |  |
      *
20 |  |
      * See www.webmacro.org for more information on the WebMacro project.
21 |  |
      */
22 |  |
     package org.webmacro.util;
23 |  |
24 |  |
     /**
25 |  |
      * A singleton for casting one type to another.
26 |  |
      *
27 |  |
      * @author  keats_kirsch
28 |  |
      * @since 0.96
29 |  |
      */
30 |  |
     public final class CastUtil
31 |  |
     {
32 |  |
33 | 2 |
         static private CastUtil _singleton = new CastUtil();
34 |  |
35 |  |
         /** private constructor for singleton */
36 |  |
         private CastUtil ()
37 | 2 |
         {
38 | 2 |
         }
39 |  |
40 |  |
         public static CastUtil getInstance ()
41 |  |
         {
42 | 0 |
             return _singleton;
43 |  |
         }
44 |  |
45 |  |
         public static Boolean toBoolean (java.lang.Object o)
46 |  |
         {
47 | 0 |
             if (o == null) return Boolean.FALSE;
48 | 0 |
             return Boolean.valueOf(o.toString());
49 |  |
         }
50 |  |
51 |  |
         public static Character toChar (Object o) throws org.webmacro.PropertyException
52 |  |
         {
53 | 0 |
             Character c = null;
54 | 0 |
             if (o == null || o == "") return null;
55 | 0 |
             if (o instanceof Character) return (Character) o;
56 | 0 |
             if (o instanceof Number)
57 |  |
             {
58 |  |
                 try
59 |  |
                 {
60 | 0 |
                     c = new Character((char) ((Number) o).intValue());
61 |  |
                 }
62 | 0 |
                 catch (Exception e)
63 |  |
                 {
64 | 0 |
                     throw new IllegalArgumentException(
65 |  |
                             "Not a valid char: " +
66 |  |
                                 o.toString() + "; type=" + o.getClass().getName());
67 | 0 |
                 }
68 |  |
             }
69 | 0 |
             else if (o instanceof String)
70 |  |
             {
71 | 0 |
                 String s = (String) o;
72 | 0 |
                 if (s.startsWith("\\u") && s.length() == 6)
73 |  |
                 {
74 |  |
                     // unicode char
75 | 0 |
                     int i = Integer.parseInt(s.substring(2), 16);
76 | 0 |
                     c = new Character((char) i);
77 | 0 |
                 }
78 |  |
                 else
79 |  |
                 { // use the first character of the string
80 | 0 |
                     char ch = s.charAt(0);
81 | 0 |
                     c = new Character(ch);
82 |  |
                 }
83 | 0 |
             }
84 |  |
             else
85 |  |
             { // throw exception
86 | 0 |
                 throw new org.webmacro.PropertyException(
87 |  |
                         "$Type.toChar() is unable to convert the supplied value to a Character." +
88 |  |
                         "  The argument"
89 |  |
                         + " must be a number or a String.  The supplied argument was "
90 |  |
                         + o + " of type " + o.getClass().getName());
91 |  |
             }
92 | 0 |
             return c;
93 |  |
         }
94 |  |
95 |  |
         public static Byte toByte (java.lang.Object o)
96 |  |
         {
97 | 0 |
             if (o instanceof Byte) return (Byte) o;
98 | 0 |
             Byte v = null;
99 |  |
             try
100 |  |
             {
101 | 0 |
                 int i = Integer.parseInt(o.toString());
102 | 0 |
                 v = new Byte((byte) i);
103 |  |
             }
104 | 0 |
             catch (Exception e)
105 |  |
             {
106 | 0 |
                 throw new IllegalArgumentException("Not a valid byte: " +
107 |  |
                         ((o == null) ? "null" :
108 |  |
                             (o.toString() + "; type=" + o.getClass().getName())));
109 | 0 |
             }
110 | 0 |
             return v;
111 |  |
         }
112 |  |
113 |  |
         public static Short toShort (java.lang.Object o)
114 |  |
         {
115 | 0 |
             if (o instanceof Short) return (Short) o;
116 | 0 |
             Short v = null;
117 |  |
             try
118 |  |
             {
119 | 0 |
                 int i = Integer.parseInt(o.toString());
120 | 0 |
                 v = new Short((short) i);
121 |  |
             }
122 | 0 |
             catch (Exception e)
123 |  |
             {
124 | 0 |
                 throw new IllegalArgumentException("Not a valid short: " +
125 |  |
                         ((o == null) ? "null" :
126 |  |
                             (o.toString() + "; type=" + o.getClass().getName())));
127 | 0 |
             }
128 | 0 |
             return v;
129 |  |
         }
130 |  |
131 |  |
         public static Integer toInt (java.lang.Object o)
132 |  |
         {
133 | 0 |
             Integer i = null;
134 | 0 |
             if (o instanceof Integer) return (Integer) o;
135 |  |
             try
136 |  |
             {
137 | 0 |
                 i = new Integer(o.toString());
138 |  |
             }
139 | 0 |
             catch (Exception e)
140 |  |
             {
141 | 0 |
                 throw new IllegalArgumentException(
142 |  |
                         "Not a valid int: " +
143 |  |
                         ((o == null) ? "null" :
144 |  |
                             (o.toString() + "; type=" + o.getClass().getName())));
145 | 0 |
             }
146 | 0 |
             return i;
147 |  |
         }
148 |  |
149 |  |
         public static Long toLong (java.lang.Object o)
150 |  |
         {
151 | 0 |
             Long l = null;
152 | 0 |
             if (o instanceof Long) return (Long) o;
153 |  |
             try
154 |  |
             {
155 | 0 |
                 l = new Long(o.toString());
156 |  |
             }
157 | 0 |
             catch (Exception e)
158 |  |
             {
159 | 0 |
                 throw new IllegalArgumentException("Not a valid long: " +
160 |  |
                         ((o == null) ? "null" :
161 |  |
                             (o.toString() + "; type=" + o.getClass().getName())));
162 | 0 |
             }
163 | 0 |
             return l;
164 |  |
         }
165 |  |
166 |  |
         public static Float toFloat (java.lang.Object o)
167 |  |
         {
168 | 0 |
             Float f = null;
169 | 0 |
             if (o instanceof Float) return (Float) o;
170 |  |
             try
171 |  |
             {
172 | 0 |
                 f = new Float(o.toString());
173 |  |
             }
174 | 0 |
             catch (Exception e)
175 |  |
             {
176 | 0 |
                 throw new IllegalArgumentException(
177 |  |
                         "Not a valid float: " + ((o == null) ? "null" :
178 |  |
                             (o.toString() + "; type=" + o.getClass().getName())));
179 | 0 |
             }
180 | 0 |
             return f;
181 |  |
         }
182 |  |
183 |  |
         public static Double toDouble (java.lang.Object o)
184 |  |
         {
185 | 0 |
             Double d = null;
186 | 0 |
             if (o instanceof Double) return (Double) o;
187 |  |
             try
188 |  |
             {
189 | 0 |
                 d = new Double(o.toString());
190 |  |
             }
191 | 0 |
             catch (Exception e)
192 |  |
             {
193 | 0 |
                 throw new IllegalArgumentException(
194 |  |
                         "Not a valid double: " + ((o == null) ? "null" :
195 |  |
                             (o.toString() + "; type=" + o.getClass().getName())));
196 | 0 |
             }
197 | 0 |
             return d;
198 |  |
         }
199 |  |
200 |  |
         public static String toString(java.lang.Object o) {
201 | 0 |
                 return (o != null ? o.toString() : null);
202 |  |
         }
203 |  |
204 |  |
         static public void main (String[] args)
205 |  |
         {
206 | 0 |
             println("77"
207 |  |
                     + test(BOOLEAN, "77")
208 |  |
                     + test(CHAR, "77")
209 |  |
                     + test(BYTE, "77")
210 |  |
                     + test(SHORT, "77")
211 |  |
                     + test(INTEGER, "77")
212 |  |
                     + test(LONG, "77")
213 |  |
                     + test(FLOAT, "77")
214 |  |
                     + test(DOUBLE, "77")
215 |  |
                     + ".");
216 | 0 |
             println("true: " + test(BOOLEAN, "true") + ".");
217 | 0 |
             println("FalSE: " + test(BOOLEAN, "FalSE") + ".");
218 | 0 |
             println("null: " + test(BOOLEAN, null) + ".");
219 | 0 |
         }
220 |  |
221 |  |
         static private void println (String s)
222 |  |
         {
223 | 0 |
             System.out.println(s);
224 | 0 |
         }
225 |  |
226 |  |
         final static public int BOOLEAN = 1;
227 |  |
         final static public int CHAR = 2;
228 |  |
         final static public int BYTE = 3;
229 |  |
         final static public int SHORT = 4;
230 |  |
         final static public int INTEGER = 5;
231 |  |
         final static public int LONG = 6;
232 |  |
         final static public int FLOAT = 7;
233 |  |
         final static public int DOUBLE = 8;
234 |  |
235 |  |
         static private String test (int type, Object val)
236 |  |
         {
237 | 0 |
             String s = null;
238 |  |
             try
239 |  |
             {
240 | 0 |
                 Object o = null;
241 | 0 |
                 switch (type)
242 |  |
                 {
243 |  |
                     case BOOLEAN:
244 | 0 |
                         o = toBoolean(val);
245 | 0 |
                         break;
246 |  |
                     case CHAR:
247 | 0 |
                         o = toChar(val);
248 | 0 |
                         break;
249 |  |
                     case BYTE:
250 | 0 |
                         o = toByte(val);
251 | 0 |
                         break;
252 |  |
                     case SHORT:
253 | 0 |
                         o = toShort(val);
254 | 0 |
                         break;
255 |  |
                     case INTEGER:
256 | 0 |
                         o = toInt(val);
257 | 0 |
                         break;
258 |  |
                     case LONG:
259 | 0 |
                         o = toLong(val);
260 | 0 |
                         break;
261 |  |
                     case FLOAT:
262 | 0 |
                         o = toFloat(val);
263 | 0 |
                         break;
264 |  |
                     case DOUBLE:
265 | 0 |
                         o = toDouble(val);
266 | 0 |
                         break;
267 |  |
                     default:
268 | 0 |
                         throw new IllegalArgumentException("Unknown type: " + type);
269 |  |
                 }
270 | 0 |
                 s = ", " + o + " (" + o.getClass().getName() + ")";
271 |  |
             }
272 | 0 |
             catch (Exception e)
273 |  |
             {
274 | 0 |
                 e.printStackTrace();
275 | 0 |
             }
276 | 0 |
             return s;
277 |  |
         }
278 |  |
279 |  |
     }