Skip to content

Commit

Permalink
chore(test-util): fix take while
Browse files Browse the repository at this point in the history
 * add logic to have takeWhileInclusive
  • Loading branch information
Zelldon committed Aug 20, 2019
1 parent dc57eba commit a4f5ca0
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;
import java.util.Optional;
import java.util.Spliterator;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
Expand Down Expand Up @@ -51,7 +52,17 @@ public S skipUntil(Predicate<T> predicate) {
* short-circuiting operation; limits the stream to the first element that fulfills the predicate
*/
public S limit(Predicate<T> predicate) {
return supply(this.takeWhile(predicate.negate()));
final AtomicBoolean inclusionFlag = new AtomicBoolean(true);
return supply(
this.takeWhile(
s ->
// #takeWhile + inclusion of element which fulfills the predicate
// when predicate negation becomes false, this means predicate is fulfilled
// we toggle the flag, this means we will add this to the stream
// not to add everything after that we have to check the inclusion flag,
// which is then false -> takeWhile will stop then
(inclusionFlag.get() && predicate.negate().test(s))
|| inclusionFlag.compareAndSet(true, false)));
}

// Helper to extract values
Expand Down

0 comments on commit a4f5ca0

Please sign in to comment.