use of java.util.Spliterator in project archiva by apache.
the class CassandraMetadataRepository method createResultSpliterator.
private <T> Spliterator<T> createResultSpliterator(ResultSet result, BiFunction<Row, T, T> converter) throws MetadataRepositoryException {
final Iterator<Row> it = result.iterator();
return new Spliterator<T>() {
private T lastItem = null;
@Override
public boolean tryAdvance(Consumer<? super T> action) {
if (it.hasNext()) {
while (it.hasNext()) {
Row row = it.next();
T item = converter.apply(row, lastItem);
if (item != null && lastItem != null && item != lastItem) {
action.accept(lastItem);
lastItem = item;
return true;
}
lastItem = item;
}
action.accept(lastItem);
return true;
} else {
return false;
}
}
@Override
public Spliterator<T> trySplit() {
return null;
}
@Override
public long estimateSize() {
return Long.MAX_VALUE;
}
@Override
public int characteristics() {
return ORDERED + NONNULL;
}
};
}
use of java.util.Spliterator in project j2cl by google.
the class LongStreamImpl method flatMap.
@Override
public LongStream flatMap(LongFunction<? extends LongStream> mapper) {
throwIfTerminated();
final Spliterator<? extends LongStream> spliteratorOfStreams = new MapToObjSpliterator<>(mapper, spliterator);
AbstractLongSpliterator flatMapSpliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE, 0) {
LongStream nextStream;
Spliterator.OfLong next;
@Override
public boolean tryAdvance(LongConsumer action) {
// look for a new spliterator
while (advanceToNextSpliterator()) {
// if we have one, try to read and use it
if (next.tryAdvance(action)) {
return true;
} else {
nextStream.close();
nextStream = null;
// failed, null it out so we can find another
next = null;
}
}
return false;
}
private boolean advanceToNextSpliterator() {
while (next == null) {
if (!spliteratorOfStreams.tryAdvance(n -> {
if (n != null) {
nextStream = n;
next = n.spliterator();
}
})) {
return false;
}
}
return true;
}
};
return new LongStreamImpl(this, flatMapSpliterator);
}
use of java.util.Spliterator in project j2cl by google.
the class StreamImpl method flatMapToLong.
@Override
public LongStream flatMapToLong(Function<? super T, ? extends LongStream> mapper) {
throwIfTerminated();
final Spliterator<? extends LongStream> spliteratorOfStreams = new MapToObjSpliterator<>(mapper, spliterator);
AbstractLongSpliterator flatMapSpliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE, 0) {
LongStream nextStream;
Spliterator.OfLong next;
@Override
public boolean tryAdvance(LongConsumer action) {
// look for a new spliterator
while (advanceToNextSpliterator()) {
// if we have one, try to read and use it
if (next.tryAdvance(action)) {
return true;
} else {
nextStream.close();
nextStream = null;
// failed, null it out so we can find another
next = null;
}
}
return false;
}
private boolean advanceToNextSpliterator() {
while (next == null) {
if (!spliteratorOfStreams.tryAdvance(n -> {
if (n != null) {
nextStream = n;
next = n.spliterator();
}
})) {
return false;
}
}
return true;
}
};
return new LongStreamImpl(this, flatMapSpliterator);
}
use of java.util.Spliterator in project proxima-platform by O2-Czech-Republic.
the class HadoopFileSystem method list.
@Override
public Stream<Path> list(long minTs, long maxTs) {
URI remappedUri = HadoopStorage.remap(getUri());
RemoteIterator<LocatedFileStatus> iterator = ExceptionUtils.uncheckedFactory(() -> fs().listFiles(new org.apache.hadoop.fs.Path(remappedUri), true));
Spliterator<LocatedFileStatus> spliterator = asSpliterator(iterator);
return StreamSupport.stream(spliterator, false).filter(LocatedFileStatus::isFile).map(f -> f.getPath().toUri().toString().substring(remappedUri.toString().length())).filter(name -> namingConvention.isInRange(name, minTs, maxTs)).map(name -> HadoopPath.of(this, remappedUri + name, accessor));
}
use of java.util.Spliterator in project timbuctoo by HuygensING.
the class ChangeLog method getProvenanceChanges.
protected Stream<Change> getProvenanceChanges(DataSet dataSet, String[] subjects, CustomProvenance provenance, Map<String, JsonNode> values) {
TypeNameStore typeNameStore = dataSet.getTypeNameStore();
Stream<Change> customProv = provenance.getFields().stream().filter(field -> field.getValueType() != null).flatMap(field -> {
String graphQlPred = typeNameStore.makeGraphQlnameForPredicate(field.getUri(), Direction.OUT, field.isList());
return Stream.of(subjects).map(subject -> new Change(subject, field.getUri(), getValues(dataSet, values.get(graphQlPred)), Stream.empty()));
});
Stream<Change> customProvNested = provenance.getFields().stream().filter(field -> field.getObject() != null).flatMap(field -> {
String graphQlPred = typeNameStore.makeGraphQlnameForPredicate(field.getUri(), Direction.OUT, field.isList());
JsonNode objectValues = values.get(graphQlPred);
if (objectValues.isArray()) {
Spliterator<JsonNode> spliterator = Spliterators.spliteratorUnknownSize(objectValues.iterator(), Spliterator.ORDERED);
return StreamSupport.stream(spliterator, false).flatMap(newObjectValues -> getChangesForProvObject(dataSet, newObjectValues, subjects, field));
}
return getChangesForProvObject(dataSet, objectValues, subjects, field);
});
return Stream.concat(customProv, customProvNested);
}
Aggregations