Interface IteratorNumber

  • All Known Subinterfaces:
    IteratorByte, IteratorDouble, IteratorFloat, IteratorInteger, IteratorLong, IteratorShort, IteratorUByte, IteratorUInteger, IteratorULong, IteratorUShort

    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 IteratorDouble, IteratorFloat, IteratorLong, IteratorInteger, IteratorShort and IteratorByte.

    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.

    Author:
    Gabriele Carcassi
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      boolean hasNext()
      Returns true if the iteration has more elements.
      byte nextByte()
      Returns the next element in the iteration casted to a byte.
      double nextDouble()
      Returns the next element in the iteration casted to a double.
      float nextFloat()
      Returns the next element in the iteration casted to a float.
      int nextInt()
      Returns the next element in the iteration casted to an int.
      long nextLong()
      Returns the next element in the iteration casted to a long.
      short nextShort()
      Returns the next element in the iteration casted to a short.
    • Method Detail

      • 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