Skip to content

Commit

Permalink
Prefer Sets#intersection over more contrived alternatives.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsamehsalah committed May 1, 2023
1 parent f675cf0 commit 5b58f40
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import static java.util.Collections.singleton;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;
import com.google.common.collect.Streams;
import com.google.errorprone.refaster.Refaster;
Expand All @@ -15,6 +17,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;
Expand Down Expand Up @@ -211,4 +214,43 @@ ImmutableSet<T> after(T e1, T e2, T e3, T e4, T e5) {
return ImmutableSet.of(e1, e2, e3, e4, e5);
}
}

/** Prefer {@link Sets#intersection(Set, Set)} ()} over more contrived alternatives. */
static final class SetsIntersection<T> {
@BeforeTemplate
ImmutableSet<T> before(Set<T> set1, Set<T> set2) {
return set1.stream().filter(set2::contains).collect(toImmutableSet());
}

@AfterTemplate
ImmutableSet<T> after(Set<T> set1, Set<T> set2) {
return Sets.intersection(set1, set2).immutableCopy();
}
}

/** Prefer {@link Sets#intersection(Set, Set)} ()} over more contrived alternatives. */
static final class MapKeySetIntersection<K, V> {
@BeforeTemplate
ImmutableSet<K> before(Set<K> set, Map<K, V> map) {
return set.stream().filter(map::containsKey).collect(toImmutableSet());
}

@AfterTemplate
ImmutableSet<K> after(Set<K> set, Map<K, V> map) {
return Sets.intersection(set, map.keySet()).immutableCopy();
}
}

/** Prefer {@link Sets#intersection(Set, Set)} ()} over more contrived alternatives. */
static final class SetMultimapKeySetIntersection<K, V> {
@BeforeTemplate
ImmutableSet<K> before(Set<K> set, SetMultimap<K, V> setMultimap) {
return set.stream().filter(setMultimap::containsKey).collect(toImmutableSet());
}

@AfterTemplate
ImmutableSet<K> after(Set<K> set, SetMultimap<K, V> setMultimap) {
return Sets.intersection(set, setMultimap.keySet()).immutableCopy();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import static com.google.common.collect.ImmutableSet.toImmutableSet;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Sets;
import com.google.common.collect.Streams;
import java.util.Arrays;
Expand Down Expand Up @@ -72,4 +74,21 @@ Set<Integer> testImmutableSetOf4() {
Set<Integer> testImmutableSetOf5() {
return Set.of(1, 2, 3, 4, 5);
}

ImmutableSet<Integer> testSetsIntersection() {
ImmutableSet<Integer> set = ImmutableSet.of(1);
return ImmutableSet.of(1, 2, 3).stream().filter(set::contains).collect(toImmutableSet());
}

ImmutableSet<Integer> testMapKeySetIntersection() {
ImmutableMap<Integer, Integer> map = ImmutableMap.of(1, 4);
return ImmutableSet.of(1, 2, 3).stream().filter(map::containsKey).collect(toImmutableSet());
}

ImmutableSet<Integer> testSetMultimapKeySetIntersection() {
ImmutableSetMultimap<Integer, Integer> setMultiMap = ImmutableSetMultimap.of(1, 4);
return ImmutableSet.of(1, 2, 3).stream()
.filter(setMultiMap::containsKey)
.collect(toImmutableSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import static com.google.common.collect.ImmutableSet.toImmutableSet;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Sets;
import com.google.common.collect.Streams;
import java.util.Arrays;
Expand Down Expand Up @@ -67,4 +69,19 @@ Set<Integer> testImmutableSetOf4() {
Set<Integer> testImmutableSetOf5() {
return ImmutableSet.of(1, 2, 3, 4, 5);
}

ImmutableSet<Integer> testSetsIntersection() {
ImmutableSet<Integer> set = ImmutableSet.of(1);
return Sets.intersection(ImmutableSet.of(1, 2, 3), set).immutableCopy();
}

ImmutableSet<Integer> testMapKeySetIntersection() {
ImmutableMap<Integer, Integer> map = ImmutableMap.of(1, 4);
return Sets.intersection(ImmutableSet.of(1, 2, 3), map.keySet()).immutableCopy();
}

ImmutableSet<Integer> testSetMultimapKeySetIntersection() {
ImmutableSetMultimap<Integer, Integer> setMultiMap = ImmutableSetMultimap.of(1, 4);
return Sets.intersection(ImmutableSet.of(1, 2, 3), setMultiMap.keySet()).immutableCopy();
}
}

0 comments on commit 5b58f40

Please sign in to comment.