Example usage for com.google.common.collect PeekingIterator peek

List of usage examples for com.google.common.collect PeekingIterator peek

Introduction

In this page you can find the example usage for com.google.common.collect PeekingIterator peek.

Prototype

E peek();

Source Link

Document

Returns the next element in the iteration, without advancing the iteration.

Usage

From source file:org.glowroot.ui.TraceCommonService.java

private static void writeEntries(JsonGenerator jg, List<Trace.Entry> entries) throws IOException {
    jg.writeStartArray();/*from  ww  w  .  j av  a2 s  .c o m*/
    PeekingIterator<Trace.Entry> i = Iterators.peekingIterator(entries.iterator());
    while (i.hasNext()) {
        Trace.Entry entry = i.next();
        int depth = entry.getDepth();
        jg.writeStartObject();
        writeJson(entry, jg);
        int nextDepth = i.hasNext() ? i.peek().getDepth() : 0;
        if (nextDepth > depth) {
            jg.writeArrayFieldStart("childEntries");
        } else if (nextDepth < depth) {
            jg.writeEndObject();
            for (int j = depth; j > nextDepth; j--) {
                jg.writeEndArray();
                jg.writeEndObject();
            }
        } else {
            jg.writeEndObject();
        }
    }
    jg.writeEndArray();
}

From source file:com.metamx.common.guava.MergeIterator.java

public MergeIterator(final Comparator<T> comparator, List<Iterator<T>> iterators) {
    pQueue = new PriorityQueue<>(16, new Comparator<PeekingIterator<T>>() {
        @Override/*  w w w  . j av  a 2 s .  c  o m*/
        public int compare(PeekingIterator<T> lhs, PeekingIterator<T> rhs) {
            return comparator.compare(lhs.peek(), rhs.peek());
        }
    });

    for (Iterator<T> iterator : iterators) {
        final PeekingIterator<T> iter = Iterators.peekingIterator(iterator);

        if (iter != null && iter.hasNext()) {
            pQueue.add(iter);
        }
    }

}

From source file:sg.atom.utils._beta.functional.MergeIterator.java

public MergeIterator(final Comparator<T> comparator, List<Iterator<T>> iterators) {
    pQueue = new PriorityQueue<PeekingIterator<T>>(16, new Comparator<PeekingIterator<T>>() {
        @Override/*  w  w  w .ja  va2s  .  c om*/
        public int compare(PeekingIterator<T> lhs, PeekingIterator<T> rhs) {
            return comparator.compare(lhs.peek(), rhs.peek());
        }
    });

    for (Iterator<T> iterator : iterators) {
        final PeekingIterator<T> iter = Iterators.peekingIterator(iterator);

        if (iter != null && iter.hasNext()) {
            pQueue.add(iter);
        }
    }

}

From source file:org.apache.kylin.storage.gtrecord.SortMergedPartitionResultIterator.java

SortMergedPartitionResultIterator(List<PartitionResultIterator> partitionResults, GTInfo info,
        final Comparator<GTRecord> comparator) {

    this.record = new GTRecord(info);
    Comparator<PeekingIterator<GTRecord>> heapComparator = new Comparator<PeekingIterator<GTRecord>>() {
        public int compare(PeekingIterator<GTRecord> o1, PeekingIterator<GTRecord> o2) {
            return comparator.compare(o1.peek(), o2.peek());
        }/*from   w w  w .ja  v a 2  s . co m*/
    };
    this.heap = new PriorityQueue<>(partitionResults.size(), heapComparator);

    for (PartitionResultIterator it : partitionResults) {
        if (it.hasNext()) {
            heap.offer(Iterators.peekingIterator(it));
        }
    }
}

From source file:io.druid.collections.OrderedMergeIterator.java

public OrderedMergeIterator(final Comparator<T> comparator, Iterator<Iterator<T>> iterators) {
    this.comparator = comparator;
    pQueue = new PriorityQueue<PeekingIterator<T>>(16, new Comparator<PeekingIterator<T>>() {
        @Override//from   w  w w .  ja  v  a  2  s.c om
        public int compare(PeekingIterator<T> lhs, PeekingIterator<T> rhs) {
            return comparator.compare(lhs.peek(), rhs.peek());
        }
    });

    iterOfIterators = Iterators
            .peekingIterator(FunctionalIterator.create(iterators).filter(new Predicate<Iterator<T>>() {
                @Override
                public boolean apply(Iterator<T> input) {
                    return input.hasNext();
                }
            }).transform(new Function<Iterator<T>, PeekingIterator<T>>() {
                @Override
                public PeekingIterator<T> apply(Iterator<T> input) {
                    return Iterators.peekingIterator(input);
                }
            }));
}

From source file:com.metamx.druid.collect.OrderedMergeIterator.java

public OrderedMergeIterator(final Comparator<T> comparator, Iterator<Iterator<T>> iterators) {
    this.comparator = comparator;
    pQueue = new PriorityQueue<PeekingIterator<T>>(16, new Comparator<PeekingIterator<T>>() {
        @Override//from  www . j  a v  a2  s.co  m
        public int compare(PeekingIterator<T> lhs, PeekingIterator<T> rhs) {
            return comparator.compare(lhs.peek(), rhs.peek());
        }
    });

    iterOfIterators = Iterators
            .peekingIterator(FunctionalIterator.create(iterators).filter(new Predicate<Iterator<T>>() {
                @Override
                public boolean apply(@Nullable Iterator<T> input) {
                    return input.hasNext();
                }
            }).transform(new Function<Iterator<T>, PeekingIterator<T>>() {
                @Override
                public PeekingIterator<T> apply(@Nullable Iterator<T> input) {
                    return Iterators.peekingIterator(input);
                }
            }));
}

From source file:org.apache.accumulo.examples.filedata.FileDataQuery.java

public ChunkInputStream getData(String hash) throws IOException {
    scanner.setRange(new Range(hash));
    scanner.setBatchSize(1);//from  w w w .  jav  a2 s.c  o m
    lastRefs.clear();
    PeekingIterator<Entry<Key, Value>> pi = Iterators.peekingIterator(scanner.iterator());
    if (pi.hasNext()) {
        while (!pi.peek().getKey().getColumnFamily().equals(FileDataIngest.CHUNK_CF)) {
            lastRefs.add(pi.peek());
            pi.next();
        }
    }
    cis.clear();
    cis.setSource(pi);
    return cis;
}

From source file:org.apache.druid.collections.OrderedMergeIterator.java

public OrderedMergeIterator(final Comparator<T> comparator, Iterator<Iterator<T>> iterators) {
    this.comparator = comparator;
    firstElementComparedPQueue = new PriorityQueue<PeekingIterator<T>>(16,
            new Comparator<PeekingIterator<T>>() {
                @Override/*  ww w.j ava2 s  .  c o  m*/
                public int compare(PeekingIterator<T> lhs, PeekingIterator<T> rhs) {
                    return comparator.compare(lhs.peek(), rhs.peek());
                }
            });

    iterOfIterators = Iterators
            .peekingIterator(FunctionalIterator.create(iterators).filter(new Predicate<Iterator<T>>() {
                @Override
                public boolean apply(Iterator<T> input) {
                    return input.hasNext();
                }
            }).transform(new Function<Iterator<T>, PeekingIterator<T>>() {
                @Override
                public PeekingIterator<T> apply(Iterator<T> input) {
                    return Iterators.peekingIterator(input);
                }
            }));
}

From source file:com.google.errorprone.bugpatterns.MultiVariableDeclaration.java

private Description checkDeclarations(List<? extends Tree> children, VisitorState state) {
    PeekingIterator<Tree> it = Iterators.<Tree>peekingIterator(children.iterator());
    while (it.hasNext()) {
        if (it.peek().getKind() != Tree.Kind.VARIABLE) {
            it.next();/*from w  w  w  .  j  a  v a 2 s. com*/
            continue;
        }
        VariableTree variableTree = (VariableTree) it.next();
        ArrayList<VariableTree> fragments = new ArrayList<>();
        fragments.add(variableTree);
        // Javac handles multi-variable declarations by lowering them in the parser into a series of
        // individual declarations, all of which have the same start position. We search for the first
        // declaration in the group, which is either the first variable declared in this scope or has
        // a distinct end position from the previous declaration.
        while (it.hasNext() && it.peek().getKind() == Tree.Kind.VARIABLE
                && ((JCTree) variableTree).getStartPosition() == ((JCTree) it.peek()).getStartPosition()) {
            fragments.add((VariableTree) it.next());
        }
        if (fragments.size() == 1) {
            continue;
        }
        Fix fix = SuggestedFix.replace(((JCTree) fragments.get(0)).getStartPosition(),
                state.getEndPosition(Iterables.getLast(fragments)), Joiner.on("; ").join(fragments) + ";");
        state.reportMatch(describeMatch(fragments.get(0), fix));
    }
    return NO_MATCH;
}

From source file:io.crate.operation.merge.PlainSortedMergeIterator.java

public PlainSortedMergeIterator(Iterable<? extends KeyIterable<TKey, TRow>> iterables,
        final Comparator<? super TRow> itemComparator) {

    Comparator<PeekingIterator<TRow>> heapComparator = new Comparator<PeekingIterator<TRow>>() {
        @Override/*from  w ww .j a v  a2 s .  com*/
        public int compare(PeekingIterator<TRow> o1, PeekingIterator<TRow> o2) {
            return itemComparator.compare(o1.peek(), o2.peek());
        }
    };
    queue = new PriorityQueue<>(2, heapComparator);
    addIterators(iterables);
}