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

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

Introduction

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

Prototype

@Override
E next();

Source Link

Document

The objects returned by consecutive calls to #peek() then #next() are guaranteed to be equal to each other.

Usage

From source file:edu.harvard.med.screensaver.model.libraries.PlateRange.java

private static PlateRange findNextPlateRange(PeekingIterator<Plate> iter) {
    SortedSet<Plate> platesScreened = Sets.newTreeSet();
    platesScreened.add(iter.next());
    while (iter.hasNext()) {
        Plate next = iter.peek();/*w w  w .  j  av a 2 s.c o m*/
        Plate last = platesScreened.last();
        if (next.getPlateNumber() > last.getPlateNumber() + 1) {
            break;
        } else if (!next.getCopy().equals(last.getCopy())) {
            break;
        }
        platesScreened.add(iter.next());
    }
    return new PlateRange(platesScreened);
}

From source file:org.kmworks.util.cp.CodepointSetUtil.java

public static void toText(CodepointSet set, Writer w, int radix) throws IOException {
    final PeekingIterator<Integer> iter = set.iterator();

    while (iter.hasNext()) {
        int curr = iter.next();
        int ende = curr;
        while (iter.hasNext() && iter.peek().equals(ende + 1)) {
            ende = iter.next();//from  ww  w. j a  v  a  2s.co m
        }
        if (ende == curr) {
            writeCodepoint(w, curr, radix);
            writeln(w);
        } else {
            writeCodepoint(w, curr, radix);
            w.write('-');
            writeCodepoint(w, ende, radix);
            writeln(w);
        }
    }
}

From source file:io.prestosql.sql.planner.optimizations.LocalProperties.java

public static <T> List<LocalProperty<T>> stripLeadingConstants(List<? extends LocalProperty<T>> properties) {
    PeekingIterator<? extends LocalProperty<T>> iterator = peekingIterator(properties.iterator());
    while (iterator.hasNext() && iterator.peek() instanceof ConstantProperty) {
        iterator.next();
    }/*from  ww  w . jav a  2 s. c om*/
    return ImmutableList.copyOf(iterator);
}

From source file:org.apache.cassandra.dht.Bounds.java

/**
 * Retrieves non-overlapping bounds for the list of input bounds
 *
 * Assume we have the following bounds/*from  w w w.jav  a  2  s.c om*/
 * (brackets representing left/right bound):
 * [   ] [   ]    [   ]   [  ]
 * [   ]         [       ]
 * This method will return the following bounds:
 * [         ]    [          ]
 *
 * @param bounds unsorted bounds to find overlaps
 * @return the non-overlapping bounds
 */
public static <T extends RingPosition<T>> Set<Bounds<T>> getNonOverlappingBounds(Iterable<Bounds<T>> bounds) {
    ArrayList<Bounds<T>> sortedBounds = Lists.newArrayList(bounds);
    Collections.sort(sortedBounds, new Comparator<Bounds<T>>() {
        public int compare(Bounds<T> o1, Bounds<T> o2) {
            return o1.left.compareTo(o2.left);
        }
    });

    Set<Bounds<T>> nonOverlappingBounds = Sets.newHashSet();

    PeekingIterator<Bounds<T>> it = Iterators.peekingIterator(sortedBounds.iterator());
    while (it.hasNext()) {
        Bounds<T> beginBound = it.next();
        Bounds<T> endBound = beginBound;
        while (it.hasNext() && endBound.right.compareTo(it.peek().left) >= 0)
            endBound = it.next();
        nonOverlappingBounds.add(new Bounds<>(beginBound.left, endBound.right));
    }

    return nonOverlappingBounds;
}

From source file:com.github.rinde.rinsim.core.model.time.TimeUtil.java

public static List<Double> interArrivalTimes(Iterable<Long> timeStamps) {
    final PeekingIterator<Long> it = Iterators.peekingIterator(timeStamps.iterator());
    final List<Double> interArrivalTimes = new ArrayList<>();
    for (long l1 = it.next(); it.hasNext(); l1 = it.next()) {
        final long l2 = it.peek();
        interArrivalTimes.add((l2 - l1) / 1000000d);
    }//from  w  w w  .ja  va  2  s .  c om
    return interArrivalTimes;
}

From source file:com.twitter.aurora.scheduler.base.Numbers.java

/**
 * Converts a set of integers into a set of contiguous closed ranges that equally represent the
 * input integers.//from  ww w.  j  a v a 2  s.  c o  m
 * <p>
 * The resulting ranges will be in ascending order.
 *
 * @param values Values to transform to ranges.
 * @return Closed ranges with identical members to the input set.
 */
public static Set<Range<Integer>> toRanges(Iterable<Integer> values) {
    ImmutableSet.Builder<Range<Integer>> builder = ImmutableSet.builder();

    PeekingIterator<Integer> iterator = Iterators.peekingIterator(Sets.newTreeSet(values).iterator());

    // Build ranges until there are no numbers left.
    while (iterator.hasNext()) {
        // Start a new range.
        int start = iterator.next();
        int end = start;
        // Increment the end until the range is non-contiguous.
        while (iterator.hasNext() && (iterator.peek() == (end + 1))) {
            end++;
            iterator.next();
        }

        builder.add(Range.closed(start, end));
    }

    return builder.build();
}

From source file:io.prestosql.sql.planner.optimizations.LocalProperties.java

public static <T> Set<T> extractLeadingConstants(List<? extends LocalProperty<T>> properties) {
    ImmutableSet.Builder<T> builder = ImmutableSet.builder();
    PeekingIterator<? extends LocalProperty<T>> iterator = peekingIterator(properties.iterator());
    while (iterator.hasNext() && iterator.peek() instanceof ConstantProperty) {
        builder.add(((ConstantProperty<T>) iterator.next()).getColumn());
    }//from   w w  w .j a v  a 2s.c om
    return builder.build();
}

From source file:org.apache.aurora.scheduler.base.Numbers.java

/**
 * Converts a set of integers into a set of contiguous closed ranges that equally represent the
 * input integers./*w  ww .j av  a 2  s  . c o m*/
 * <p>
 * The resulting ranges will be in ascending order.
 * <p>
 * TODO(wfarner): Change this to return a canonicalized RangeSet.
 *
 * @param values Values to transform to ranges.
 * @return Closed ranges with identical members to the input set.
 */
public static Set<Range<Integer>> toRanges(Iterable<Integer> values) {
    ImmutableSet.Builder<Range<Integer>> builder = ImmutableSet.builder();

    PeekingIterator<Integer> iterator = Iterators.peekingIterator(Sets.newTreeSet(values).iterator());

    // Build ranges until there are no numbers left.
    while (iterator.hasNext()) {
        // Start a new range.
        int start = iterator.next();
        int end = start;
        // Increment the end until the range is non-contiguous.
        while (iterator.hasNext() && iterator.peek() == end + 1) {
            end++;
            iterator.next();
        }

        builder.add(Range.closed(start, end));
    }

    return builder.build();
}

From source file:org.elasticsearch.common.collect.Iterators2.java

/** Remove duplicated elements from an iterator over sorted content. */
public static <T> Iterator<T> deduplicateSorted(Iterator<? extends T> iterator,
        final Comparator<? super T> comparator) {
    final PeekingIterator<T> it = Iterators.peekingIterator(iterator);
    return new UnmodifiableIterator<T>() {

        @Override/*ww  w  .  j  a v  a2  s  .  c o m*/
        public boolean hasNext() {
            return it.hasNext();
        }

        @Override
        public T next() {
            final T ret = it.next();
            while (it.hasNext() && comparator.compare(ret, it.peek()) == 0) {
                it.next();
            }
            assert !it.hasNext() || comparator.compare(ret, it.peek()) < 0 : "iterator is not sorted: " + ret
                    + " > " + it.peek();
            return ret;
        }

    };
}

From source file:com.palantir.atlasdb.keyvalue.partition.util.RowResultUtil.java

public static RowResult<Set<Long>> allTimestamps(PeekingIterator<RowResult<Set<Long>>> it) {
    Preconditions.checkArgument(it.hasNext());

    final byte[] row = it.peek().getRowName();
    final SortedMap<byte[], Set<Long>> result = Maps.newTreeMap(UnsignedBytes.lexicographicalComparator());
    while (it.hasNext() && Arrays.equals(row, it.peek().getRowName())) {
        RowResult<Set<Long>> kvsResult = it.next();
        for (Map.Entry<Cell, Set<Long>> e : kvsResult.getCells()) {
            if (!result.containsKey(e.getKey().getColumnName())) {
                result.put(e.getKey().getColumnName(), Sets.<Long>newHashSet());
            }/*  w w  w .ja  va 2  s  . com*/
            result.get(e.getKey().getColumnName()).addAll(e.getValue());
        }
    }
    return RowResult.create(row, result);
}