001    /*******************************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     *******************************************************************************/
019    package org.apache.commons.convert;
020    
021    /** Boolean <code>Converter</code> classes. */
022    public class BooleanConverters implements ConverterLoader {
023        /**
024         * An object that converts a <code>Boolean</code> to an <code>Integer</code>.
025         */
026        public static class BooleanToInteger extends AbstractConverter<Boolean, Integer> {
027            public BooleanToInteger() {
028                super(Boolean.class, Integer.class);
029            }
030    
031            /**
032             * Returns 1 if <code>obj</code> is true, or zero if 
033             * <code>obj</code> is false.
034             */
035            public Integer convert(Boolean obj) throws ConversionException {
036                 return obj.booleanValue() ? 1 : 0;
037            }
038        }
039    
040        /**
041         * An object that converts an <code>Integer</code> to a <code>Boolean</code>.
042         */
043        public static class IntegerToBoolean extends AbstractConverter<Integer, Boolean> {
044            public IntegerToBoolean() {
045                super(Integer.class, Boolean.class);
046            }
047    
048            /**
049             * Returns <code>true</code> if <code>obj</code> is non-zero, or
050             * <code>false</code> if <code>obj</code> is zero.
051             */
052            public Boolean convert(Integer obj) throws ConversionException {
053                 return obj.intValue() == 0 ? Boolean.FALSE : Boolean.TRUE;
054            }
055        }
056    
057        /**
058         * An object that converts a <code>String</code> to a <code>Boolean</code>.
059         */
060        public static class StringToBoolean extends AbstractConverter<String, Boolean> {
061            public StringToBoolean() {
062                super(String.class, Boolean.class);
063            }
064    
065            /**
066             * Returns <code>true</code> if <code>obj</code> equals "true", or
067             * <code>false</code> if <code>obj</code> is any other value. The
068             * test for "true" is case-insensitive.
069             */
070            public Boolean convert(String obj) throws ConversionException {
071                return "TRUE".equals(obj.trim().toUpperCase());
072            }
073        }
074    
075        public void loadConverters() {
076            Converters.loadContainedConverters(BooleanConverters.class);
077            Converters.registerConverter(new GenericSingletonToList<Boolean>(Boolean.class));
078            Converters.registerConverter(new GenericSingletonToSet<Boolean>(Boolean.class));
079            Converters.registerConverter(new GenericToStringConverter<Boolean>(Boolean.class));
080        }
081    }