I found myself in an odd situation with generics, and so far it doesn't seem possible. I tried isolating the situation in simple classes. What I was trying to accomplish is this: A service class that dispatches handles to handle packets of data. Each handler has a Packet type parameter as there is supposed to be a handler for each packet. The service class also has a Handler type parameter because there are a few types of handlers, so several services are needed for the different handler types. This description may not be very clear, so here is the example I made:
public abstract class Service<T extends Handler<?>> {
//Some random logic
//If only I could use T<E>
protected abstract <E extends Packet> void handle(T handler, E packet);
}
public class ServiceImpl extends Service<HandlerOne<?>> {
@Override
protected <E extends Packet> void handle(HandlerOne<?> handler, E packet) {
handler.handle("someString", packet); //ERROR
}
}
public interface Handler<E extends Packet> {
}
public interface HandlerOne<E extends Packet> extends Handler<E> {
void handle(Object someObject, E packet);
}
public class HandlerOneImpl implements HandlerOne<SomePacket> {
@Override
public void handle(Object someObject, SomePacket packet) {
}
}
public interface Packet {
}
public class SomePacket implements Packet {
}
Is there a way to accomplish something like this, or any suggestions? Thanks
EDIT: Here is the modified code from manouti's answer (Note: I changed SomePacket to PacketOne for clarity). The problem I encountered is creating the insance of ServiceImpl.
public static void main(String[] args) {
HashMap<Class<? extends Packet>, HandlerOne<? extends Packet>> handlerMap = new HashMap<>();
handlerMap.put(PacketOne.class, new HandlerOneImpl());
ServiceImpl<Packet, HandlerOne<?>> s = new ServiceImpl<>(handlerMap);//ERROR
}
public static abstract class Service<E extends Packet, T extends Handler<E>> {
Map<Class<? extends Packet>, T> handlerMap;
public Service(Map<Class<? extends Packet>, T> handlerMap) {
this.handlerMap = handlerMap;
}
//Some random logic
//If only I could use T<E>
protected abstract void handle(T handler, E packet);
}
public static class ServiceImpl<E extends Packet, T extends HandlerOne<E>> extends Service<E, T> {
public ServiceImpl(Map<Class<? extends Packet>, T> handlerMap) {
super(handlerMap);
}
@Override
protected void handle(T handler, E packet) {
handler.handle("someObject", packet);
}
}