Class LimitingIntIterator

java.lang.Object
com.github.tommyettinger.ds.support.util.LimitingIntIterator
All Implemented Interfaces:
IntIterator, Iterator<Integer>

public class LimitingIntIterator extends Object implements IntIterator
Wraps an Iterator so that it only returns at most a specific amount of items (defined by calls to nextInt(). 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(IntIterator, int), and can also just change the Iterator with set(IntIterator).
  • Field Details

    • iterator

      public IntIterator iterator
    • limit

      protected int limit
    • remaining

      protected int remaining
  • Constructor Details

    • LimitingIntIterator

      public LimitingIntIterator()
    • LimitingIntIterator

      public LimitingIntIterator(IntIterator iterator, int limit)
  • Method Details

    • set

      public void set(IntIterator iterator, int limit)
    • set

      public void set(IntIterator iterator)
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<Integer>
    • nextInt

      public int nextInt()
      Description copied from interface: IntIterator
      Returns the next int element in the iteration.
      Specified by:
      nextInt in interface IntIterator
      Returns:
      the next int element in the iteration
    • remove

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