7 changed files with 281 additions and 87 deletions
@ -0,0 +1,73 @@
|
||||
package dev.kske.eventbus.core.handler; |
||||
|
||||
import java.lang.reflect.InvocationTargetException; |
||||
import java.util.function.Consumer; |
||||
|
||||
/** |
||||
* An event handler wrapping a callback method. |
||||
* |
||||
* @author Kai S. K. Engelbart |
||||
* @since 1.2.0 |
||||
*/ |
||||
public final class CallbackEventHandler implements EventHandler { |
||||
|
||||
private final Class<?> eventType; |
||||
private final Consumer<Object> callback; |
||||
private final boolean polymorphic; |
||||
private final int priority; |
||||
|
||||
/** |
||||
* Constructs a callback event handler. |
||||
* |
||||
* @param <E> the event type of the handler |
||||
* @param eventType the event type of the handler |
||||
* @param callback the callback method to execute when the handler is invoked |
||||
* @param polymorphic whether the handler is polymorphic |
||||
* @param priority the priority of the handler |
||||
* @since 1.2.0 |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
public <E> CallbackEventHandler(Class<E> eventType, Consumer<E> callback, boolean polymorphic, |
||||
int priority) { |
||||
this.eventType = eventType; |
||||
this.callback = (Consumer<Object>) callback; |
||||
this.polymorphic = polymorphic; |
||||
this.priority = priority; |
||||
} |
||||
|
||||
@Override |
||||
public void execute(Object event) throws InvocationTargetException { |
||||
try { |
||||
callback.accept(event); |
||||
} catch (RuntimeException e) { |
||||
throw new InvocationTargetException(e, "Callback event handler failed!"); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return String.format( |
||||
"CallbackEventHandler[eventType=%s, polymorphic=%b, priority=%d]", |
||||
eventType, polymorphic, priority); |
||||
} |
||||
|
||||
@Override |
||||
public Consumer<?> getListener() { |
||||
return callback; |
||||
} |
||||
|
||||
@Override |
||||
public Class<?> getEventType() { |
||||
return eventType; |
||||
} |
||||
|
||||
@Override |
||||
public int getPriority() { |
||||
return priority; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isPolymorphic() { |
||||
return polymorphic; |
||||
} |
||||
} |
@ -0,0 +1,53 @@
|
||||
package dev.kske.eventbus.core.handler; |
||||
|
||||
import java.lang.reflect.InvocationTargetException; |
||||
|
||||
import dev.kske.eventbus.core.*; |
||||
|
||||
/** |
||||
* Internal representation of an event handling method. |
||||
* |
||||
* @author Kai S. K. Engelbart |
||||
* @since 1.2.0 |
||||
* @see EventBus |
||||
*/ |
||||
public interface EventHandler { |
||||
|
||||
/** |
||||
* Executes the event handler. |
||||
* |
||||
* @param event the event used as the method parameter |
||||
* @throws EventBusException if the event handler isn't accessible or has an invalid |
||||
* signature |
||||
* @throws InvocationTargetException if the handler throws an exception |
||||
* @throws EventBusException if the handler has the wrong signature or is inaccessible |
||||
* @since 1.2.0 |
||||
*/ |
||||
void execute(Object event) throws EventBusException, InvocationTargetException; |
||||
|
||||
/** |
||||
* @return the listener containing this handler |
||||
* @since 1.2.0 |
||||
*/ |
||||
Object getListener(); |
||||
|
||||
/** |
||||
* @return the event type this handler listens for |
||||
* @since 1.2.0 |
||||
*/ |
||||
Class<?> getEventType(); |
||||
|
||||
/** |
||||
* @return the priority of this handler |
||||
* @since 1.2.0 |
||||
* @see Priority |
||||
*/ |
||||
int getPriority(); |
||||
|
||||
/** |
||||
* @return whether this handler also accepts subtypes of the event type |
||||
* @since 1.2.0 |
||||
* @see Polymorphic |
||||
*/ |
||||
boolean isPolymorphic(); |
||||
} |
@ -0,0 +1,8 @@
|
||||
/** |
||||
* Contains the internal representation of event handling methods. |
||||
* |
||||
* @author Kai S. K. Engelbart |
||||
* @since 1.2.0 |
||||
* @see dev.kske.eventbus.core.handler.EventHandler |
||||
*/ |
||||
package dev.kske.eventbus.core.handler; |
Loading…
Reference in new issue