sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1467466 - in /sis/branches/JDK7: sis-metadata/src/main/java/org/apache/sis/metadata/Cloner.java sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
Date Fri, 12 Apr 2013 21:16:42 GMT
Author: desruisseaux
Date: Fri Apr 12 21:16:41 2013
New Revision: 1467466

URL: http://svn.apache.org/r1467466
Log:
immutableSet as an internal method (research in Geotk code base suggests that it will be used
again later).

Modified:
    sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/Cloner.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java

Modified: sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/Cloner.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/Cloner.java?rev=1467466&r1=1467465&r2=1467466&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/Cloner.java [UTF-8]
(original)
+++ sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/Cloner.java [UTF-8]
Fri Apr 12 21:16:41 2013
@@ -16,17 +16,14 @@
  */
 package org.apache.sis.metadata;
 
-import java.util.Arrays;
 import java.util.Map;
 import java.util.Set;
 import java.util.Iterator;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-
 import org.apache.sis.util.collection.Containers;
-import static org.apache.sis.internal.util.CollectionsExt.unmodifiableOrCopy;
+import org.apache.sis.internal.util.CollectionsExt;
 
 
 /**
@@ -104,11 +101,7 @@ final class Cloner extends org.apache.si
                 // Do not use the SIS Checked* classes since we don't
                 // need synchronization or type checking anymore.
                 if (isSet) {
-                    switch (array.length) {
-                        case 0:  collection = Collections.emptySet(); break;
-                        case 1:  collection = Collections.singleton(array[0]); break;
-                        default: collection = Collections.unmodifiableSet(new LinkedHashSet<>(Arrays.asList(array)));
break;
-                    }
+                    collection = CollectionsExt.immutableSet(array);
                 } else {
                     // Conservatively assumes a List if we are not sure to have a Set,
                     // since the list is less destructive (no removal of duplicated).
@@ -127,7 +120,7 @@ final class Cloner extends org.apache.si
                 final Map.Entry<Object,Object> entry = it.next();
                 entry.setValue(clone(entry.getValue()));
             }
-            return unmodifiableOrCopy(map);
+            return CollectionsExt.unmodifiableOrCopy(map);
         }
         /*
          * CASE 4 - The object is presumed cloneable.

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java?rev=1467466&r1=1467465&r2=1467466&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
[UTF-8] Fri Apr 12 21:16:41 2013
@@ -83,6 +83,30 @@ public final class CollectionsExt extend
     }
 
     /**
+     * Returns the specified array as an immutable set, or {@code null} if the array is null.
+     * If the given array contains duplicated elements, i.e. elements that are equal in the
+     * sense of {@link Object#equals(Object)}, then only the last instance of the duplicated
+     * values will be included in the returned set.
+     *
+     * @param  <E> The type of array elements.
+     * @param  array The array to copy in a set. May be {@code null}.
+     * @return A set containing the array elements, or {@code null} if the given array was
null.
+     *
+     * @see Collections#unmodifiableSet(Set)
+     */
+    @SafeVarargs
+    public static <E> Set<E> immutableSet(final E... array) {
+        if (array == null) {
+            return null;
+        }
+        switch (array.length) {
+            case 0:  return Collections.emptySet();
+            case 1:  return Collections.singleton(array[0]);
+            default: return Collections.unmodifiableSet(new LinkedHashSet<>(Arrays.asList(array)));
+        }
+    }
+
+    /**
      * Returns a unmodifiable version of the given set.
      * This method is different than the standard {@link Collections#unmodifiableSet(Set)}
      * in that it tries to returns a more efficient object when there is zero or one element.



Mime
View raw message