-1

I have a Interface IEvent that looks like this:

public abstract interface IEvent<T> {
    public abstract void handleEvent(T payload);
}

and 2 Interfaces with a concrete generic type that looks like this:

public interface IKeyDownEvent extends IEvent<KeyDownEventPayload> {}
public interface IKeyUpEvent extends IEvent<KeyUpEventPayload> {}

Both KeyUpEvent and KeyDownEvent are extending from EventPayload.

How do I go about and iterate over a ArrayDeque and only call handleEvent() for classes that implement IKeyDownEvent but not for example IKeyUpEvent?

I tried this but it seem to not work:

if (payload.getClass() == ev.getClass().getGenericSuperclass())
    ev.handleEvent(payload);
Teekeks
  • 196
  • 10
  • _"concrete Interfaces "_ -- Sorry, there's no such thing. Interfaces cannot be "concrete". Have you considered `instanceof`? – Jim Garrison Apr 06 '17 at 15:06
  • @JimGarrison Seems obvious that OP is referring to a non-abstract interface. – nicomp Apr 06 '17 at 15:07
  • _"non-abstract interface"_ -- Also no such thing. Do you mean "non-generic"? – Jim Garrison Apr 06 '17 at 15:08
  • Oh my word. Seems obvious that I was referring to an interface that is not abstract. – nicomp Apr 06 '17 at 15:08
  • I guess "concrete interface" = interface with concrete generic types. – Thomas Apr 06 '17 at 15:08
  • Maybe this question helps http://stackoverflow.com/questions/12145185/determine-if-a-class-implements-a-interface-in-java – airos Apr 06 '17 at 15:09
  • "abstract" and "concrete" have very specific meanings. Let's not abuse them please – Jim Garrison Apr 06 '17 at 15:09
  • "...classes that implement IKeyDownEvent but not for example IKeyUpEvent" - Note that you'd run into problems properly implementing `handleEvent()` in classes that implement both. You'd probably be better off with separate methods like `onKeyDown(KeyDownEventPayload)` etc. Could you elaborate (best with some more code) on what you actually try to achieve with `IEvent`? I have the strong feeling that this is a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Thomas Apr 06 '17 at 15:10
  • @Jim Garrison I ment a interfact with a concrete generic type, sorry. – Teekeks Apr 06 '17 at 15:17
  • @Thomas I want something like this: https://pastebin.com/Mde19T67 The Reason why I want it like that is so that you can add your own events without changing code in the eventHandler – Teekeks Apr 06 '17 at 15:26
  • Please don't post such small amounts of code as external links but rather add them to your question. Links can get invalid and often are inconvenient anyways (e.g. having to switch between the linked page and here). – Thomas Apr 06 '17 at 16:03

1 Answers1

1

To literally answer your question, use Class.getGenericInterfaces instead of Class.getGenericSuperclass as IEvent is an immediately super interface and not an immediate superclass. Or arguably better particularly now we don't have interface interface, make IEvent an abstract class.

Still that very fragile. Only checking the immediate types, for instance. Also checking runtime types is such a bad idea.

Better, don't put the IEvent straight in to the Deque. Wrap IKeyDownEvent in a class that deals with KeyDownEventPayload correctly, and similarly for other types.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305