Class LimitingLongIterator

java.lang.Object
com.github.tommyettinger.ds.support.util.LimitingLongIterator
All Implemented Interfaces:
LongIterator, Iterator<Long>

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

    • iterator

      public LongIterator iterator
    • limit

      protected int limit
    • remaining

      protected int remaining
  • Constructor Details

    • LimitingLongIterator

      public LimitingLongIterator()
    • LimitingLongIterator

      public LimitingLongIterator(LongIterator iterator, int limit)
  • Method Details

    • set

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

      public void set(LongIterator iterator)
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<Long>
    • nextLong

      public long nextLong()
      Description copied from interface: LongIterator
      Returns the next long element in the iteration.
      Specified by:
      nextLong in interface LongIterator
      Returns:
      the next long element in the iteration
    • remove

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