Class LimitingIterator<T>

java.lang.Object
com.github.tommyettinger.ds.support.util.LimitingIterator<T>
Type Parameters:
T - the type of items this can return, and the type the wrapped Iterator returns
All Implemented Interfaces:
Iterator<T>

public class LimitingIterator<T> extends Object implements Iterator<T>
Wraps an Iterator so that it only returns at most a specific amount of items (defined by calls to next(). This can be useful to limit infinite Iterators so they only produce a finite amount of results. This has undefined behavior if any items the Iterator could return are modified during iteration.
You can change the Iterator and limit at once using set(Iterator, int), and can also just change the Iterator with set(Iterator).
  • Field Details

    • iterator

      public Iterator<T> iterator
    • limit

      protected int limit
    • remaining

      protected int remaining
  • Constructor Details

    • LimitingIterator

      public LimitingIterator()
    • LimitingIterator

      public LimitingIterator(Iterator<T> iterator, int limit)
  • Method Details

    • set

      public void set(Iterator<T> iterator, int limit)
    • set

      public void set(Iterator<T> iterator)
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<T>
    • next

      public T next()
      Specified by:
      next in interface Iterator<T>
    • remove

      public void remove()
      Specified by:
      remove in interface Iterator<T>