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: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  w  w  w. ja v  a 2 s  .  c  o  m
    }
    return ImmutableList.copyOf(iterator);
}

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   ww  w  . j  a  v  a2s .  c  om
    return builder.build();
}

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());
            }//from   ww  w  .  j ava2s . c  om
            result.get(e.getKey().getColumnName()).addAll(e.getValue());
        }
    }
    return RowResult.create(row, result);
}

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

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

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

    // Assert that there is no multiple values for same key
    for (Set<Value> cell : result.values()) {
        for (Value val : cell) {
            for (Value otherVal : cell) {
                if (val != otherVal) {
                    assert val.getTimestamp() != otherVal.getTimestamp();
                }
            }
        }
    }

    return RowResult.create(row, result);
}

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());/*from  w ww .ja v a 2  s. c om*/
    while (iter.hasNext()) {
        Plate next = iter.peek();
        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 w  w w  .  j a v a2  s. c o 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:com.palantir.atlasdb.keyvalue.partition.util.RowResultUtil.java

public static RowResult<Value> mergeResults(PeekingIterator<RowResult<Value>> it,
        QuorumParameters.QuorumRequestParameters quorumRequestParameters) {
    Preconditions.checkArgument(it.hasNext());

    byte[] row = it.peek().getRowName();
    final SortedMap<byte[], Value> result = Maps.newTreeMap(UnsignedBytes.lexicographicalComparator());
    int failCount = 0;
    int succCount = 0;
    RuntimeException lastSuppressedException = null;

    while (it.hasNext() && Arrays.equals(it.peek().getRowName(), row)) {
        try {/*from w ww.j a  v  a2s .  com*/
            for (Map.Entry<Cell, Value> e : it.next().getCells()) {
                final byte[] col = e.getKey().getColumnName();

                // Assert that there is not contradictory data
                if (result.containsKey(col) && e.getValue().getTimestamp() == result.get(col).getTimestamp()) {
                    assert Arrays.equals(result.get(col).getContents(), e.getValue().getContents());
                }

                if (!result.containsKey(col) || result.get(col).getTimestamp() < e.getValue().getTimestamp()) {
                    result.put(col, e.getValue());
                }
            }
            succCount++;
        } catch (RuntimeException e) {
            System.err.println("Could not read for rangeRequest.");
            failCount++;
            if (failCount >= quorumRequestParameters.getFailureFactor()) {
                throw Throwables.rewrapAndThrowUncheckedException("Could not get enough reads.", e);
            }
            lastSuppressedException = e;
        }
    }

    if (succCount < quorumRequestParameters.getSuccessFactor()) {
        if (lastSuppressedException != null) {
            throw lastSuppressedException;
        } else {
            throw new RuntimeException("Not enough reads for row " + Arrays.toString(row));
        }
    }

    return RowResult.create(row, result);
}

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  ww w  .j  a v a 2 s.c  om*/
    return interArrivalTimes;
}

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//  w w w.  ja  va2 s . com
 * (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: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//  w  w  w.  j  ava 2 s. c om
        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;
        }

    };
}