下面列出了怎么用java.util.function.DoubleConsumer的API类实例代码及写法,或者点击链接到github查看源代码。
@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);
}
/**
* 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);
}
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));
}
@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);
}
}
@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);
}
};
}
};
}
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));
}
@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();
}
@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));
}
}
@Override
public void forEachOrdered(DoubleConsumer consumer) {
if (!isParallel()) {
adapt(sourceStageSpliterator()).forEachRemaining(consumer);
}
else {
super.forEachOrdered(consumer);
}
}
/**
* {@inheritDoc}
* @implSpec
* If the action is an instance of {@code DoubleConsumer} then it is
* cast to {@code DoubleConsumer} and passed to
* {@link #forEachRemaining}; otherwise the action is adapted to
* an instance of {@code DoubleConsumer}, by boxing the argument of
* {@code DoubleConsumer}, and then passed to
* {@link #forEachRemaining}.
*/
@Override
default void forEachRemaining(Consumer<? super Double> action) {
if (action instanceof DoubleConsumer) {
forEachRemaining((DoubleConsumer) action);
}
else {
// The method reference action::accept is never null
Objects.requireNonNull(action);
if (Tripwire.ENABLED)
Tripwire.trip(getClass(), "{0} calling PrimitiveIterator.OfDouble.forEachRemainingDouble(action::accept)");
forEachRemaining((DoubleConsumer) action::accept);
}
}
@Override
public void forEachRemaining(DoubleConsumer action) {
double[] 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);
}
}
@Override
public void forEachRemaining(DoubleConsumer action) {
double[] 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);
}
}
@Override
public void forEach(DoubleConsumer consumer) {
if (!isParallel()) {
adapt(sourceStageSpliterator()).forEachRemaining(consumer);
}
else {
super.forEach(consumer);
}
}
/**
* {@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);
}
}
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);
}
}
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;
}
@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));
}
}
@Override
public boolean tryAdvance(DoubleConsumer action) {
Objects.requireNonNull(action);
if (count == -2) {
action.accept(first);
count = -1;
return true;
}
else {
return false;
}
}
public void testAndThen_null() throws Exception {
DoubleConsumer one = s -> {};
try {
one.andThen(null);
fail();
} catch (NullPointerException expected) {}
}
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);
}
}
/**
* {@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);
}
}
/**
* Adapt a {@code Sink<Double> to a {@code DoubleConsumer}, ideally simply
* by casting.
*/
private static DoubleConsumer adapt(Sink<Double> sink) {
if (sink instanceof DoubleConsumer) {
return (DoubleConsumer) sink;
} else {
if (Tripwire.ENABLED)
Tripwire.trip(AbstractPipeline.class,
"using DoubleStream.adapt(Sink<Double> s)");
return sink::accept;
}
}
@Override
public boolean tryAdvance(DoubleConsumer action) {
Objects.requireNonNull(action);
if (count == -2) {
action.accept(first);
count = -1;
return true;
}
else {
return false;
}
}
@Override
public boolean tryAdvance(DoubleConsumer action) {
Objects.requireNonNull(action);
if (count == -2) {
action.accept(first);
count = -1;
return true;
}
else {
return false;
}
}
@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();
}
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);
}
}
@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();
}
@Override
public boolean tryAdvance(DoubleConsumer action) {
if (action == null) throw new NullPointerException();
if (it.hasNext()) {
action.accept(it.nextDouble());
return true;
}
return false;
}
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;
}
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;
}