Skip to content

Commit

Permalink
Introduce additional Multimap Refaster rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Jan 13, 2024
1 parent dff67fe commit fbc22dc
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import org.jspecify.annotations.Nullable;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;

Expand All @@ -28,6 +30,21 @@ Set<K> after(Multimap<K, V> multimap) {
}
}

/** Prefer {@link Multimap#isEmpty()} over more contrived alternatives. */
static final class MultimapIsEmpty<K, V> {
@BeforeTemplate
boolean before(Multimap<K, V> multimap) {
return Refaster.anyOf(
multimap.keySet(), multimap.keys(), multimap.values(), multimap.entries())
.isEmpty();
}

@AfterTemplate
boolean after(Multimap<K, V> multimap) {
return multimap.isEmpty();
}
}

/** Prefer {@link Multimap#size()} over more contrived alternatives. */
static final class MultimapSize<K, V> {
@BeforeTemplate
Expand All @@ -41,6 +58,32 @@ int after(Multimap<K, V> multimap) {
}
}

/** Prefer {@link Multimap#containsKey(Object)} over more contrived alternatives. */
static final class MultimapContainsKey<K, V, T> {
@BeforeTemplate
boolean before(Multimap<K, V> multimap, T key) {
return Refaster.anyOf(multimap.keySet(), multimap.keys()).contains(key);
}

@AfterTemplate
boolean after(Multimap<K, V> multimap, T key) {
return multimap.containsKey(key);
}
}

/** Prefer {@link Multimap#containsValue(Object)} over more contrived alternatives. */
static final class MultimapContainsValue<K, V, T> {
@BeforeTemplate
boolean before(Multimap<K, V> multimap, T value) {
return multimap.values().contains(value);
}

@AfterTemplate
boolean after(Multimap<K, V> multimap, T value) {
return multimap.containsValue(value);
}
}

/**
* Prefer {@link Multimap#get(Object)} over more contrived alternatives.
*
Expand All @@ -59,4 +102,30 @@ Collection<V> after(Multimap<K, V> multimap, K key) {
return multimap.get(key);
}
}

/** Don't unnecessarily use {@link Multimap#entries()}. */
static final class MultimapKeysStream<K, V> {
@BeforeTemplate
Stream<K> before(Multimap<K, V> multimap) {
return multimap.entries().stream().map(Map.Entry::getKey);
}

@AfterTemplate
Stream<K> after(Multimap<K, V> multimap) {
return multimap.keys().stream();
}
}

/** Don't unnecessarily use {@link Multimap#entries()}. */
static final class MultimapValuesStream<K, V> {
@BeforeTemplate
Stream<V> before(Multimap<K, V> multimap) {
return multimap.entries().stream().map(Map.Entry::getValue);
}

@AfterTemplate
Stream<V> after(Multimap<K, V> multimap) {
return multimap.values().stream();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,54 @@
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

final class MultimapRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Multimaps.class);
return ImmutableSet.of(Map.class, Multimaps.class);
}

Set<String> testMultimapKeySet() {
return ImmutableSetMultimap.of("foo", "bar").asMap().keySet();
}

ImmutableSet<Boolean> testMultimapIsEmpty() {
return ImmutableSet.of(
ImmutableSetMultimap.of("foo", 1).keySet().isEmpty(),
ImmutableSetMultimap.of("bar", 2).keys().isEmpty(),
ImmutableSetMultimap.of("baz", 3).values().isEmpty(),
ImmutableSetMultimap.of("qux", 54).entries().isEmpty());
}

int testMultimapSize() {
return ImmutableSetMultimap.of().values().size();
}

ImmutableSet<Boolean> testMultimapContainsKey() {
return ImmutableSet.of(
ImmutableSetMultimap.of("foo", 1).keySet().contains("bar"),
ImmutableSetMultimap.of("baz", 1).keys().contains("qux"));
}

boolean testMultimapContainsValue() {
return ImmutableSetMultimap.of("foo", 1).values().contains(2);
}

ImmutableSet<Collection<Integer>> testMultimapGet() {
return ImmutableSet.of(
ImmutableSetMultimap.of(1, 2).asMap().get(1),
Multimaps.asMap((Multimap<Integer, Integer>) ImmutableSetMultimap.of(1, 2)).get(1));
}

Stream<String> testMultimapKeysStream() {
return ImmutableSetMultimap.of("foo", 1).entries().stream().map(Map.Entry::getKey);
}

Stream<Integer> testMultimapValuesStream() {
return ImmutableSetMultimap.of("foo", 1).entries().stream().map(Map.Entry::getValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,54 @@
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

final class MultimapRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(Multimaps.class);
return ImmutableSet.of(Map.class, Multimaps.class);
}

Set<String> testMultimapKeySet() {
return ImmutableSetMultimap.of("foo", "bar").keySet();
}

ImmutableSet<Boolean> testMultimapIsEmpty() {
return ImmutableSet.of(
ImmutableSetMultimap.of("foo", 1).isEmpty(),
ImmutableSetMultimap.of("bar", 2).isEmpty(),
ImmutableSetMultimap.of("baz", 3).isEmpty(),
ImmutableSetMultimap.of("qux", 54).isEmpty());
}

int testMultimapSize() {
return ImmutableSetMultimap.of().size();
}

ImmutableSet<Boolean> testMultimapContainsKey() {
return ImmutableSet.of(
ImmutableSetMultimap.of("foo", 1).containsKey("bar"),
ImmutableSetMultimap.of("baz", 1).containsKey("qux"));
}

boolean testMultimapContainsValue() {
return ImmutableSetMultimap.of("foo", 1).containsValue(2);
}

ImmutableSet<Collection<Integer>> testMultimapGet() {
return ImmutableSet.of(
ImmutableSetMultimap.of(1, 2).get(1),
((Multimap<Integer, Integer>) ImmutableSetMultimap.of(1, 2)).get(1));
}

Stream<String> testMultimapKeysStream() {
return ImmutableSetMultimap.of("foo", 1).keys().stream();
}

Stream<Integer> testMultimapValuesStream() {
return ImmutableSetMultimap.of("foo", 1).values().stream();
}
}

0 comments on commit fbc22dc

Please sign in to comment.