diff options
author | Vitaly Takmazov | 2018-06-13 11:58:48 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-06-13 11:58:48 +0300 |
commit | d9cc4e7060e81a583016b7b69203ab397f3d3478 (patch) | |
tree | d129d44e953b1b083aef0ca376c53d1f106b1dad /juick-common/src/main/java/com/juick/util/StreamUtils.java | |
parent | 47fca902ee7b11a4bead5fefc34fcc5be0550436 (diff) |
fix processing message with empty * tag
Diffstat (limited to 'juick-common/src/main/java/com/juick/util/StreamUtils.java')
-rw-r--r-- | juick-common/src/main/java/com/juick/util/StreamUtils.java | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/juick-common/src/main/java/com/juick/util/StreamUtils.java b/juick-common/src/main/java/com/juick/util/StreamUtils.java new file mode 100644 index 00000000..de19a43e --- /dev/null +++ b/juick-common/src/main/java/com/juick/util/StreamUtils.java @@ -0,0 +1,34 @@ +package com.juick.util; + +import java.util.Spliterator; +import java.util.Spliterators; +import java.util.function.Consumer; +import java.util.function.Predicate; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +public class StreamUtils { + private static <T> Spliterator<T> takeWhile( + Spliterator<T> splitr, Predicate<? super T> predicate) { + return new Spliterators.AbstractSpliterator<T>(splitr.estimateSize(), 0) { + boolean stillGoing = true; + @Override public boolean tryAdvance(Consumer<? super T> consumer) { + if (stillGoing) { + boolean hadNext = splitr.tryAdvance(elem -> { + if (predicate.test(elem)) { + consumer.accept(elem); + } else { + stillGoing = false; + } + }); + return hadNext && stillGoing; + } + return false; + } + }; + } + + public static <T> Stream<T> takeWhile(Stream<T> stream, Predicate<? super T> predicate) { + return StreamSupport.stream(takeWhile(stream.spliterator(), predicate), false); + } +} |