Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BooleanConverter |
|
| 6.666666666666667;6.667 |
1 | /* | |
2 | * Copyright 2003-2004 The Apache Software Foundation | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | package org.apache.commons.convert1.string; | |
17 | ||
18 | ||
19 | import org.apache.commons.convert1.ConversionException; | |
20 | import org.apache.commons.convert1.Converter; | |
21 | ||
22 | ||
23 | /** | |
24 | * <p>Standard {@link Converter} implementation that converts an incoming | |
25 | * String into a <code>java.lang.Boolean</code> object, optionally using a | |
26 | * default value or throwing a {@link ConversionException} if a conversion | |
27 | * error occurs.</p> | |
28 | * | |
29 | * @author Craig R. McClanahan | |
30 | * @version $Id: BooleanConverter.java 155441 2005-02-26 13:19:22Z dirkv $ | |
31 | * @since 0.1 | |
32 | */ | |
33 | ||
34 | public final class BooleanConverter implements Converter { | |
35 | ||
36 | ||
37 | // ----------------------------------------------------------- Constructors | |
38 | ||
39 | ||
40 | /** | |
41 | * Create a {@link Converter} that will throw a {@link ConversionException} | |
42 | * if a conversion error occurs. | |
43 | */ | |
44 | 0 | public BooleanConverter() { |
45 | ||
46 | 0 | this.defaultValue = null; |
47 | 0 | this.useDefault = false; |
48 | ||
49 | 0 | } |
50 | ||
51 | ||
52 | /** | |
53 | * Create a {@link Converter} that will return the specified default value | |
54 | * if a conversion error occurs. | |
55 | * | |
56 | * @param defaultValue The default value to be returned | |
57 | */ | |
58 | 0 | public BooleanConverter(Object defaultValue) { |
59 | ||
60 | 0 | this.defaultValue = defaultValue; |
61 | 0 | this.useDefault = true; |
62 | ||
63 | 0 | } |
64 | ||
65 | ||
66 | // ----------------------------------------------------- Instance Variables | |
67 | ||
68 | ||
69 | /** | |
70 | * The default value specified to our Constructor, if any. | |
71 | */ | |
72 | 0 | private Object defaultValue = null; |
73 | ||
74 | ||
75 | /** | |
76 | * Should we return the default value on conversion errors? | |
77 | */ | |
78 | 0 | private boolean useDefault = true; |
79 | ||
80 | ||
81 | // --------------------------------------------------------- Public Methods | |
82 | ||
83 | ||
84 | /** | |
85 | * Convert the specified input object into an output object of the | |
86 | * specified type. | |
87 | * | |
88 | * @param type Data type to which this value should be converted | |
89 | * @param value The input value to be converted | |
90 | * | |
91 | * @exception ConversionException if conversion cannot be performed | |
92 | * successfully | |
93 | */ | |
94 | public Object convert(Class type, Object value) { | |
95 | ||
96 | 0 | if (value == null) { |
97 | 0 | if (useDefault) { |
98 | 0 | return (defaultValue); |
99 | } else { | |
100 | 0 | throw new ConversionException("No value specified"); |
101 | } | |
102 | } | |
103 | ||
104 | 0 | if (value instanceof Boolean) { |
105 | 0 | return (value); |
106 | } | |
107 | ||
108 | try { | |
109 | 0 | String stringValue = value.toString(); |
110 | 0 | if (stringValue.equalsIgnoreCase("yes") || |
111 | stringValue.equalsIgnoreCase("y") || | |
112 | stringValue.equalsIgnoreCase("true") || | |
113 | stringValue.equalsIgnoreCase("on") || | |
114 | stringValue.equalsIgnoreCase("1")) { | |
115 | 0 | return (Boolean.TRUE); |
116 | 0 | } else if (stringValue.equalsIgnoreCase("no") || |
117 | stringValue.equalsIgnoreCase("n") || | |
118 | stringValue.equalsIgnoreCase("false") || | |
119 | stringValue.equalsIgnoreCase("off") || | |
120 | stringValue.equalsIgnoreCase("0")) { | |
121 | 0 | return (Boolean.FALSE); |
122 | 0 | } else if (useDefault) { |
123 | 0 | return (defaultValue); |
124 | } else { | |
125 | 0 | throw new ConversionException(stringValue); |
126 | } | |
127 | 0 | } catch (ClassCastException e) { |
128 | 0 | if (useDefault) { |
129 | 0 | return (defaultValue); |
130 | } else { | |
131 | 0 | throw new ConversionException(e); |
132 | } | |
133 | } | |
134 | ||
135 | } | |
136 | ||
137 | ||
138 | } |