Interface IteratorNumber

All Known Implementing Classes:
IteratorByte, IteratorDouble, IteratorFloat, IteratorInt, IteratorLong, IteratorShort

public interface IteratorNumber
An iterator for a stream of primitive numbers, which allows to retrieve the value casted in the type you prefer. This class allows to implement a single binding for iterating over a collection instead of six different binding. If the original type is required, instanceof can be used to differentiate between IteratorByte, IteratorShort, IteratorInt, IteratorLong, IteratorFloat and IteratorDouble.

We looked into making this class implement Iterator, but unfortunately, because of generics being invariant, we cannot provide a scheme that would work naturally in all cases. Ideally, we would want to have IteratorNumber be Iterator<Number> and IteratorDouble be Iterator<Double>, but this does not work because generics are invariant. We could have Iterator<T extends Number> and Iterator<Double>, but that would mean IteratorNumber would need a type parameter, and the user of the API would have to use bound type parameters like IteratorNumber<? extends Number>, which is awful. We could have all extend Iterator<Number> but then the foreach loops would return Number in all cases, even for collections of more specific type.

  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns true if the iteration has more elements.
    byte
    Returns the next element in the iteration casted to a byte.
    double
    Returns the next element in the iteration casted to a double.
    float
    Returns the next element in the iteration casted to a float.
    int
    Returns the next element in the iteration casted to an int.
    long
    Returns the next element in the iteration casted to a long.
    short
    Returns the next element in the iteration casted to a short.
  • Method Details

    • hasNext

      boolean hasNext()
      Returns true if the iteration has more elements. (In other words, returns true if nextXxx would return an element rather than throwing an exception.)
      Returns:
      true if the iteration has more elements
    • nextFloat

      float nextFloat()
      Returns the next element in the iteration casted to a float.
      Returns:
      the next element in the iteration
      Throws:
      NoSuchElementException - if the iteration has no more elements
    • nextDouble

      double nextDouble()
      Returns the next element in the iteration casted to a double.
      Returns:
      the next element in the iteration
      Throws:
      NoSuchElementException - if the iteration has no more elements
    • nextByte

      byte nextByte()
      Returns the next element in the iteration casted to a byte.
      Returns:
      the next element in the iteration
      Throws:
      NoSuchElementException - if the iteration has no more elements
    • nextShort

      short nextShort()
      Returns the next element in the iteration casted to a short.
      Returns:
      the next element in the iteration
      Throws:
      NoSuchElementException - if the iteration has no more elements
    • nextInt

      int nextInt()
      Returns the next element in the iteration casted to an int.
      Returns:
      the next element in the iteration
      Throws:
      NoSuchElementException - if the iteration has no more elements
    • nextLong

      long nextLong()
      Returns the next element in the iteration casted to a long.
      Returns:
      the next element in the iteration
      Throws:
      NoSuchElementException - if the iteration has no more elements