StringConverter.Boolean at revision 23637

While the idea to provide a converter from String to Boolean was found in GeoTools (as well as many other libraries), the code has been totally rewritten. It still shares in common with the original code a sequence of if (source.equals("foo")) statements, but this very simple logic can hardly be written in an other way.

The table below compares the GeoTools code with the Geotoolkit.org one.

Command line:

svn cat -r23637 https://svn.osgeo.org/geotools/trunk/modules/library/main/src/main/java/org/geotools/util/BooleanConverterFactory.java
Revision 23637Geotoolkit.org
/*
 *    GeoTools - OpenSource mapping toolkit
 *    http://geotools.org
 *    (C) 2002-2006, GeoTools Project Managment Committee (PMC)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.util;

import org.geotools.factory.Hints;

/**
 * ConverterFactory for handling boolean conversions.
 * <p>
 * Supported conversions:
 * <ul>
 *  <li>"true" -> Boolean.TRUE
 *  <li>"false" -> Boolean.FALSE
 *  <li>"1" -> Boolean.TRUE
 *  <li>"0" -> Boolean.FALSE
 *  <li>1 -> Boolean.TRUE
 *  <li>0 -> Boolean.FALSE
 * </ul>
 * </p>
 * @author Justin Deoliveira, The Open Planning Project
 * @since 2.4
 */
public class BooleanConverterFactory implements ConverterFactory {

    public Converter createConverter(Class source, Class target, Hints hints) {
        if ( target.equals( Boolean.class ) ) {

            //string to boolean
            if ( source.equals( String.class ) ) {
                return new Converter() {

                    public Object convert(Object source, Class target) throws Exception {
                        if ( "true".equals( source ) || "1".equals( source ) ) {
                                return Boolean.TRUE;
                        }
                        if ( "false".equals( source ) || "0".equals( source ) ) {
                                return Boolean.FALSE;
                        }

                        return null;
                    }

                };
            }

            //integer to boolean
            if ( source.equals( Integer.class ) ) {
                return new Converter() {

                    public Object convert(Object source, Class target) throws Exception {
                        if ( new Integer( 1 ).equals( source ) ) {
                                return Boolean.TRUE;
                        }
                        if ( new Integer( 0 ).equals( source ) ) {
                                return Boolean.FALSE;
                        }

                        return null;
                    }

                };
            }

        }

        return null;
    }

}
/**
 * Converter from {@link java.lang.String} to {@link java.lang.Boolean}.
 * Conversion table:
 *
 * <table>
 *    <tr><th>source</th>          <th>target</th></tr>
 *    <tr><td>{@code "true"}  </td><td>{@link java.lang.Boolean#TRUE}  </td></tr>
 *    <tr><td>{@code "false"} </td><td>{@link java.lang.Boolean#FALSE} </td></tr>
 *    <tr><td>{@code "yes"}   </td><td>{@link java.lang.Boolean#TRUE}  </td></tr>
 *    <tr><td>{@code "no"}    </td><td>{@link java.lang.Boolean#FALSE} </td></tr>
 *    <tr><td>{@code "on"}    </td><td>{@link java.lang.Boolean#TRUE}  </td></tr>
 *    <tr><td>{@code "off"}   </td><td>{@link java.lang.Boolean#FALSE} </td></tr>
 *    <tr><td>{@code "1"}     </td><td>{@link java.lang.Boolean#TRUE}  </td></tr>
 *    <tr><td>{@code "0"}     </td><td>{@link java.lang.Boolean#FALSE} </td></tr>
 * </table>
 *
 * @author Justin Deoliveira (TOPP)
 * @author Martin Desruisseaux (Geomatys)
 * @version 3.00
 *
 * @since 2.4
 */
@Immutable
static final class Boolean extends StringConverter<java.lang.Boolean> {
    private static final long serialVersionUID = -27525398425996373L;
    public static final Boolean INSTANCE = new Boolean();
    private Boolean() {
    }

    @Override
    public Class<java.lang.Boolean> getTargetClass() {
        return java.lang.Boolean.class;
    }

    @Override
    public java.lang.Boolean convert(String source) throws NonconvertibleObjectException {
        if (source == null) {
            return null;
        }
        source = source.trim();
        if (source.equalsIgnoreCase("true") ||
            source.equalsIgnoreCase("yes")  ||
            source.equalsIgnoreCase("on"))
        {
            return java.lang.Boolean.TRUE;
        }
        if (source.equalsIgnoreCase("false") ||
            source.equalsIgnoreCase("no")    ||
            source.equalsIgnoreCase("off"))
        {
            return java.lang.Boolean.FALSE;
        }
        final int n;
        try {
            n = java.lang.Integer.parseInt(source);
        } catch (NumberFormatException e) {
            throw new NonconvertibleObjectException(e);
        }
        return java.lang.Boolean.valueOf(n != 0);
    }

    /** Returns the singleton instance on deserialization. */
    protected Object readResolve() throws ObjectStreamException {
        return INSTANCE;
    }
}