StringConverter.Charset changes for revisions 30614:30615

While the idea to provide a converter from String to Charset was found in GeoTools, the code has been rewritten using a different strategy (types are resolved more accurately when searching for a converter, and the various kind of conversions are separated in different convert(S) methods, so there is no sequences of ifelse statements). The only common code is the following line, inside a trycatch block performing a different work in case of failure:

return Charset.forName(source);

There is no reasonable way to write the above line differently. The table below compares the GeoTools code with the Geotoolkit.org one.

Command line:

svn cat -r30615 https://svn.osgeo.org/geotools/trunk/modules/library/main/src/main/java/org/geotools/util/CharsetConverterFactory.java
Revision 30615Geotoolkit.org
package org.geotools.util;

import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;

import org.geotools.factory.Hints;

/**
 * Converter for going from a String to a {@link Charset} and vice versa.
 *
 * @author Justin Deoliveira, The Open Planning Project
 * @since 2.5
 */
public class CharsetConverterFactory implements ConverterFactory {

    public Converter createConverter(Class<?> source, Class<?> target,
            Hints hints) {

        if ( CharSequence.class.isAssignableFrom( source ) &&
                Charset.class.isAssignableFrom( target ) ) {
            return new Converter() {
                public <T> T convert(Object source, Class<T> target) throws Exception {
                    try {
                        return(T) Charset.forName( (String) source );
                    }
                    catch( UnsupportedCharsetException e ) {
                        //TODO: log this
                        return null;
                    }
                }
            };
        }
        if ( Charset.class.isAssignableFrom( source ) &&
                CharSequence.class.isAssignableFrom( target ) ) {
            return new Converter() {
                public <T> T convert(Object source, Class<T> target) throws Exception {
                    return (T) ((Charset)source).toString();
                }

            };
        }

        return null;
    }

}
/**
 * Converter from {@link java.lang.String} to {@link java.nio.charset.Charset}.
 *
 * @author Justin Deoliveira (TOPP)
 * @author Martin Desruisseaux (Geomatys)
 * @version 3.02
 *
 * @since 2.4
 */
@Immutable
static final class Charset extends StringConverter<java.nio.charset.Charset> {
    private static final long serialVersionUID = 4539755855992944656L;
    public static final Charset INSTANCE = new Charset();
    private Charset() {
    }

    @Override
    public Class<java.nio.charset.Charset> getTargetClass() {
        return java.nio.charset.Charset.class;
    }

    @Override
    public java.nio.charset.Charset convert(String source) throws NonconvertibleObjectException {
        if (source == null) {
            return null;
        }
        source = source.trim();
        try {
            return java.nio.charset.Charset.forName(source);
        }
        catch (UnsupportedCharsetException e) {
            throw new NonconvertibleObjectException(e);
        }
    }

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