@NotThreadSafe
public class EventHub
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
protected static class |
EventHub.SubscriptionImpl<E> |
Modifier and Type | Field and Description |
---|---|
protected static java.util.function.Consumer[] |
EMPTY_LISTENERS_ARRAY |
protected java.util.Map<java.lang.Class<?>,java.util.function.Consumer[]> |
events |
protected static int |
EVENTS_MAP_EXPECTED_MAX_SIZE |
Constructor and Description |
---|
EventHub() |
Modifier and Type | Method and Description |
---|---|
<E> boolean |
hasSubscriptions(java.lang.Class<E> eventType)
Check if there are listeners for event type E.
|
protected java.util.function.Consumer[] |
newListenersArray(int length) |
<E> void |
publish(java.lang.Class<E> eventType,
E event)
Fire listeners for event type E.
|
<E> Subscription |
subscribe(java.lang.Class<E> eventType,
java.util.function.Consumer<E> listener)
Add an event listener for events with type E.
|
void |
unsubscribe(java.lang.Class<?> eventType)
Remove all listeners with the specified event type.
|
<E> boolean |
unsubscribe(java.lang.Class<E> eventType,
java.util.function.Consumer<E> listener)
Remove an event listener for events with type E.
|
protected static final int EVENTS_MAP_EXPECTED_MAX_SIZE
protected static final java.util.function.Consumer[] EMPTY_LISTENERS_ARRAY
protected java.util.Map<java.lang.Class<?>,java.util.function.Consumer[]> events
public <E> Subscription subscribe(java.lang.Class<E> eventType, java.util.function.Consumer<E> listener)
E
- type of eventeventType
- event classlistener
- listenerpublic <E> boolean unsubscribe(java.lang.Class<E> eventType, java.util.function.Consumer<E> listener)
E
- type of eventeventType
- event classlistener
- listenerpublic void unsubscribe(java.lang.Class<?> eventType)
eventType
- event typepublic <E> boolean hasSubscriptions(java.lang.Class<E> eventType)
E
- type of eventeventType
- event classpublic <E> void publish(java.lang.Class<E> eventType, E event)
E
- type of eventeventType
- event classevent
- event objectprotected java.util.function.Consumer[] newListenersArray(int length)