Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed May 2, 2023
1 parent b653978 commit 748a086
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static java.util.function.Predicate.not;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
Expand Down Expand Up @@ -215,41 +216,101 @@ ImmutableSet<T> after(T e1, T e2, T e3, T e4, T e5) {
}
}

/** Prefer {@link Sets#intersection(Set, Set)} over more contrived alternatives. */
static final class SetsIntersection<T> {
/**
* Prefer an immutable copy of {@link Sets#difference(Set, Set)} over more contrived alternatives.
*/
static final class SetsDifference<S, T> {
@BeforeTemplate
ImmutableSet<S> before(Set<S> set1, Set<T> set2) {
return set1.stream()
.filter(Refaster.anyOf(not(set2::contains), e -> !set2.contains(e)))
.collect(toImmutableSet());
}

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

/**
* Prefer an immutable copy of {@link Sets#difference(Set, Set)} over more contrived alternatives.
*/
static final class SetsDifferenceMap<T, K, V> {
@BeforeTemplate
ImmutableSet<T> before(Set<T> set1, Set<T> set2) {
ImmutableSet<T> before(Set<T> set, Map<K, V> map) {
return set.stream()
.filter(Refaster.anyOf(not(map::containsKey), e -> !map.containsKey(e)))
.collect(toImmutableSet());
}

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

/**
* Prefer an immutable copy of {@link Sets#difference(Set, Set)} over more contrived alternatives.
*/
static final class SetsDifferenceMultimap<T, K, V> {
@BeforeTemplate
ImmutableSet<T> before(Set<T> set, Multimap<K, V> multimap) {
return set.stream()
.filter(Refaster.anyOf(not(multimap::containsKey), e -> !multimap.containsKey(e)))
.collect(toImmutableSet());
}

@AfterTemplate
ImmutableSet<T> after(Set<T> set, Multimap<K, V> multimap) {
return Sets.difference(set, multimap.keySet()).immutableCopy();
}
}

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

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

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

@AfterTemplate
ImmutableSet<K> after(Set<K> set, Map<K, V> map) {
ImmutableSet<T> after(Set<T> 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 SetsIntersectionMultimap<K, V> {
/**
* Prefer an immutable copy of {@link Sets#intersection(Set, Set)} over more contrived
* alternatives.
*/
static final class SetsIntersectionMultimap<T, K, V> {
@BeforeTemplate
ImmutableSet<K> before(Set<K> set, Multimap<K, V> multimap) {
ImmutableSet<T> before(Set<T> set, Multimap<K, V> multimap) {
return set.stream().filter(multimap::containsKey).collect(toImmutableSet());
}

@AfterTemplate
ImmutableSet<K> after(Set<K> set, Multimap<K, V> multimap) {
ImmutableSet<T> after(Set<T> set, Multimap<K, V> multimap) {
return Sets.intersection(set, multimap.keySet()).immutableCopy();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package tech.picnic.errorprone.refasterrules;

import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.function.Predicate.not;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
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 All @@ -17,7 +18,7 @@
final class ImmutableSetRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Arrays.class, Collections.class, Streams.class);
return ImmutableSet.of(Arrays.class, Collections.class, Streams.class, not(null));
}

ImmutableSet.Builder<String> testImmutableSetBuilder() {
Expand Down Expand Up @@ -75,18 +76,51 @@ Set<Integer> testImmutableSetOf5() {
return Set.of(1, 2, 3, 4, 5);
}

ImmutableSet<ImmutableSet<Integer>> testSetsDifference() {
return ImmutableSet.of(
ImmutableSet.of(1).stream()
.filter(not(ImmutableSet.of(2)::contains))
.collect(toImmutableSet()),
ImmutableSet.of(3).stream()
.filter(v -> !ImmutableSet.of(4).contains(v))
.collect(toImmutableSet()));
}

ImmutableSet<ImmutableSet<Integer>> testSetsDifferenceMap() {
return ImmutableSet.of(
ImmutableSet.of(1).stream()
.filter(not(ImmutableMap.of(2, 3)::containsKey))
.collect(toImmutableSet()),
ImmutableSet.of(4).stream()
.filter(v -> !ImmutableMap.of(5, 6).containsKey(v))
.collect(toImmutableSet()));
}

ImmutableSet<ImmutableSet<Integer>> testSetsDifferenceMultimap() {
return ImmutableSet.of(
ImmutableSet.of(1).stream()
.filter(not(ImmutableSetMultimap.of(2, 3)::containsKey))
.collect(toImmutableSet()),
ImmutableSet.of(4).stream()
.filter(v -> !ImmutableSetMultimap.of(5, 6).containsKey(v))
.collect(toImmutableSet()));
}

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

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

ImmutableSet<Integer> testSetsIntersectionMultimap() {
ImmutableMultimap<Integer, Integer> multimap = ImmutableMultimap.of(1, 2);
return ImmutableSet.of(3).stream().filter(multimap::containsKey).collect(toImmutableSet());
return ImmutableSet.of(1).stream()
.filter(ImmutableSetMultimap.of(2, 3)::containsKey)
.collect(toImmutableSet());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package tech.picnic.errorprone.refasterrules;

import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.function.Predicate.not;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
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 All @@ -17,7 +18,7 @@
final class ImmutableSetRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Arrays.class, Collections.class, Streams.class);
return ImmutableSet.of(Arrays.class, Collections.class, Streams.class, not(null));
}

ImmutableSet.Builder<String> testImmutableSetBuilder() {
Expand Down Expand Up @@ -70,18 +71,35 @@ Set<Integer> testImmutableSetOf5() {
return ImmutableSet.of(1, 2, 3, 4, 5);
}

ImmutableSet<ImmutableSet<Integer>> testSetsDifference() {
return ImmutableSet.of(
Sets.difference(ImmutableSet.of(1), ImmutableSet.of(2)).immutableCopy(),
Sets.difference(ImmutableSet.of(3), ImmutableSet.of(4)).immutableCopy());
}

ImmutableSet<ImmutableSet<Integer>> testSetsDifferenceMap() {
return ImmutableSet.of(
Sets.difference(ImmutableSet.of(1), ImmutableMap.of(2, 3).keySet()).immutableCopy(),
Sets.difference(ImmutableSet.of(4), ImmutableMap.of(5, 6).keySet()).immutableCopy());
}

ImmutableSet<ImmutableSet<Integer>> testSetsDifferenceMultimap() {
return ImmutableSet.of(
Sets.difference(ImmutableSet.of(1), ImmutableSetMultimap.of(2, 3).keySet()).immutableCopy(),
Sets.difference(ImmutableSet.of(4), ImmutableSetMultimap.of(5, 6).keySet())
.immutableCopy());
}

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

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

ImmutableSet<Integer> testSetsIntersectionMultimap() {
ImmutableMultimap<Integer, Integer> multimap = ImmutableMultimap.of(1, 2);
return Sets.intersection(ImmutableSet.of(3), multimap.keySet()).immutableCopy();
return Sets.intersection(ImmutableSet.of(1), ImmutableSetMultimap.of(2, 3).keySet())
.immutableCopy();
}
}

0 comments on commit 748a086

Please sign in to comment.