Skip to content

Commit 3557062

Browse files
authored
Introduce {Flux,Stream}DistinctSort{,ed}{,WithComparator} Refaster rules (#1950)
While there, simplify some unrelated test cases. Resolves #1828.
1 parent 89a3bf3 commit 3557062

File tree

6 files changed

+104
-8
lines changed

6 files changed

+104
-8
lines changed

error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,38 @@ Flux<T> after(Flux<T> flux, Predicate<? super T> predicate, Comparator<? super T
19351935
}
19361936
}
19371937

1938+
/**
1939+
* Apply {@link Flux#distinct()} before {@link Flux#sort()} to reduce the number of elements to
1940+
* sort.
1941+
*/
1942+
static final class FluxDistinctSort<T> {
1943+
@BeforeTemplate
1944+
Flux<T> before(Flux<T> flux) {
1945+
return flux.sort().distinct();
1946+
}
1947+
1948+
@AfterTemplate
1949+
Flux<T> after(Flux<T> flux) {
1950+
return flux.distinct().sort();
1951+
}
1952+
}
1953+
1954+
/**
1955+
* Apply {@link Flux#distinct()} before {@link Flux#sort(Comparator)} to reduce the number of
1956+
* elements to sort.
1957+
*/
1958+
static final class FluxDistinctSortWithComparator<S, T extends S> {
1959+
@BeforeTemplate
1960+
Flux<T> before(Flux<T> flux, Comparator<S> comparator) {
1961+
return flux.sort(comparator).distinct();
1962+
}
1963+
1964+
@AfterTemplate
1965+
Flux<T> after(Flux<T> flux, Comparator<S> comparator) {
1966+
return flux.distinct().sort(comparator);
1967+
}
1968+
}
1969+
19381970
/**
19391971
* Do not unnecessarily {@link Flux#filter(Predicate) filter} the result of {@link
19401972
* Flux#takeWhile(Predicate)} using the same {@link Predicate}.

error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/StreamRules.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,38 @@ Stream<T> after(
250250
}
251251
}
252252

253+
/**
254+
* Apply {@link Stream#distinct()} before {@link Stream#sorted()} to reduce the number of elements
255+
* to sort.
256+
*/
257+
static final class StreamDistinctSorted<T> {
258+
@BeforeTemplate
259+
Stream<T> before(Stream<T> stream) {
260+
return stream.sorted().distinct();
261+
}
262+
263+
@AfterTemplate
264+
Stream<T> after(Stream<T> stream) {
265+
return stream.distinct().sorted();
266+
}
267+
}
268+
269+
/**
270+
* Apply {@link Stream#distinct()} before {@link Stream#sorted(Comparator)} to reduce the number
271+
* of elements to sort.
272+
*/
273+
static final class StreamDistinctSortedWithComparator<S, T extends S> {
274+
@BeforeTemplate
275+
Stream<T> before(Stream<T> stream, Comparator<S> comparator) {
276+
return stream.sorted(comparator).distinct();
277+
}
278+
279+
@AfterTemplate
280+
Stream<T> after(Stream<T> stream, Comparator<S> comparator) {
281+
return stream.distinct().sorted(comparator);
282+
}
283+
}
284+
253285
/**
254286
* Where possible, clarify that a mapping operation will be applied only to a single stream
255287
* element.

error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,11 +688,19 @@ Flux<Integer> testFluxOnErrorReturn() {
688688
}
689689

690690
Flux<Integer> testFluxFilterSort() {
691-
return Flux.just(1, 4, 3, 2).sort().filter(i -> i % 2 == 0);
691+
return Flux.just(1).sort().filter(i -> i % 2 == 0);
692692
}
693693

694694
Flux<Integer> testFluxFilterSortWithComparator() {
695-
return Flux.just(1, 4, 3, 2).sort(reverseOrder()).filter(i -> i % 2 == 0);
695+
return Flux.just(1).sort(reverseOrder()).filter(i -> i % 2 == 0);
696+
}
697+
698+
Flux<Integer> testFluxDistinctSort() {
699+
return Flux.just(1).sort().distinct();
700+
}
701+
702+
Flux<Integer> testFluxDistinctSortWithComparator() {
703+
return Flux.just(1).sort(reverseOrder()).distinct();
696704
}
697705

698706
Flux<Integer> testFluxTakeWhile() {

error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,11 +663,19 @@ Flux<Integer> testFluxOnErrorReturn() {
663663
}
664664

665665
Flux<Integer> testFluxFilterSort() {
666-
return Flux.just(1, 4, 3, 2).filter(i -> i % 2 == 0).sort();
666+
return Flux.just(1).filter(i -> i % 2 == 0).sort();
667667
}
668668

669669
Flux<Integer> testFluxFilterSortWithComparator() {
670-
return Flux.just(1, 4, 3, 2).filter(i -> i % 2 == 0).sort(reverseOrder());
670+
return Flux.just(1).filter(i -> i % 2 == 0).sort(reverseOrder());
671+
}
672+
673+
Flux<Integer> testFluxDistinctSort() {
674+
return Flux.just(1).distinct().sort();
675+
}
676+
677+
Flux<Integer> testFluxDistinctSortWithComparator() {
678+
return Flux.just(1).distinct().sort(reverseOrder());
671679
}
672680

673681
Flux<Integer> testFluxTakeWhile() {

error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestInput.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,19 @@ Stream<Integer> testFlatMapOuterStreamAfterFlatMap() {
122122
}
123123

124124
Stream<Integer> testStreamFilterSorted() {
125-
return Stream.of(1, 4, 3, 2).sorted().filter(i -> i % 2 == 0);
125+
return Stream.of(1).sorted().filter(i -> i % 2 == 0);
126126
}
127127

128128
Stream<Integer> testStreamFilterSortedWithComparator() {
129-
return Stream.of(1, 4, 3, 2).sorted(reverseOrder()).filter(i -> i % 2 == 0);
129+
return Stream.of(1).sorted(reverseOrder()).filter(i -> i % 2 == 0);
130+
}
131+
132+
Stream<Integer> testStreamDistinctSorted() {
133+
return Stream.of(1).sorted().distinct();
134+
}
135+
136+
Stream<Integer> testStreamDistinctSortedWithComparator() {
137+
return Stream.of(1).sorted(reverseOrder()).distinct();
130138
}
131139

132140
ImmutableSet<Optional<Integer>> testStreamMapFirst() {

error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/StreamRulesTestOutput.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,19 @@ Stream<Integer> testFlatMapOuterStreamAfterFlatMap() {
123123
}
124124

125125
Stream<Integer> testStreamFilterSorted() {
126-
return Stream.of(1, 4, 3, 2).filter(i -> i % 2 == 0).sorted();
126+
return Stream.of(1).filter(i -> i % 2 == 0).sorted();
127127
}
128128

129129
Stream<Integer> testStreamFilterSortedWithComparator() {
130-
return Stream.of(1, 4, 3, 2).filter(i -> i % 2 == 0).sorted(reverseOrder());
130+
return Stream.of(1).filter(i -> i % 2 == 0).sorted(reverseOrder());
131+
}
132+
133+
Stream<Integer> testStreamDistinctSorted() {
134+
return Stream.of(1).distinct().sorted();
135+
}
136+
137+
Stream<Integer> testStreamDistinctSortedWithComparator() {
138+
return Stream.of(1).distinct().sorted(reverseOrder());
131139
}
132140

133141
ImmutableSet<Optional<Integer>> testStreamMapFirst() {

0 commit comments

Comments
 (0)