java.util.function.DoubleConsumer Java Examples

The following examples show how to use java.util.function.DoubleConsumer. 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: DoublePipeline.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final DoubleStream peek(DoubleConsumer action) {
    Objects.requireNonNull(action);
    return new StatelessOp<Double>(this, StreamShape.DOUBLE_VALUE,
                                   0) {
        @Override
        Sink<Double> opWrapSink(int flags, Sink<Double> sink) {
            return new Sink.ChainedDouble<Double>(sink) {
                @Override
                public void accept(double t) {
                    action.accept(t);
                    downstream.accept(t);
                }
            };
        }
    };
}
 
Example #2
Source File: DoubleStream.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns an infinite sequential ordered {@code DoubleStream} 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 DoubleStream}
 * 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 DoubleStream}
 */
public static DoubleStream iterate(final double seed, final DoubleUnaryOperator f) {
    Objects.requireNonNull(f);
    Spliterator.OfDouble spliterator = new Spliterators.AbstractDoubleSpliterator(Long.MAX_VALUE,
           Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
        double prev;
        boolean started;

        @Override
        public boolean tryAdvance(DoubleConsumer action) {
            Objects.requireNonNull(action);
            double t;
            if (started)
                t = f.applyAsDouble(prev);
            else {
                t = seed;
                started = true;
            }
            action.accept(prev = t);
            return true;
        }
    };
    return StreamSupport.doubleStream(spliterator, false);
}
 
Example #3
Source File: PrimitiveIteratorDefaults.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public void testDoubleForEachRemainingWithNull() {
    PrimitiveIterator.OfDouble i = new PrimitiveIterator.OfDouble() {
        @Override
        public double nextDouble() {
            return 0;
        }

        @Override
        public boolean hasNext() {
            return false;
        }
    };

    executeAndCatch(() -> i.forEachRemaining((DoubleConsumer) null));
    executeAndCatch(() -> i.forEachRemaining((Consumer<Double>) null));
}
 
Example #4
Source File: WhileOps.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean tryAdvance(DoubleConsumer action) {
    if (takeOrDrop) {
        takeOrDrop = false;
        boolean adv;
        boolean dropped = false;
        while ((adv = s.tryAdvance(this)) &&  // If advanced one element
               checkCancelOnCount() &&        // and if not cancelled
               p.test(t)) {                   // and test on element passes
            dropped = true;                   // then drop element
        }

        // Report advanced element, if any
        if (adv) {
            // Cancel all further dropping if one or more elements
            // were previously dropped
            if (dropped)
                cancel.set(true);
            action.accept(t);
        }
        return adv;
    }
    else {
        return s.tryAdvance(action);
    }
}
 
Example #5
Source File: PrimitiveIteratorDefaults.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void testDoubleForEachRemainingWithNull() {
    PrimitiveIterator.OfDouble i = new PrimitiveIterator.OfDouble() {
        @Override
        public double nextDouble() {
            return 0;
        }

        @Override
        public boolean hasNext() {
            return false;
        }
    };

    executeAndCatch(() -> i.forEachRemaining((DoubleConsumer) null));
    executeAndCatch(() -> i.forEachRemaining((Consumer<Double>) null));
}
 
Example #6
Source File: SpinedBufferTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "DoubleSpinedBuffer", groups = { "serialization-hostile" })
public void testLongLastSplit(double[] array, SpinedBuffer.OfDouble sb) {
    Spliterator.OfDouble spliterator = sb.spliterator();
    Spliterator.OfDouble split = spliterator.trySplit();
    long splitSizes = (split == null) ? 0 : split.getExactSizeIfKnown();
    long lastSplitSize = spliterator.getExactSizeIfKnown();
    splitSizes += lastSplitSize;

    assertEquals(splitSizes, array.length);

    List<Double> contentOfLastSplit = new ArrayList<>();
    spliterator.forEachRemaining((DoubleConsumer) contentOfLastSplit::add);

    assertEquals(contentOfLastSplit.size(), lastSplitSize);

    List<Double> end = Arrays.stream(array)
            .boxed()
            .skip(array.length - lastSplitSize)
            .collect(Collectors.toList());
    assertEquals(contentOfLastSplit, end);
}
 
Example #7
Source File: Node.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
default Node.OfDouble truncate(long from, long to, IntFunction<Double[]> generator) {
    if (from == 0 && to == count())
        return this;
    long size = to - from;
    Spliterator.OfDouble spliterator = spliterator();
    Node.Builder.OfDouble nodeBuilder = Nodes.doubleBuilder(size);
    nodeBuilder.begin(size);
    for (int i = 0; i < from && spliterator.tryAdvance((DoubleConsumer) e -> { }); i++) { }
    for (int i = 0; (i < size) && spliterator.tryAdvance((DoubleConsumer) nodeBuilder); i++) { }
    nodeBuilder.end();
    return nodeBuilder.build();
}
 
Example #8
Source File: Random.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
public void forEachRemaining(DoubleConsumer consumer) {
    if (consumer == null) throw new NullPointerException();
    long i = index, f = fence;
    if (i < f) {
        index = f;
        Random r = rng;
        double o = origin, b = bound;
        do {
            consumer.accept(r.internalNextDouble(o, b));
        } while (++i < f);
    }
}
 
Example #9
Source File: Streams.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
@Override
public void forEachRemaining(DoubleConsumer action) {
    Objects.requireNonNull(action);

    if (count == -2) {
        action.accept(first);
        count = -1;
    }
}
 
Example #10
Source File: Node.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
default Node.OfDouble truncate(long from, long to, IntFunction<Double[]> generator) {
    if (from == 0 && to == count())
        return this;
    long size = to - from;
    Spliterator.OfDouble spliterator = spliterator();
    Node.Builder.OfDouble nodeBuilder = Nodes.doubleBuilder(size);
    nodeBuilder.begin(size);
    for (int i = 0; i < from && spliterator.tryAdvance((DoubleConsumer) e -> { }); i++) { }
    for (int i = 0; (i < size) && spliterator.tryAdvance((DoubleConsumer) nodeBuilder); i++) { }
    nodeBuilder.end();
    return nodeBuilder.build();
}
 
Example #11
Source File: Spliterators.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean tryAdvance(DoubleConsumer action) {
    if (action == null) throw new NullPointerException();
    if (it.hasNext()) {
        action.accept(it.nextDouble());
        return true;
    }
    return false;
}
 
Example #12
Source File: Random.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public boolean tryAdvance(DoubleConsumer consumer) {
    if (consumer == null) throw new NullPointerException();
    long i = index, f = fence;
    if (i < f) {
        consumer.accept(rng.internalNextDouble(origin, bound));
        index = i + 1;
        return true;
    }
    return false;
}
 
Example #13
Source File: Streams.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean tryAdvance(DoubleConsumer action) {
    Objects.requireNonNull(action);

    if (count == -2) {
        action.accept(first);
        count = -1;
        return true;
    }
    else {
        return false;
    }
}
 
Example #14
Source File: Random.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public boolean tryAdvance(DoubleConsumer consumer) {
    if (consumer == null) throw new NullPointerException();
    long i = index, f = fence;
    if (i < f) {
        consumer.accept(rng.internalNextDouble(origin, bound));
        index = i + 1;
        return true;
    }
    return false;
}
 
Example #15
Source File: Streams.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean tryAdvance(DoubleConsumer action) {
    Objects.requireNonNull(action);

    if (count == -2) {
        action.accept(first);
        count = -1;
        return true;
    }
    else {
        return false;
    }
}
 
Example #16
Source File: Spliterator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * @implSpec
 * If the action is an instance of {@code DoubleConsumer} then it is
 * cast to {@code DoubleConsumer} and passed to
 * {@link #tryAdvance(java.util.function.DoubleConsumer)}; otherwise
 * the action is adapted to an instance of {@code DoubleConsumer}, by
 * boxing the argument of {@code DoubleConsumer}, and then passed to
 * {@link #tryAdvance(java.util.function.DoubleConsumer)}.
 */
@Override
default boolean tryAdvance(Consumer<? super Double> action) {
    if (action instanceof DoubleConsumer) {
        return tryAdvance((DoubleConsumer) action);
    }
    else {
        if (Tripwire.ENABLED)
            Tripwire.trip(getClass(),
                          "{0} calling Spliterator.OfDouble.tryAdvance((DoubleConsumer) action::accept)");
        return tryAdvance((DoubleConsumer) action::accept);
    }
}
 
Example #17
Source File: DoubleConsumerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testAndThen_null() throws Exception {
  DoubleConsumer one = s -> {};
  try {
    one.andThen(null);
    fail();
  } catch (NullPointerException expected) {}
}
 
Example #18
Source File: Spliterator.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * @implSpec
 * If the action is an instance of {@code DoubleConsumer} then it is
 * cast to {@code DoubleConsumer} and passed to
 * {@link #forEachRemaining(java.util.function.DoubleConsumer)};
 * otherwise the action is adapted to an instance of
 * {@code DoubleConsumer}, by boxing the argument of
 * {@code DoubleConsumer}, and then passed to
 * {@link #forEachRemaining(java.util.function.DoubleConsumer)}.
 */
@Override
default void forEachRemaining(Consumer<? super Double> action) {
    if (action instanceof DoubleConsumer) {
        forEachRemaining((DoubleConsumer) action);
    }
    else {
        if (Tripwire.ENABLED)
            Tripwire.trip(getClass(),
                          "{0} calling Spliterator.OfDouble.forEachRemaining((DoubleConsumer) action::accept)");
        forEachRemaining((DoubleConsumer) action::accept);
    }
}
 
Example #19
Source File: Streams.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void forEachRemaining(DoubleConsumer action) {
    Objects.requireNonNull(action);

    if (count == -2) {
        action.accept(first);
        count = -1;
    }
}
 
Example #20
Source File: StreamSpliterators.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void forEachRemaining(DoubleConsumer consumer) {
    if (buffer == null && !finished) {
        Objects.requireNonNull(consumer);
        init();

        ph.wrapAndCopyInto((Sink.OfDouble) consumer::accept, spliterator);
        finished = true;
    }
    else {
        do { } while (tryAdvance(consumer));
    }
}
 
Example #21
Source File: DoublePipeline.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void forEach(DoubleConsumer consumer) {
    if (!isParallel()) {
        adapt(sourceStageSpliterator()).forEachRemaining(consumer);
    }
    else {
        super.forEach(consumer);
    }
}
 
Example #22
Source File: DoublePipeline.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void forEach(DoubleConsumer consumer) {
    if (!isParallel()) {
        adapt(sourceStageSpliterator()).forEachRemaining(consumer);
    }
    else {
        super.forEach(consumer);
    }
}
 
Example #23
Source File: SplittableRandom.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public boolean tryAdvance(DoubleConsumer consumer) {
    if (consumer == null) throw new NullPointerException();
    long i = index, f = fence;
    if (i < f) {
        consumer.accept(rng.internalNextDouble(origin, bound));
        index = i + 1;
        return true;
    }
    return false;
}
 
Example #24
Source File: Node.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @param consumer A {@code Consumer} that is to be invoked with each
 *        element in this {@code Node}.  If this is an
 *        {@code DoubleConsumer}, it is cast to {@code DoubleConsumer}
 *        so the elements may be processed without boxing.
 */
@Override
default void forEach(Consumer<? super Double> consumer) {
    if (consumer instanceof DoubleConsumer) {
        forEach((DoubleConsumer) consumer);
    }
    else {
        if (Tripwire.ENABLED)
            Tripwire.trip(getClass(), "{0} calling Node.OfLong.forEachRemaining(Consumer)");
        spliterator().forEachRemaining(consumer);
    }
}
 
Example #25
Source File: StreamSpliterators.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean tryAdvance(DoubleConsumer consumer) {
    Objects.requireNonNull(consumer);
    boolean hasNext = doAdvance();
    if (hasNext)
        consumer.accept(buffer.get(nextToConsume));
    return hasNext;
}
 
Example #26
Source File: DoublePipeline.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
@Override
public void forEach(DoubleConsumer consumer) {
    if (!isParallel()) {
        adapt(sourceStageSpliterator()).forEachRemaining(consumer);
    }
    else {
        super.forEach(consumer);
    }
}
 
Example #27
Source File: StreamSpliterators.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public boolean tryAdvance(DoubleConsumer action) {
    Objects.requireNonNull(action);

    action.accept(s.getAsDouble());
    return true;
}
 
Example #28
Source File: Spliterators.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates an {@code PrimitiveIterator.OfDouble} from a
 * {@code Spliterator.OfDouble}.
 *
 * <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.OfDouble iterator(Spliterator.OfDouble spliterator) {
    Objects.requireNonNull(spliterator);
    class Adapter implements PrimitiveIterator.OfDouble, DoubleConsumer {
        boolean valueReady = false;
        double nextElement;

        @Override
        public void accept(double t) {
            valueReady = true;
            nextElement = t;
        }

        @Override
        public boolean hasNext() {
            if (!valueReady)
                spliterator.tryAdvance(this);
            return valueReady;
        }

        @Override
        public double nextDouble() {
            if (!valueReady && !hasNext())
                throw new NoSuchElementException();
            else {
                valueReady = false;
                return nextElement;
            }
        }
    }

    return new Adapter();
}
 
Example #29
Source File: DoublePipeline.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void forEachOrdered(DoubleConsumer consumer) {
    if (!isParallel()) {
        adapt(sourceStageSpliterator()).forEachRemaining(consumer);
    }
    else {
        super.forEachOrdered(consumer);
    }
}
 
Example #30
Source File: Spliterator.java    From Java8CN with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * @implSpec
 * If the action is an instance of {@code DoubleConsumer} then it is
 * cast to {@code DoubleConsumer} and passed to
 * {@link #forEachRemaining(java.util.function.DoubleConsumer)};
 * otherwise the action is adapted to an instance of
 * {@code DoubleConsumer}, by boxing the argument of
 * {@code DoubleConsumer}, and then passed to
 * {@link #forEachRemaining(java.util.function.DoubleConsumer)}.
 */
@Override
default void forEachRemaining(Consumer<? super Double> action) {
    if (action instanceof DoubleConsumer) {
        forEachRemaining((DoubleConsumer) action);
    }
    else {
        if (Tripwire.ENABLED)
            Tripwire.trip(getClass(),
                          "{0} calling Spliterator.OfDouble.forEachRemaining((DoubleConsumer) action::accept)");
        forEachRemaining((DoubleConsumer) action::accept);
    }
}