I have got the following scenario were I have class Class<? extends IModel<?>> aCls
and collection Collection<? extends IModel<?>> entitiesCollection
and trying to pass them to method <T extends IModel<T>> void doIndex(Class<T> clz, Iterable<T> items)
, but keep getting compile time exception. I tried wildcard capture and other ways, but no luck. I must be missing something obvious. Will appreciate any advice.
Multimap<Class<? extends IModel<?>>, IModel<?>> entityMultimap = ArrayListMultimap.create();
for (IModel<?> entity : models) {
Class<? extends IModel<?>> aCls = entity.getClass();
entityMultimap.put(aCls, entity);
}
for (Class<? extends HasKey<?>> cls : entityMultimap.keySet()) {
Collection<? extends IModel<?>> entitiesCollection = entityMultimap.get(cls);
doIndex(cls, entitiesCollection);
}
public static <T extends IModel<T>> void doIndex(Class<T> clz, Iterable<T> items) {
.....
}
Update: Someone has marked this as duplicate of the other generic wildcard related question. However this is very different case. What we have is
public static <T extends IModel<T>> void doIndex(Class<T> clz, Iterable<T> items)
where params clz and items based on the same type T. One can use wildcard capture method in case when we have just one parameter. But here we have wildcarded variables cls
and entitiesCollection
:
Class<? extends HasKey<?>> cls and
Collection<? extends IModel<?>> entitiesCollection
which have the similar relationship as clz
and items
, but I don't know how to pass them into doIndex
.