|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
package java.util.stream; |
|
|
|
import java.util.Optional; |
|
import java.util.OptionalDouble; |
|
import java.util.OptionalInt; |
|
import java.util.OptionalLong; |
|
import java.util.Spliterator; |
|
import java.util.concurrent.CountedCompleter; |
|
import java.util.function.Predicate; |
|
import java.util.function.Supplier; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
final class FindOps { |
|
|
|
private FindOps() { } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@SuppressWarnings("unchecked") |
|
public static <T> TerminalOp<T, Optional<T>> makeRef(boolean mustFindFirst) { |
|
return (TerminalOp<T, Optional<T>>) |
|
(mustFindFirst ? FindSink.OfRef.OP_FIND_FIRST : FindSink.OfRef.OP_FIND_ANY); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static TerminalOp<Integer, OptionalInt> makeInt(boolean mustFindFirst) { |
|
return mustFindFirst ? FindSink.OfInt.OP_FIND_FIRST : FindSink.OfInt.OP_FIND_ANY; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static TerminalOp<Long, OptionalLong> makeLong(boolean mustFindFirst) { |
|
return mustFindFirst ? FindSink.OfLong.OP_FIND_FIRST : FindSink.OfLong.OP_FIND_ANY; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static TerminalOp<Double, OptionalDouble> makeDouble(boolean mustFindFirst) { |
|
return mustFindFirst ? FindSink.OfDouble.OP_FIND_FIRST : FindSink.OfDouble.OP_FIND_ANY; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private static final class FindOp<T, O> implements TerminalOp<T, O> { |
|
private final StreamShape shape; |
|
final int opFlags; |
|
final O emptyValue; |
|
final Predicate<O> presentPredicate; |
|
final Supplier<TerminalSink<T, O>> sinkSupplier; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
FindOp(boolean mustFindFirst, |
|
StreamShape shape, |
|
O emptyValue, |
|
Predicate<O> presentPredicate, |
|
Supplier<TerminalSink<T, O>> sinkSupplier) { |
|
this.opFlags = StreamOpFlag.IS_SHORT_CIRCUIT | (mustFindFirst ? 0 : StreamOpFlag.NOT_ORDERED); |
|
this.shape = shape; |
|
this.emptyValue = emptyValue; |
|
this.presentPredicate = presentPredicate; |
|
this.sinkSupplier = sinkSupplier; |
|
} |
|
|
|
@Override |
|
public int getOpFlags() { |
|
return opFlags; |
|
} |
|
|
|
@Override |
|
public StreamShape inputShape() { |
|
return shape; |
|
} |
|
|
|
@Override |
|
public <S> O evaluateSequential(PipelineHelper<T> helper, |
|
Spliterator<S> spliterator) { |
|
O result = helper.wrapAndCopyInto(sinkSupplier.get(), spliterator).get(); |
|
return result != null ? result : emptyValue; |
|
} |
|
|
|
@Override |
|
public <P_IN> O evaluateParallel(PipelineHelper<T> helper, |
|
Spliterator<P_IN> spliterator) { |
|
// This takes into account the upstream ops flags and the terminal |
|
|
|
boolean mustFindFirst = StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags()); |
|
return new FindTask<>(this, mustFindFirst, helper, spliterator).invoke(); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private abstract static class FindSink<T, O> implements TerminalSink<T, O> { |
|
boolean hasValue; |
|
T value; |
|
|
|
FindSink() {} |
|
|
|
@Override |
|
public void accept(T value) { |
|
if (!hasValue) { |
|
hasValue = true; |
|
this.value = value; |
|
} |
|
} |
|
|
|
@Override |
|
public boolean cancellationRequested() { |
|
return hasValue; |
|
} |
|
|
|
|
|
static final class OfRef<T> extends FindSink<T, Optional<T>> { |
|
@Override |
|
public Optional<T> get() { |
|
return hasValue ? Optional.of(value) : null; |
|
} |
|
|
|
static final TerminalOp<?, ?> OP_FIND_FIRST = new FindOp<>(true, |
|
StreamShape.REFERENCE, Optional.empty(), |
|
Optional::isPresent, FindSink.OfRef::new); |
|
|
|
static final TerminalOp<?, ?> OP_FIND_ANY = new FindOp<>(false, |
|
StreamShape.REFERENCE, Optional.empty(), |
|
Optional::isPresent, FindSink.OfRef::new); |
|
} |
|
|
|
|
|
static final class OfInt extends FindSink<Integer, OptionalInt> |
|
implements Sink.OfInt { |
|
@Override |
|
public void accept(int value) { |
|
|
|
accept((Integer) value); |
|
} |
|
|
|
@Override |
|
public OptionalInt get() { |
|
return hasValue ? OptionalInt.of(value) : null; |
|
} |
|
|
|
static final TerminalOp<Integer, OptionalInt> OP_FIND_FIRST = new FindOp<>(true, |
|
StreamShape.INT_VALUE, OptionalInt.empty(), |
|
OptionalInt::isPresent, FindSink.OfInt::new); |
|
static final TerminalOp<Integer, OptionalInt> OP_FIND_ANY = new FindOp<>(false, |
|
StreamShape.INT_VALUE, OptionalInt.empty(), |
|
OptionalInt::isPresent, FindSink.OfInt::new); |
|
} |
|
|
|
|
|
static final class OfLong extends FindSink<Long, OptionalLong> |
|
implements Sink.OfLong { |
|
@Override |
|
public void accept(long value) { |
|
|
|
accept((Long) value); |
|
} |
|
|
|
@Override |
|
public OptionalLong get() { |
|
return hasValue ? OptionalLong.of(value) : null; |
|
} |
|
|
|
static final TerminalOp<Long, OptionalLong> OP_FIND_FIRST = new FindOp<>(true, |
|
StreamShape.LONG_VALUE, OptionalLong.empty(), |
|
OptionalLong::isPresent, FindSink.OfLong::new); |
|
static final TerminalOp<Long, OptionalLong> OP_FIND_ANY = new FindOp<>(false, |
|
StreamShape.LONG_VALUE, OptionalLong.empty(), |
|
OptionalLong::isPresent, FindSink.OfLong::new); |
|
} |
|
|
|
|
|
static final class OfDouble extends FindSink<Double, OptionalDouble> |
|
implements Sink.OfDouble { |
|
@Override |
|
public void accept(double value) { |
|
|
|
accept((Double) value); |
|
} |
|
|
|
@Override |
|
public OptionalDouble get() { |
|
return hasValue ? OptionalDouble.of(value) : null; |
|
} |
|
|
|
static final TerminalOp<Double, OptionalDouble> OP_FIND_FIRST = new FindOp<>(true, |
|
StreamShape.DOUBLE_VALUE, OptionalDouble.empty(), |
|
OptionalDouble::isPresent, FindSink.OfDouble::new); |
|
static final TerminalOp<Double, OptionalDouble> OP_FIND_ANY = new FindOp<>(false, |
|
StreamShape.DOUBLE_VALUE, OptionalDouble.empty(), |
|
OptionalDouble::isPresent, FindSink.OfDouble::new); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@SuppressWarnings("serial") |
|
private static final class FindTask<P_IN, P_OUT, O> |
|
extends AbstractShortCircuitTask<P_IN, P_OUT, O, FindTask<P_IN, P_OUT, O>> { |
|
private final FindOp<P_OUT, O> op; |
|
private final boolean mustFindFirst; |
|
|
|
FindTask(FindOp<P_OUT, O> op, |
|
boolean mustFindFirst, |
|
PipelineHelper<P_OUT> helper, |
|
Spliterator<P_IN> spliterator) { |
|
super(helper, spliterator); |
|
this.mustFindFirst = mustFindFirst; |
|
this.op = op; |
|
} |
|
|
|
FindTask(FindTask<P_IN, P_OUT, O> parent, Spliterator<P_IN> spliterator) { |
|
super(parent, spliterator); |
|
this.mustFindFirst = parent.mustFindFirst; |
|
this.op = parent.op; |
|
} |
|
|
|
@Override |
|
protected FindTask<P_IN, P_OUT, O> makeChild(Spliterator<P_IN> spliterator) { |
|
return new FindTask<>(this, spliterator); |
|
} |
|
|
|
@Override |
|
protected O getEmptyResult() { |
|
return op.emptyValue; |
|
} |
|
|
|
private void foundResult(O answer) { |
|
if (isLeftmostNode()) |
|
shortCircuit(answer); |
|
else |
|
cancelLaterNodes(); |
|
} |
|
|
|
@Override |
|
protected O doLeaf() { |
|
O result = helper.wrapAndCopyInto(op.sinkSupplier.get(), spliterator).get(); |
|
if (!mustFindFirst) { |
|
if (result != null) |
|
shortCircuit(result); |
|
return null; |
|
} |
|
else { |
|
if (result != null) { |
|
foundResult(result); |
|
return result; |
|
} |
|
else |
|
return null; |
|
} |
|
} |
|
|
|
@Override |
|
public void onCompletion(CountedCompleter<?> caller) { |
|
if (mustFindFirst) { |
|
for (FindTask<P_IN, P_OUT, O> child = leftChild, p = null; child != p; |
|
p = child, child = rightChild) { |
|
O result = child.getLocalResult(); |
|
if (result != null && op.presentPredicate.test(result)) { |
|
setLocalResult(result); |
|
foundResult(result); |
|
break; |
|
} |
|
} |
|
} |
|
super.onCompletion(caller); |
|
} |
|
} |
|
} |
|
|