mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Renaming, refactor and type safety
This commit is contained in:
@@ -430,7 +430,7 @@ public class EventFlow {
|
|||||||
*/
|
*/
|
||||||
public void unsubscribe(Consumer<?> action) {
|
public void unsubscribe(Consumer<?> action) {
|
||||||
this.listeners.removeIf(handler -> {
|
this.listeners.removeIf(handler -> {
|
||||||
if (handler.getAction().equals(action)) {
|
if (handler.handler().equals(action)) {
|
||||||
eventBus.unsubscribe(handler);
|
eventBus.unsubscribe(handler);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -445,7 +445,7 @@ public class EventFlow {
|
|||||||
*/
|
*/
|
||||||
public void unsubscribe(String name) {
|
public void unsubscribe(String name) {
|
||||||
this.listeners.removeIf(handler -> {
|
this.listeners.removeIf(handler -> {
|
||||||
if (handler.getId().equals(name)) {
|
if (handler.id().equals(name)) {
|
||||||
eventBus.unsubscribe(handler);
|
eventBus.unsubscribe(handler);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ public class DefaultEventBus implements EventBus {
|
|||||||
var subs = eventsHolder.get(eventType);
|
var subs = eventsHolder.get(eventType);
|
||||||
if (subs != null) {
|
if (subs != null) {
|
||||||
for (Subscriber<?, ?> subscriber : subs) {
|
for (Subscriber<?, ?> subscriber : subs) {
|
||||||
Class<T> eventClass = (Class<T>) subscriber.getEvent();
|
Class<T> eventClass = (Class<T>) subscriber.event();
|
||||||
Consumer<EventType> action = (Consumer<EventType>) subscriber.getAction();
|
Consumer<EventType> action = (Consumer<EventType>) subscriber.handler();
|
||||||
|
|
||||||
action.accept((EventType) eventClass.cast(event));
|
action.accept((EventType) eventClass.cast(event));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -109,8 +109,8 @@ public class DisruptorEventBus implements EventBus {
|
|||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private <T> void callListener(Subscriber<?, ?> subscriber, T event) {
|
private <T> void callListener(Subscriber<?, ?> subscriber, T event) {
|
||||||
Class<T> eventClass = (Class<T>) subscriber.getEvent();
|
Class<T> eventClass = (Class<T>) subscriber.event();
|
||||||
Consumer<EventType> action = (Consumer<EventType>) subscriber.getAction();
|
Consumer<EventType> action = (Consumer<EventType>) subscriber.handler();
|
||||||
|
|
||||||
action.accept((EventType) eventClass.cast(event));
|
action.accept((EventType) eventClass.cast(event));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,16 +11,16 @@ public class AsyncSubscriberStore implements SubscriberStore {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(Subscriber<?, ?> sub) {
|
public void add(Subscriber<?, ?> sub) {
|
||||||
queues.computeIfAbsent(sub.getEvent(), _ -> new ConcurrentLinkedQueue<>()).add(sub);
|
queues.computeIfAbsent(sub.event(), _ -> new ConcurrentLinkedQueue<>()).add(sub);
|
||||||
rebuildSnapshot(sub.getEvent());
|
rebuildSnapshot(sub.event());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(Subscriber<?, ?> sub) {
|
public void remove(Subscriber<?, ?> sub) {
|
||||||
ConcurrentLinkedQueue<Subscriber<?, ?>> queue = queues.get(sub.getEvent());
|
ConcurrentLinkedQueue<Subscriber<?, ?>> queue = queues.get(sub.event());
|
||||||
if (queue != null) {
|
if (queue != null) {
|
||||||
queue.remove(sub);
|
queue.remove(sub);
|
||||||
rebuildSnapshot(sub.getEvent());
|
rebuildSnapshot(sub.event());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public class DefaultSubscriberStore implements SubscriberStore {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(Subscriber<?, ?> sub) {
|
public void add(Subscriber<?, ?> sub) {
|
||||||
listeners.compute(sub.getEvent(), (_, arr) -> {
|
listeners.compute(sub.event(), (_, arr) -> {
|
||||||
if (arr == null || arr.length == 0) {
|
if (arr == null || arr.length == 0) {
|
||||||
return new Subscriber<?, ?>[]{sub};
|
return new Subscriber<?, ?>[]{sub};
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,7 @@ public class DefaultSubscriberStore implements SubscriberStore {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(Subscriber<?, ?> sub) {
|
public void remove(Subscriber<?, ?> sub) {
|
||||||
listeners.computeIfPresent(sub.getEvent(), (_, arr) -> {
|
listeners.computeIfPresent(sub.event(), (_, arr) -> {
|
||||||
int len = arr.length;
|
int len = arr.length;
|
||||||
|
|
||||||
if (len == 1) {
|
if (len == 1) {
|
||||||
|
|||||||
@@ -13,12 +13,12 @@ public class SyncSubscriberStore implements SubscriberStore {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(Subscriber<?, ?> sub) {
|
public void add(Subscriber<?, ?> sub) {
|
||||||
LISTENERS.computeIfAbsent(sub.getEvent(), _ -> new ArrayList<>()).add(sub);
|
LISTENERS.computeIfAbsent(sub.event(), _ -> new ArrayList<>()).add(sub);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(Subscriber<?, ?> sub) {
|
public void remove(Subscriber<?, ?> sub) {
|
||||||
LISTENERS.getOrDefault(sub.getEvent(), new ArrayList<>()).remove(sub);
|
LISTENERS.getOrDefault(sub.event(), new ArrayList<>()).remove(sub);
|
||||||
LISTENERS.entrySet().removeIf(entry -> entry.getValue().isEmpty());
|
LISTENERS.entrySet().removeIf(entry -> entry.getValue().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,29 +2,4 @@ package org.toop.framework.eventbus.subscriber;
|
|||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public class DefaultSubscriber<T, K> implements Subscriber<K, T> {
|
public record DefaultSubscriber<K>(String id, Class<K> event, Consumer<K> handler) implements NamedSubscriber<K> {}
|
||||||
private final K id;
|
|
||||||
private final Class<T> event;
|
|
||||||
private final Consumer<T> action;
|
|
||||||
|
|
||||||
public DefaultSubscriber(K id, Class<T> eventClass, Consumer<T> action) {
|
|
||||||
this.id = id;
|
|
||||||
this.event = eventClass;
|
|
||||||
this.action = action;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public K getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Class<T> getEvent() {
|
|
||||||
return event;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Consumer<T> getAction() {
|
|
||||||
return action;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
|
public interface IdSubscriber<T> extends Subscriber<Long, T> {}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public record LongIdSubscriber<K>(Long id, Class<K> event, Consumer<K> handler) implements IdSubscriber<K> {}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
|
public interface NamedSubscriber<T> extends Subscriber<String, T> {}
|
||||||
@@ -2,8 +2,8 @@ package org.toop.framework.eventbus.subscriber;
|
|||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public interface Subscriber<T, K> {
|
public interface Subscriber<ID, K> {
|
||||||
T getId();
|
ID id();
|
||||||
Class<K> getEvent();
|
Class<K> event();
|
||||||
Consumer<K> getAction();
|
Consumer<K> handler();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user