Interface LongStreamEx.LongEmitter
-
- Enclosing class:
- LongStreamEx
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface public static interface LongStreamEx.LongEmitter
A helper interface to build a new stream by emitting elements and creating new emitters in a chain.Using this interface it's possible to create custom sources which cannot be easily expressed using
LongStreamEx.iterate(long, LongUnaryOperator)
orLongStreamEx.generate(LongSupplier)
. For example, the following method generates a Collatz sequence starting from given number:public static LongEmitter collatz(long start) { return action -> { action.accept(start); return start == 1 ? null : collatz(start % 2 == 0 ? start / 2 : start * 3 + 1); }; }
Now you can use
collatz(17).stream()
to get the stream of Collatz numbers.- Since:
- 0.6.0
- Author:
- Tagir Valeev
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description LongStreamEx.LongEmitter
next(LongConsumer action)
Calls the supplied consumer zero or more times to emit some elements, then returns the next emitter which will emit more, or null if nothing more to emit.default Spliterator.OfLong
spliterator()
Returns the spliterator which covers all the elements emitted by this emitter.default LongStreamEx
stream()
Returns the stream which covers all the elements emitted by this emitter.
-
-
-
Method Detail
-
next
LongStreamEx.LongEmitter next(LongConsumer action)
Calls the supplied consumer zero or more times to emit some elements, then returns the next emitter which will emit more, or null if nothing more to emit.Normally one element is emitted during the
next()
method call. However, it's not restricted: you may emit as many elements as you want, though in some cases if many elements were emitted they might be buffered consuming additional memory.It's allowed not to emit anything (don't call the consumer). However if you do this and return new emitter which also does not emit anything, you will end up in endless loop.
- Parameters:
action
- consumer to be called to emit elements- Returns:
- next emitter or null
-
spliterator
default Spliterator.OfLong spliterator()
Returns the spliterator which covers all the elements emitted by this emitter.- Returns:
- the new spliterator
-
stream
default LongStreamEx stream()
Returns the stream which covers all the elements emitted by this emitter.- Returns:
- the new stream
-
-