I have a generic Item
-interface and an implementation with a date property like so:
public interface Item<T extends Temporal> {
T getDate();
}
public class SpecialItem implements Item<LocalDateTime> {
private final LocalDateTime localDateTime;
public SpecialItem(LocalDateTime localDateTime) {
this.localDateTime = localDateTime;
}
@Override
public LocalDateTime getDate() {
return localDateTime;
}
}
Now I have a provider class and interface for said Item
public interface ItemProvider<T extends Temporal> {
List<Item<T>> getEntries();
}
public class SpecialItemProvider<T extends Temporal> implements ItemProvider<T> {
private final List<Item<T>> entries;
public SpecialItemProvider(List<Item<T>> entries) {
this.entries = entries;
}
@Override
public List<Item<T>> getEntries() {
return entries;
}
}
I have the problem that I can not really call the constructor of the SpecialItemProvider
like so (I really want an object of type ItemProvider
):
@Test
public void genericTest(){
final List<SpecialItem> dateTimeList =
Collections.singletonList(new SpecialItem(LocalDateTime.MAX)));
ItemProvider itemProvider = new SpecialItemProvider<LocalDateTime>(dateTimeList);
}
It always fails with the following error message:
java: incompatible types: java.util.List<GenericTest.SpecialItem>
cannot be converted to java.util.List<GenericTest.Item<java.time.LocalDateTime>>
Which makes me wonder why it is not accepted although SpecialItem
explicitly implements Item<LocalDateTime>
What am I doing wrong here?