1 /*******************************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *******************************************************************************/
19 package org.apache.commons.convert;
20
21 /** Boolean <code>Converter</code> classes. */
22 public class BooleanConverters implements ConverterLoader {
23 /**
24 * An object that converts a <code>Boolean</code> to an <code>Integer</code>.
25 */
26 public static class BooleanToInteger extends AbstractConverter<Boolean, Integer> {
27 public BooleanToInteger() {
28 super(Boolean.class, Integer.class);
29 }
30
31 /**
32 * Returns 1 if <code>obj</code> is true, or zero if
33 * <code>obj</code> is false.
34 */
35 public Integer convert(Boolean obj) throws ConversionException {
36 return obj.booleanValue() ? 1 : 0;
37 }
38 }
39
40 /**
41 * An object that converts an <code>Integer</code> to a <code>Boolean</code>.
42 */
43 public static class IntegerToBoolean extends AbstractConverter<Integer, Boolean> {
44 public IntegerToBoolean() {
45 super(Integer.class, Boolean.class);
46 }
47
48 /**
49 * Returns <code>true</code> if <code>obj</code> is non-zero, or
50 * <code>false</code> if <code>obj</code> is zero.
51 */
52 public Boolean convert(Integer obj) throws ConversionException {
53 return obj.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
54 }
55 }
56
57 /**
58 * An object that converts a <code>String</code> to a <code>Boolean</code>.
59 */
60 public static class StringToBoolean extends AbstractConverter<String, Boolean> {
61 public StringToBoolean() {
62 super(String.class, Boolean.class);
63 }
64
65 /**
66 * Returns <code>true</code> if <code>obj</code> equals "true", or
67 * <code>false</code> if <code>obj</code> is any other value. The
68 * test for "true" is case-insensitive.
69 */
70 public Boolean convert(String obj) throws ConversionException {
71 return "TRUE".equals(obj.trim().toUpperCase());
72 }
73 }
74
75 public void loadConverters() {
76 Converters.loadContainedConverters(BooleanConverters.class);
77 Converters.registerConverter(new GenericSingletonToList<Boolean>(Boolean.class));
78 Converters.registerConverter(new GenericSingletonToSet<Boolean>(Boolean.class));
79 Converters.registerConverter(new GenericToStringConverter<Boolean>(Boolean.class));
80 }
81 }