private boolean init() { if(context == null) return false; if(target == null) { Box<T> first = new Box<>(); source = TailSpliterator.tryAdvanceWithTail(source, first); Stream<U> stream = source == null ? emptyMapper.get() : mapper.apply(first.a, StreamEx.of(source)); source = null; mapper = null; emptyMapper = null; if(stream == null) { target = Spliterators.emptySpliterator(); } else { StreamContext ctx = StreamContext.of(stream); if(ctx.closeHandler != null) context.onClose(ctx.closeHandler); target = stream.spliterator(); } } return true; }
@Override public Spliterator<R> tryAdvanceOrTail(Consumer<? super R> action) { if (mode != MODE_MAP_FIRST || right != EMPTY) { return tryAdvance(action) ? this : null; } if (left != null) { Sink<T> l = left; left = null; source = TailSpliterator.tryAdvanceWithTail(source, this); if (source == null) { right = null; return null; } if (l.push(cur, fn(action), false)) return this; } @SuppressWarnings("unchecked") Spliterator<R> s = (Spliterator<R>)source; source = null; return s; }
@Override public boolean tryAdvance(Consumer<? super T> action) { if (left != null) { if (left.tryAdvance(action)) { if (size > 0 && size != Long.MAX_VALUE) size--; return true; } left = null; } if(right != null) right = TailSpliterator.tryAdvanceWithTail(right, action); return right != null; }
@Override public boolean tryAdvance(Consumer<? super U> action) { if(!init()) return false; target = TailSpliterator.tryAdvanceWithTail(target, action); if(target == null) { context = null; return false; } return true; }