Example usage for com.google.common.collect Iterators advance

List of usage examples for com.google.common.collect Iterators advance

Introduction

In this page you can find the example usage for com.google.common.collect Iterators advance.

Prototype

public static int advance(Iterator<?> iterator, int numberToAdvance) 

Source Link

Document

Calls next() on iterator , either numberToAdvance times or until hasNext() returns false , whichever comes first.

Usage

From source file:c5db.util.C5Iterators.java

/**
 * Lazily advance the passed Iterator the given number of times. A lazy variant of
 * {@link com.google.common.collect.Iterators#advance}
 *
 * @return A new Iterator object representing the given Iterator advanced a specified
 * number of times, calling the given Iterator's next() method when needed.
 *//*from w  w  w .j  a  v a2  s .c  o  m*/
public static <T> Iterator<T> advanced(Iterator<T> backingIterator, int numberToAdvance) {
    return new Iterator<T>() {
        private boolean advanced;

        @Override
        public boolean hasNext() {
            lazilyAdvance();
            return backingIterator.hasNext();
        }

        @Override
        public T next() {
            lazilyAdvance();
            return backingIterator.next();
        }

        @Override
        public void remove() {
            lazilyAdvance();
            backingIterator.remove();
        }

        private void lazilyAdvance() {
            if (!advanced) {
                Iterators.advance(backingIterator, numberToAdvance);
                advanced = true;
            }
        }
    };
}

From source file:io.crate.operation.join.RelationIterable.java

public Iterator<Object[]> forCurrentPage() {
    Iterator<Object[]> iter = iterator();
    Iterators.advance(iter, currentPageInfo().position());
    return iter;
}

From source file:edu.byu.nlp.util.Collections3.java

/**
 * Only if list://from  ww w. j  a va 2s  .c  o m
 * The semantics of the collection returned by this method become undefined if the backing collection (i.e., this
 * collection) is structurally modified in any way other than via the returned collection. (Structural modifications
 * are those that change the size of this collection, or otherwise perturb it in such a fashion that iterations in
 * progress may yield incorrect results.)
 */
public static <E> Collection<E> skip(final Collection<E> coll, final int numberToSkip) {
    if (coll instanceof List) {
        return ((List<E>) coll).subList(numberToSkip, coll.size());
    }

    Preconditions.checkElementIndex(numberToSkip, coll.size());

    return new AbstractCollection<E>() {

        @Override
        public Iterator<E> iterator() {
            Iterator<E> it = coll.iterator();
            Iterators.advance(it, numberToSkip);
            return it;
        }

        @Override
        public int size() {
            return Math.max(0, coll.size() - numberToSkip);
        }

    };
}

From source file:org.locationtech.geogig.web.api.commands.GetCommitGraph.java

/**
 * Runs the command and builds the appropriate response.
 * // w  ww.ja  va 2  s  . c o  m
 * @param context - the context to use for this command
 * 
 * @throws CommandSpecException
 */
@Override
public void run(CommandContext context) {
    if (commitId.equals(ObjectId.NULL.toString())) {
        throw new CommandSpecException("No commitId was given.");
    }
    final GeoGIG geogig = context.getGeoGIG();
    RevCommit commit = geogig.getRepository().getCommit(ObjectId.valueOf(commitId));
    final List<RevCommit> history = Lists.newLinkedList();

    List<CommitNode> nodes = Lists.newLinkedList();
    CommitNode node = new CommitNode(commit, 1);
    nodes.add(node);

    while (!nodes.isEmpty()) {
        node = nodes.remove(0);
        if (!history.contains(node.commit)) {
            history.add(node.commit);
        }
        if (this.depth == 0 || node.depth < this.depth) {
            for (ObjectId id : node.commit.getParentIds()) {
                nodes.add(new CommitNode(geogig.getRepository().getCommit(id), node.depth + 1));
            }
        }
    }

    final Iterator<RevCommit> historyIterator = history.iterator();
    Iterators.advance(historyIterator, page * elementsPerPage);

    context.setResponseContent(new CommandResponse() {

        @Override
        public void write(ResponseWriter out) throws Exception {
            out.start();
            out.writeCommits(history.iterator(), elementsPerPage, false);
            out.finish();
        }
    });
}

From source file:eu.stratosphere.sopremo.type.SubArrayNode.java

@Override
public Iterator<T> iterator() {
    final Iterator<T> iterator = this.originalArray.iterator();
    Iterators.advance(iterator, this.startIndex);
    return Iterators.limit(iterator, this.length);
}

From source file:com.yandex.yoctodb.v1.immutable.V1Database.java

@Override
public void execute(@NotNull final Query query, @NotNull final DocumentProcessor processor) {
    final BitSet docs = query.filteredUnlimited(this, bitSetPool);
    if (docs == null) {
        return;//w  w w . j  ava2s.c  o  m
    }

    final Iterator<? extends ScoredDocument<?>> unlimited = query.sortedUnlimited(docs, this, bitSetPool);

    if (query.getSkip() != 0) {
        Iterators.advance(unlimited, query.getSkip());
    }

    final Iterator<? extends ScoredDocument<?>> limited;
    if (query.getLimit() == Integer.MAX_VALUE) {
        limited = unlimited;
    } else {
        limited = Iterators.limit(unlimited, query.getLimit());
    }

    while (limited.hasNext()) {
        if (!processor.process(limited.next().getDocument(), this)) {
            return;
        }
    }
}

From source file:com.yandex.yoctodb.v1.immutable.V1CompositeDatabase.java

@Override
public void execute(@NotNull final Query query, @NotNull final DocumentProcessor processor) {
    final Iterator<ScoredDocument<?>> iterator;

    // Doing merging iff there is sorting
    if (query.hasSorting()) {
        final List<Iterator<? extends ScoredDocument<?>>> results = new ArrayList<>(databases.size());
        for (IndexedDatabase db : databases) {
            final BitSet docs = query.filteredUnlimited(db, bitSetPool);

            if (docs == null) {
                continue;
            }//from   w  ww  . j a  v a 2 s.  c o  m

            assert !docs.isEmpty();

            results.add(query.sortedUnlimited(docs, db, bitSetPool));
        }

        if (results.isEmpty()) {
            return;
        }

        iterator = Iterators.mergeSorted(results, SCORED_DOCUMENT_COMPARATOR);
    } else {
        iterator = Iterators.concat(new FilterResultIterator(query, databases.iterator(), bitSetPool));
    }

    // Skipping values
    if (query.getSkip() != 0) {
        Iterators.advance(iterator, query.getSkip());
    }

    // Limited
    final Iterator<ScoredDocument<?>> limitedIterator;
    if (query.getLimit() == Integer.MAX_VALUE) {
        limitedIterator = iterator;
    } else {
        limitedIterator = Iterators.limit(iterator, query.getLimit());
    }

    while (limitedIterator.hasNext()) {
        final ScoredDocument<?> document = limitedIterator.next();
        if (!processor.process(document.getDocument(), document.getDatabase())) {
            return;
        }
    }
}

From source file:org.apache.drill.exec.store.sys.store.ZookeeperPersistentStore.java

@Override
public Iterator<Map.Entry<String, V>> getRange(final int skip, final int take) {
    final Iterator<Map.Entry<String, byte[]>> entries = client.entries();
    Iterators.advance(entries, skip);
    return Iterators.transform(Iterators.limit(entries, take),
            new Function<Map.Entry<String, byte[]>, Map.Entry<String, V>>() {
                @Nullable//from   w w w . j a v  a  2 s  .co m
                @Override
                public Map.Entry<String, V> apply(@Nullable Map.Entry<String, byte[]> input) {
                    try {
                        final V value = config.getSerializer().deserialize(input.getValue());
                        return new ImmutableEntry<>(input.getKey(), value);
                    } catch (final IOException e) {
                        throw new DrillRuntimeException(
                                String.format("unable to deserialize value at key %s", input.getKey()), e);
                    }
                }
            });
}

From source file:org.fcrepo.kernel.rdf.impl.HierarchyRdfContext.java

private Iterator<Triple> childrenContext() throws RepositoryException {

    final Iterator<javax.jcr.Node> niceChildren = Iterators.filter(new NodeIterator(node().getNodes()),
            not(nastyChildren));/* ww w.  j a  v  a 2  s  .com*/

    final Iterator<javax.jcr.Node> salientChildren;

    if (options.hasOffset()) {
        int offset = options.getOffset();
        Iterators.advance(niceChildren, offset);
    }

    if (options.hasLimit()) {
        salientChildren = Iterators.limit(niceChildren, options.getLimit());
    } else {
        salientChildren = niceChildren;
    }

    return Iterators.concat(Iterators.transform(salientChildren, child2triples()));
}

From source file:org.fcrepo.kernel.impl.rdf.impl.HierarchyRdfContext.java

private Iterator<Triple> childrenContext() throws RepositoryException {

    final Iterator<javax.jcr.Node> niceChildren = Iterators.filter(new NodeIterator(node().getNodes()),
            not(nastyChildren));//from ww w .j  av a  2 s. com

    final Iterator<javax.jcr.Node> salientChildren;

    if (options.hasOffset()) {
        final int offset = options.getOffset();
        Iterators.advance(niceChildren, offset);
    }

    if (options.hasLimit()) {
        salientChildren = Iterators.limit(niceChildren, options.getLimit());
    } else {
        salientChildren = niceChildren;
    }

    return Iterators.concat(Iterators.transform(salientChildren, child2triples()));
}