mirror of
https://github.com/jlengrand/error-prone-support.git
synced 2026-03-10 08:11:25 +00:00
Introduce additional Multimap Refaster rules (#971)
This commit is contained in:
committed by
GitHub
parent
641bb5c566
commit
6222bcb0d4
@@ -6,7 +6,9 @@ import com.google.errorprone.refaster.Refaster;
|
||||
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;
|
||||
|
||||
@@ -28,6 +30,21 @@ final class MultimapRules {
|
||||
}
|
||||
}
|
||||
|
||||
/** 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
|
||||
@@ -41,6 +58,32 @@ final class MultimapRules {
|
||||
}
|
||||
}
|
||||
|
||||
/** 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.
|
||||
*
|
||||
@@ -59,4 +102,30 @@ final class MultimapRules {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,26 +5,54 @@ import com.google.common.collect.ImmutableSetMultimap;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,26 +5,54 @@ import com.google.common.collect.ImmutableSetMultimap;
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user