Skip to content

Commit 68be169

Browse files
committed
Added generic type to IEventBus for future, so we can seperate events based on which bus they fire on.
1 parent 8d3a1d1 commit 68be169

File tree

20 files changed

+140
-45
lines changed

20 files changed

+140
-45
lines changed

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

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

2525
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
26-
import org.mangorage.eventbus.annotations.SubscribeEvent;
27-
import org.mangorage.eventbus.event.Event;
28-
import org.mangorage.eventbus.event.GenericEvent;
26+
import org.mangorage.eventbus.event.core.Event;
2927
import org.mangorage.eventbus.interfaces.IEventBus;
28+
import org.mangorage.eventbus.interfaces.IEventType;
3029
import org.mangorage.eventbus.interfaces.IGenericEvent;
3130

32-
import java.lang.reflect.InvocationTargetException;
33-
import java.lang.reflect.Method;
34-
import java.lang.reflect.Modifier;
3531
import java.util.Map;
3632
import java.util.concurrent.Semaphore;
3733
import java.util.function.Consumer;
3834

39-
public final class EventBus implements IEventBus {
35+
public final class EventBus<F extends IEventType<F>> implements IEventBus<F> {
4036

41-
public static IEventBus create() {
42-
return new EventBus();
37+
public static <F extends IEventType<F>> IEventBus<F> create() {
38+
return new EventBus<>();
4339
}
4440

4541

@@ -50,13 +46,13 @@ private EventBus() {
5046
}
5147

5248
@Override
53-
public <E extends Event> void addListener(int priority, Class<E> eventClass, Consumer<E> consumer) {
49+
public <E extends Event & IEventType<F>> void addListener(int priority, Class<E> eventClass, Consumer<E> consumer) {
5450
var list = getListenerList(eventClass, null);
5551
if (list != null) list.register(priority, consumer);
5652
}
5753

5854
@Override
59-
public <E extends Event & IGenericEvent<G>, G> void addGenericListener(int priority, Class<G> baseFilterClass, Class<E> eventClass, Consumer<E> consumer) {
55+
public <E extends Event & IGenericEvent<G> & IEventType<F>, G> void addGenericListener(int priority, Class<G> baseFilterClass, Class<E> eventClass, Consumer<E> consumer) {
6056
var list = getListenerList(eventClass, baseFilterClass);
6157
if (list != null) list.register(priority, consumer);
6258
}
@@ -73,6 +69,8 @@ public void registerObject(Object object) {
7369

7470
@SuppressWarnings("unchecked")
7571
private void register(Class<?> clazz, Object instance) {
72+
// TODO: FIX LATER
73+
/**
7674
for (Method method : clazz.getDeclaredMethods()) {
7775
var subscribeEvent = method.getDeclaredAnnotation(SubscribeEvent.class);
7876
@@ -124,6 +122,7 @@ private void register(Class<?> clazz, Object instance) {
124122
addListener(subscribeEvent.priority(), eventClass, consumer);
125123
}
126124
}
125+
**/
127126
}
128127

129128
@SuppressWarnings("unchecked")
@@ -144,7 +143,7 @@ private <E extends Event> ListenerList<E> getListenerList(Class<E> eventClass, C
144143

145144
@Override
146145
@SuppressWarnings("unchecked")
147-
public <E extends Event> void post(E event) {
146+
public <E extends Event & IEventType<F>> void post(E event) {
148147
Class<?> genericType = null;
149148
if (event instanceof IGenericEvent<?> genericEvent)
150149
genericType = genericEvent.getGenericType();

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

Lines changed: 1 addition & 1 deletion
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.Event;
25+
import org.mangorage.eventbus.event.core.Event;
2626

2727
public record EventKey<E extends Event, G>(Class<E> eventClass, Class<G> genericClass) {
2828
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
package org.mangorage.eventbus;
2424

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

2828
import java.util.function.Consumer;
2929

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

Lines changed: 1 addition & 1 deletion
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.Event;
25+
import org.mangorage.eventbus.event.core.Event;
2626

2727
import java.util.ArrayList;
2828
import java.util.Collections;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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.event.core.Event;
26+
import org.mangorage.eventbus.interfaces.IEventType;
27+
28+
public abstract class NormalEvent extends Event implements IEventType.INormalBusEvent {
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.event.core.GenericEvent;
26+
import org.mangorage.eventbus.interfaces.IEventType;
27+
import org.mangorage.eventbus.interfaces.IGenericEvent;
28+
29+
public abstract class NormalGenericEvent<G> extends GenericEvent<G> implements IGenericEvent<G>, IEventType.INormalBusEvent {
30+
public NormalGenericEvent(Class<G> tClass) {
31+
super(tClass);
32+
}
33+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* OR OTHER DEALINGS IN THE SOFTWARE.
2121
*/
2222

23-
package org.mangorage.eventbus.event;
23+
package org.mangorage.eventbus.event.core;
2424

25-
public class Event {
25+
public abstract class Event {
2626
}

src/main/java/org/mangorage/eventbus/event/GenericEvent.java renamed to src/main/java/org/mangorage/eventbus/event/core/GenericEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* OR OTHER DEALINGS IN THE SOFTWARE.
2121
*/
2222

23-
package org.mangorage.eventbus.event;
23+
package org.mangorage.eventbus.event.core;
2424

2525
import org.mangorage.eventbus.interfaces.IGenericEvent;
2626

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

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

2323
package org.mangorage.eventbus.interfaces;
2424

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

2727
import java.util.function.Consumer;
2828

29-
public interface IEventBus {
30-
<E extends Event> void addListener(int priority, Class<E> eventClass, Consumer<E> consumer);
31-
<E extends Event & IGenericEvent<G>, G> void addGenericListener(int priority, Class<G> baseFilterClass, Class<E> eventClass, Consumer<E> consumer);
29+
public interface IEventBus<F extends IEventType<F>> {
30+
<E extends Event & IEventType<F>> void addListener(int priority, Class<E> eventClass, Consumer<E> consumer);
31+
32+
<E extends Event & IGenericEvent<G> & IEventType<F>, G> void addGenericListener(int priority, Class<G> baseFilterClass, Class<E> eventClass, Consumer<E> consumer);
3233

3334
void registerClass(Class<?> clazz);
3435

3536
void registerObject(Object object);
3637

3738

38-
<E extends Event> void post(E event);
39+
<E extends Event & IEventType<F>> void post(E event);
3940

4041
void startup();
4142
void shutdown();
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.interfaces;
24+
25+
public interface IEventType<T> {
26+
interface INormalBusEvent extends IEventType<INormalBusEvent> {
27+
}
28+
}

0 commit comments

Comments
 (0)