You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2022/12/10 20:49:41 UTC

[sis] 01/02: Rollback two changes from last commit where the order of elements in the `Set` matter.

This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 11293da0ed384e3707eafeaf43fc8b958c9e2928
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Sat Dec 10 21:43:20 2022 +0100

    Rollback two changes from last commit where the order of elements in the `Set` matter.
---
 .../internal/referencing/CoordinateOperations.java |  5 +++--
 .../java/org/apache/sis/io/wkt/WKTDictionary.java  |  3 ++-
 .../apache/sis/internal/util/CollectionsExt.java   | 24 +++++++++++++++++++---
 .../org/apache/sis/internal/util/package-info.java |  2 +-
 4 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/CoordinateOperations.java b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/CoordinateOperations.java
index 018b5db1c0..7fcd3a1eae 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/CoordinateOperations.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/internal/referencing/CoordinateOperations.java
@@ -41,6 +41,7 @@ import org.apache.sis.internal.metadata.NameToIdentifier;
 import org.apache.sis.internal.system.DefaultFactories;
 import org.apache.sis.internal.system.Modules;
 import org.apache.sis.internal.system.SystemListener;
+import org.apache.sis.internal.util.CollectionsExt;
 import org.apache.sis.internal.util.Numerics;
 import org.apache.sis.util.Deprecable;
 import org.apache.sis.util.collection.Containers;
@@ -51,7 +52,7 @@ import org.apache.sis.util.collection.Containers;
  * until first needed. Contains also utility methods related to coordinate operations.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.1
+ * @version 1.4
  * @since   0.7
  * @module
  */
@@ -292,7 +293,7 @@ public final class CoordinateOperations extends SystemListener {
             indices[i] = dim;
             r &= ~(1L << dim);
         }
-        final Set<Integer> dimensions = Set.of(indices);
+        final Set<Integer> dimensions = CollectionsExt.immutableSet(true, indices);
         if (useCache) {
             synchronized (CACHE) {
                 final Set<Integer> existing = CACHE[(int) changes];
diff --git a/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTDictionary.java b/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTDictionary.java
index a3397abd62..04268b3420 100644
--- a/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTDictionary.java
+++ b/core/sis-referencing/src/main/java/org/apache/sis/io/wkt/WKTDictionary.java
@@ -149,6 +149,7 @@ public class WKTDictionary extends GeodeticAuthorityFactory {
     /**
      * Code spaces of authority codes recognized by this factory.
      * This set is computed from the {@code "ID[…]"} elements found in WKT definitions.
+     * Code spaces are sorted with most frequently used space first.
      *
      * @see #getCodeSpaces()
      */
@@ -882,7 +883,7 @@ public class WKTDictionary extends GeodeticAuthorityFactory {
     public Set<String> getCodeSpaces() {
         lock.readLock().lock();
         try {
-            return Set.copyOf(codespaces);
+            return CollectionsExt.copyPreserveOrder(codespaces);
         } finally {
             lock.readLock().unlock();
         }
diff --git a/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java b/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
index 81f8edc66a..566185a50d 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/internal/util/CollectionsExt.java
@@ -49,7 +49,7 @@ import static org.apache.sis.util.collection.Containers.hashMapCapacity;
  * bit tedious to explain, which is another indication that they should not be in public API.
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
- * @version 1.1
+ * @version 1.4
  * @since   0.3
  * @module
  */
@@ -335,14 +335,14 @@ public final class CollectionsExt extends Static {
      * sense of {@link Object#equals(Object)}, then only the last instance of the duplicated
      * values will be included in the returned set.
      *
+     * <p>This method differs from {@link Set#of(Object...)} in that it preserves element order.</p>
+     *
      * @param  <E>          the type of array elements.
      * @param  excludeNull  {@code true} for excluding the {@code null} element from the returned set.
      * @param  array        the array to copy in a set. May be {@code null} or contain null elements.
      * @return a set containing the array elements, or {@code null} if the given array was null.
      *
      * @see Collections#unmodifiableSet(Set)
-     *
-     * @todo Consider replacing by {@code Set.of(...)} in JDK9.
      */
     @SafeVarargs
     @SuppressWarnings("fallthrough")
@@ -691,6 +691,24 @@ public final class CollectionsExt extends Static {
         return list;
     }
 
+    /**
+     * Returns a unmodifiable copy of the given set, preserving order.
+     *
+     * @param  <E>   the type of elements.
+     * @param  set   the set to copy, or {@code null}.
+     * @return a copy of the given set, or {@code null} if the given set was null.
+     */
+    public static <E> Set<E> copyPreserveOrder(final Set<E> set) {
+        if (set == null) {
+            return null;
+        }
+        switch (set.size()) {
+            case 0:  return Collections.emptySet();
+            case 1:  return Collections.singleton(set.iterator().next());
+            default: return Collections.unmodifiableSet(new LinkedHashSet<>(set));
+        }
+    }
+
     /**
      * Returns a clone of the given set. This method is only intended to avoid the "unchecked cast" warning.
      *
diff --git a/core/sis-utility/src/main/java/org/apache/sis/internal/util/package-info.java b/core/sis-utility/src/main/java/org/apache/sis/internal/util/package-info.java
index 0146bec0dd..f384a4c31e 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/internal/util/package-info.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/internal/util/package-info.java
@@ -30,7 +30,7 @@
  * so some serialized classes still exist in this package.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.3
+ * @version 1.4
  * @since   0.3
  * @module
  */