Skip to content

Commit

Permalink
Made inheritance work for GenericEvents aswell, just not there Generi…
Browse files Browse the repository at this point in the history
…cType.
  • Loading branch information
RealMangorage committed May 20, 2024
1 parent bdc183a commit fecf73a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023. MangoRage
* Copyright (c) 2023-2024. MangoRage
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -33,5 +33,12 @@
public @interface SubscribeEvent {
int priority() default 0;

boolean recieveCancelled() default false;
boolean receiveCancelled() default false;

/**
* Used for {@link org.mangorage.mboteventbus.base.GenericEvent<?>}
*
* @return Generic Class for (?) {@link org.mangorage.mboteventbus.base.GenericEvent<?>}
*/
Class<?> baseGenericClass() default Object.class;
}
2 changes: 1 addition & 1 deletion src/main/java/org/mangorage/mboteventbus/base/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@

package org.mangorage.mboteventbus.base;

public class Event {
public abstract class Event {
}
51 changes: 40 additions & 11 deletions src/main/java/org/mangorage/mboteventbus/base/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.mangorage.mboteventbus.impl.IListenerList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

public final class EventBus implements IEventBus {
Expand All @@ -38,15 +38,15 @@ public static IEventBus create() {
private record ListenerKey(Class<?> eventClass, Class<?> genericClass) {
}

private final Map<ListenerKey, IListenerList<? extends Event>> LISTENERS = new HashMap<>();
private final Map<ListenerKey, IListenerList<? extends Event>> LISTENERS = new ConcurrentHashMap<>();

public <T extends Event> void addListener(int priority, Class<T> eventClass, Consumer<T> eventConsumer) {
IListenerList<T> list = (IListenerList<T>) getListenerList(eventClass, null);
list.register(eventConsumer, "default", priority);
}

public <G, T extends GenericEvent<G>> void addGenericListener(int priority, Class<G> genericClass, Class<T> genericEvent, Consumer<T> genericEventListener) {
IListenerList<T> list = (IListenerList<T>) getListenerList(genericEvent, genericClass);
public <T extends GenericEvent<? extends G>, G> void addGenericListener(int priority, Class<G> genericClassFilter, Class<T> eventType, Consumer<T> genericEventListener) {
IListenerList<T> list = (IListenerList<T>) getListenerList(eventType, genericClassFilter);
list.register(genericEventListener, "default", priority);
}

Expand All @@ -62,19 +62,19 @@ public void post(Event event) {

@SuppressWarnings("unchecked")
private IListenerList<?> getListenerList(Class<?> eventClass, Class<?> genericClass) {
if (genericClass != null) {
return LISTENERS.computeIfAbsent(new ListenerKey(eventClass, genericClass), e -> new ListenerListImpl<Event>(new ArrayList<>(), null));
}

var list = LISTENERS.get(eventClass);
var key = new ListenerKey(eventClass, genericClass);
var list = LISTENERS.get(key);
if (list != null) {
return list;
}

if (eventClass == Object.class)
return null;

return LISTENERS.computeIfAbsent(new ListenerKey(eventClass, null), e -> new ListenerListImpl<>(new ArrayList<>(), getListenerList(eventClass.getSuperclass(), null)));
if (eventClass == Event.class && genericClass != null)
return null;

return LISTENERS.computeIfAbsent(key, e -> new ListenerListImpl<>(new ArrayList<>(), getListenerList(eventClass.getSuperclass(), genericClass)));
}

@Override
Expand All @@ -89,16 +89,45 @@ public void shutdown() {


public static class MyEvent extends Event {
public MyEvent() {
//super(Integer.class);
}
}

public static class MyBetterEvent extends MyEvent {
}

public static class MyCrazyEvent extends MyBetterEvent {
}


public static void main(String[] args) {
IEventBus bus = create();


// bus.addGenericListener(10, Integer.class, MyEvent.class, e -> {
// System.out.println("Sweet -> " + e.getClass());
// });
//
bus.addGenericListener(10, Integer.class, GenericEvent.class, e -> {
System.out.println("Generic -> " + e.getClass());

});

bus.addListener(10, Event.class, e -> {
System.out.println(e.getClass());
});

bus.post(new Event());
bus.addListener(10, MyEvent.class, e -> {
System.out.println("Sweeet! -> " + e.getClass());
});

bus.post(new MyBetterEvent());
bus.post(new MyEvent());
bus.post(new MyCrazyEvent());
bus.post(new GenericEvent<>(Integer.class) {
});

System.out.println("END");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

package org.mangorage.mboteventbus.base;

public class GenericEvent<T> extends Event {
public abstract class GenericEvent<T> extends Event {
private final Class<T> tClass;

public GenericEvent(Class<T> tClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public interface IEventBus {
<T extends Event> void addListener(int priority, Class<T> eventClass, Consumer<T> eventConsumer);

<G, T extends GenericEvent<G>> void addGenericListener(int priority, Class<G> genericClass, Class<T> genericEvent, Consumer<T> genericEventListener);
<T extends GenericEvent<? extends G>, G> void addGenericListener(int priority, Class<G> genericClass, Class<T> genericEvent, Consumer<T> genericEventListener);

void post(Event event);
void startup();
Expand Down

0 comments on commit fecf73a

Please sign in to comment.