Interface IteratorNumber
-
- All Known Subinterfaces:
IteratorByte,IteratorDouble,IteratorFloat,IteratorInteger,IteratorLong,IteratorShort,IteratorUByte,IteratorUInteger,IteratorULong,IteratorUShort
public interface IteratorNumberAn 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 betweenIteratorDouble,IteratorFloat,IteratorLong,IteratorInteger,IteratorShortandIteratorByte.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
IteratorNumberbeIterator<Number>andIteratorDoublebeIterator<Double>, but this does not work because generics are invariant. We could haveIterator<T extends Number>andIterator<Double>, but that would meanIteratorNumberwould need a type parameter, and the user of the API would have to use bound type parameters likeIteratorNumber<? extends Number>, which is awful. We could have all extendIterator<Number>but then the foreach loops would returnNumberin 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 booleanhasNext()Returnstrueif the iteration has more elements.bytenextByte()Returns the next element in the iteration casted to a byte.doublenextDouble()Returns the next element in the iteration casted to a double.floatnextFloat()Returns the next element in the iteration casted to a float.intnextInt()Returns the next element in the iteration casted to an int.longnextLong()Returns the next element in the iteration casted to a long.shortnextShort()Returns the next element in the iteration casted to a short.
-
-
-
Method Detail
-
hasNext
boolean hasNext()
Returnstrueif the iteration has more elements. (In other words, returnstrueifnextXxxwould return an element rather than throwing an exception.)- Returns:
trueif 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
-
-