Java Code Examples for java.util.function.IntConsumer#accept()

The following examples show how to use java.util.function.IntConsumer#accept() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Streams.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@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);
    }
}
 
Example 2
Source File: IntStream.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: EdgeSpliterator.java    From constellation with Apache License 2.0 5 votes vote down vote up
@Override
public boolean tryAdvance(final IntConsumer action) {
    if (position < limit) {
        final int vxId = rg.getEdge(position++);
        action.accept(vxId);
        return true;
    }

    return false;
}
 
Example 4
Source File: CharSequence.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns a stream of {@code int} zero-extending the {@code char} values
 * from this sequence.  Any char which maps to a <a
 * href="{@docRoot}/java/lang/Character.html#unicode">surrogate code
 * point</a> is passed through uninterpreted.
 *
 * <p>If the sequence is mutated while the stream is being read, the
 * result is undefined.
 *
 * @return an IntStream of char values from this sequence
 * @since 1.8
 */
public default IntStream chars() {
    class CharIterator implements PrimitiveIterator.OfInt {
        int cur = 0;

        public boolean hasNext() {
            return cur < length();
        }

        public int nextInt() {
            if (hasNext()) {
                return charAt(cur++);
            } else {
                throw new NoSuchElementException();
            }
        }

        @Override
        public void forEachRemaining(IntConsumer block) {
            for (; cur < length(); cur++) {
                block.accept(charAt(cur));
            }
        }
    }

    return StreamSupport.intStream(() ->
            Spliterators.spliterator(
                    new CharIterator(),
                    length(),
                    Spliterator.ORDERED),
            Spliterator.SUBSIZED | Spliterator.SIZED | Spliterator.ORDERED,
            false);
}
 
Example 5
Source File: Streams.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean tryAdvance(IntConsumer action) {
    Objects.requireNonNull(action);

    if (count == -2) {
        action.accept(first);
        count = -1;
        return true;
    }
    else {
        return false;
    }
}
 
Example 6
Source File: StreamSpliterators.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean tryAdvance(IntConsumer consumer) {
    Objects.requireNonNull(consumer);
    boolean hasNext = doAdvance();
    if (hasNext)
        consumer.accept(buffer.get(nextToConsume));
    return hasNext;
}
 
Example 7
Source File: StreamSpliterators.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean tryAdvance(IntConsumer consumer) {
    Objects.requireNonNull(consumer);
    boolean hasNext = doAdvance();
    if (hasNext)
        consumer.accept(buffer.get(nextToConsume));
    return hasNext;
}
 
Example 8
Source File: Random.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: Streams.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Override
public void forEachRemaining(IntConsumer action) {
    Objects.requireNonNull(action);

    if (count == -2) {
        action.accept(first);
        count = -1;
    }
}
 
Example 10
Source File: CharBufferSpliterator.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 11
Source File: SpinedBuffer.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
@Override
protected void arrayForEach(int[] array,
                            int from, int to,
                            IntConsumer consumer) {
    for (int i = from; i < to; i++)
        consumer.accept(array[i]);
}
 
Example 12
Source File: CharSequence.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a stream of {@code int} zero-extending the {@code char} values
 * from this sequence.  Any char which maps to a <a
 * href="{@docRoot}/java/lang/Character.html#unicode">surrogate code
 * point</a> is passed through uninterpreted.
 *
 * <p>If the sequence is mutated while the stream is being read, the
 * result is undefined.
 *
 * @return an IntStream of char values from this sequence
 * @since 1.8
 */
public default IntStream chars() {
    class CharIterator implements PrimitiveIterator.OfInt {
        int cur = 0;

        public boolean hasNext() {
            return cur < length();
        }

        public int nextInt() {
            if (hasNext()) {
                return charAt(cur++);
            } else {
                throw new NoSuchElementException();
            }
        }

        @Override
        public void forEachRemaining(IntConsumer block) {
            for (; cur < length(); cur++) {
                block.accept(charAt(cur));
            }
        }
    }

    return StreamSupport.intStream(() ->
            Spliterators.spliterator(
                    new CharIterator(),
                    length(),
                    Spliterator.ORDERED),
            Spliterator.SUBSIZED | Spliterator.SIZED | Spliterator.ORDERED,
            false);
}
 
Example 13
Source File: SplittableRandom.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void forEachRemaining(IntConsumer consumer) {
    if (consumer == null) throw new NullPointerException();
    long i = index, f = fence;
    if (i < f) {
        index = f;
        SplittableRandom r = rng;
        int o = origin, b = bound;
        do {
            consumer.accept(r.internalNextInt(o, b));
        } while (++i < f);
    }
}
 
Example 14
Source File: SplittableRandom.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 15
Source File: CharBufferSpliterator.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Override
public void forEachRemaining(IntConsumer action) {
    if (action == null)
        throw new NullPointerException();
    CharBuffer cb = buffer;
    int i = index;
    int hi = limit;
    index = hi;
    while (i < hi) {
        action.accept(cb.getUnchecked(i++));
    }
}
 
Example 16
Source File: Nodes.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void forEach(IntConsumer consumer) {
    for (int i = 0; i < curSize; i++) {
        consumer.accept(array[i]);
    }
}
 
Example 17
Source File: StreamSpliterators.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void forEach(IntConsumer action, long fence) {
    for (int i = 0; i < fence; i++) {
        action.accept(array[i]);
    }
}
 
Example 18
Source File: StreamSpliterators.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void acceptConsumed(IntConsumer action) {
    action.accept(tmpValue);
}
 
Example 19
Source File: PrimitiveIterator.java    From openjdk-jdk8u with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Performs the given action for each remaining element until all elements
 * have been processed or the action throws an exception.  Actions are
 * performed in the order of iteration, if that order is specified.
 * Exceptions thrown by the action are relayed to the caller.
 *
 * @implSpec
 * <p>The default implementation behaves as if:
 * <pre>{@code
 *     while (hasNext())
 *         action.accept(nextInt());
 * }</pre>
 *
 * @param action The action to be performed for each element
 * @throws NullPointerException if the specified action is null
 */
default void forEachRemaining(IntConsumer action) {
    Objects.requireNonNull(action);
    while (hasNext())
        action.accept(nextInt());
}
 
Example 20
Source File: PrimitiveIterator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Performs the given action for each remaining element until all elements
 * have been processed or the action throws an exception.  Actions are
 * performed in the order of iteration, if that order is specified.
 * Exceptions thrown by the action are relayed to the caller.
 *
 * @implSpec
 * <p>The default implementation behaves as if:
 * <pre>{@code
 *     while (hasNext())
 *         action.accept(nextInt());
 * }</pre>
 *
 * @param action The action to be performed for each element
 * @throws NullPointerException if the specified action is null
 */
default void forEachRemaining(IntConsumer action) {
    Objects.requireNonNull(action);
    while (hasNext())
        action.accept(nextInt());
}