Skip to content

Commit

Permalink
Added Inheritance and priority
Browse files Browse the repository at this point in the history
  • Loading branch information
RealMangorage committed May 23, 2024
1 parent 1da1286 commit 9d5a10e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 26 deletions.
12 changes: 11 additions & 1 deletion src/main/java/org/mangorage/eventbus/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ public <E extends Event & IGenericEvent<G>, G> void addGenericListener(int prior

@SuppressWarnings("unchecked")
private <E extends Event> IListenerList<E> getListenerList(Class<E> eventClass, Class<?> genericType) {
return (IListenerList<E>) LISTENERS.computeIfAbsent(new EventKey<>(eventClass, genericType), e -> new ListenerListImpl<>());
var key = new EventKey<>(eventClass, genericType);
var list = LISTENERS.get(key);
if (list != null) {
return (IListenerList<E>) list;
}

if (((Class<?>) eventClass) == Object.class)
return null;


return (IListenerList<E>) LISTENERS.computeIfAbsent(key, e -> new ListenerListImpl<>(getListenerList((Class<E>) eventClass.getSuperclass(), genericType)));
}

@Override
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/org/mangorage/eventbus/Listener.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@

package org.mangorage.eventbus;

import org.jetbrains.annotations.NotNull;
import org.mangorage.eventbus.event.Event;

import java.util.function.Consumer;

public record Listener<E extends Event>(int priority, Consumer<E> consumer) {
public record Listener<E extends Event>(int priority, Consumer<E> consumer) implements Comparable<Listener<E>> {
/**
* @param o the object to be compared.
* @return
*/
@Override
public int compareTo(@NotNull Listener<E> o) {
return Integer.compare(o.priority, priority);
}
}
45 changes: 44 additions & 1 deletion src/main/java/org/mangorage/eventbus/impl/ListenerListImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,69 @@

package org.mangorage.eventbus.impl;

import org.mangorage.basicutils.misc.Lazy;
import org.mangorage.eventbus.Listener;
import org.mangorage.eventbus.event.Event;
import org.mangorage.eventbus.interfaces.IListenerList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.function.Consumer;
import java.util.stream.Stream;

public class ListenerListImpl<E extends Event> implements IListenerList<E> {
private final List<Listener<E>> listeners = Collections.synchronizedList(new ArrayList<>());
private final Set<IListenerList<E>> children = Collections.synchronizedSet(new CopyOnWriteArraySet<>());
private final IListenerList<?> parent;
private final Lazy<Listener<E>[]> allListeners;

public ListenerListImpl(IListenerList<E> parent) {
this.parent = parent;
if (parent != null) parent.addChild(this);
this.allListeners = Lazy.concurrentOf(() -> parent == null ? listeners.toArray(Listener[]::new) : Stream.of(listeners, List.of(parent.getListeners()))
.flatMap(List::stream)
.sorted()
.toArray(Listener[]::new));
}


@Override
public void register(int priority, Consumer<E> consumer) {
listeners.add(new Listener<E>(priority, consumer));
children.forEach(IListenerList::invalidate);
if (parent != null) parent.invalidate();
}

/**
* @return
*/
@Override
public Listener<E>[] getListeners() {
return allListeners.get();
}

/**
*
*/
@Override
public void invalidate() {
allListeners.invalidate();
}

/**
* @param child
*/
@Override
public void addChild(IListenerList<E> child) {
children.add(child);
}

@Override
public void post(E event) {
for (Listener<E> listener : listeners) {
for (Listener<E> listener : getListeners()) {
listener.consumer().accept(event);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@

package org.mangorage.eventbus.interfaces;

import org.mangorage.eventbus.Listener;
import org.mangorage.eventbus.event.Event;

import java.util.function.Consumer;

public interface IListenerList<E extends Event> {
void post(E event);
void register(int priority, Consumer<E> consumer);

void post(E event);
Listener<E>[] getListeners();

void invalidate();

void addChild(IListenerList<E> child);
}
31 changes: 9 additions & 22 deletions src/test/java/org/mangorage/mangobot/test/EventBusTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@

import org.mangorage.eventbus.EventBus;
import org.mangorage.eventbus.event.Event;
import org.mangorage.mangobot.test.misc.TimedTest;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand All @@ -46,28 +43,18 @@ public static class MyOther extends MyEvent {
public static void main(String[] args) throws InterruptedException {
var bus = EventBus.create();

var registration = TimedTest.of(() -> {
for (int i = 0; i < 1; i++) {
bus.addListener(10, MyOther.class, e -> {
System.out.println("LOL");
});
}
bus.addListener(10, Event.class, e -> {
System.out.println("All -> " + e);
});

long amount = 10;
var post = TimedTest.of(() -> {
for (int i = 0; i < amount; i++) {
bus.post(new MyOther());
bus.post(new Event());
}
bus.addListener(10, MyEvent.class, e -> {
System.out.println(e.getClass());
});

List<MyEvent> myEvents = new ArrayList<>();
for (int i = 0; i < amount; i++)
myEvents.add(new MyOther());
bus.addListener(11, MyOther.class, e -> {
System.out.println("Cool !");
});

System.out.println("Registration time for 1 listener -> " + registration.getResult());
System.out.println("AVG -> " + post.getResult() / amount);
var a = 1;
bus.post(new MyOther());
bus.post(new MyEvent());
}
}

0 comments on commit 9d5a10e

Please sign in to comment.