You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by da...@apache.org on 2024/12/22 09:31:32 UTC

(jackrabbit-oak) 01/01: OAK-11322 : removed usage of guava Sets.intersection

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

daim pushed a commit to branch OAK-11322
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git

commit f4c56719d11e2828811f13c4d0b680d50f706dd7
Author: Rishabh Kumar <di...@adobe.com>
AuthorDate: Sun Dec 22 15:01:18 2024 +0530

    OAK-11322 : removed usage of guava Sets.intersection
---
 .../cug/impl/CugAccessControlManagerTest.java      |  3 +-
 .../oak/commons/collections/CollectionUtils.java   | 17 ++++++++
 .../commons/collections/CollectionUtilsTest.java   | 51 ++++++++++++++++++++++
 .../security/user/RepMembersConflictHandler.java   |  2 +-
 .../composite/CompositeProviderCustomMixTest.java  |  4 +-
 .../jackrabbit/oak/explorer/NodeStoreTree.java     |  3 +-
 .../jackrabbit/oak/run/DataStoreCommandTest.java   |  4 +-
 .../oak/segment/SegmentBufferWriterPoolTest.java   |  6 +--
 .../plugins/document/DocumentNodeStoreBranch.java  |  5 +--
 .../oak/plugins/document/rdb/RDBDocumentStore.java |  2 +-
 10 files changed, 80 insertions(+), 17 deletions(-)

diff --git a/oak-authorization-cug/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/cug/impl/CugAccessControlManagerTest.java b/oak-authorization-cug/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/cug/impl/CugAccessControlManagerTest.java
index cdc445031c..628d0a56a9 100644
--- a/oak-authorization-cug/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/cug/impl/CugAccessControlManagerTest.java
+++ b/oak-authorization-cug/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/cug/impl/CugAccessControlManagerTest.java
@@ -32,7 +32,6 @@ import javax.jcr.security.Privilege;
 
 import org.apache.jackrabbit.guava.common.collect.ImmutableList;
 import org.apache.jackrabbit.guava.common.collect.Iterators;
-import org.apache.jackrabbit.guava.common.collect.Sets;
 import org.apache.jackrabbit.JcrConstants;
 import org.apache.jackrabbit.api.security.JackrabbitAccessControlPolicy;
 import org.apache.jackrabbit.oak.api.ContentSession;
@@ -585,7 +584,7 @@ public class CugAccessControlManagerTest extends AbstractCugTest {
         AccessControlPolicy[] testgroupEffective = cugAccessControlManager.getEffectivePolicies(Set.of(getTestGroupPrincipal()));
         assertEquals(2, testgroupEffective.length);
 
-        assertTrue(Sets.intersection(Set.of(everyoneEffective), Set.of(testgroupEffective)).isEmpty());
+        assertTrue(CollectionUtils.intersection(Set.of(everyoneEffective), Set.of(testgroupEffective)).isEmpty());
     }
 
     @Test
diff --git a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java
index 323ee6be8d..82bcbc201d 100644
--- a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java
+++ b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java
@@ -223,6 +223,23 @@ public class CollectionUtils {
         return Stream.concat(s1.stream(), s2.stream()).collect(Collectors.toSet());
     }
 
+    /**
+     * Returns a new set containing the intersection of the two specified sets.
+     * The intersection of two sets is a set containing only the elements that are present in both sets.
+     *
+     * @param <T> the type of elements in the sets
+     * @param s1 the first set, must not be null
+     * @param s2 the second set, must not be null
+     * @return a new set containing the intersection of the two specified sets
+     * @throws NullPointerException if either of the sets is null
+     */
+    @NotNull
+    public static <T> Set<T> intersection(@NotNull final Set<T> s1, @NotNull final Set<T> s2) {
+        Objects.requireNonNull(s1);
+        Objects.requireNonNull(s2);
+        return s1.stream().filter(s2::contains).collect(Collectors.toSet());
+    }
+
     /**
      * Convert an iterable to a {@link java.util.ArrayDeque}.
      * The returning array deque is mutable and supports all optional operations.
diff --git a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java
index 9508e4e432..5ae5266b40 100644
--- a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java
+++ b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java
@@ -257,6 +257,57 @@ public class CollectionUtilsTest {
         Assert.assertEquals(expected, CollectionUtils.union(set1, set2));
     }
 
+    @Test
+    public void testIntersectionWithNonEmptySets() {
+        final Set<String> set1 = Set.of("a", "b", "c");
+        final Set<String> set2 = Set.of("b", "c", "d");
+
+        final Set<String> result = CollectionUtils.intersection(set1, set2);
+
+        final Set<String> expected = Set.of("b", "c");
+        Assert.assertEquals(expected, result);
+    }
+
+    @Test
+    public void testIntersectionWithEmptySet() {
+        final Set<String> set1 = Set.of("a", "b", "c");
+        final Set<String> set2 = Set.of();
+
+        Assert.assertEquals(Collections.EMPTY_SET, CollectionUtils.intersection(set1, set2));
+    }
+
+    @Test
+    public void testIntersectionWithBothEmptySets() {
+        final Set<String> set1 = new HashSet<>();
+        final Set<String> set2 = new HashSet<>();
+
+        Assert.assertEquals(Collections.EMPTY_SET, CollectionUtils.intersection(set1, set2));
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testIntersectionWithNullFirstSet() {
+        final Set<String> set1 = null;
+        final Set<String> set2 = Set.of("a", "b", "c");
+
+        CollectionUtils.intersection(set1, set2);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testIntersectionWithNullSecondSet() {
+        final Set<String> set1 = Set.of("a", "b", "c");
+        final Set<String> set2 = null;
+
+        CollectionUtils.intersection(set1, set2);
+    }
+
+    @Test
+    public void testIntersectionWithNoCommonElements() {
+        final Set<String> set1 = Set.of("a", "b", "c");
+        final Set<String> set2 = Set.of("d", "e", "f");
+
+        Assert.assertEquals(Collections.EMPTY_SET, CollectionUtils.intersection(set1, set2));
+    }
+
     @Test
     public void iteratorToIIteratable() {
         Iterator<String> iterator = List.of("a", "b", "c").iterator();
diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/RepMembersConflictHandler.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/RepMembersConflictHandler.java
index 97fa82bb05..fd3bb2dd87 100644
--- a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/RepMembersConflictHandler.java
+++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/RepMembersConflictHandler.java
@@ -161,7 +161,7 @@ class RepMembersConflictHandler implements ThreeWayConflictHandler {
         Set<String> ourMembers = CollectionUtils.toSet(ours.getValue(Type.STRINGS));
 
         // merge ours and theirs to a de-duplicated set
-        Set<String> combined = new LinkedHashSet<>(Sets.intersection(ourMembers, theirMembers));
+        Set<String> combined = new LinkedHashSet<>(CollectionUtils.intersection(ourMembers, theirMembers));
         for (String m : Sets.difference(ourMembers, theirMembers)) {
             if (!base.contains(m)) {
                 combined.add(m);
diff --git a/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/composite/CompositeProviderCustomMixTest.java b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/composite/CompositeProviderCustomMixTest.java
index 6407a51677..cbd93a9d2f 100644
--- a/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/composite/CompositeProviderCustomMixTest.java
+++ b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/composite/CompositeProviderCustomMixTest.java
@@ -218,9 +218,9 @@ public class CompositeProviderCustomMixTest extends AbstractSecurityTest {
         if (type == CompositionType.OR) {
             return Sets.difference(Sets.difference(check, granted1), granted2).isEmpty();
         } else {
-            Set<String> f1 = Sets.intersection(supported1, check);
+            Set<String> f1 = CollectionUtils.intersection(supported1, check);
             boolean hasf1 = granted1.containsAll(f1);
-            Set<String> f2 = Sets.intersection(supported2, check);
+            Set<String> f2 = CollectionUtils.intersection(supported2, check);
             boolean hasf2 = granted2.containsAll(f2);
             return hasf1 && hasf2;
         }
diff --git a/oak-run/src/main/java/org/apache/jackrabbit/oak/explorer/NodeStoreTree.java b/oak-run/src/main/java/org/apache/jackrabbit/oak/explorer/NodeStoreTree.java
index 6ee544075c..9ed539fbe2 100644
--- a/oak-run/src/main/java/org/apache/jackrabbit/oak/explorer/NodeStoreTree.java
+++ b/oak-run/src/main/java/org/apache/jackrabbit/oak/explorer/NodeStoreTree.java
@@ -18,7 +18,6 @@
  */
 package org.apache.jackrabbit.oak.explorer;
 
-import static org.apache.jackrabbit.guava.common.collect.Sets.intersection;
 import static org.apache.jackrabbit.guava.common.escape.Escapers.builder;
 import static java.util.Collections.sort;
 import static javax.jcr.PropertyType.BINARY;
@@ -413,7 +412,7 @@ class NodeStoreTree extends JPanel implements TreeSelectionListener, Closeable {
             }
         }
 
-        Set<UUID> inMem = intersection(backend.getReferencedSegmentIds(), uuids);
+        Set<UUID> inMem = CollectionUtils.intersection(backend.getReferencedSegmentIds(), uuids);
         if (!inMem.isEmpty()) {
             sb.append("In Memory segment references: ");
             sb.append(newline);
diff --git a/oak-run/src/test/java/org/apache/jackrabbit/oak/run/DataStoreCommandTest.java b/oak-run/src/test/java/org/apache/jackrabbit/oak/run/DataStoreCommandTest.java
index f0cc1d8d1f..0ac5bdd757 100644
--- a/oak-run/src/test/java/org/apache/jackrabbit/oak/run/DataStoreCommandTest.java
+++ b/oak-run/src/test/java/org/apache/jackrabbit/oak/run/DataStoreCommandTest.java
@@ -44,8 +44,6 @@ import org.apache.jackrabbit.guava.common.base.Splitter;
 import org.apache.jackrabbit.guava.common.base.Strings;
 import org.apache.jackrabbit.guava.common.collect.ImmutableList;
 import org.apache.jackrabbit.guava.common.collect.Iterators;
-import org.apache.jackrabbit.guava.common.collect.Lists;
-import org.apache.jackrabbit.guava.common.collect.Maps;
 import org.apache.jackrabbit.guava.common.collect.Sets;
 import joptsimple.OptionException;
 import org.apache.commons.io.FileUtils;
@@ -753,7 +751,7 @@ public class DataStoreCommandTest {
         if (!markOnly) {
             // Verbose would have paths as well as ids changed but normally only DocumentNS would have paths suffixed
             assertFileEquals(dump, "gccand-", verbose ?
-                    encodedIdsAndPath(verboseRootPath ? Sets.intersection(data.addedSubset, data.missingDataStore) :
+                    encodedIdsAndPath(verboseRootPath ? CollectionUtils.intersection(data.addedSubset, data.missingDataStore) :
                             data.missingDataStore, blobFixture.getType(), data.idToPath, true) :
                     (storeFixture instanceof StoreFixture.MongoStoreFixture) ?
                             encodedIdsAndPath(data.missingDataStore, blobFixture.getType(), data.idToPath, false) :
diff --git a/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterPoolTest.java b/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterPoolTest.java
index a894cbf966..7bc95cdac7 100644
--- a/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterPoolTest.java
+++ b/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterPoolTest.java
@@ -19,7 +19,6 @@
 
 package org.apache.jackrabbit.oak.segment;
 
-import static org.apache.jackrabbit.guava.common.collect.Sets.intersection;
 import static org.apache.jackrabbit.guava.common.util.concurrent.Uninterruptibles.sleepUninterruptibly;
 import static java.util.concurrent.Executors.newSingleThreadExecutor;
 import static java.util.concurrent.TimeUnit.MILLISECONDS;
@@ -38,6 +37,7 @@ import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeoutException;
 
+import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
 import org.apache.jackrabbit.oak.segment.WriteOperationHandler.WriteOperation;
 import org.apache.jackrabbit.oak.segment.file.tar.GCGeneration;
 import org.apache.jackrabbit.oak.segment.memory.MemoryStore;
@@ -159,7 +159,7 @@ public class SegmentBufferWriterPoolTest {
         assertEquals(rootId, res5.get());
         assertEquals(rootId, res6.get());
         assertEquals(3, map2.size());
-        assertTrue(intersection(new HashSet<>(map1.values()), new HashSet<>(map2.values())).isEmpty());
+        assertTrue(CollectionUtils.intersection(new HashSet<>(map1.values()), new HashSet<>(map2.values())).isEmpty());
     }
 
     @Test
@@ -209,7 +209,7 @@ public class SegmentBufferWriterPoolTest {
         assertEquals(rootId, res8.get());
         assertEquals(rootId, res9.get());
         assertEquals(3, map3.size());
-        assertTrue(intersection(new HashSet<>(map1.values()), new HashSet<>(map3.values())).isEmpty());
+        assertTrue(CollectionUtils.intersection(new HashSet<>(map1.values()), new HashSet<>(map3.values())).isEmpty());
     }
 
     @Test
diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java
index ce43bb6f1e..0afb88244b 100644
--- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java
+++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java
@@ -32,9 +32,8 @@ import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReadWriteLock;
 
 import org.apache.jackrabbit.guava.common.collect.Iterables;
-import org.apache.jackrabbit.guava.common.collect.Sets;
-
 import org.apache.jackrabbit.oak.api.CommitFailedException;
+import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
 import org.apache.jackrabbit.oak.plugins.document.util.Utils;
 import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeBuilder;
 import org.apache.jackrabbit.oak.spi.commit.CommitHook;
@@ -752,7 +751,7 @@ class DocumentNodeStoreBranch implements NodeStoreBranch {
             Set<Revision> collisions = new HashSet<>(doc.getLocalMap(COLLISIONS).keySet());
             Set<Revision> commits = new HashSet<>();
             Iterables.transform(b.getCommits(), Revision::asTrunkRevision).forEach(commits::add);
-            Set<Revision> conflicts = Sets.intersection(collisions, commits);
+            Set<Revision> conflicts = CollectionUtils.intersection(collisions, commits);
             if (!conflicts.isEmpty()) {
                 throw new CommitFailedException(STATE, 2,
                         "Conflicting concurrent change on branch commits " + conflicts);
diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
index 186295ad51..d08345d556 100755
--- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
+++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java
@@ -1831,7 +1831,7 @@ public class RDBDocumentStore implements DocumentStore {
             final List<String> excludeKeyPatterns, final List<QueryCondition> conditions, final int limit, final String sortBy) {
 
         final RDBTableMetaData tmd = getTable(collection);
-        Set<String> allowedProps = Sets.intersection(INDEXEDPROPERTIES, tmd.getColumnProperties());
+        Set<String> allowedProps = CollectionUtils.intersection(INDEXEDPROPERTIES, tmd.getColumnProperties());
         for (QueryCondition cond : conditions) {
             if (!allowedProps.contains(cond.getPropertyName())) {
                 String message = "indexed property " + cond.getPropertyName() + " not supported, query was '" + cond