Skip to content

Commit

Permalink
Introduce {Flux,{Double,Int,Long,}Stream}TakeWhile Refaster rules (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsamehsalah authored Aug 12, 2023
1 parent 17cc8e0 commit 35a5944
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,16 @@ boolean after(DoubleStream stream) {
return stream.allMatch(e -> test(e));
}
}

static final class DoubleStreamTakeWhile {
@BeforeTemplate
DoubleStream before(DoubleStream stream, DoublePredicate predicate) {
return stream.takeWhile(predicate).filter(predicate);
}

@AfterTemplate
DoubleStream after(DoubleStream stream, DoublePredicate predicate) {
return stream.takeWhile(predicate);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,16 @@ boolean after(IntStream stream) {
return stream.allMatch(e -> test(e));
}
}

static final class IntStreamTakeWhile {
@BeforeTemplate
IntStream before(IntStream stream, IntPredicate predicate) {
return stream.takeWhile(predicate).filter(predicate);
}

@AfterTemplate
IntStream after(IntStream stream, IntPredicate predicate) {
return stream.takeWhile(predicate);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,16 @@ boolean after(LongStream stream) {
return stream.allMatch(e -> test(e));
}
}

static final class LongStreamTakeWhile {
@BeforeTemplate
LongStream before(LongStream stream, LongPredicate predicate) {
return stream.takeWhile(predicate).filter(predicate);
}

@AfterTemplate
LongStream after(LongStream stream, LongPredicate predicate) {
return stream.takeWhile(predicate);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,22 @@ Flux<T> after(Flux<T> flux, Predicate<? super T> predicate, Comparator<? super T
}
}

/**
* Do not unnecessarily {@link Flux#filter(Predicate) filter} the result of {@link
* Flux#takeWhile(Predicate)} using the same {@link Predicate}.
*/
static final class FluxTakeWhile<T> {
@BeforeTemplate
Flux<T> before(Flux<T> flux, Predicate<? super T> predicate) {
return flux.takeWhile(predicate).filter(predicate);
}

@AfterTemplate
Flux<T> after(Flux<T> flux, Predicate<? super T> predicate) {
return flux.takeWhile(predicate);
}
}

/**
* Prefer {@link Flux#collect(Collector)} with {@link ImmutableList#toImmutableList()} over
* alternatives that do not explicitly return an immutable collection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -639,4 +639,16 @@ Stream<T> after(@Repeated Stream<T> stream) {
return Streams.concat(Refaster.asVarargs(stream));
}
}

static final class StreamTakeWhile<T> {
@BeforeTemplate
Stream<T> before(Stream<T> stream, Predicate<? super T> predicate) {
return stream.takeWhile(predicate).filter(predicate);
}

@AfterTemplate
Stream<T> after(Stream<T> stream, Predicate<? super T> predicate) {
return stream.takeWhile(predicate);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,8 @@ boolean testDoubleStreamAllMatch() {
boolean testDoubleStreamAllMatch2() {
return DoubleStream.of(1).noneMatch(n -> !(n > 1));
}

DoubleStream testDoubleStreamTakeWhile() {
return DoubleStream.of(1, 2, 3).takeWhile(i -> i < 2).filter(i -> i < 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,8 @@ boolean testDoubleStreamAllMatch() {
boolean testDoubleStreamAllMatch2() {
return DoubleStream.of(1).allMatch(n -> n > 1);
}

DoubleStream testDoubleStreamTakeWhile() {
return DoubleStream.of(1, 2, 3).takeWhile(i -> i < 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,8 @@ boolean testIntStreamAllMatch() {
boolean testIntStreamAllMatch2() {
return IntStream.of(1).noneMatch(n -> !(n > 1));
}

IntStream testIntStreamTakeWhile() {
return IntStream.of(1, 2, 3).takeWhile(i -> i < 2).filter(i -> i < 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,8 @@ boolean testIntStreamAllMatch() {
boolean testIntStreamAllMatch2() {
return IntStream.of(1).allMatch(n -> n > 1);
}

IntStream testIntStreamTakeWhile() {
return IntStream.of(1, 2, 3).takeWhile(i -> i < 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,8 @@ boolean testLongStreamAllMatch() {
boolean testLongStreamAllMatch2() {
return LongStream.of(1).noneMatch(n -> !(n > 1));
}

LongStream testLongStreamTakeWhile() {
return LongStream.of(1, 2, 3).takeWhile(i -> i < 2).filter(i -> i < 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,8 @@ boolean testLongStreamAllMatch() {
boolean testLongStreamAllMatch2() {
return LongStream.of(1).allMatch(n -> n > 1);
}

LongStream testLongStreamTakeWhile() {
return LongStream.of(1, 2, 3).takeWhile(i -> i < 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ Flux<Integer> testFluxFilterSortWithComparator() {
return Flux.just(1, 4, 3, 2).sort(reverseOrder()).filter(i -> i % 2 == 0);
}

Flux<Integer> testFluxTakeWhile() {
return Flux.just(1, 2, 3).takeWhile(i -> i % 2 == 0).filter(i -> i % 2 == 0);
}

Mono<List<Integer>> testFluxCollectToImmutableList() {
return Flux.just(1).collectList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ Flux<Integer> testFluxFilterSortWithComparator() {
return Flux.just(1, 4, 3, 2).filter(i -> i % 2 == 0).sort(reverseOrder());
}

Flux<Integer> testFluxTakeWhile() {
return Flux.just(1, 2, 3).takeWhile(i -> i % 2 == 0);
}

Mono<List<Integer>> testFluxCollectToImmutableList() {
return Flux.just(1).collect(toImmutableList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,8 @@ ImmutableSet<Stream<Integer>> testStreamsConcat() {
Stream.of(Stream.of(1), Stream.of(2)).flatMap(identity()),
Stream.of(Stream.of(3), Stream.of(4)).flatMap(v -> v));
}

Stream<Integer> testStreamTakeWhile() {
return Stream.of(1, 2, 3).takeWhile(i -> i < 2).filter(i -> i < 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,8 @@ ImmutableSet<Stream<Integer>> testStreamsConcat() {
return ImmutableSet.of(
Streams.concat(Stream.of(1), Stream.of(2)), Streams.concat(Stream.of(3), Stream.of(4)));
}

Stream<Integer> testStreamTakeWhile() {
return Stream.of(1, 2, 3).takeWhile(i -> i < 2);
}
}

0 comments on commit 35a5944

Please sign in to comment.