Search in sources :

Example 26 with Spliterator

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;
        }
    };
}
Also used : Consumer(java.util.function.Consumer) Row(com.datastax.oss.driver.api.core.cql.Row) Spliterator(java.util.Spliterator)

Example 27 with Spliterator

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);
}
Also used : Arrays(java.util.Arrays) LongBinaryOperator(java.util.function.LongBinaryOperator) Spliterators(java.util.Spliterators) OptionalDouble(java.util.OptionalDouble) IntConsumer(java.util.function.IntConsumer) DoubleConsumer(java.util.function.DoubleConsumer) Supplier(java.util.function.Supplier) HashSet(java.util.HashSet) AbstractLongSpliterator(java.util.Spliterators.AbstractLongSpliterator) OptionalLong(java.util.OptionalLong) InternalPreconditions.checkNotNull(javaemul.internal.InternalPreconditions.checkNotNull) BiConsumer(java.util.function.BiConsumer) LongPredicate(java.util.function.LongPredicate) LongSummaryStatistics(java.util.LongSummaryStatistics) LongToDoubleFunction(java.util.function.LongToDoubleFunction) LongToIntFunction(java.util.function.LongToIntFunction) LongFunction(java.util.function.LongFunction) LongConsumer(java.util.function.LongConsumer) PrimitiveIterator(java.util.PrimitiveIterator) Consumer(java.util.function.Consumer) InternalPreconditions.checkState(javaemul.internal.InternalPreconditions.checkState) LongUnaryOperator(java.util.function.LongUnaryOperator) ObjLongConsumer(java.util.function.ObjLongConsumer) Comparator(java.util.Comparator) Spliterator(java.util.Spliterator) LongConsumer(java.util.function.LongConsumer) ObjLongConsumer(java.util.function.ObjLongConsumer) AbstractLongSpliterator(java.util.Spliterators.AbstractLongSpliterator)

Example 28 with Spliterator

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);
}
Also used : AbstractIntSpliterator(java.util.Spliterators.AbstractIntSpliterator) AbstractSpliterator(java.util.Spliterators.AbstractSpliterator) Spliterators(java.util.Spliterators) BiFunction(java.util.function.BiFunction) IntConsumer(java.util.function.IntConsumer) DoubleConsumer(java.util.function.DoubleConsumer) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AbstractLongSpliterator(java.util.Spliterators.AbstractLongSpliterator) InternalPreconditions.checkNotNull(javaemul.internal.InternalPreconditions.checkNotNull) BiConsumer(java.util.function.BiConsumer) ToLongFunction(java.util.function.ToLongFunction) IntFunction(java.util.function.IntFunction) AbstractDoubleSpliterator(java.util.Spliterators.AbstractDoubleSpliterator) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) ToIntFunction(java.util.function.ToIntFunction) BinaryOperator(java.util.function.BinaryOperator) LongConsumer(java.util.function.LongConsumer) Consumer(java.util.function.Consumer) List(java.util.List) InternalPreconditions.checkState(javaemul.internal.InternalPreconditions.checkState) ToDoubleFunction(java.util.function.ToDoubleFunction) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) Spliterator(java.util.Spliterator) LongConsumer(java.util.function.LongConsumer) AbstractLongSpliterator(java.util.Spliterators.AbstractLongSpliterator)

Example 29 with Spliterator

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));
}
Also used : Path(cz.o2.proxima.direct.bulk.Path) Consumer(java.util.function.Consumer) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) Stream(java.util.stream.Stream) FileSystem(cz.o2.proxima.direct.bulk.FileSystem) Spliterators(java.util.Spliterators) LocatedFileStatus(org.apache.hadoop.fs.LocatedFileStatus) StreamSupport(java.util.stream.StreamSupport) URI(java.net.URI) NamingConvention(cz.o2.proxima.direct.bulk.NamingConvention) Spliterator(java.util.Spliterator) RemoteIterator(org.apache.hadoop.fs.RemoteIterator) Path(cz.o2.proxima.direct.bulk.Path) LocatedFileStatus(org.apache.hadoop.fs.LocatedFileStatus) URI(java.net.URI)

Example 30 with Spliterator

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);
}
Also used : Change(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) RdfConstants(nl.knaw.huygens.timbuctoo.v5.util.RdfConstants) Spliterators(java.util.Spliterators) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Collectors(java.util.stream.Collectors) List(java.util.List) Lists(com.google.common.collect.Lists) Stream(java.util.stream.Stream) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) StreamSupport(java.util.stream.StreamSupport) Direction(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction) TypeReference(com.fasterxml.jackson.core.type.TypeReference) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) Spliterator(java.util.Spliterator) TypeNameStore(nl.knaw.huygens.timbuctoo.v5.datastores.prefixstore.TypeNameStore) JsonNode(com.fasterxml.jackson.databind.JsonNode) Change(nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change) TypeNameStore(nl.knaw.huygens.timbuctoo.v5.datastores.prefixstore.TypeNameStore)

Aggregations

Spliterator (java.util.Spliterator)101 List (java.util.List)42 ArrayList (java.util.ArrayList)34 HashSet (java.util.HashSet)30 IntConsumer (java.util.function.IntConsumer)30 Set (java.util.Set)25 Function (java.util.function.Function)23 Spliterators (java.util.Spliterators)21 Collectors (java.util.stream.Collectors)21 Stream (java.util.stream.Stream)21 Objects (java.util.Objects)20 Map (java.util.Map)19 Comparator (java.util.Comparator)18 Iterator (java.util.Iterator)18 Consumer (java.util.function.Consumer)18 Arrays (java.util.Arrays)17 LongConsumer (java.util.function.LongConsumer)17 Supplier (java.util.function.Supplier)16 StreamSupport (java.util.stream.StreamSupport)16 Collections (java.util.Collections)15