01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.util;
17:
18: import org.geotools.factory.Hints;
19:
20: /**
21: * ConverterFactory for handling boolean conversions.
22: * <p>
23: * Supported conversions:
24: * <ul>
25: * <li>"true" -> Boolean.TRUE
26: * <li>"false" -> Boolean.FALSE
27: * <li>"1" -> Boolean.TRUE
28: * <li>"0" -> Boolean.FALSE
29: * <li>1 -> Boolean.TRUE
30: * <li>0 -> Boolean.FALSE
31: * </ul>
32: * </p>
33: * @author Justin Deoliveira, The Open Planning Project
34: * @since 2.4
35: */
36: public class BooleanConverterFactory implements ConverterFactory {
37:
38: public Converter createConverter(Class source, Class target,
39: Hints hints) {
40: if (target.equals(Boolean.class)) {
41:
42: //string to boolean
43: if (source.equals(String.class)) {
44: return new Converter() {
45:
46: public Object convert(Object source, Class target)
47: throws Exception {
48: if ("true".equals(source) || "1".equals(source)) {
49: return Boolean.TRUE;
50: }
51: if ("false".equals(source)
52: || "0".equals(source)) {
53: return Boolean.FALSE;
54: }
55:
56: return null;
57: }
58:
59: };
60: }
61:
62: //integer to boolean
63: if (source.equals(Integer.class)) {
64: return new Converter() {
65:
66: public Object convert(Object source, Class target)
67: throws Exception {
68: if (new Integer(1).equals(source)) {
69: return Boolean.TRUE;
70: }
71: if (new Integer(0).equals(source)) {
72: return Boolean.FALSE;
73: }
74:
75: return null;
76: }
77:
78: };
79: }
80:
81: }
82:
83: return null;
84: }
85:
86: }
|