add EventBus
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package p.studio.utilities.events;
|
||||
|
||||
public interface EventSubscription {
|
||||
void unsubscribe();
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package p.studio.utilities.events;
|
||||
|
||||
public interface SimpleEvent {
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package p.studio.utilities.events;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class TypedEventBus {
|
||||
private final AtomicLong nextSubscriptionId = new AtomicLong(1L);
|
||||
private final ConcurrentMap<Class<?>, ConcurrentMap<Long, Consumer<Object>>> subscriptionsByType =
|
||||
new ConcurrentHashMap<>();
|
||||
|
||||
public <T extends SimpleEvent> EventSubscription subscribe(Class<T> eventType, Consumer<? super T> consumer) {
|
||||
Objects.requireNonNull(eventType, "eventType");
|
||||
Objects.requireNonNull(consumer, "consumer");
|
||||
|
||||
final long subscriptionId = nextSubscriptionId.getAndIncrement();
|
||||
final Consumer<Object> erasedConsumer = event -> consumer.accept(eventType.cast(event));
|
||||
final ConcurrentMap<Long, Consumer<Object>> subscriptions =
|
||||
subscriptionsByType.computeIfAbsent(eventType, ignored -> new ConcurrentHashMap<>());
|
||||
subscriptions.put(subscriptionId, erasedConsumer);
|
||||
|
||||
final AtomicBoolean active = new AtomicBoolean(true);
|
||||
return () -> {
|
||||
if (!active.compareAndSet(true, false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
final ConcurrentMap<Long, Consumer<Object>> currentSubscriptions = subscriptionsByType.get(eventType);
|
||||
if (currentSubscriptions == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
currentSubscriptions.remove(subscriptionId);
|
||||
if (currentSubscriptions.isEmpty()) {
|
||||
subscriptionsByType.remove(eventType, currentSubscriptions);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void publish(Object event) {
|
||||
Objects.requireNonNull(event, "event");
|
||||
|
||||
final ConcurrentMap<Long, Consumer<Object>> subscriptions = subscriptionsByType.get(event.getClass());
|
||||
if (subscriptions == null || subscriptions.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (Consumer<Object> consumer : subscriptions.values()) {
|
||||
consumer.accept(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user