KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > util > TypeConversionUtil


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.util;
6
7 /**
8  * @author Ara Abrahamian (ara_e@email.com)
9  * @created Oct 15, 2001
10  * @version $Revision: 1.5 $
11  */

12 public final class TypeConversionUtil
13 {
14     /**
15      * A utility method for converting a string to a boolean. "yes", "no", "true", "false", "1", "0", "on" and "off" are
16      * valid values for a boolean string (ignoring case). If not one of then then the value of defaultValue parameter is
17      * returned.
18      *
19      * @param defaultValue Description of Parameter
20      * @param in The String to convert
21      * @return true or false
22      */

23     public static boolean stringToBoolean(String JavaDoc in, boolean defaultValue)
24     {
25         if (in == null || in.trim().length() == 0) {
26             return defaultValue;
27         }
28         else {
29             if (in.equalsIgnoreCase("on")) {
30                 return true;
31             }
32             if (in.equalsIgnoreCase("off")) {
33                 return false;
34             }
35
36             switch (in.charAt(0)) {
37             case '1':
38             case 't':
39             case 'T':
40             case 'y':
41             case 'Y':
42                 return true;
43             case '0':
44             case 'f':
45             case 'F':
46             case 'n':
47             case 'N':
48                 return false;
49             default:
50                 return defaultValue;
51             }
52         }
53     }
54 }
55
Popular Tags