Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Oct 7, 2023
1 parent 2723e7a commit e7d6176
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,84 +263,4 @@ ImmutableList<T> after(T e1, T e2, T e3, T e4, T e5) {
return ImmutableList.of(e1, e2, e3, e4, e5);
}
}

/**
* Prefer {@link Stream#of(Object[])} over constructing a list and then directly calling {@link
* ImmutableList#stream()} on it.
*/
static final class StreamImmutableListOf1<T> {
@BeforeTemplate
Stream<T> before(T e1) {
return ImmutableList.of(e1).stream();
}

@AfterTemplate
Stream<T> after(T e1) {
return Stream.of(e1);
}
}

/**
* Prefer {@link Stream#of(Object[])} over constructing a list and then directly calling {@link
* ImmutableList#stream()} on it.
*/
static final class StreamImmutableListOf2<T> {
@BeforeTemplate
Stream<T> before(T e1, T e2) {
return ImmutableList.of(e1, e2).stream();
}

@AfterTemplate
Stream<T> after(T e1, T e2) {
return Stream.of(e1, e2);
}
}

/**
* Prefer {@link Stream#of(Object[])} over constructing a list and then directly calling {@link
* ImmutableList#stream()} on it.
*/
static final class StreamImmutableListOf3<T> {
@BeforeTemplate
Stream<T> before(T e1, T e2, T e3) {
return ImmutableList.of(e1, e2, e3).stream();
}

@AfterTemplate
Stream<T> after(T e1, T e2, T e3) {
return Stream.of(e1, e2, e3);
}
}

/**
* Prefer {@link Stream#of(Object[])} over constructing a list and then directly calling {@link
* ImmutableList#stream()} on it.
*/
static final class StreamImmutableListOf4<T> {
@BeforeTemplate
Stream<T> before(T e1, T e2, T e3, T e4) {
return ImmutableList.of(e1, e2, e3, e4).stream();
}

@AfterTemplate
Stream<T> after(T e1, T e2, T e3, T e4) {
return Stream.of(e1, e2, e3, e4);
}
}

/**
* Prefer {@link Stream#of(Object[])} over constructing a list and then directly calling {@link
* ImmutableList#stream()} on it.
*/
static final class StreamImmutableListOf5<T> {
@BeforeTemplate
Stream<T> before(T e1, T e2, T e3, T e4, T e5) {
return ImmutableList.of(e1, e2, e3, e4, e5).stream();
}

@AfterTemplate
Stream<T> after(T e1, T e2, T e3, T e4, T e5) {
return Stream.of(e1, e2, e3, e4, e5);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static java.util.stream.Collectors.summingInt;
import static java.util.stream.Collectors.summingLong;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Streams;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.refaster.Refaster;
Expand Down Expand Up @@ -668,4 +669,74 @@ Stream<T> after(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next) {
return Stream.iterate(seed, hasNext, next);
}
}

/** Prefer {@link Stream#of(Object)} over more contrived alternatives. */
// XXX: Generalize this and similar rules using an Error Prone check.
static final class StreamOf1<T> {
@BeforeTemplate
Stream<T> before(T e1) {
return ImmutableList.of(e1).stream();
}

@AfterTemplate
Stream<T> after(T e1) {
return Stream.of(e1);
}
}

/** Prefer {@link Stream#of(Object[])} over more contrived alternatives. */
// XXX: Generalize this and similar rules using an Error Prone check.
static final class StreamOf2<T> {
@BeforeTemplate
Stream<T> before(T e1, T e2) {
return ImmutableList.of(e1, e2).stream();
}

@AfterTemplate
Stream<T> after(T e1, T e2) {
return Stream.of(e1, e2);
}
}

/** Prefer {@link Stream#of(Object[])} over more contrived alternatives. */
// XXX: Generalize this and similar rules using an Error Prone check.
static final class StreamOf3<T> {
@BeforeTemplate
Stream<T> before(T e1, T e2, T e3) {
return ImmutableList.of(e1, e2, e3).stream();
}

@AfterTemplate
Stream<T> after(T e1, T e2, T e3) {
return Stream.of(e1, e2, e3);
}
}

/** Prefer {@link Stream#of(Object[])} over more contrived alternatives. */
// XXX: Generalize this and similar rules using an Error Prone check.
static final class StreamOf4<T> {
@BeforeTemplate
Stream<T> before(T e1, T e2, T e3, T e4) {
return ImmutableList.of(e1, e2, e3, e4).stream();
}

@AfterTemplate
Stream<T> after(T e1, T e2, T e3, T e4) {
return Stream.of(e1, e2, e3, e4);
}
}

/** Prefer {@link Stream#of(Object[])} over more contrived alternatives. */
// XXX: Generalize this and similar rules using an Error Prone check.
static final class StreamOf5<T> {
@BeforeTemplate
Stream<T> before(T e1, T e2, T e3, T e4, T e5) {
return ImmutableList.of(e1, e2, e3, e4, e5).stream();
}

@AfterTemplate
Stream<T> after(T e1, T e2, T e3, T e4, T e5) {
return Stream.of(e1, e2, e3, e4, e5);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,4 @@ List<Integer> testImmutableListOf4() {
List<Integer> testImmutableListOf5() {
return List.of(1, 2, 3, 4, 5);
}

Stream<Integer> testStreamImmutableListOf1() {
return ImmutableList.of(1).stream();
}

Stream<Integer> testStreamImmutableListOf2() {
return ImmutableList.of(1, 2).stream();
}

Stream<Integer> testStreamImmutableListOf3() {
return ImmutableList.of(1, 2, 3).stream();
}

Stream<Integer> testStreamImmutableListOf4() {
return ImmutableList.of(1, 2, 3, 4).stream();
}

Stream<Integer> testStreamImmutableListOf5() {
return ImmutableList.of(1, 2, 3, 4, 5).stream();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,24 +83,4 @@ List<Integer> testImmutableListOf4() {
List<Integer> testImmutableListOf5() {
return ImmutableList.of(1, 2, 3, 4, 5);
}

Stream<Integer> testStreamImmutableListOf1() {
return Stream.of(1);
}

Stream<Integer> testStreamImmutableListOf2() {
return Stream.of(1, 2);
}

Stream<Integer> testStreamImmutableListOf3() {
return Stream.of(1, 2, 3);
}

Stream<Integer> testStreamImmutableListOf4() {
return Stream.of(1, 2, 3, 4);
}

Stream<Integer> testStreamImmutableListOf5() {
return Stream.of(1, 2, 3, 4, 5);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static java.util.stream.Collectors.summingInt;
import static java.util.stream.Collectors.summingLong;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import java.util.DoubleSummaryStatistics;
Expand All @@ -36,6 +37,7 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(
ImmutableList.class,
Objects.class,
Streams.class,
counting(),
Expand Down Expand Up @@ -266,4 +268,24 @@ Stream<Integer> testStreamTakeWhile() {
Stream<Integer> testStreamIterate() {
return Stream.iterate(0, i -> i + 1).takeWhile(i -> i < 10);
}

Stream<Integer> testStreamOf1() {
return ImmutableList.of(1).stream();
}

Stream<Integer> testStreamOf2() {
return ImmutableList.of(1, 2).stream();
}

Stream<Integer> testStreamOf3() {
return ImmutableList.of(1, 2, 3).stream();
}

Stream<Integer> testStreamOf4() {
return ImmutableList.of(1, 2, 3, 4).stream();
}

Stream<Integer> testStreamOf5() {
return ImmutableList.of(1, 2, 3, 4, 5).stream();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static java.util.stream.Collectors.summingInt;
import static java.util.stream.Collectors.summingLong;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import java.util.Arrays;
Expand All @@ -38,6 +39,7 @@ final class StreamRulesTest implements RefasterRuleCollectionTestCase {
@Override
public ImmutableSet<Object> elidedTypesAndStaticImports() {
return ImmutableSet.of(
ImmutableList.class,
Objects.class,
Streams.class,
counting(),
Expand Down Expand Up @@ -266,4 +268,24 @@ Stream<Integer> testStreamTakeWhile() {
Stream<Integer> testStreamIterate() {
return Stream.iterate(0, i -> i < 10, i -> i + 1);
}

Stream<Integer> testStreamOf1() {
return Stream.of(1);
}

Stream<Integer> testStreamOf2() {
return Stream.of(1, 2);
}

Stream<Integer> testStreamOf3() {
return Stream.of(1, 2, 3);
}

Stream<Integer> testStreamOf4() {
return Stream.of(1, 2, 3, 4);
}

Stream<Integer> testStreamOf5() {
return Stream.of(1, 2, 3, 4, 5);
}
}

0 comments on commit e7d6176

Please sign in to comment.