I'm trying to figure out how to structure a program using Java's generics, and wondering if I am doing something fundamentally wrong or just missing a simple bug in my code.
Say I have a generic class:
public interface Handler<T>{
public void process(T t);
}
Another generic class takes Handler as a generic parameter (pseudo code):
public interface Processor<S extends Handler<T>>{ //<== Error: cannot find symbol 'T'
public void addHandler(S u);
public void process(T t);
}
Abstract implementation providing boiler-plate implementations
public abstract class ProcessorImpl<.....> implements Processor<.....>{
...
}
Think of a processor as an object that dispatches requests to process data to any number of handlers. Specific instances can be variations of process pipelines, intercepting filters, event systems, etc.
I'd like to be able to use it like the following:
Handler<String> myHandler1 = new HandlerImpl<String>();
Handler<String> myHandler2 = new HandlerImpl<String>();
Handler<Integer> myHandler3 = new HandlerImpl<Integer>();
Processor<Handler<String>> proc = ProcessorImpl<Handler<String>>();
proc.addHandler(myHandler1);
proc.addhandler(myHandler2);
proc.addhandler(myHandler3);//this should be an error!
I can't get it to work. On paper it looks like it should be trivial, any ideas?
Thanks