Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Feb 10, 2024
1 parent d651e57 commit 3467e5b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 167 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import static java.util.stream.Collectors.summingLong;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.refaster.Refaster;
Expand All @@ -39,12 +37,10 @@
import java.util.Comparator;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -256,112 +252,25 @@ Optional<S> after(Stream<T> stream, Function<? super T, S> function) {
}
}

// XXX: This rule assumes that any matched `Collector` does not perform any filtering.
static final class StreamCollectingAndThenCollectionIsEmpty<T> {
@BeforeTemplate
boolean before(Stream<T> stream, Collector<? super T, ?, ? extends Collection<?>> collector) {
return stream.collect(collectingAndThen(collector, Collection::isEmpty));
}

@AfterTemplate
boolean after(Stream<T> stream) {
return stream.findAny().isEmpty();
}
}

// XXX: This rule assumes that any matched `Collector` does not perform any filtering.
static final class StreamCollectingAndThenImmutableListIsEmpty<T> {
@BeforeTemplate
boolean before(
Stream<T> stream, Collector<? super T, ?, ? extends ImmutableList<?>> collector) {
return stream.collect(collectingAndThen(collector, ImmutableList::isEmpty));
}

@AfterTemplate
boolean after(Stream<T> stream) {
return stream.findAny().isEmpty();
}
}

// XXX: This rule assumes that any matched `Collector` does not perform any filtering.
static final class StreamCollectingAndThenListIsEmpty<T> {
@BeforeTemplate
boolean before(Stream<T> stream, Collector<? super T, ?, ? extends List<?>> collector) {
return stream.collect(collectingAndThen(collector, List::isEmpty));
}

@AfterTemplate
boolean after(Stream<T> stream) {
return stream.findAny().isEmpty();
}
}

// XXX: This rule assumes that any matched `Collector` does not perform any filtering.
static final class StreamCollectingAndThenImmutableSetIsEmpty<T> {
@BeforeTemplate
boolean before(Stream<T> stream, Collector<? super T, ?, ? extends ImmutableSet<?>> collector) {
return stream.collect(collectingAndThen(collector, ImmutableSet::isEmpty));
}

@AfterTemplate
boolean after(Stream<T> stream) {
return stream.findAny().isEmpty();
}
}

// XXX: This rule assumes that any matched `Collector` does not perform any filtering.
static final class StreamCollectingAndThenSetIsEmpty<T> {
@BeforeTemplate
boolean before(Stream<T> stream, Collector<? super T, ?, ? extends Set<?>> collector) {
return stream.collect(collectingAndThen(collector, Set::isEmpty));
}

@AfterTemplate
boolean after(Stream<T> stream) {
return stream.findAny().isEmpty();
}
}

// XXX: This rule assumes that any matched `Collector` does not perform any filtering.
static final class StreamCollectingAndThenImmutableMapIsEmpty<T> {
@BeforeTemplate
boolean before(
Stream<T> stream, Collector<? super T, ?, ? extends ImmutableMap<?, ?>> collector) {
return stream.collect(collectingAndThen(collector, ImmutableMap::isEmpty));
}

@AfterTemplate
boolean after(Stream<T> stream) {
return stream.findAny().isEmpty();
}
}

// XXX: This rule assumes that any matched `Collector` does not perform any filtering.
static final class StreamCollectingAndThenMapIsEmpty<T> {
@BeforeTemplate
boolean before(Stream<T> stream, Collector<? super T, ?, ? extends Map<?, ?>> collector) {
return stream.collect(collectingAndThen(collector, Map::isEmpty));
}

@AfterTemplate
boolean after(Stream<T> stream) {
return stream.findAny().isEmpty();
}
}

/** In order to test whether a stream has any element, simply try to find one. */
// XXX: This rule assumes that any matched `Collector` does not perform any filtering.
// (Perhaps we could add a `@Matches` guard that validates that the collector expression does not
// contain a `Collectors#filtering` call. That'd still not be 100% accurate, though.)
static final class StreamIsEmpty<T> {
static final class StreamIsEmpty<T, K, V, C extends Collection<K>, M extends Map<K, V>> {
@BeforeTemplate
boolean before(Stream<T> stream, Collector<? super T, ?, ? extends Collection<?>> collector) {
boolean before(Stream<T> stream, Collector<? super T, ?, ? extends C> collector) {
return Refaster.anyOf(
stream.count() == 0,
stream.count() <= 0,
stream.count() < 1,
stream.findFirst().isEmpty(),
stream.collect(collector).isEmpty());
stream.collect(collector).isEmpty(),
stream.collect(collectingAndThen(collector, C::isEmpty)));
}

@BeforeTemplate
boolean before2(Stream<T> stream, Collector<? super T, ?, ? extends M> collector) {
return stream.collect(collectingAndThen(collector, M::isEmpty));
}

@AfterTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,18 @@
import static java.util.stream.Collectors.summingDouble;
import static java.util.stream.Collectors.summingInt;
import static java.util.stream.Collectors.summingLong;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import java.util.Collection;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
Expand All @@ -49,8 +44,12 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(
ImmutableList.class,
ImmutableMap.class,
List.class,
Map.class,
Objects.class,
Streams.class,
collectingAndThen(null, null),
counting(),
filtering(null, null),
flatMapping(null, null),
Expand All @@ -65,7 +64,9 @@ public ImmutableSet<Object> elidedTypesAndStaticImports() {
summarizingLong(null),
summingDouble(null),
summingInt(null),
summingLong(null));
summingLong(null),
toImmutableList(),
toImmutableMap(null, null));
}

String testJoining() {
Expand Down Expand Up @@ -119,43 +120,18 @@ ImmutableSet<Optional<Integer>> testStreamMapFirst() {
Stream.of("bar").map(String::length).findFirst());
}

Boolean testStreamCollectingAndThenCollectionIsEmpty() {
return Stream.of(1).collect(collectingAndThen(toList(), Collection::isEmpty));
}

Boolean testStreamCollectingAndThenImmutableListIsEmpty() {
return Stream.of(1).collect(collectingAndThen(toImmutableList(), ImmutableList::isEmpty));
}

Boolean testStreamCollectingAndThenListIsEmpty() {
return Stream.of(1).collect(collectingAndThen(toList(), List::isEmpty));
}

Boolean testStreamCollectingAndThenImmutableSetIsEmpty() {
return Stream.of(1).collect(collectingAndThen(toImmutableSet(), ImmutableSet::isEmpty));
}

Boolean testStreamCollectingAndThenSetIsEmpty() {
return Stream.of(1).collect(collectingAndThen(toSet(), Set::isEmpty));
}

Boolean testStreamCollectingAndThenImmutableMapIsEmpty() {
return Stream.of(1)
.collect(
collectingAndThen(toImmutableMap(key -> key, value -> value), ImmutableMap::isEmpty));
}

Boolean testStreamCollectingAndThenMapIsEmpty() {
return Stream.of(1).collect(collectingAndThen(toMap(key -> key, value -> value), Map::isEmpty));
}

ImmutableSet<Boolean> testStreamIsEmpty() {
return ImmutableSet.of(
Stream.of(1).count() == 0,
Stream.of(2).count() <= 0,
Stream.of(3).count() < 1,
Stream.of(4).findFirst().isEmpty(),
Stream.of(5).collect(toImmutableSet()).isEmpty());
Stream.of(5).collect(toImmutableSet()).isEmpty(),
Stream.of(6).collect(collectingAndThen(toImmutableList(), List::isEmpty)),
Stream.of(7).collect(collectingAndThen(toImmutableList(), ImmutableList::isEmpty)),
Stream.of(8).collect(collectingAndThen(toImmutableMap(k -> k, v -> v), Map::isEmpty)),
Stream.of(9)
.collect(collectingAndThen(toImmutableMap(k -> k, v -> v), ImmutableMap::isEmpty)));
}

ImmutableSet<Boolean> testStreamIsNotEmpty() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package tech.picnic.errorprone.refasterrules;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.Comparator.comparingInt;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.reverseOrder;
import static java.util.function.Function.identity;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.filtering;
import static java.util.stream.Collectors.flatMapping;
Expand All @@ -22,12 +25,15 @@
import static java.util.stream.Collectors.summingLong;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.LongSummaryStatistics;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
Expand All @@ -40,8 +46,12 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(
ImmutableList.class,
ImmutableMap.class,
List.class,
Map.class,
Objects.class,
Streams.class,
collectingAndThen(null, null),
counting(),
filtering(null, null),
flatMapping(null, null),
Expand All @@ -56,7 +66,9 @@ public ImmutableSet<Object> elidedTypesAndStaticImports() {
summarizingLong(null),
summingDouble(null),
summingInt(null),
summingLong(null));
summingLong(null),
toImmutableList(),
toImmutableMap(null, null));
}

String testJoining() {
Expand Down Expand Up @@ -109,41 +121,17 @@ ImmutableSet<Optional<Integer>> testStreamMapFirst() {
Stream.of("bar").findFirst().map(String::length));
}

Boolean testStreamCollectingAndThenCollectionIsEmpty() {
return Stream.of(1).findAny().isEmpty();
}

Boolean testStreamCollectingAndThenImmutableListIsEmpty() {
return Stream.of(1).findAny().isEmpty();
}

Boolean testStreamCollectingAndThenListIsEmpty() {
return Stream.of(1).findAny().isEmpty();
}

Boolean testStreamCollectingAndThenImmutableSetIsEmpty() {
return Stream.of(1).findAny().isEmpty();
}

Boolean testStreamCollectingAndThenSetIsEmpty() {
return Stream.of(1).findAny().isEmpty();
}

Boolean testStreamCollectingAndThenImmutableMapIsEmpty() {
return Stream.of(1).findAny().isEmpty();
}

Boolean testStreamCollectingAndThenMapIsEmpty() {
return Stream.of(1).findAny().isEmpty();
}

ImmutableSet<Boolean> testStreamIsEmpty() {
return ImmutableSet.of(
Stream.of(1).findAny().isEmpty(),
Stream.of(2).findAny().isEmpty(),
Stream.of(3).findAny().isEmpty(),
Stream.of(4).findAny().isEmpty(),
Stream.of(5).findAny().isEmpty());
Stream.of(5).findAny().isEmpty(),
Stream.of(6).findAny().isEmpty(),
Stream.of(7).findAny().isEmpty(),
Stream.of(8).findAny().isEmpty(),
Stream.of(9).findAny().isEmpty());
}

ImmutableSet<Boolean> testStreamIsNotEmpty() {
Expand Down

0 comments on commit 3467e5b

Please sign in to comment.