Correct way to parameterize java event generator

I have code as follows (excerpt):

public interface Event<S> {
    S getSource();
}

public interface Subscriber<E> {
    void update(E event);
}

public interface EventPublisher<S, E extends Event<S>> {
    void addSubscription(S source, Subscriber<E> subscriber);
    void removeSubscription(S source, Subscriber<E> subscriber);
}

public class SubscriptionManager<S, E extends Event<S>> implements Subscriber<E>, EventPublisher<S, E> {
    ...
}

public class MyEvent implements Event<MyEventSource> {
    ...
}

      

This all works fine, however my problem is that I am trying something like this:

public class MyEventHandler {
    private final SubscriptionManager<Class<? extends Event<?>>, ? extends Event<?>> subscriptionManager = new SubscriptionManager<>();

    Subscriber<? extends Event<?>> subscriber = ...;
    subscriptionManager.addSubscription(MyEvent.class, subscriber); **// Compile error**
}

      

I am getting the following error:

The method addSubscription(Class<? extends Event<?>>, Subscriber<capture#3-of ? extends Event<?>>) in the type SubscriptionManager<Class<? extends Event<?>>,capture#3-of ? extends Event<?>> is not applicable for the arguments (Class<MyEvent>, Subscriber<capture#5-of ? extends Event<?>>)

      

Can anyone tell me what happened?

thank

+3


source to share


1 answer


To be honest, I think there is some design flaw in your code. It looks almost perfect, but it doesn't add up. Perhaps you can omit half of the common parameters and make it simpler.

Please consider the below code. The entire infrastructure is parameterized by one parameter. Everything compiles and there are no raw types. Also note that MyEvent is never used in a frame definition. This is a convenience class. You can safely call subscriptionManager.update (new MyEvent ()); somewhere in your code.



More complex measures are possible, but I believe that is the one you need. Please let me know if this works for you.

static interface Event<S> {
    S getSource();
}

static interface Subscriber<S> {
    void update(Event<S> event);
}

static interface EventPublisher<S> {
    void addSubscription(Class<S> sourceClass, Subscriber<S> subscriber);
    void removeSubscription(Class<S> sourceClass, Subscriber<S> subscriber);
}

static class SubscriptionManager<S> implements Subscriber<S>, EventPublisher<S> {
    public void addSubscription(Class<S> sourceClass, Subscriber<S> subscriber) {
    }
    public void removeSubscription(Class<S> sourceClass, Subscriber<S> subscriber) {
    }
    public void update(Event<S> event) {
    }
}

static class MyEvent implements Event<String> {
    public String getSource() {
        return null;
    }
}

static class MyEventHandler {
    private final SubscriptionManager<String> subscriptionManager = new SubscriptionManager<String>();
    public MyEventHandler() {
        Subscriber<String> subscriber = null;
        subscriptionManager.addSubscription(String.class, subscriber);
    }
}

      

+3


source







All Articles