CollectionConverter at revision 27557

While the idea to provide a converter from collection to other collection types was found in GeoTools, the code has been totally rewritten and share nothing in common. The table below compares the GeoTools code with the Geotoolkit.org one.

Command line:

svn cat -r27557 https://svn.osgeo.org/geotools/trunk/modules/library/main/src/main/java/org/geotools/util/CollectionConverterFactory.java
Revision 27557Geotoolkit.org
package org.geotools.util;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.geotools.factory.Hints;

/**
 * Converts among arrays and different collection classes.
 * <p>
 * THe following conversions are supported:
 * <ul>
 *   <li>Collection to Collection where collections are different types ( ex list to set )
 *   <li>Collection to Array
 *   <li>Array to Collection
 *   <li>Array to Array where the declared type of the target array is assignable from
 *   the declared type of the source array
 * </ul>
 * </p>
 *
 * @author Justin Deoliveira, The Open Planning Project
 *
 */
public class CollectionConverterFactory implements ConverterFactory {

    /**
     * Converter for collection to collection
     */
    protected static final Converter CollectionToCollection = new Converter() {

        public Object convert(Object source, Class target) throws Exception {
            //if source is already an instance nevermind
            if ( target.isInstance( source ) ) {
                return source;
            }

            //dynamically create and add
            Collection converted = newCollection( target );
            if ( converted != null ) {
                converted.addAll( (Collection) source );
            }

            return converted;
        }

    };

    /**
     * Converter for collection to array.
     */
    protected static final Converter CollectionToArray = new Converter() {

        public Object convert(Object source, Class target) throws Exception {
            Collection s = (Collection) source;
            Object array = Array.newInstance(target.getComponentType(), s.size());

            try {
                int x = 0;
                for ( Iterator i = s.iterator(); i.hasNext(); x++ ) {
                    Array.set( array, x, i.next() );
                }

                return array;
            }
            catch( Exception e ) {
                //Means an incompatable type assignment

            }

            return null;
        }

    };

    /**
     * Converter for array to collection.
     */
    protected static final Converter ArrayToCollection = new Converter() {

        public Object convert(Object source, Class target) throws Exception {
            Collection collection = newCollection(target);
            if ( collection != null ) {
                int length = Array.getLength(source);
                for ( int i = 0; i < length; i++ ) {
                    collection.add( Array.get( source, i) );
                }
            }

            return collection;
        }

    };

    /**
     * Converter for array to array.
     */
    protected static final Converter ArrayToArray = new Converter() {

        public Object convert(Object source, Class target) throws Exception {
          //get the individual component types
            Class s = source.getClass().getComponentType();
            Class t = target.getComponentType();

            //make sure the source can be assiigned to the target
            if ( t.isAssignableFrom(s) ) {
                int length = Array.getLength(source);
                Object converted = Array.newInstance( t, length );

                for ( int i = 0; i < length; i++ ) {
                    Array.set(converted, i, Array.get( source, i ) );
                }

                return converted;
            }

            return null;
        }
    };

    protected static Collection newCollection( Class target ) throws Exception {
        if ( target.isInterface() ) {
            //try the common ones
            if ( List.class.isAssignableFrom( target ) ) {
                return new ArrayList();
            }
            if ( SortedSet.class.isAssignableFrom( target ) ) {
                return new TreeSet();
            }
            else if ( Set.class.isAssignableFrom( target ) ) {
                return new HashSet();
            }

            //could not figure out
            return null;
        }
        else {
            //instantiate directly
            return (Collection) target.newInstance();
        }
    }
    public Converter createConverter(Class source, Class target, Hints hints) {
        if ( ( Collection.class.isAssignableFrom( source ) || source.isArray() )
            && ( Collection.class.isAssignableFrom( target ) || target.isArray() ) ) {

            //both collections?
            if ( Collection.class.isAssignableFrom( source ) &&
                    Collection.class.isAssignableFrom( target ) ) {
               return CollectionToCollection;
            }

            //both arrays?
            if ( source.getClass().isArray() && target.isArray() ) {
                return ArrayToArray;
            }

            //collection to array?
            if ( Collection.class.isAssignableFrom( source ) && target.isArray() ) {
               return CollectionToArray;
            }

            //array to collection?
            if ( source.getClass().isArray() && Collection.class.isAssignableFrom( target ) ) {
                return ArrayToCollection;
            }
        }

        return null;
    }
}
/*
 *    Geotoolkit.org - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2009-2012, Open Source Geospatial Foundation (OSGeo)
 *    (C) 2009-2012, Geomatys
 *
 *    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.geotoolkit.util.converter;

import java.util.Collection;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.io.Serializable;
import java.io.ObjectStreamException;
import net.jcip.annotations.Immutable;


/**
 * Handles conversions from {@link java.util.Collection} to various objects.
 *
 * @author Martin Desruisseaux (Geomatys)
 * @version 3.02
 *
 * @since 3.02
 * @module
 */
@Immutable
abstract class CollectionConverter<T> extends SimpleConverter<Collection<?>,T> implements Serializable {
    /**
     * For cross-version compatibility.
     */
    private static final long serialVersionUID = -4515250904953131514L;

    /**
     * Returns the source class, which is always {@link String}.
     */
    @Override
    @SuppressWarnings({"unchecked","rawtypes"})
    public final Class<Collection<?>> getSourceClass() {
        return (Class) Collection.class;
    }


    /**
     * Converter from {@link java.util.Collection} to {@link java.util.List}.
     *
     * @author Martin Desruisseaux (Geomatys)
     * @version 3.02
     *
     * @since 3.02
     */
    @Immutable
    static final class List extends CollectionConverter<java.util.List<?>> {
        private static final long serialVersionUID = 5492247760609833586L;
        public static final List INSTANCE = new List();
        private List() {
        }

        @Override
        @SuppressWarnings({"unchecked","rawtypes"})
        public Class<java.util.List<?>> getTargetClass() {
            return (Class) java.util.List.class;
        }

        @Override
        public java.util.List<?> convert(final Collection<?> source) {
            if (source == null) {
                return null;
            }
            if (source instanceof java.util.List<?>) {
                return (java.util.List<?>) source;
            }
            return new ArrayList<>(source);
        }

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


    /**
     * Converter from {@link java.util.Collection} to {@link java.util.Set}.
     *
     * @author Martin Desruisseaux (Geomatys)
     * @version 3.02
     *
     * @since 3.02
     */
    @Immutable
    static final class Set extends CollectionConverter<java.util.Set<?>> {
        private static final long serialVersionUID = -4200659837453206164L;
        public static final Set INSTANCE = new Set();
        private Set() {
        }

        @Override
        @SuppressWarnings({"unchecked","rawtypes"})
        public Class<java.util.Set<?>> getTargetClass() {
            return (Class) java.util.Set.class;
        }

        @Override
        public java.util.Set<?> convert(final Collection<?> source) {
            if (source == null) {
                return null;
            }
            if (source instanceof java.util.Set<?>) {
                return (java.util.Set<?>) source;
            }
            return new LinkedHashSet<>(source);
        }

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