This repository has been archived by the owner on May 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventManager.java
95 lines (83 loc) · 3.66 KB
/
EventManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.felix.chimera.base.implementation.event;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
public class EventManager {
public interface EventListener<T extends Event> {
void onEvent(T event);
default int getPriority() {
return 0;
}
}
private final ConcurrentHashMap<Type, CopyOnWriteArrayList<EventListener<?>>> listenerMap = new ConcurrentHashMap<>();
private final Map<Class<?>, Field[]> declaredFieldsCache = new HashMap<>();
private final Comparator<EventListener<?>> priorityOrder = Comparator.comparingInt((EventListener<?> listener) -> listener.getPriority()).reversed();
private final BiConsumer<List<EventListener<?>>, Comparator<EventListener<?>>> sortCallback = List::sort;
private final Consumer<Throwable> errorHandler = Throwable::printStackTrace;
private Field[] getCachedDeclaredFields(final Class<?> clazz) {
return declaredFieldsCache.computeIfAbsent(clazz, Class::getDeclaredFields);
}
public void subscribe(final Object o) {
modifyEventListenerState(o, (type, listener) -> {
listenerMap.computeIfAbsent(type, k -> new CopyOnWriteArrayList<>()).add(listener);
sortCallback.accept(listenerMap.get(type), priorityOrder);
});
}
public <T extends Event> void triggerEvent(T event) {
Type eventType = event.getClass();
CopyOnWriteArrayList<EventListener<?>> listeners = listenerMap.get(eventType);
if (listeners != null) {
for (EventListener<?> listener : listeners) {
try {
@SuppressWarnings("unchecked")
EventListener<T> castedListener = (EventListener<T>) listener;
castedListener.onEvent(event);
} catch (Throwable t) {
errorHandler.accept(t);
}
}
}
}
public void unsubscribe(final Object o) {
modifyEventListenerState(o, (type, listener) -> {
CopyOnWriteArrayList<EventListener<?>> listeners = listenerMap.get(type);
if (listeners != null) {
listeners.remove(listener);
if (listeners.isEmpty()) {
listenerMap.remove(type);
}
}
});
}
private void modifyEventListenerState(final Object o, final BiConsumer<Type, EventListener<?>> eventListenerBiConsumer) {
final Class<?> type = o.getClass();
for (final Field field : getCachedDeclaredFields(type)) {
if (field.getType() == EventListener.class) {
final EventListener<?> eventListener = getEventListener(o, field);
final Type eventType = ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
eventListenerBiConsumer.accept(eventType, eventListener);
}
}
}
private EventListener<?> getEventListener(final Object o, final Field field) {
final boolean accessible = field.canAccess(o);
field.setAccessible(true);
EventListener<?> fieldSubscription = null;
try {
fieldSubscription = (EventListener<?>) field.get(o);
} catch (IllegalAccessException e) {
e.printStackTrace();
} finally {
field.setAccessible(accessible);
}
return fieldSubscription;
}
}