List of usage examples for com.google.common.collect PeekingIterator hasNext
boolean hasNext();
From source file:org.glowroot.ui.TraceCommonService.java
private static void writeEntries(JsonGenerator jg, List<Trace.Entry> entries) throws IOException { jg.writeStartArray();/* w w w .ja v a 2 s . c om*/ 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:org.diqube.util.SortedSetUnionIterator.java
@Override public E next() { if (!hasNext()) return null; PeekingIterator<E> nextSmallestIt = null; for (PeekingIterator<E> it : iterators) { if (!it.hasNext()) continue; if (nextSmallestIt == null || (nextSmallestIt.peek().compareTo(it.peek()) > 0)) nextSmallestIt = it;/* ww w. j ava2s. c om*/ } return nextSmallestIt.next(); }
From source file:com.metamx.common.guava.MergeIterator.java
@Override public T next() { if (!hasNext()) { throw new NoSuchElementException(); }//ww w . j a va 2 s . c om PeekingIterator<T> retIt = pQueue.remove(); T retVal = retIt.next(); if (retIt.hasNext()) { pQueue.add(retIt); } return retVal; }
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/*from w ww .j av a 2s .com*/ 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 ww . ja va 2s. 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:org.apache.accumulo.examples.filedata.FileDataQuery.java
public ChunkInputStream getData(String hash) throws IOException { scanner.setRange(new Range(hash)); scanner.setBatchSize(1);//from ww w. j a va 2 s. c om 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: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();//w w w . j a v a2 s .co m 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:eu.lp0.cursus.scoring.scores.impl.TopCountryOverallPositionData.java
@Override protected LinkedListMultimap<Integer, Pilot> calculateOverallPositionsWithOrder() { if (scores.getRaces().size() < 2) { return super.calculateOverallPositionsWithOrder(); }//from ww w. j a va 2 s . c o m Comparator<Pilot> averagePoints = new AveragePointsComparator<T>(scores, placingMethod); Comparator<Pilot> racePlacings = new PilotRacePlacingComparator<T>(scores, placingMethod); Comparator<Pilot> fallbackOrdering = new PilotRaceNumberComparator(); SortedSet<Pilot> pilots = new TreeSet<Pilot>( Ordering.from(averagePoints).compound(racePlacings).compound(fallbackOrdering)); pilots.addAll(scores.getPilots()); LinkedListMultimap<Integer, Pilot> overallPositions = LinkedListMultimap.create(); List<Pilot> collectedPilots = new ArrayList<Pilot>(scores.getPilots().size()); int position = 1; PeekingIterator<Pilot> it = Iterators.peekingIterator(pilots.iterator()); while (it.hasNext()) { Pilot pilot = it.next(); collectedPilots.add(pilot); // If this pilot compares equally with the next pilot, add them too while (it.hasNext() && racePlacings.compare(it.peek(), pilot) == 0) { collectedPilots.add(it.next()); } // Sort them by an arbitrary order Collections.sort(collectedPilots, fallbackOrdering); // Add them all to this position overallPositions.putAll(position, collectedPilots); position += collectedPilots.size(); collectedPilots.clear(); } return overallPositions; }
From source file:org.calrissian.mango.collect.CloseableIterators.java
/** * Returns a {@code PeekingCloseableIterator} backed by the given closeable iterator. * * Calls to peek do not change the state of the iterator. The subsequent call to next * after peeking will always return the same value. *//*from w w w. j av a2s . c om*/ public static <T> PeekingCloseableIterator<T> peekingIterator(final CloseableIterator<T> iterator) { final PeekingIterator<T> peeking = Iterators.peekingIterator(iterator); return new PeekingCloseableIterator<T>() { @Override public void closeQuietly() { iterator.closeQuietly(); } @Override public void close() throws IOException { iterator.close(); } @Override public T peek() { return peeking.peek(); } @Override public T next() { return peeking.next(); } @Override public void remove() { peeking.remove(); } @Override public boolean hasNext() { return peeking.hasNext(); } }; }
From source file:com.metamx.druid.indexer.granularity.ArbitraryGranularitySpec.java
@JsonCreator public ArbitraryGranularitySpec(@JsonProperty("intervals") List<Interval> inputIntervals) { intervals = Sets.newTreeSet(Comparators.intervalsByStartThenEnd()); // Insert all intervals for (final Interval inputInterval : inputIntervals) { intervals.add(inputInterval);/* w ww. jav a 2 s .c o m*/ } // Ensure intervals are non-overlapping (but they may abut each other) final PeekingIterator<Interval> intervalIterator = Iterators.peekingIterator(intervals.iterator()); while (intervalIterator.hasNext()) { final Interval currentInterval = intervalIterator.next(); if (intervalIterator.hasNext()) { final Interval nextInterval = intervalIterator.peek(); if (currentInterval.overlaps(nextInterval)) { throw new IllegalArgumentException( String.format("Overlapping intervals: %s, %s", currentInterval, nextInterval)); } } } }