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

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

Introduction

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

Prototype

@Deprecated
public static <T> PeekingIterator<T> peekingIterator(PeekingIterator<T> iterator) 

Source Link

Document

Simply returns its argument.

Usage

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

public static List<PlateRange> splitIntoPlateCopyRanges(SortedSet<Plate> plates) {
    PeekingIterator<Plate> iter = Iterators.peekingIterator(plates.iterator());
    List<PlateRange> plateRanges = Lists.newArrayList();
    ;//from   w  w  w .  ja v a 2  s. c  o m
    while (iter.hasNext()) {
        PlateRange plateRange = PlateRange.findNextPlateRange(iter);
        plateRanges.add(plateRange);
    }
    return plateRanges;
}

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 .ja  v a  2 s.co  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.twitter.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  2s.  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: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);
    }// w  ww  . ja v a 2s . com
    return interArrivalTimes;
}

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

private ClosablePeekingIterator(ClosableIterator<T> ci) {
    this.ci = ci;
    this.pi = Iterators.peekingIterator(ci);
}

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 . jav a2 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  .jav  a  2  s .  co  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:com.splicemachine.hbase.util.AbstractIteratorRegionScanner.java

public AbstractIteratorRegionScanner(Iterator<Set<Cell>> kvs, Scan scan) {
    this.kvs = Iterators.peekingIterator(kvs);
    this.scan = scan;
    this.filter = scan.getFilter();
}

From source file:org.diqube.util.SortedSetUnionIterator.java

@SuppressWarnings("unchecked")
public SortedSetUnionIterator(SortedSet<E>... sets) {
    iterators = new PeekingIterator[sets.length];
    for (int i = 0; i < sets.length; i++)
        iterators[i] = Iterators.peekingIterator(sets[i].iterator());
}

From source file:com.google.gerrit.server.mail.receive.HtmlParser.java

/**
 * Parses comments from html email.//from   w  w  w  .j  a va 2s. c  o m
 *
 * @param email MailMessage as received from the email service.
 * @param comments A specific set of comments as sent out in the original notification email.
 *     Comments are expected to be in the same order as they were sent out to in the email
 * @param changeUrl Canonical change URL that points to the change on this Gerrit instance.
 *     Example: https://go-review.googlesource.com/#/c/91570
 * @return List of MailComments parsed from the html part of the email.
 */
public static List<MailComment> parse(MailMessage email, Collection<Comment> comments, String changeUrl) {
    // TODO(hiesel) Add support for Gmail Mobile
    // TODO(hiesel) Add tests for other popular email clients

    // This parser goes though all html elements in the email and checks for
    // matching patterns. It keeps track of the last file and comments it
    // encountered to know in which context a parsed comment belongs.
    // It uses the href attributes of <a> tags to identify comments sent out by
    // Gerrit as these are generally more reliable then the text captions.
    List<MailComment> parsedComments = new ArrayList<>();
    Document d = Jsoup.parse(email.htmlContent());
    PeekingIterator<Comment> iter = Iterators.peekingIterator(comments.iterator());

    String lastEncounteredFileName = null;
    Comment lastEncounteredComment = null;
    for (Element e : d.body().getAllElements()) {
        String elementName = e.tagName();
        boolean isInBlockQuote = e.parents().stream().filter(p -> p.tagName().equals("blockquote")).findAny()
                .isPresent();

        if (elementName.equals("a")) {
            String href = e.attr("href");
            // Check if there is still a next comment that could be contained in
            // this <a> tag
            if (!iter.hasNext()) {
                continue;
            }
            Comment perspectiveComment = iter.peek();
            if (href.equals(ParserUtil.filePath(changeUrl, perspectiveComment))) {
                if (lastEncounteredFileName == null
                        || !lastEncounteredFileName.equals(perspectiveComment.key.filename)) {
                    // Not a file-level comment, but users could have typed a comment
                    // right after this file annotation to create a new file-level
                    // comment. If this file has a file-level comment, we have already
                    // set lastEncounteredComment to that file-level comment when we
                    // encountered the file link and should not reset it now.
                    lastEncounteredFileName = perspectiveComment.key.filename;
                    lastEncounteredComment = null;
                } else if (perspectiveComment.lineNbr == 0) {
                    // This was originally a file-level comment
                    lastEncounteredComment = perspectiveComment;
                    iter.next();
                }
            } else if (ParserUtil.isCommentUrl(href, changeUrl, perspectiveComment)) {
                // This is a regular inline comment
                lastEncounteredComment = perspectiveComment;
                iter.next();
            }
        } else if (!isInBlockQuote && elementName.equals("div") && !e.className().startsWith("gmail")) {
            // This is a comment typed by the user
            // Replace non-breaking spaces and trim string
            String content = e.ownText().replace('\u00a0', ' ').trim();
            if (!Strings.isNullOrEmpty(content)) {
                if (lastEncounteredComment == null && lastEncounteredFileName == null) {
                    // Remove quotation line, email signature and
                    // "Sent from my xyz device"
                    content = ParserUtil.trimQuotation(content);
                    // TODO(hiesel) Add more sanitizer
                    if (!Strings.isNullOrEmpty(content)) {
                        parsedComments.add(
                                new MailComment(content, null, null, MailComment.CommentType.CHANGE_MESSAGE));
                    }
                } else if (lastEncounteredComment == null) {
                    parsedComments.add(new MailComment(content, lastEncounteredFileName, null,
                            MailComment.CommentType.FILE_COMMENT));
                } else {
                    parsedComments.add(new MailComment(content, null, lastEncounteredComment,
                            MailComment.CommentType.INLINE_COMMENT));
                }
            }
        }
    }
    return parsedComments;
}