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 betweenIteratorDouble
,IteratorFloat
,IteratorLong
,IteratorInteger
,IteratorShort
andIteratorByte
.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
beIterator<Number>
andIteratorDouble
beIterator<Double>
, but this does not work because generics are invariant. We could haveIterator<T extends Number>
andIterator<Double>
, but that would meanIteratorNumber
would 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 returnNumber
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()
Returnstrue
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()
Returnstrue
if the iteration has more elements. (In other words, returnstrue
ifnextXxx
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
-
-