I have created an Iterator
wrapper which returns elements until a certain threshold is reached as following:
public class MyIterators {
public static <E extends Comparable<E>> Iterator<E> threshold(final Iterator<? extends E> iterator, final E threshold) {
...
}
}
I want to use this for an Iterator<ChronoZonedDateTime>
in a utility function as following:
public static Iterator<ZonedDateTime> oneYear(final Iterator<ZonedDateTime> iterator) {
return MyIterators.threshold(iterator, ZonedDateTime.now().plusYears(1));
}
I am getting:
method threshold in class MyIterators cannot be applied to given types;
[ERROR] required: java.util.Iterator<? extends E>,E
[ERROR] found: java.util.Iterator<java.time.chrono.ChronoZonedDateTime>,java.time.ZonedDateTime
[ERROR] reason: cannot infer type-variable(s) E
[ERROR] (argument mismatch; java.util.Iterator<java.time.chrono.ChronoZonedDateTime> cannot be converted to java.util.Iterator<? extends java.time.chrono.ChronoZonedDateTime<?>>)
The compiler is inferring ChronoZonedDateTime
instead of ZonedDateTime
, while I am not declaring/using that class at all.
How do I cope with this? Do I introduce a manual cast, or is there still a way to do all this in a type-safe manner?
I am working with JDK 8.x.