/** * Return an ordered stream produced by consecutive calls of the supplied * producer until it returns false. * * <p> * The producer function may call the passed consumer any number of times * and return true if the producer should be called again or false * otherwise. It's guaranteed that the producer will not be called anymore, * once it returns false. * * <p> * This method is particularly useful when producer changes the mutable * object which should be left in known state after the full stream * consumption. Note however that if a short-circuiting operation is used, * then the final state of the mutable object cannot be guaranteed. * * @param producer a predicate which calls the passed consumer to emit * stream element(s) and returns true if it producer should be * applied again. * @return the new stream * @since 0.6.0 */ public static LongStreamEx produce(Predicate<LongConsumer> producer) { Box<LongEmitter> box = new Box<>(); return (box.a = action -> producer.test(action) ? box.a : null).stream(); }
@Override public boolean tryAdvance(LongConsumer action) { if (buf != null) { if (buf.tryAdvance(action)) return true; buf = null; } cons = action; for (vals = 0; vals == 0; e = e.next(this)) { if (e == null) return false; } if (vals > 1) { buf = ((LongStream.Builder) cons).build().spliterator(); } cons = null; return true; }
@Override public void forEachRemaining(LongConsumer action) { if (buf != null) { buf.forEachRemaining(action); buf = null; } LongStreamEx.LongEmitter e = this.e; this.e = null; while (e != null) e = e.next(action); }
/** * Returns the stream which covers all the elements emitted by this * emitter. * * @return the new stream */ default LongStreamEx stream() { return of(spliterator()); } }