Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java class with 2 generic parameters

Tags:

java

I'm trying to crate my own thread safe event handling but I get E cannot be resolved to a type error on class decleration line below. How can I fix this?

Observers.java

public final class Observers<T extends EventHandler<E>> {

    private CopyOnWriteArrayList<T> mListeners = new CopyOnWriteArrayList<T>();

    public interface EventHandler<E> {
       public void HandleEvent(Object sender, E e);
    }

    /*...*/

    public void dispatchEvent(Object sender, E args) {
        /*...*/
    }
}

EventHandler.java

public interface EventHandler<E extends EventArgs> {
 /* ... */
}
like image 562
Caner Avatar asked Dec 17 '22 08:12

Caner


1 Answers

You've only actually declared a single type parameter in Observers. Try this:

public final class Observers<E extends EventArgs, T extends EventHandler<E>> {

Note that it looks highly odd for your Observers class to declare its own nested EventHandler interface while implementing the outer one - if you really need both of those, I would suggest you rename one of them.

like image 89
Jon Skeet Avatar answered Dec 18 '22 22:12

Jon Skeet