Skip to content

Commit

Permalink
Introduce ArraysAsList refaster rule
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsamehsalah committed Aug 10, 2024
1 parent 32ec35a commit 42321e0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
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;
Expand Down Expand Up @@ -122,6 +123,21 @@ Stream<T> after(T[] array) {
}
}

/**
* Prefer {@link Arrays#asList(Object[])} over {@link Stream#toList()} as the former is clearer.
*/
static final class ArraysAsList<T> {
@BeforeTemplate
List<T> before(@NotMatches(IsRefasterAsVarargs.class) T[] array) {
return Refaster.anyOf(Arrays.stream(array).toList(), Stream.of(array).toList());
}

@AfterTemplate
List<T> after(T[] array) {
return Arrays.asList(array);
}
}

/** Don't unnecessarily call {@link Streams#concat(Stream...)}. */
static final class ConcatOneStream<T> {
@BeforeTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand Down Expand Up @@ -86,6 +87,12 @@ Stream<String> testStreamOfArray() {
return Stream.of(new String[] {"foo", "bar"});
}

ImmutableSet<List<String>> testArraysAsList() {
return ImmutableSet.of(
Arrays.stream(new String[] {"foo", "bar"}).toList(),
Stream.of(new String[] {"foo", "bar"}).toList());
}

Stream<Integer> testConcatOneStream() {
return Streams.concat(Stream.of(1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ Stream<String> testStreamOfArray() {
return Arrays.stream(new String[] {"foo", "bar"});
}

ImmutableSet<List<String>> testArraysAsList() {
return ImmutableSet.of(
Arrays.asList(new String[] {"foo", "bar"}), Arrays.asList(new String[] {"foo", "bar"}));
}

Stream<Integer> testConcatOneStream() {
return Stream.of(1);
}
Expand Down

0 comments on commit 42321e0

Please sign in to comment.