use of java.util.Spliterator in project cas by apereo.
the class CouchbaseServiceRegistry method load.
@Override
public Collection<RegisteredService> load() {
val allServices = queryForAllServices().rowsAsObject();
val spliterator = Spliterators.spliteratorUnknownSize(allServices.iterator(), Spliterator.ORDERED);
return StreamSupport.stream(spliterator, false).filter(document -> document.containsKey(couchbase.getBucket())).map(document -> {
val json = document.getString(couchbase.getBucket());
LOGGER.trace("Found service: [{}]", json);
return this.registeredServiceJsonSerializer.from(json);
}).filter(Objects::nonNull).map(this::invokeServiceRegistryListenerPostLoad).filter(Objects::nonNull).peek(service -> publishEvent(new CasRegisteredServiceLoadedEvent(this, service))).collect(Collectors.toList());
}
use of java.util.Spliterator in project openj9 by eclipse.
the class StringStreams method testStringCharsSpliterator.
@Test
public void testStringCharsSpliterator() {
Spliterator.OfInt s = DIGITS.codePoints().spliterator();
final ArrayList<Integer> actualIntegers = new ArrayList<>();
s.forEachRemaining((IntConsumer) i -> actualIntegers.add(Integer.valueOf(i)));
for (int c = 0; c < DIGITS.length(); ++c) {
assertEquals(actualIntegers.get(c), Integer.valueOf(DIGITS.codePointAt(c)), // $NON-NLS-1$
"Wrong value at position " + c);
}
}
use of java.util.Spliterator in project openj9 by eclipse.
the class StringStreams method testStringCharsSpliteratorSingleChar.
@Test
public void testStringCharsSpliteratorSingleChar() {
Spliterator.OfInt s = SINGLECHAR.chars().spliterator();
final ArrayList<Integer> actualIntegers = new ArrayList<>();
s.forEachRemaining((IntConsumer) i -> actualIntegers.add(Integer.valueOf(i)));
checkDigitsOrdered(SINGLECHAR, actualIntegers);
}
use of java.util.Spliterator in project force-oneself by Force-oneself.
the class DistinctOps method makeRef.
/**
* Appends a "distinct" operation to the provided stream, and returns the
* new stream.
*
* @param <T> the type of both input and output elements
* @param upstream a reference stream with element type T
* @return the new stream
*/
static <T> ReferencePipeline<T, T> makeRef(AbstractPipeline<?, T, ?> upstream) {
return new ReferencePipeline.StatefulOp<T, T>(upstream, StreamShape.REFERENCE, StreamOpFlag.IS_DISTINCT | StreamOpFlag.NOT_SIZED) {
<P_IN> Node<T> reduce(PipelineHelper<T> helper, Spliterator<P_IN> spliterator) {
// If the stream is SORTED then it should also be ORDERED so the following will also
// preserve the sort order
TerminalOp<T, LinkedHashSet<T>> reduceOp = ReduceOps.<T, LinkedHashSet<T>>makeRef(LinkedHashSet::new, LinkedHashSet::add, LinkedHashSet::addAll);
return Nodes.node(reduceOp.evaluateParallel(helper, spliterator));
}
@Override
<P_IN> Node<T> opEvaluateParallel(PipelineHelper<T> helper, Spliterator<P_IN> spliterator, IntFunction<T[]> generator) {
if (StreamOpFlag.DISTINCT.isKnown(helper.getStreamAndOpFlags())) {
// No-op
return helper.evaluate(spliterator, false, generator);
} else if (StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags())) {
return reduce(helper, spliterator);
} else {
// Holder of null state since ConcurrentHashMap does not support null values
AtomicBoolean seenNull = new AtomicBoolean(false);
ConcurrentHashMap<T, Boolean> map = new ConcurrentHashMap<>();
TerminalOp<T, Void> forEachOp = ForEachOps.makeRef(t -> {
if (t == null)
seenNull.set(true);
else
map.putIfAbsent(t, Boolean.TRUE);
}, false);
forEachOp.evaluateParallel(helper, spliterator);
// If null has been seen then copy the key set into a HashSet that supports null values
// and add null
Set<T> keys = map.keySet();
if (seenNull.get()) {
// TODO Implement a more efficient set-union view, rather than copying
keys = new HashSet<>(keys);
keys.add(null);
}
return Nodes.node(keys);
}
}
@Override
<P_IN> Spliterator<T> opEvaluateParallelLazy(PipelineHelper<T> helper, Spliterator<P_IN> spliterator) {
if (StreamOpFlag.DISTINCT.isKnown(helper.getStreamAndOpFlags())) {
// No-op
return helper.wrapSpliterator(spliterator);
} else if (StreamOpFlag.ORDERED.isKnown(helper.getStreamAndOpFlags())) {
// Not lazy, barrier required to preserve order
return reduce(helper, spliterator).spliterator();
} else {
// Lazy
return new StreamSpliterators.DistinctSpliterator<>(helper.wrapSpliterator(spliterator));
}
}
@Override
Sink<T> opWrapSink(int flags, Sink<T> sink) {
Objects.requireNonNull(sink);
if (StreamOpFlag.DISTINCT.isKnown(flags)) {
return sink;
} else if (StreamOpFlag.SORTED.isKnown(flags)) {
return new Sink.ChainedReference<T, T>(sink) {
boolean seenNull;
T lastSeen;
@Override
public void begin(long size) {
seenNull = false;
lastSeen = null;
downstream.begin(-1);
}
@Override
public void end() {
seenNull = false;
lastSeen = null;
downstream.end();
}
//
@Override
public void accept(T t) {
if (t == null) {
if (!seenNull) {
seenNull = true;
downstream.accept(lastSeen = null);
}
} else if (lastSeen == null || !t.equals(lastSeen)) {
downstream.accept(lastSeen = t);
}
}
};
} else {
return new Sink.ChainedReference<T, T>(sink) {
Set<T> seen;
@Override
public void begin(long size) {
seen = new HashSet<>();
downstream.begin(-1);
}
@Override
public void end() {
seen = null;
downstream.end();
}
@Override
public void accept(T t) {
if (!seen.contains(t)) {
seen.add(t);
downstream.accept(t);
}
}
};
}
}
};
}
use of java.util.Spliterator in project force-oneself by Force-oneself.
the class DoublePipeline method forEachWithCancel.
@Override
final void forEachWithCancel(Spliterator<Double> spliterator, Sink<Double> sink) {
Spliterator.OfDouble spl = adapt(spliterator);
DoubleConsumer adaptedSink = adapt(sink);
do {
} while (!sink.cancellationRequested() && spl.tryAdvance(adaptedSink));
}
Aggregations