TimurMahammadov / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

RFE - Iterators.limit(Iterator<T>, Predicate<T>) #70

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Given a predicate P, return an iterator that is done when the predicate
evaluates to true.

While this can be accomplished using Iterators.filter, there are times (for
performance), when a short-circuit is handy.

Sample implementation:

    public static <T> Iterator<T> limit(final Iterator<T> sourceIterator,
final Predicate<T> limitingPredicate) {
        return new AbstractIterator<T>() {
            @Override
            protected T computeNext() {
                if (!sourceIterator.hasNext()) {
                    return this.endOfData();
                }
                final T tee = sourceIterator.next();
                return limitingPredicate.apply(tee) ? this.endOfData() : tee;
            }
        };
    }

Original issue reported on code.google.com by nkp...@gmail.com on 2 Jun 2008 at 6:26

GoogleCodeExporter commented 9 years ago
Thanks for the suggestion.

Unfortunately, the added benefit of that method doesn't justify including it in 
the
library. You can always call the existing filter and limit methods in 
succession.

Original comment by jared.l....@gmail.com on 2 Jun 2008 at 5:22