Skip to content

Commit 7a94dee

Browse files
committed
Large update to how EventBus works. Made it alot more complicated but also added support for record events...
1 parent 68be169 commit 7a94dee

File tree

13 files changed

+260
-33
lines changed

13 files changed

+260
-33
lines changed

src/main/java/org/mangorage/eventbus/EventBus.java

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,60 @@
2323
package org.mangorage.eventbus;
2424

2525
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
26+
import org.mangorage.eventbus.annotations.SubscribeEvent;
2627
import org.mangorage.eventbus.event.core.Event;
28+
import org.mangorage.eventbus.event.core.GenericEvent;
29+
import org.mangorage.eventbus.interfaces.IEvent;
2730
import org.mangorage.eventbus.interfaces.IEventBus;
2831
import org.mangorage.eventbus.interfaces.IEventType;
32+
import org.mangorage.eventbus.interfaces.IEventTypeHandler;
2933
import org.mangorage.eventbus.interfaces.IGenericEvent;
3034

35+
import java.lang.reflect.InvocationTargetException;
36+
import java.lang.reflect.Method;
37+
import java.lang.reflect.Modifier;
3138
import java.util.Map;
32-
import java.util.concurrent.Semaphore;
3339
import java.util.function.Consumer;
3440

35-
public final class EventBus<F extends IEventType<F>> implements IEventBus<F> {
41+
public final class EventBus<EE extends IEvent & IEventType<F>, GG extends IGenericEvent<?> & IEventType<F>, F extends IEventType<F>> implements IEventBus<F> {
3642

37-
public static <F extends IEventType<F>> IEventBus<F> create() {
38-
return new EventBus<>();
43+
public static <E extends IEvent & IEventType<F>, G extends IGenericEvent<?> & IEventType<F>, F extends IEventType<F>> IEventBus<F> create(IEventTypeHandler<E, G, F> handler, Class<F> flagType) {
44+
return new EventBus<>(handler, flagType);
3945
}
4046

41-
4247
private final Map<EventKey<?, ?>, ListenerList<?>> LISTENERS = new Object2ObjectArrayMap<>();
43-
private final Semaphore writeLock = new Semaphore(1, true);
48+
private final IEventTypeHandler<EE, GG, F> handler;
49+
private final Class<F> flagType;
4450

45-
private EventBus() {
51+
private EventBus(IEventTypeHandler<EE, GG, F> handler, Class<F> flagType) {
52+
this.handler = handler;
53+
this.flagType = flagType;
4654
}
4755

56+
4857
@Override
49-
public <E extends Event & IEventType<F>> void addListener(int priority, Class<E> eventClass, Consumer<E> consumer) {
58+
public <E extends IEvent & IEventType<F>> void addListener(int priority, Class<E> eventClass, Consumer<E> consumer) {
5059
var list = getListenerList(eventClass, null);
5160
if (list != null) list.register(priority, consumer);
5261
}
5362

5463
@Override
55-
public <E extends Event & IGenericEvent<G> & IEventType<F>, G> void addGenericListener(int priority, Class<G> baseFilterClass, Class<E> eventClass, Consumer<E> consumer) {
64+
public <E extends IEvent & IGenericEvent<G> & IEventType<F>, G> void addGenericListener(int priority, Class<G> baseFilterClass, Class<E> eventClass, Consumer<E> consumer) {
5665
var list = getListenerList(eventClass, baseFilterClass);
5766
if (list != null) list.register(priority, consumer);
5867
}
5968

69+
70+
private <E extends IEvent & IGenericEvent<G> & IEventType<F>, G> void addGenericListenerCast(int priority, Class<G> baseFilterClass, Class<?> eventClass, Consumer<?> consumer) {
71+
addGenericListener(
72+
priority,
73+
baseFilterClass,
74+
(Class<E>) eventClass,
75+
(Consumer<E>) consumer
76+
);
77+
}
78+
79+
6080
@Override
6181
public void registerClass(Class<?> clazz) {
6282
register(clazz, null);
@@ -70,7 +90,6 @@ public void registerObject(Object object) {
7090
@SuppressWarnings("unchecked")
7191
private void register(Class<?> clazz, Object instance) {
7292
// TODO: FIX LATER
73-
/**
7493
for (Method method : clazz.getDeclaredMethods()) {
7594
var subscribeEvent = method.getDeclaredAnnotation(SubscribeEvent.class);
7695

@@ -94,9 +113,9 @@ private void register(Class<?> clazz, Object instance) {
94113
}
95114

96115
var eventClass = (Class<Event>) params[0].getType();
97-
if (IGenericEvent.class.isAssignableFrom(eventClass)) {
98-
var genericClass = (Class<GenericEvent<Object>>) params[0].getType();
116+
if (handler.canHandle(eventClass, ListenerType.GENERIC)) {
99117
var genericType = subscribeEvent.genericType();
118+
var genericClass = handler.castGenericClass(params[0].getType(), genericType);
100119
if (genericType == SubscribeEvent.NullClass.class) {
101120
throw new IllegalStateException("Cannot Register a GenericListener due to no GenericType being defined...");
102121
}
@@ -109,8 +128,13 @@ private void register(Class<?> clazz, Object instance) {
109128
}
110129
};
111130

112-
addGenericListener(subscribeEvent.priority(), (Class<Object>) genericType, genericClass, consumer);
113-
} else {
131+
addGenericListenerCast(
132+
subscribeEvent.priority(),
133+
(Class<Object>) genericType,
134+
genericClass,
135+
consumer
136+
);
137+
} else if (handler.canHandle(eventClass, ListenerType.NORMAL)) {
114138
Consumer<Event> consumer = e -> {
115139
try {
116140
method.invoke(instance, e);
@@ -119,14 +143,19 @@ private void register(Class<?> clazz, Object instance) {
119143
}
120144
};
121145

122-
addListener(subscribeEvent.priority(), eventClass, consumer);
146+
addListener(
147+
subscribeEvent.priority(),
148+
handler.castNormalClass(eventClass),
149+
handler.castNormalConsumer(consumer)
150+
);
151+
} else {
152+
throw new IllegalStateException("Cannot register Method %s with EventType of %s needs to implement %s".formatted(method, eventClass, flagType));
123153
}
124154
}
125-
**/
126155
}
127156

128157
@SuppressWarnings("unchecked")
129-
private <E extends Event> ListenerList<E> getListenerList(Class<E> eventClass, Class<?> genericType) {
158+
private <E extends IEvent> ListenerList<E> getListenerList(Class<E> eventClass, Class<?> genericType) {
130159
var key = new EventKey<>(eventClass, genericType);
131160
var list = LISTENERS.get(key);
132161

@@ -143,7 +172,7 @@ private <E extends Event> ListenerList<E> getListenerList(Class<E> eventClass, C
143172

144173
@Override
145174
@SuppressWarnings("unchecked")
146-
public <E extends Event & IEventType<F>> void post(E event) {
175+
public <E extends IEvent & IEventType<F>> void post(E event) {
147176
Class<?> genericType = null;
148177
if (event instanceof IGenericEvent<?> genericEvent)
149178
genericType = genericEvent.getGenericType();

src/main/java/org/mangorage/eventbus/EventKey.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
package org.mangorage.eventbus;
2424

25-
import org.mangorage.eventbus.event.core.Event;
25+
import org.mangorage.eventbus.interfaces.IEvent;
2626

27-
public record EventKey<E extends Event, G>(Class<E> eventClass, Class<G> genericClass) {
27+
public record EventKey<E extends IEvent, G>(Class<E> eventClass, Class<G> genericClass) {
2828
}

src/main/java/org/mangorage/eventbus/EventListener.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
package org.mangorage.eventbus;
2424

2525
import org.jetbrains.annotations.NotNull;
26-
import org.mangorage.eventbus.event.core.Event;
26+
import org.mangorage.eventbus.interfaces.IEvent;
2727

2828
import java.util.function.Consumer;
2929

30-
public record EventListener<E extends Event>(int priority,
31-
Consumer<E> consumer) implements Comparable<EventListener<E>> {
30+
public record EventListener<E extends IEvent>(int priority,
31+
Consumer<E> consumer) implements Comparable<EventListener<E>> {
3232
/**
3333
* @param o the object to be compared.
3434
* @return

src/main/java/org/mangorage/eventbus/ListenerList.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
package org.mangorage.eventbus;
2424

25-
import org.mangorage.eventbus.event.core.Event;
25+
import org.mangorage.eventbus.interfaces.IEvent;
2626

2727
import java.util.ArrayList;
2828
import java.util.Collections;
@@ -31,7 +31,7 @@
3131
import java.util.concurrent.atomic.AtomicReference;
3232
import java.util.function.Consumer;
3333

34-
public final class ListenerList<E extends Event> {
34+
public final class ListenerList<E extends IEvent> {
3535
private final List<EventListener<E>> listeners = Collections.synchronizedList(new ArrayList<>(2));
3636
private final List<ListenerList<E>> children = Collections.synchronizedList(new ArrayList<>(2));
3737
private final ListenerList<?> parent;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2024. MangoRage
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
20+
* OR OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
package org.mangorage.eventbus;
24+
25+
public enum ListenerType {
26+
GENERIC,
27+
NORMAL
28+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2024. MangoRage
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
20+
* OR OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
package org.mangorage.eventbus.event;
24+
25+
import org.mangorage.eventbus.ListenerType;
26+
import org.mangorage.eventbus.event.core.GenericEvent;
27+
import org.mangorage.eventbus.interfaces.IEventType;
28+
import org.mangorage.eventbus.interfaces.IEventTypeHandler;
29+
30+
public class NormalEventHandler implements IEventTypeHandler<NormalEvent, NormalGenericEvent<?>, IEventType.INormalBusEvent> {
31+
32+
33+
@Override
34+
public boolean canHandle(Class<?> eventClass, ListenerType listenerType) {
35+
return switch (listenerType) {
36+
case NORMAL -> NormalEvent.class.isAssignableFrom(eventClass);
37+
case GENERIC -> NormalGenericEvent.class.isAssignableFrom(eventClass);
38+
};
39+
}
40+
41+
@Override
42+
public <GT, T extends GenericEvent<GT> & IEventType<IEventType.INormalBusEvent>> Class<T> castGenericClass(Class<?> clz, Class<GT> genericType) {
43+
return (Class<T>) clz;
44+
}
45+
46+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2024. MangoRage
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
20+
* OR OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
package org.mangorage.eventbus.event;
24+
25+
import org.mangorage.eventbus.EventBus;
26+
import org.mangorage.eventbus.annotations.SubscribeEvent;
27+
import org.mangorage.eventbus.interfaces.IEventBus;
28+
import org.mangorage.eventbus.interfaces.IEventType;
29+
30+
public class Test {
31+
private static final IEventBus<IEventType.INormalBusEvent> NORMAL = EventBus.create(new NormalEventHandler(), IEventType.INormalBusEvent.class);
32+
33+
34+
public static class MyGeneric<S> extends NormalGenericEvent<S> {
35+
36+
public MyGeneric(Class<S> tClass) {
37+
super(tClass);
38+
}
39+
}
40+
41+
42+
public static void main(String[] args) {
43+
NORMAL.registerClass(Test.class);
44+
NORMAL.post(new MyGeneric<>(String.class));
45+
}
46+
47+
@SubscribeEvent(genericType = String.class)
48+
public static void test(MyGeneric<String> test) {
49+
System.out.println(test.getGenericType());
50+
}
51+
}

src/main/java/org/mangorage/eventbus/event/core/Event.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@
2222

2323
package org.mangorage.eventbus.event.core;
2424

25-
public abstract class Event {
25+
import org.mangorage.eventbus.interfaces.IEvent;
26+
27+
public abstract class Event implements IEvent {
2628
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2024. MangoRage
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
20+
* OR OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
package org.mangorage.eventbus.interfaces;
24+
25+
public interface IEvent {
26+
}

src/main/java/org/mangorage/eventbus/interfaces/IEventBus.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,19 @@
2222

2323
package org.mangorage.eventbus.interfaces;
2424

25-
import org.mangorage.eventbus.event.core.Event;
26-
2725
import java.util.function.Consumer;
2826

2927
public interface IEventBus<F extends IEventType<F>> {
30-
<E extends Event & IEventType<F>> void addListener(int priority, Class<E> eventClass, Consumer<E> consumer);
28+
<E extends IEvent & IEventType<F>> void addListener(int priority, Class<E> eventClass, Consumer<E> consumer);
3129

32-
<E extends Event & IGenericEvent<G> & IEventType<F>, G> void addGenericListener(int priority, Class<G> baseFilterClass, Class<E> eventClass, Consumer<E> consumer);
30+
<E extends IEvent & IGenericEvent<G> & IEventType<F>, G> void addGenericListener(int priority, Class<G> baseFilterClass, Class<E> eventClass, Consumer<E> consumer);
3331

3432
void registerClass(Class<?> clazz);
3533

3634
void registerObject(Object object);
3735

3836

39-
<E extends Event & IEventType<F>> void post(E event);
37+
<E extends IEvent & IEventType<F>> void post(E event);
4038

4139
void startup();
4240
void shutdown();

0 commit comments

Comments
 (0)