@Override public boolean tryAdvance(IntConsumer action) { boolean test = true; if (takeOrDrop && // If can take checkCancelOnCount() && // and if not cancelled s.tryAdvance(this) && // and if advanced one element (test = p.test(t))) { // and test on element passes action.accept(t); // then accept element return true; } else { // Taking is finished takeOrDrop = false; // Cancel all further traversal and splitting operations // only if test of element failed (short-circuited) if (!test) cancel.set(true); return false; } }
@Override default Node.OfInt truncate(long from, long to, IntFunction<Integer[]> generator) { if (from == 0 && to == count()) return this; long size = to - from; Spliterator.OfInt spliterator = spliterator(); Node.Builder.OfInt nodeBuilder = Nodes.intBuilder(size); nodeBuilder.begin(size); for (int i = 0; i < from && spliterator.tryAdvance((IntConsumer) e -> { }); i++) { } if (to == count()) { spliterator.forEachRemaining((IntConsumer) nodeBuilder); } else { for (int i = 0; i < size && spliterator.tryAdvance((IntConsumer) nodeBuilder); i++) { } } nodeBuilder.end(); return nodeBuilder.build(); }
public static Future<Void> list(GuildMessageReceivedEvent event, int timeoutSeconds, boolean canEveryoneUse, List<MessageEmbed> embeds, IntConsumer beforePageSent) { if(embeds.size() == 0) return null; AtomicInteger index = new AtomicInteger(); Message m = event.getChannel().sendMessage(embeds.get(0)).complete(); return ReactionOperations.create(m, timeoutSeconds, (e)->{ if(!canEveryoneUse && e.getUser().getIdLong() != event.getAuthor().getIdLong()) return Operation.IGNORED; switch(e.getReactionEmote().getName()) { case "\u2b05": //left arrow if(index.get() == 0) break; m.editMessage(embeds.get(index.decrementAndGet())).queue(); break; case "\u27a1": //right arrow if(index.get() + 1 >= embeds.size()) break; m.editMessage(embeds.get(index.incrementAndGet())).queue(); break; } if(event.getGuild().getSelfMember().hasPermission(e.getTextChannel(), Permission.MESSAGE_MANAGE)) { e.getReaction().removeReaction(e.getUser()).queue(); } return Operation.RESET_TIMEOUT; }, "\u2b05", "\u27a1"); }
@Override public final IntStream peek(IntConsumer action) { Objects.requireNonNull(action); return new StatelessOp<Integer>(this, StreamShape.INT_VALUE, 0) { @Override Sink<Integer> opWrapSink(int flags, Sink<Integer> sink) { return new Sink.ChainedInt<Integer>(sink) { @Override public void accept(int t) { action.accept(t); downstream.accept(t); } }; } }; }
public void testIntForEachRemainingWithNull() { PrimitiveIterator.OfInt i = new PrimitiveIterator.OfInt() { @Override public int nextInt() { return 0; } @Override public boolean hasNext() { return false; } }; executeAndCatch(() -> i.forEachRemaining((IntConsumer) null)); executeAndCatch(() -> i.forEachRemaining((Consumer<Integer>) null)); }
/** * Returns an infinite sequential ordered {@code IntStream} produced by iterative * application of a function {@code f} to an initial element {@code seed}, * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, * {@code f(f(seed))}, etc. * * <p>The first element (position {@code 0}) in the {@code IntStream} will be * the provided {@code seed}. For {@code n > 0}, the element at position * {@code n}, will be the result of applying the function {@code f} to the * element at position {@code n - 1}. * * <p>The action of applying {@code f} for one element * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a> * the action of applying {@code f} for subsequent elements. For any given * element the action may be performed in whatever thread the library * chooses. * * @param seed the initial element * @param f a function to be applied to the previous element to produce * a new element * @return a new sequential {@code IntStream} */ public static IntStream iterate(final int seed, final IntUnaryOperator f) { Objects.requireNonNull(f); Spliterator.OfInt spliterator = new Spliterators.AbstractIntSpliterator(Long.MAX_VALUE, Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) { int prev; boolean started; @Override public boolean tryAdvance(IntConsumer action) { Objects.requireNonNull(action); int t; if (started) t = f.applyAsInt(prev); else { t = seed; started = true; } action.accept(prev = t); return true; } }; return StreamSupport.intStream(spliterator, false); }
@Override public boolean tryAdvance(IntConsumer consumer) { Objects.requireNonNull(consumer); final int i = from; if (i < upTo) { from++; consumer.accept(i); return true; } else if (last > 0) { last = 0; consumer.accept(i); return true; } return false; }
@Override public void forEachRemaining(IntConsumer consumer) { Objects.requireNonNull(consumer); int i = from; final int hUpTo = upTo; int hLast = last; from = upTo; last = 0; while (i < hUpTo) { consumer.accept(i++); } if (hLast > 0) { // Last element of closed range consumer.accept(i); } }
@Override public boolean tryAdvance(IntConsumer action) { Objects.requireNonNull(action); if (count == -2) { action.accept(first); count = -1; return true; } else { return false; } }
@Override public boolean tryAdvance(IntConsumer action) { if (action == null) throw new NullPointerException(); if (index >= 0 && index < limit) { action.accept(buffer.getUnchecked(index++)); return true; } return false; }
public boolean tryAdvance(IntConsumer consumer) { if (consumer == null) throw new NullPointerException(); long i = index, f = fence; if (i < f) { consumer.accept(ThreadLocalRandom.current().internalNextInt(origin, bound)); index = i + 1; return true; } return false; }
@Override public boolean tryAdvance(IntConsumer action) { if (action == null) throw new NullPointerException(); int i = index; if (i >= 0 && i < fence) { action.accept(charAt(array, i)); index++; return true; } return false; }
@Override public boolean tryAdvance(IntConsumer action) { if (action == null) throw new NullPointerException(); if (index >= 0 && index < fence) { action.accept(array[index++] & 0xff); return true; } return false; }
@Test public void run() { Arrays.stream(arr).forEach(new IntConsumer() { @Override public void accept(int value) { System.out.println(value); } }); }
/** * 这里首先使用函数引用,直接定义了两个Int-Consumer接口实例,一个指向标准输出,另一个指向标准错误。 * 使用接口默认函数IntConsumer.addThen(),将两个IntConsumer进行组合,得到一个新的IntConsumer, * 这个新的Int-Consumer会依次调用outprintln和errprintln,完成对数组中元素的处理。 * * 这个新的IntConsumer会先调用第1个IntConsumer进行处理,接着调用第2个Int-Consumer处理,从而实现多个处理器的整合 */ @Test public void run3() { IntConsumer outprintln = System.out::println; IntConsumer errprintln = System.err::println; Arrays.stream(arr).forEach(outprintln.andThen(errprintln)); }
public static void main(String[] args) { Arrays.stream(arr).forEach(new IntConsumer() { @Override public void accept(int value) { System.out.println(value); } }); }
public boolean tryAdvance(IntConsumer consumer) { if (consumer == null) throw new NullPointerException(); long i = index, f = fence; if (i < f) { consumer.accept(rng.internalNextInt(origin, bound)); index = i + 1; return true; } return false; }
@Override public void forEachRemaining(IntConsumer action) { int[] a; int i, hi; // hoist accesses and checks from loop if (action == null) throw new NullPointerException(); if ((a = array).length >= (hi = fence) && (i = index) >= 0 && i < (index = hi)) { do { action.accept(a[i]); } while (++i < hi); } }
/** * {@inheritDoc} * @implSpec * If the action is an instance of {@code IntConsumer} then it is cast * to {@code IntConsumer} and passed to * {@link #tryAdvance(java.util.function.IntConsumer)}; otherwise * the action is adapted to an instance of {@code IntConsumer}, by * boxing the argument of {@code IntConsumer}, and then passed to * {@link #tryAdvance(java.util.function.IntConsumer)}. */ @Override default boolean tryAdvance(Consumer<? super Integer> action) { if (action instanceof IntConsumer) { return tryAdvance((IntConsumer) action); } else { if (Tripwire.ENABLED) Tripwire.trip(getClass(), "{0} calling Spliterator.OfInt.tryAdvance((IntConsumer) action::accept)"); return tryAdvance((IntConsumer) action::accept); } }
/** * Creates an {@code PrimitiveIterator.OfInt} from a * {@code Spliterator.OfInt}. * * <p>Traversal of elements should be accomplished through the iterator. * The behaviour of traversal is undefined if the spliterator is operated * after the iterator is returned. * * @param spliterator The spliterator * @return An iterator * @throws NullPointerException if the given spliterator is {@code null} */ public static PrimitiveIterator.OfInt iterator(Spliterator.OfInt spliterator) { Objects.requireNonNull(spliterator); class Adapter implements PrimitiveIterator.OfInt, IntConsumer { boolean valueReady = false; int nextElement; @Override public void accept(int t) { valueReady = true; nextElement = t; } @Override public boolean hasNext() { if (!valueReady) spliterator.tryAdvance(this); return valueReady; } @Override public int nextInt() { if (!valueReady && !hasNext()) throw new NoSuchElementException(); else { valueReady = false; return nextElement; } } } return new Adapter(); }
@Override public final IntStream flatMapToInt(Function<? super P_OUT, ? extends IntStream> mapper) { Objects.requireNonNull(mapper); // We can do better than this, by polling cancellationRequested when stream is infinite return new IntPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) { @Override Sink<P_OUT> opWrapSink(int flags, Sink<Integer> sink) { return new Sink.ChainedReference<P_OUT, Integer>(sink) { IntConsumer downstreamAsInt = downstream::accept; @Override public void begin(long size) { downstream.begin(-1); } @Override public void accept(P_OUT u) { try (IntStream result = mapper.apply(u)) { // We can do better that this too; optimize for depth=0 case and just grab spliterator and forEach it if (result != null) result.sequential().forEach(downstreamAsInt); } } }; } }; }
@Override public void forEach(Consumer<? super Integer> consumer) { if (consumer instanceof IntConsumer) { forEach((IntConsumer) consumer); } else { if (Tripwire.ENABLED) Tripwire.trip(getClass(), "{0} calling SpinedBuffer.OfInt.forEach(Consumer)"); spliterator().forEachRemaining(consumer); } }
@Override public boolean tryAdvance(IntConsumer consumer) { Objects.requireNonNull(consumer); boolean hasNext = doAdvance(); if (hasNext) consumer.accept(buffer.get(nextToConsume)); return hasNext; }
@Override public boolean tryAdvance(IntConsumer action) { if (action == null) throw new NullPointerException(); if (index >= 0 && index < fence) { index = advance(array, index, fence, action); return true; } return false; }
@Override public boolean tryAdvance(IntConsumer action) { Objects.requireNonNull(action); action.accept(s.getAsInt()); return true; }
/** * {@inheritDoc} * * @param consumer a {@code Consumer} that is to be invoked with each * element in this {@code Node}. If this is an * {@code IntConsumer}, it is cast to {@code IntConsumer} so the * elements may be processed without boxing. */ @Override default void forEach(Consumer<? super Integer> consumer) { if (consumer instanceof IntConsumer) { forEach((IntConsumer) consumer); } else { if (Tripwire.ENABLED) Tripwire.trip(getClass(), "{0} calling Node.OfInt.forEachRemaining(Consumer)"); spliterator().forEachRemaining(consumer); } }
public void forEachRemaining(IntConsumer consumer) { if (consumer == null) throw new NullPointerException(); long i = index, f = fence; if (i < f) { index = f; Random r = rng; int o = origin, b = bound; do { consumer.accept(r.internalNextInt(o, b)); } while (++i < f); } }
@Override public void forEach(IntConsumer action) { if (!isParallel()) { adapt(sourceStageSpliterator()).forEachRemaining(action); } else { super.forEach(action); } }
@Override public void forEachOrdered(IntConsumer action) { if (!isParallel()) { adapt(sourceStageSpliterator()).forEachRemaining(action); } else { super.forEachOrdered(action); } }
/** * {@inheritDoc} * @implSpec * If the action is an instance of {@code IntConsumer} then it is cast * to {@code IntConsumer} and passed to {@link #forEachRemaining}; * otherwise the action is adapted to an instance of * {@code IntConsumer}, by boxing the argument of {@code IntConsumer}, * and then passed to {@link #forEachRemaining}. */ @Override default void forEachRemaining(Consumer<? super Integer> action) { if (action instanceof IntConsumer) { forEachRemaining((IntConsumer) action); } else { // The method reference action::accept is never null Objects.requireNonNull(action); if (Tripwire.ENABLED) Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfInt.forEachRemainingInt(action::accept)"); forEachRemaining((IntConsumer) action::accept); } }
/** * {@inheritDoc} * @implSpec * If the action is an instance of {@code IntConsumer} then it is cast * to {@code IntConsumer} and passed to * {@link #forEachRemaining(java.util.function.IntConsumer)}; otherwise * the action is adapted to an instance of {@code IntConsumer}, by * boxing the argument of {@code IntConsumer}, and then passed to * {@link #forEachRemaining(java.util.function.IntConsumer)}. */ @Override default void forEachRemaining(Consumer<? super Integer> action) { if (action instanceof IntConsumer) { forEachRemaining((IntConsumer) action); } else { if (Tripwire.ENABLED) Tripwire.trip(getClass(), "{0} calling Spliterator.OfInt.forEachRemaining((IntConsumer) action::accept)"); forEachRemaining((IntConsumer) action::accept); } }
@Override public boolean tryAdvance(IntConsumer action) { if (action == null) throw new NullPointerException(); if (it.hasNext()) { action.accept(it.nextInt()); return true; } return false; }
public void forEachRemaining(IntConsumer consumer) { if (consumer == null) throw new NullPointerException(); long i = index, f = fence; if (i < f) { index = f; int o = origin, b = bound; ThreadLocalRandom rng = ThreadLocalRandom.current(); do { consumer.accept(rng.internalNextInt(o, b)); } while (++i < f); } }