Skip to content

Commit eed00e0

Browse files
authored
Merge pull request #1919 from apache/OAK-11322
OAK-11322 : removed usage of guava Sets.intersection
2 parents c5222f7 + 2403944 commit eed00e0

File tree

10 files changed

+80
-17
lines changed

10 files changed

+80
-17
lines changed

oak-authorization-cug/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/cug/impl/CugAccessControlManagerTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import javax.jcr.security.Privilege;
3232

3333
import org.apache.jackrabbit.guava.common.collect.Iterators;
34-
import org.apache.jackrabbit.guava.common.collect.Sets;
3534
import org.apache.jackrabbit.JcrConstants;
3635
import org.apache.jackrabbit.api.security.JackrabbitAccessControlPolicy;
3736
import org.apache.jackrabbit.oak.api.ContentSession;
@@ -584,7 +583,7 @@ public void testGetEffectivePrincipalDistributed() throws Exception {
584583
AccessControlPolicy[] testgroupEffective = cugAccessControlManager.getEffectivePolicies(Set.of(getTestGroupPrincipal()));
585584
assertEquals(2, testgroupEffective.length);
586585

587-
assertTrue(Sets.intersection(Set.of(everyoneEffective), Set.of(testgroupEffective)).isEmpty());
586+
assertTrue(CollectionUtils.intersection(Set.of(everyoneEffective), Set.of(testgroupEffective)).isEmpty());
588587
}
589588

590589
@Test

oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,23 @@ public static <T> Set<T> union(@NotNull final Set<T> s1, @NotNull final Set<T> s
223223
return Stream.concat(s1.stream(), s2.stream()).collect(Collectors.toSet());
224224
}
225225

226+
/**
227+
* Returns a new set containing the intersection of the two specified sets.
228+
* The intersection of two sets is a set containing only the elements that are present in both sets.
229+
*
230+
* @param <T> the type of elements in the sets
231+
* @param s1 the first set, must not be null
232+
* @param s2 the second set, must not be null
233+
* @return a new set containing the intersection of the two specified sets
234+
* @throws NullPointerException if either of the sets is null
235+
*/
236+
@NotNull
237+
public static <T> Set<T> intersection(@NotNull final Set<T> s1, @NotNull final Set<T> s2) {
238+
Objects.requireNonNull(s1);
239+
Objects.requireNonNull(s2);
240+
return s1.stream().filter(s2::contains).collect(Collectors.toSet());
241+
}
242+
226243
/**
227244
* Convert an iterable to a {@link java.util.ArrayDeque}.
228245
* The returning array deque is mutable and supports all optional operations.

oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,57 @@ public void testUnionWithOverlappingSets() {
257257
Assert.assertEquals(expected, CollectionUtils.union(set1, set2));
258258
}
259259

260+
@Test
261+
public void testIntersectionWithNonEmptySets() {
262+
final Set<String> set1 = Set.of("a", "b", "c");
263+
final Set<String> set2 = Set.of("b", "c", "d");
264+
265+
final Set<String> result = CollectionUtils.intersection(set1, set2);
266+
267+
final Set<String> expected = Set.of("b", "c");
268+
Assert.assertEquals(expected, result);
269+
}
270+
271+
@Test
272+
public void testIntersectionWithEmptySet() {
273+
final Set<String> set1 = Set.of("a", "b", "c");
274+
final Set<String> set2 = Set.of();
275+
276+
Assert.assertEquals(Collections.EMPTY_SET, CollectionUtils.intersection(set1, set2));
277+
}
278+
279+
@Test
280+
public void testIntersectionWithBothEmptySets() {
281+
final Set<String> set1 = new HashSet<>();
282+
final Set<String> set2 = new HashSet<>();
283+
284+
Assert.assertEquals(Collections.EMPTY_SET, CollectionUtils.intersection(set1, set2));
285+
}
286+
287+
@Test(expected = NullPointerException.class)
288+
public void testIntersectionWithNullFirstSet() {
289+
final Set<String> set1 = null;
290+
final Set<String> set2 = Set.of("a", "b", "c");
291+
292+
CollectionUtils.intersection(set1, set2);
293+
}
294+
295+
@Test(expected = NullPointerException.class)
296+
public void testIntersectionWithNullSecondSet() {
297+
final Set<String> set1 = Set.of("a", "b", "c");
298+
final Set<String> set2 = null;
299+
300+
CollectionUtils.intersection(set1, set2);
301+
}
302+
303+
@Test
304+
public void testIntersectionWithNoCommonElements() {
305+
final Set<String> set1 = Set.of("a", "b", "c");
306+
final Set<String> set2 = Set.of("d", "e", "f");
307+
308+
Assert.assertEquals(Collections.EMPTY_SET, CollectionUtils.intersection(set1, set2));
309+
}
310+
260311
@Test
261312
public void iteratorToIIteratable() {
262313
Iterator<String> iterator = List.of("a", "b", "c").iterator();

oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/RepMembersConflictHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private static void mergeChange(@NotNull NodeBuilder parent, @NotNull PropertySt
161161
Set<String> ourMembers = CollectionUtils.toSet(ours.getValue(Type.STRINGS));
162162

163163
// merge ours and theirs to a de-duplicated set
164-
Set<String> combined = new LinkedHashSet<>(Sets.intersection(ourMembers, theirMembers));
164+
Set<String> combined = new LinkedHashSet<>(CollectionUtils.intersection(ourMembers, theirMembers));
165165
for (String m : Sets.difference(ourMembers, theirMembers)) {
166166
if (!base.contains(m)) {
167167
combined.add(m);

oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/composite/CompositeProviderCustomMixTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ private boolean expected(Set<String> check, Set<String> supported1, Set<String>
218218
if (type == CompositionType.OR) {
219219
return Sets.difference(Sets.difference(check, granted1), granted2).isEmpty();
220220
} else {
221-
Set<String> f1 = Sets.intersection(supported1, check);
221+
Set<String> f1 = CollectionUtils.intersection(supported1, check);
222222
boolean hasf1 = granted1.containsAll(f1);
223-
Set<String> f2 = Sets.intersection(supported2, check);
223+
Set<String> f2 = CollectionUtils.intersection(supported2, check);
224224
boolean hasf2 = granted2.containsAll(f2);
225225
return hasf1 && hasf2;
226226
}

oak-run/src/main/java/org/apache/jackrabbit/oak/explorer/NodeStoreTree.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.apache.jackrabbit.oak.explorer;
2020

21-
import static org.apache.jackrabbit.guava.common.collect.Sets.intersection;
2221
import static org.apache.jackrabbit.guava.common.escape.Escapers.builder;
2322
import static java.util.Collections.sort;
2423
import static javax.jcr.PropertyType.BINARY;
@@ -413,7 +412,7 @@ void printTarInfo(String file) {
413412
}
414413
}
415414

416-
Set<UUID> inMem = intersection(backend.getReferencedSegmentIds(), uuids);
415+
Set<UUID> inMem = CollectionUtils.intersection(backend.getReferencedSegmentIds(), uuids);
417416
if (!inMem.isEmpty()) {
418417
sb.append("In Memory segment references: ");
419418
sb.append(newline);

oak-run/src/test/java/org/apache/jackrabbit/oak/run/DataStoreCommandTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@
4343
import org.apache.jackrabbit.guava.common.base.Splitter;
4444
import org.apache.jackrabbit.guava.common.base.Strings;
4545
import org.apache.jackrabbit.guava.common.collect.Iterators;
46-
import org.apache.jackrabbit.guava.common.collect.Lists;
47-
import org.apache.jackrabbit.guava.common.collect.Maps;
4846
import org.apache.jackrabbit.guava.common.collect.Sets;
4947
import joptsimple.OptionException;
5048
import org.apache.commons.io.FileUtils;
@@ -752,7 +750,7 @@ private void testConsistency(File dump, Data data, boolean verbose, boolean verb
752750
if (!markOnly) {
753751
// Verbose would have paths as well as ids changed but normally only DocumentNS would have paths suffixed
754752
assertFileEquals(dump, "gccand-", verbose ?
755-
encodedIdsAndPath(verboseRootPath ? Sets.intersection(data.addedSubset, data.missingDataStore) :
753+
encodedIdsAndPath(verboseRootPath ? CollectionUtils.intersection(data.addedSubset, data.missingDataStore) :
756754
data.missingDataStore, blobFixture.getType(), data.idToPath, true) :
757755
(storeFixture instanceof StoreFixture.MongoStoreFixture) ?
758756
encodedIdsAndPath(data.missingDataStore, blobFixture.getType(), data.idToPath, false) :

oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterPoolTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.apache.jackrabbit.oak.segment;
2121

22-
import static org.apache.jackrabbit.guava.common.collect.Sets.intersection;
2322
import static org.apache.jackrabbit.guava.common.util.concurrent.Uninterruptibles.sleepUninterruptibly;
2423
import static java.util.concurrent.Executors.newSingleThreadExecutor;
2524
import static java.util.concurrent.TimeUnit.MILLISECONDS;
@@ -38,6 +37,7 @@
3837
import java.util.concurrent.Future;
3938
import java.util.concurrent.TimeoutException;
4039

40+
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
4141
import org.apache.jackrabbit.oak.segment.WriteOperationHandler.WriteOperation;
4242
import org.apache.jackrabbit.oak.segment.file.tar.GCGeneration;
4343
import org.apache.jackrabbit.oak.segment.memory.MemoryStore;
@@ -159,7 +159,7 @@ public void testFlush() throws ExecutionException, InterruptedException, IOExcep
159159
assertEquals(rootId, res5.get());
160160
assertEquals(rootId, res6.get());
161161
assertEquals(3, map2.size());
162-
assertTrue(intersection(new HashSet<>(map1.values()), new HashSet<>(map2.values())).isEmpty());
162+
assertTrue(CollectionUtils.intersection(new HashSet<>(map1.values()), new HashSet<>(map2.values())).isEmpty());
163163
}
164164

165165
@Test
@@ -209,7 +209,7 @@ public void testCompaction() throws ExecutionException, InterruptedException, IO
209209
assertEquals(rootId, res8.get());
210210
assertEquals(rootId, res9.get());
211211
assertEquals(3, map3.size());
212-
assertTrue(intersection(new HashSet<>(map1.values()), new HashSet<>(map3.values())).isEmpty());
212+
assertTrue(CollectionUtils.intersection(new HashSet<>(map1.values()), new HashSet<>(map3.values())).isEmpty());
213213
}
214214

215215
@Test

oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@
3232
import java.util.concurrent.locks.ReadWriteLock;
3333

3434
import org.apache.jackrabbit.guava.common.collect.Iterables;
35-
import org.apache.jackrabbit.guava.common.collect.Sets;
36-
3735
import org.apache.jackrabbit.oak.api.CommitFailedException;
36+
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
3837
import org.apache.jackrabbit.oak.plugins.document.util.Utils;
3938
import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeBuilder;
4039
import org.apache.jackrabbit.oak.spi.commit.CommitHook;
@@ -752,7 +751,7 @@ private void checkForConflicts() throws CommitFailedException {
752751
Set<Revision> collisions = new HashSet<>(doc.getLocalMap(COLLISIONS).keySet());
753752
Set<Revision> commits = new HashSet<>();
754753
Iterables.transform(b.getCommits(), Revision::asTrunkRevision).forEach(commits::add);
755-
Set<Revision> conflicts = Sets.intersection(collisions, commits);
754+
Set<Revision> conflicts = CollectionUtils.intersection(collisions, commits);
756755
if (!conflicts.isEmpty()) {
757756
throw new CommitFailedException(STATE, 2,
758757
"Conflicting concurrent change on branch commits " + conflicts);

oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1831,7 +1831,7 @@ protected <T extends Document> Iterable<T> queryAsIterable(final Collection<T> c
18311831
final List<String> excludeKeyPatterns, final List<QueryCondition> conditions, final int limit, final String sortBy) {
18321832

18331833
final RDBTableMetaData tmd = getTable(collection);
1834-
Set<String> allowedProps = Sets.intersection(INDEXEDPROPERTIES, tmd.getColumnProperties());
1834+
Set<String> allowedProps = CollectionUtils.intersection(INDEXEDPROPERTIES, tmd.getColumnProperties());
18351835
for (QueryCondition cond : conditions) {
18361836
if (!allowedProps.contains(cond.getPropertyName())) {
18371837
String message = "indexed property " + cond.getPropertyName() + " not supported, query was '" + cond

0 commit comments

Comments
 (0)