Author: desruisseaux Date: Fri Mar 8 14:13:25 2013 New Revision: 1454395 URL: http://svn.apache.org/r1454395 Log: Added IP review for CollectionConverter. Added: sis/ip-review/CollectionConverter.xhtml (with props) sis/ip-review/rev/27557/ sis/ip-review/rev/27557/CollectionConverter.xhtml (with props) sis/ip-review/rev/27557/HEADER.html (with props) Added: sis/ip-review/CollectionConverter.xhtml URL: http://svn.apache.org/viewvc/sis/ip-review/CollectionConverter.xhtml?rev=1454395&view=auto ============================================================================== --- sis/ip-review/CollectionConverter.xhtml (added) +++ sis/ip-review/CollectionConverter.xhtml Fri Mar 8 14:13:25 2013 @@ -0,0 +1,35 @@ + + + + + CollectionConverter history + + + +
+

CollectionConverter history

+

Click on the commit message for inspecting the diff and how the code has been rewritten.

+

Command line:

+
svn log -r31996:27557 http://svn.osgeo.org/geotools/trunk/modules/library/main/src/main/java/org/geotools/util/CollectionConverterFactory.java
+ + + + + + + + + + + + + + + + +
Rev.DateAuthorMessage
306482008-06-12acusterCopyright headers: lib/main, this time with feeling (and the el in Toolkit)
305582008-06-06acusterCopyright review: update headers on library/main
305112008-06-04acusterHeader cleanup 0 -- prep for Cedric's script --- modules/library: resolve conflicts; add missing
302582008-05-08acusterReshuffle the top level repo: drop uDig, move up trunk, tags, and branches.
302572008-05-08acusterMove trunk/gt/ directory contents up to trunk/ and drop gt
289222008-01-24acusterBump the (at)since version to 2.5 since WKTParser was cut from 2.4
285402007-12-29acusterHide buttons which are not yet used
275722007-10-22desruisseauxApplied svn properties prior to GEOT-1516.
275572007-10-19jdeoliveadded converter for converting between various collection and array types
+
+ + Propchange: sis/ip-review/CollectionConverter.xhtml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sis/ip-review/CollectionConverter.xhtml ------------------------------------------------------------------------------ svn:mime-type = text/html Added: sis/ip-review/rev/27557/CollectionConverter.xhtml URL: http://svn.apache.org/viewvc/sis/ip-review/rev/27557/CollectionConverter.xhtml?rev=1454395&view=auto ============================================================================== --- sis/ip-review/rev/27557/CollectionConverter.xhtml (added) +++ sis/ip-review/rev/27557/CollectionConverter.xhtml Fri Mar 8 14:13:25 2013 @@ -0,0 +1,332 @@ + + + + + CollectionConverter changes at revision 27557 + + + +
+

CollectionConverter changes 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 http://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;
+        }
+    }
+}
+
+ + Propchange: sis/ip-review/rev/27557/CollectionConverter.xhtml ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sis/ip-review/rev/27557/CollectionConverter.xhtml ------------------------------------------------------------------------------ svn:mime-type = text/html Added: sis/ip-review/rev/27557/HEADER.html URL: http://svn.apache.org/viewvc/sis/ip-review/rev/27557/HEADER.html?rev=1454395&view=auto ============================================================================== --- sis/ip-review/rev/27557/HEADER.html (added) +++ sis/ip-review/rev/27557/HEADER.html Fri Mar 8 14:13:25 2013 @@ -0,0 +1,17 @@ + + + + + Revision 27557 + + +
+

Revision 27557

+ + + + +
Author:jdeolive
Date:2007-10-19
Message:added converter for converting between various collection and array types
+
+ + Propchange: sis/ip-review/rev/27557/HEADER.html ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sis/ip-review/rev/27557/HEADER.html ------------------------------------------------------------------------------ svn:mime-type = text/html