KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > typeconverter > BooleanConverter


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.typeconverter;
4
5 /**
6  * Converts given object to Boolean. Given object (if not already instance of
7  * Boolean) is first converted to String and then analyzed.
8  */

9 public class BooleanConverter implements TypeConverter {
10
11     public static Boolean JavaDoc valueOf(Object JavaDoc value) {
12         if (value == null) {
13             return null;
14         }
15         if (value instanceof Boolean JavaDoc) {
16             return (Boolean JavaDoc) value;
17         }
18         String JavaDoc stringValue = value.toString();
19         if (stringValue.equalsIgnoreCase("yes") ||
20                 stringValue.equalsIgnoreCase("y") ||
21                 stringValue.equalsIgnoreCase("true") ||
22                 stringValue.equalsIgnoreCase("on") ||
23                 stringValue.equalsIgnoreCase("1")) {
24             return Boolean.TRUE;
25         }
26         if (stringValue.equalsIgnoreCase("no") ||
27                 stringValue.equalsIgnoreCase("n") ||
28                 stringValue.equalsIgnoreCase("false") ||
29                 stringValue.equalsIgnoreCase("off") ||
30                 stringValue.equalsIgnoreCase("0")) {
31             return Boolean.FALSE;
32         }
33         throw new TypeConversionException();
34     }
35
36     public Object JavaDoc convert(Object JavaDoc value) {
37         return valueOf(value);
38     }
39     
40 }
41
Popular Tags