You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2020/01/31 11:50:24 UTC

[isis] branch master updated: ISIS-2158: _Sets.minus(...) add variant for preserving order

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

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new 524bd01  ISIS-2158: _Sets.minus(...) add variant for preserving order
524bd01 is described below

commit 524bd01059383cb715af3d3be45d245937be2158
Author: Andi Huber <ah...@apache.org>
AuthorDate: Fri Jan 31 12:50:15 2020 +0100

    ISIS-2158: _Sets.minus(...) add variant for preserving order
---
 .../core/commons/internal/collections/_Sets.java   | 58 ++++++++++++++++++++--
 .../services/grid/GridSystemServiceAbstract.java   |  5 +-
 2 files changed, 56 insertions(+), 7 deletions(-)

diff --git a/core/commons/src/main/java/org/apache/isis/core/commons/internal/collections/_Sets.java b/core/commons/src/main/java/org/apache/isis/core/commons/internal/collections/_Sets.java
index 09a086f..4662062 100644
--- a/core/commons/src/main/java/org/apache/isis/core/commons/internal/collections/_Sets.java
+++ b/core/commons/src/main/java/org/apache/isis/core/commons/internal/collections/_Sets.java
@@ -43,6 +43,9 @@ import org.apache.isis.core.commons.internal.base._With;
 
 import static org.apache.isis.core.commons.internal.functions._Predicates.not;
 
+import lombok.NonNull;
+import lombok.val;
+
 /**
  * <h1>- internal use only -</h1>
  * <p>
@@ -246,7 +249,7 @@ public final class _Sets {
                 .filter(b::contains)
                 .collect(toUnmodifiableSorted());
     }
-
+    
     /**
      * Returns a new set containing all the elements of {@code a} that are not in {@code b}, 
      * not retaining any order. 
@@ -257,16 +260,49 @@ public final class _Sets {
      * @return {@code a - b}, non null, unmodifiable 
      */
     public static <T> Set<T> minus(@Nullable Set<T> a, @Nullable Set<T> b) {
+        return minus(a, b, HashSet::new);
+    }
+    
+    /**
+     * Returns a new (sorted) set containing all the elements of {@code a} that are not in {@code b}, 
+     * retaining order only when natural order. 
+     * Any {@code null} elements are ignored and will not be contained in the resulting set.
+     * @param <T>
+     * @param a
+     * @param b
+     * @return {@code a - b}, non null, unmodifiable 
+     */
+    public static <T> SortedSet<T> minusSorted(@Nullable SortedSet<T> a, @Nullable SortedSet<T> b) {
+        return minusSorted(a, b, TreeSet::new);
+    }
+
+    /**
+     * Returns a new set containing all the elements of {@code a} that are not in {@code b}, 
+     * not retaining any order. 
+     * Any {@code null} elements are ignored and will not be contained in the resulting set.
+     * @param <T>
+     * @param a
+     * @param b
+     * @param collectionFactory
+     * @return {@code a - b}, non null, unmodifiable 
+     */
+    public static <T> Set<T> minus(
+            @Nullable Set<T> a, 
+            @Nullable Set<T> b, 
+            @NonNull Supplier<Set<T>> collectionFactory) {
+        
         if(a==null || a.isEmpty()) {
             return Collections.emptySet();
         }
         if(b==null || b.isEmpty()) {
-            return Collections.unmodifiableSet(new HashSet<>(a));
+            val copy = collectionFactory.get();
+            copy.addAll(a);
+            return Collections.unmodifiableSet(copy);
         }
         return a.stream()
                 .filter(Objects::nonNull)
                 .filter(not(b::contains))
-                .collect(toUnmodifiable());
+                .collect(toUnmodifiable(collectionFactory));
     }
     
     /**
@@ -276,14 +312,21 @@ public final class _Sets {
      * @param <T>
      * @param a
      * @param b
+     * @param collectionFactory
      * @return {@code a - b}, non null, unmodifiable 
      */
-    public static <T> SortedSet<T> minusSorted(@Nullable SortedSet<T> a, @Nullable SortedSet<T> b) {
+    public static <T> SortedSet<T> minusSorted(
+            @Nullable SortedSet<T> a, 
+            @Nullable SortedSet<T> b, 
+            @NonNull Supplier<SortedSet<T>> collectionFactory) {
+        
         if(a==null || a.isEmpty()) {
             return Collections.emptySortedSet();
         }
         if(b==null || b.isEmpty()) {
-            return Collections.unmodifiableSortedSet(new TreeSet<>(a));
+            val copy = collectionFactory.get();
+            copy.addAll(a);
+            return Collections.unmodifiableSortedSet(copy);
         }
         return a.stream()
                 .filter(Objects::nonNull)
@@ -306,6 +349,11 @@ public final class _Sets {
     }
     
     public static <T> 
+    Collector<T, ?, Set<T>> toUnmodifiablePreservingOrder() {
+        return toUnmodifiable(LinkedHashSet::new);
+    }
+    
+    public static <T> 
     Collector<T, ?, SortedSet<T>> toUnmodifiableSorted(Supplier<SortedSet<T>> collectionFactory) {
         return Collectors.collectingAndThen(
                 Collectors.toCollection(collectionFactory), 
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/grid/GridSystemServiceAbstract.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/grid/GridSystemServiceAbstract.java
index 4d3e390..12a3e14 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/grid/GridSystemServiceAbstract.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/grid/GridSystemServiceAbstract.java
@@ -19,6 +19,7 @@
 package org.apache.isis.core.metamodel.services.grid;
 
 import java.util.Comparator;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -383,8 +384,8 @@ implements GridSystemService<G> {
     }
 
     protected static SurplusAndMissing surplusAndMissing(final Set<String> first, final Set<String> second){
-        val firstNotSecond = _Sets.minus(first, second);
-        val secondNotFirst = _Sets.minus(second, first);
+        val firstNotSecond = _Sets.minus(first, second, LinkedHashSet::new); // preserve order
+        val secondNotFirst = _Sets.minus(second, first, LinkedHashSet::new); // preserve order
         return SurplusAndMissing.of(firstNotSecond, secondNotFirst);
     }