mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 19:04:49 +00:00
Compare commits
4 Commits
912d25c01f
...
0132981d94
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0132981d94 | ||
|
|
322197494c | ||
|
|
a9c99df5d2 | ||
|
|
afb4844084 |
80
.github/workflows/checks.yaml
vendored
80
.github/workflows/checks.yaml
vendored
@@ -1,42 +1,42 @@
|
|||||||
name: Checks
|
#name: Checks
|
||||||
|
|
||||||
on:
|
#on:
|
||||||
push:
|
# push:
|
||||||
branches:
|
# branches:
|
||||||
- 'main'
|
# - 'main'
|
||||||
pull_request:
|
# pull_request:
|
||||||
branches:
|
# branches:
|
||||||
- 'main'
|
# - 'main'
|
||||||
|
#
|
||||||
|
#jobs:
|
||||||
|
# formatting-check:
|
||||||
|
# name: Follow Google Formatting Guidelines
|
||||||
|
# runs-on: ubuntu-latest
|
||||||
|
# steps:
|
||||||
|
# - uses: actions/checkout@v5
|
||||||
|
# with:
|
||||||
|
# fetch-depth: 0 # Fix for incremental formatting
|
||||||
|
# - uses: actions/setup-java@v5
|
||||||
|
# with:
|
||||||
|
# java-version: '25'
|
||||||
|
# distribution: 'temurin'
|
||||||
|
# cache: maven
|
||||||
|
# - name: Run Format Check
|
||||||
|
# run: mvn spotless:check
|
||||||
|
|
||||||
jobs:
|
# tests:
|
||||||
formatting-check:
|
# name: Unittests
|
||||||
name: Follow Google Formatting Guidelines
|
# runs-on: ${{ matrix.os }}
|
||||||
runs-on: ubuntu-latest
|
# needs: formatting-check
|
||||||
steps:
|
# strategy:
|
||||||
- uses: actions/checkout@v5
|
# matrix:
|
||||||
with:
|
# os: [ubuntu-latest] #windows-latest, macos-latest
|
||||||
fetch-depth: 0 # Fix for incremental formatting
|
# steps:
|
||||||
- uses: actions/setup-java@v5
|
# - uses: actions/checkout@v5
|
||||||
with:
|
# - uses: actions/setup-java@v5
|
||||||
java-version: '25'
|
# with:
|
||||||
distribution: 'temurin'
|
# java-version: '25'
|
||||||
cache: maven
|
# distribution: 'temurin'
|
||||||
- name: Run Format Check
|
# cache: maven
|
||||||
run: mvn spotless:check
|
# - name: Run Unittests
|
||||||
|
# run: mvn -B test
|
||||||
tests:
|
|
||||||
name: Unittests
|
|
||||||
runs-on: ${{ matrix.os }}
|
|
||||||
needs: formatting-check
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
os: [ubuntu-latest] #windows-latest, macos-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v5
|
|
||||||
- uses: actions/setup-java@v5
|
|
||||||
with:
|
|
||||||
java-version: '25'
|
|
||||||
distribution: 'temurin'
|
|
||||||
cache: maven
|
|
||||||
- name: Run Unittests
|
|
||||||
run: mvn -B test
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package org.toop.app;
|
|||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.scene.input.KeyCode;
|
import javafx.scene.input.KeyCode;
|
||||||
import javafx.scene.input.KeyCodeCombination;
|
import javafx.scene.input.KeyCodeCombination;
|
||||||
import javafx.scene.input.KeyCombination;
|
|
||||||
import javafx.scene.input.KeyEvent;
|
import javafx.scene.input.KeyEvent;
|
||||||
|
|
||||||
import org.toop.app.widget.Primitive;
|
import org.toop.app.widget.Primitive;
|
||||||
@@ -112,20 +111,19 @@ public final class App extends Application {
|
|||||||
Platform.runLater(() -> stage.setOpacity(1.0));
|
Platform.runLater(() -> stage.setOpacity(1.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
Platform.runLater(() -> loading.setMaxAmount(e.isLoadingAmount()));
|
|
||||||
|
|
||||||
Platform.runLater(() -> {
|
Platform.runLater(() -> {
|
||||||
|
loading.setMaxAmount(e.isLoadingAmount());
|
||||||
try {
|
try {
|
||||||
loading.setAmount(e.hasLoadedAmount());
|
loading.setAmount(e.hasLoadedAmount());
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new RuntimeException(ex);
|
throw new RuntimeException(ex);
|
||||||
}
|
}
|
||||||
|
if (e.hasLoadedAmount() >= e.isLoadingAmount()-1) {
|
||||||
|
Platform.runLater(loading::triggerSuccess);
|
||||||
|
loadingFlow.unsubscribe("init_loading");
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (e.hasLoadedAmount() >= e.isLoadingAmount()) {
|
|
||||||
Platform.runLater(loading::triggerSuccess);
|
|
||||||
loadingFlow.unsubscribe("init_loading");
|
|
||||||
}
|
|
||||||
|
|
||||||
}, false, "init_loading");
|
}, false, "init_loading");
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,8 @@ import org.toop.framework.eventbus.events.EventType;
|
|||||||
import org.toop.framework.eventbus.events.ResponseToUniqueEvent;
|
import org.toop.framework.eventbus.events.ResponseToUniqueEvent;
|
||||||
import org.toop.framework.eventbus.events.UniqueEvent;
|
import org.toop.framework.eventbus.events.UniqueEvent;
|
||||||
import org.toop.framework.eventbus.bus.EventBus;
|
import org.toop.framework.eventbus.bus.EventBus;
|
||||||
import org.toop.framework.eventbus.subscriber.DefaultSubscriber;
|
import org.toop.framework.eventbus.subscriber.DefaultNamedSubscriber;
|
||||||
|
import org.toop.framework.eventbus.subscriber.NamedSubscriber;
|
||||||
import org.toop.framework.eventbus.subscriber.Subscriber;
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -43,7 +44,7 @@ public class EventFlow {
|
|||||||
private EventType event = null;
|
private EventType event = null;
|
||||||
|
|
||||||
/** The listener returned by GlobalEventBus subscription. Used for unsubscription. */
|
/** The listener returned by GlobalEventBus subscription. Used for unsubscription. */
|
||||||
private final List<Subscriber<?, ?>> listeners = new ArrayList<>();
|
private final List<NamedSubscriber<?>> listeners = new ArrayList<>();
|
||||||
|
|
||||||
/** Holds the results returned from the subscribed event, if any. */
|
/** Holds the results returned from the subscribed event, if any. */
|
||||||
private Map<String, ?> result = null;
|
private Map<String, ?> result = null;
|
||||||
@@ -161,7 +162,7 @@ public class EventFlow {
|
|||||||
this.result = eventClass.result();
|
this.result = eventClass.result();
|
||||||
};
|
};
|
||||||
|
|
||||||
var subscriber = new DefaultSubscriber<>(
|
var subscriber = new DefaultNamedSubscriber<>(
|
||||||
name,
|
name,
|
||||||
event,
|
event,
|
||||||
newAction
|
newAction
|
||||||
@@ -248,7 +249,7 @@ public class EventFlow {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var listener = new DefaultSubscriber<>(
|
var listener = new DefaultNamedSubscriber<>(
|
||||||
name,
|
name,
|
||||||
(Class<TT>) action.getClass().getDeclaredMethods()[0].getParameterTypes()[0],
|
(Class<TT>) action.getClass().getDeclaredMethods()[0].getParameterTypes()[0],
|
||||||
newAction
|
newAction
|
||||||
@@ -295,7 +296,7 @@ public class EventFlow {
|
|||||||
if (unsubscribeAfterSuccess) unsubscribe(String.valueOf(id));
|
if (unsubscribeAfterSuccess) unsubscribe(String.valueOf(id));
|
||||||
};
|
};
|
||||||
|
|
||||||
var listener = new DefaultSubscriber<>(
|
var listener = new DefaultNamedSubscriber<>(
|
||||||
name,
|
name,
|
||||||
event,
|
event,
|
||||||
newAction
|
newAction
|
||||||
@@ -378,7 +379,7 @@ public class EventFlow {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var listener = new DefaultSubscriber<>(
|
var listener = new DefaultNamedSubscriber<>(
|
||||||
name,
|
name,
|
||||||
eventClass,
|
eventClass,
|
||||||
newAction
|
newAction
|
||||||
@@ -496,7 +497,7 @@ public class EventFlow {
|
|||||||
*
|
*
|
||||||
* @return Copy of the list of listeners.
|
* @return Copy of the list of listeners.
|
||||||
*/
|
*/
|
||||||
public Subscriber<?, ?>[] getListeners() {
|
public Subscriber<?>[] getListeners() {
|
||||||
return listeners.toArray(new Subscriber[0]);
|
return listeners.toArray(new Subscriber[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package org.toop.framework.eventbus;
|
|||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.toop.framework.eventbus.bus.DisruptorEventBus;
|
import org.toop.framework.eventbus.bus.DisruptorEventBus;
|
||||||
import org.toop.framework.eventbus.bus.EventBus;
|
import org.toop.framework.eventbus.bus.EventBus;
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
import org.toop.framework.eventbus.store.DefaultSubscriberStore;
|
import org.toop.framework.eventbus.store.DefaultSubscriberStore;
|
||||||
import org.toop.framework.eventbus.subscriber.Subscriber;
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
@@ -19,17 +20,17 @@ public class GlobalEventBus implements EventBus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void subscribe(Subscriber<?, ?> listener) {
|
public void subscribe(Subscriber<? extends EventType> listener) {
|
||||||
INSTANCE.subscribe(listener);
|
INSTANCE.subscribe(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void unsubscribe(Subscriber<?, ?> listener) {
|
public void unsubscribe(Subscriber<? extends EventType> listener) {
|
||||||
INSTANCE.unsubscribe(listener);
|
INSTANCE.unsubscribe(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> void post(T event) {
|
public <T extends EventType> void post(T event) {
|
||||||
INSTANCE.post(event);
|
INSTANCE.post(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,22 +17,22 @@ public class DefaultEventBus implements EventBus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void subscribe(Subscriber<?, ?> subscriber) {
|
public void subscribe(Subscriber<? extends EventType> subscriber) {
|
||||||
eventsHolder.add(subscriber);
|
eventsHolder.add(subscriber);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void unsubscribe(Subscriber<?, ?> subscriber) {
|
public void unsubscribe(Subscriber<? extends EventType> subscriber) {
|
||||||
eventsHolder.remove(subscriber);
|
eventsHolder.remove(subscriber);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T> void post(T event) {
|
public <T extends EventType> void post(T event) {
|
||||||
Class<T> eventType = (Class<T>) event.getClass();
|
Class<T> eventType = (Class<T>) event.getClass();
|
||||||
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.event();
|
Class<T> eventClass = (Class<T>) subscriber.event();
|
||||||
Consumer<EventType> action = (Consumer<EventType>) subscriber.handler();
|
Consumer<EventType> action = (Consumer<EventType>) subscriber.handler();
|
||||||
|
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ public class DisruptorEventBus implements EventBus {
|
|||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
private final SubscriberStore eventsHolder;
|
private final SubscriberStore eventsHolder;
|
||||||
|
|
||||||
private final Disruptor<EventHolder<?>> disruptor;
|
private final Disruptor<EventHolder<? extends EventType>> disruptor;
|
||||||
private final RingBuffer<EventHolder<?>> ringBuffer;
|
private final RingBuffer<EventHolder<? extends EventType>> ringBuffer;
|
||||||
|
|
||||||
public DisruptorEventBus(Logger logger, SubscriberStore eventsHolder) {
|
public DisruptorEventBus(Logger logger, SubscriberStore eventsHolder) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
@@ -41,9 +41,9 @@ public class DisruptorEventBus implements EventBus {
|
|||||||
this.ringBuffer = disruptor.getRingBuffer();
|
this.ringBuffer = disruptor.getRingBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Disruptor<EventHolder<?>> getEventHolderDisruptor(ThreadFactory threadFactory) {
|
private Disruptor<EventHolder<? extends EventType>> getEventHolderDisruptor(ThreadFactory threadFactory) {
|
||||||
int RING_BUFFER_SIZE = 1024 * 64;
|
int RING_BUFFER_SIZE = 1024 * 64;
|
||||||
Disruptor<EventHolder<?>> disruptor = new Disruptor<>(
|
Disruptor<EventHolder<? extends EventType>> disruptor = new Disruptor<>(
|
||||||
EventHolder::new,
|
EventHolder::new,
|
||||||
RING_BUFFER_SIZE,
|
RING_BUFFER_SIZE,
|
||||||
threadFactory,
|
threadFactory,
|
||||||
@@ -61,17 +61,17 @@ public class DisruptorEventBus implements EventBus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void subscribe(Subscriber<?, ?> listener) {
|
public void subscribe(Subscriber<? extends EventType> listener) {
|
||||||
eventsHolder.add(listener);
|
eventsHolder.add(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void unsubscribe(Subscriber<?, ?> listener) {
|
public void unsubscribe(Subscriber<? extends EventType> listener) {
|
||||||
eventsHolder.remove(listener);
|
eventsHolder.remove(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> void post(T event) {
|
public <T extends EventType> void post(T event) {
|
||||||
long seq = ringBuffer.next();
|
long seq = ringBuffer.next();
|
||||||
try {
|
try {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@@ -93,10 +93,10 @@ public class DisruptorEventBus implements EventBus {
|
|||||||
eventsHolder.reset();
|
eventsHolder.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> void dispatchEvent(T event) {
|
private <T extends EventType> void dispatchEvent(T event) {
|
||||||
var classListeners = eventsHolder.get(event.getClass());
|
var classListeners = eventsHolder.get(event.getClass());
|
||||||
if (classListeners != null) {
|
if (classListeners != null) {
|
||||||
for (Subscriber<?, ?> listener : classListeners) {
|
for (Subscriber<?> listener : classListeners) {
|
||||||
try {
|
try {
|
||||||
callListener(listener, event);
|
callListener(listener, event);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
@@ -108,7 +108,7 @@ 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.event();
|
Class<T> eventClass = (Class<T>) subscriber.event();
|
||||||
Consumer<EventType> action = (Consumer<EventType>) subscriber.handler();
|
Consumer<EventType> action = (Consumer<EventType>) subscriber.handler();
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
package org.toop.framework.eventbus.bus;
|
package org.toop.framework.eventbus.bus;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
import org.toop.framework.eventbus.subscriber.Subscriber;
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
public interface EventBus {
|
public interface EventBus {
|
||||||
void subscribe(Subscriber<?, ?> subscriber);
|
void subscribe(Subscriber<? extends EventType> subscriber);
|
||||||
void unsubscribe(Subscriber<?, ?> subscriber);
|
void unsubscribe(Subscriber<? extends EventType> subscriber);
|
||||||
<T> void post(T event);
|
<T extends EventType> void post(T event);
|
||||||
void shutdown();
|
void shutdown();
|
||||||
void reset();
|
void reset();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,24 @@
|
|||||||
package org.toop.framework.eventbus.store;
|
package org.toop.framework.eventbus.store;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
import org.toop.framework.eventbus.subscriber.Subscriber;
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
|
||||||
public class AsyncSubscriberStore implements SubscriberStore {
|
public class AsyncSubscriberStore implements SubscriberStore {
|
||||||
private final ConcurrentHashMap<Class<?>, ConcurrentLinkedQueue<Subscriber<?, ?>>> queues = new ConcurrentHashMap<>();
|
private final ConcurrentHashMap<Class<? extends EventType>, ConcurrentLinkedQueue<Subscriber<? extends EventType>>> queues = new ConcurrentHashMap<>();
|
||||||
private final ConcurrentHashMap<Class<?>, Subscriber<?, ?>[]> snapshots = new ConcurrentHashMap<>();
|
private final ConcurrentHashMap<Class<? extends EventType>, Subscriber<? extends EventType>[]> snapshots = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(Subscriber<?, ?> sub) {
|
public void add(Subscriber<? extends EventType> sub) {
|
||||||
queues.computeIfAbsent(sub.event(), _ -> new ConcurrentLinkedQueue<>()).add(sub);
|
queues.computeIfAbsent(sub.event(), _ -> new ConcurrentLinkedQueue<>()).add(sub);
|
||||||
rebuildSnapshot(sub.event());
|
rebuildSnapshot(sub.event());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(Subscriber<?, ?> sub) {
|
public void remove(Subscriber<? extends EventType> sub) {
|
||||||
ConcurrentLinkedQueue<Subscriber<?, ?>> queue = queues.get(sub.event());
|
ConcurrentLinkedQueue<Subscriber<?>> queue = queues.get(sub.event());
|
||||||
if (queue != null) {
|
if (queue != null) {
|
||||||
queue.remove(sub);
|
queue.remove(sub);
|
||||||
rebuildSnapshot(sub.event());
|
rebuildSnapshot(sub.event());
|
||||||
@@ -25,8 +26,8 @@ public class AsyncSubscriberStore implements SubscriberStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Subscriber<?, ?>[] get(Class<?> event) {
|
public Subscriber<? extends EventType>[] get(Class<? extends EventType> event) {
|
||||||
return snapshots.getOrDefault(event, new Subscriber[0]);
|
return snapshots.getOrDefault(event, new Subscriber<?>[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -35,12 +36,12 @@ public class AsyncSubscriberStore implements SubscriberStore {
|
|||||||
snapshots.clear();
|
snapshots.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void rebuildSnapshot(Class<?> event) {
|
private void rebuildSnapshot(Class<? extends EventType> event) {
|
||||||
ConcurrentLinkedQueue<Subscriber<?, ?>> queue = queues.get(event);
|
ConcurrentLinkedQueue<Subscriber<?>> queue = queues.get(event);
|
||||||
if (queue != null) {
|
if (queue != null) {
|
||||||
snapshots.put(event, queue.toArray(new Subscriber[0]));
|
snapshots.put(event, queue.toArray(new Subscriber<?>[0]));
|
||||||
} else {
|
} else {
|
||||||
snapshots.put(event, new Subscriber[0]);
|
snapshots.put(event, new Subscriber<?>[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,25 +1,27 @@
|
|||||||
package org.toop.framework.eventbus.store;
|
package org.toop.framework.eventbus.store;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
import org.toop.framework.eventbus.subscriber.NamedSubscriber;
|
||||||
import org.toop.framework.eventbus.subscriber.Subscriber;
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
public class DefaultSubscriberStore implements SubscriberStore {
|
public class DefaultSubscriberStore implements SubscriberStore {
|
||||||
|
|
||||||
private static final Subscriber<?, ?>[] EMPTY = new Subscriber[0];
|
private static final Subscriber<? extends EventType>[] EMPTY = new Subscriber<?>[0];
|
||||||
|
|
||||||
private final ConcurrentHashMap<Class<?>, Subscriber<?, ?>[]> listeners =
|
private final ConcurrentHashMap<Class<? extends EventType>, Subscriber<? extends EventType>[]>
|
||||||
new ConcurrentHashMap<>();
|
listeners = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(Subscriber<?, ?> sub) {
|
public void add(Subscriber<? extends EventType> sub) {
|
||||||
listeners.compute(sub.event(), (_, 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};
|
||||||
}
|
}
|
||||||
|
|
||||||
int len = arr.length;
|
int len = arr.length;
|
||||||
Subscriber<?, ?>[] newArr = new Subscriber[len + 1];
|
Subscriber<?>[] newArr = new Subscriber[len + 1];
|
||||||
System.arraycopy(arr, 0, newArr, 0, len);
|
System.arraycopy(arr, 0, newArr, 0, len);
|
||||||
newArr[len] = sub;
|
newArr[len] = sub;
|
||||||
return newArr;
|
return newArr;
|
||||||
@@ -27,7 +29,7 @@ public class DefaultSubscriberStore implements SubscriberStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(Subscriber<?, ?> sub) {
|
public void remove(Subscriber<? extends EventType> sub) {
|
||||||
listeners.computeIfPresent(sub.event(), (_, arr) -> {
|
listeners.computeIfPresent(sub.event(), (_, arr) -> {
|
||||||
int len = arr.length;
|
int len = arr.length;
|
||||||
|
|
||||||
@@ -36,7 +38,7 @@ public class DefaultSubscriberStore implements SubscriberStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int keep = 0;
|
int keep = 0;
|
||||||
for (Subscriber<?, ?> s : arr) {
|
for (Subscriber<?> s : arr) {
|
||||||
if (!s.equals(sub)) keep++;
|
if (!s.equals(sub)) keep++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,9 +49,9 @@ public class DefaultSubscriberStore implements SubscriberStore {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Subscriber<?, ?>[] newArr = new Subscriber[keep];
|
Subscriber<?>[] newArr = new Subscriber[keep];
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (Subscriber<?, ?> s : arr) {
|
for (Subscriber<?> s : arr) {
|
||||||
if (!s.equals(sub)) {
|
if (!s.equals(sub)) {
|
||||||
newArr[i++] = s;
|
newArr[i++] = s;
|
||||||
}
|
}
|
||||||
@@ -60,7 +62,7 @@ public class DefaultSubscriberStore implements SubscriberStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Subscriber<?, ?>[] get(Class<?> event) {
|
public Subscriber<? extends EventType>[] get(Class<? extends EventType> event) {
|
||||||
return listeners.getOrDefault(event, EMPTY);
|
return listeners.getOrDefault(event, EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package org.toop.framework.eventbus.store;
|
package org.toop.framework.eventbus.store;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
import org.toop.framework.eventbus.subscriber.Subscriber;
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
public interface SubscriberStore {
|
public interface SubscriberStore {
|
||||||
void add(Subscriber<?, ?> subscriber);
|
void add(Subscriber<? extends EventType> subscriber);
|
||||||
void remove(Subscriber<?, ?> subscriber);
|
void remove(Subscriber<? extends EventType> subscriber);
|
||||||
Subscriber<?, ?>[] get(Class<?> event);
|
Subscriber<? extends EventType>[] get(Class<? extends EventType> event);
|
||||||
void reset();
|
void reset();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.toop.framework.eventbus.store;
|
package org.toop.framework.eventbus.store;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
import org.toop.framework.eventbus.subscriber.Subscriber;
|
import org.toop.framework.eventbus.subscriber.Subscriber;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -8,23 +9,23 @@ import java.util.Map;
|
|||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
public class SyncSubscriberStore implements SubscriberStore {
|
public class SyncSubscriberStore implements SubscriberStore {
|
||||||
private final Map<Class<?>, List<Subscriber<?, ?>>> LISTENERS = new ConcurrentHashMap<>();
|
private final Map<Class<? extends EventType>, List<Subscriber<? extends EventType>>> LISTENERS = new ConcurrentHashMap<>();
|
||||||
private static final Subscriber<?, ?>[] EMPTY = new Subscriber[0];
|
private static final Subscriber<? extends EventType>[] EMPTY = new Subscriber<?>[0];
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(Subscriber<?, ?> sub) {
|
public void add(Subscriber<? extends EventType> sub) {
|
||||||
LISTENERS.computeIfAbsent(sub.event(), _ -> new ArrayList<>()).add(sub);
|
LISTENERS.computeIfAbsent(sub.event(), _ -> new ArrayList<>()).add(sub);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove(Subscriber<?, ?> sub) {
|
public void remove(Subscriber<? extends EventType> sub) {
|
||||||
LISTENERS.getOrDefault(sub.event(), 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());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Subscriber<?, ?>[] get(Class<?> event) {
|
public Subscriber<? extends EventType>[] get(Class<? extends EventType> event) {
|
||||||
List<Subscriber<?, ?>> list = LISTENERS.get(event);
|
List<Subscriber<? extends EventType>> list = LISTENERS.get(event);
|
||||||
if (list == null || list.isEmpty()) return EMPTY;
|
if (list == null || list.isEmpty()) return EMPTY;
|
||||||
return list.toArray(EMPTY);
|
return list.toArray(EMPTY);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
public record DefaultNamedSubscriber<K extends EventType>(String id, Class<K> event, Consumer<K> handler)
|
||||||
|
implements NamedSubscriber<K> {}
|
||||||
@@ -1,5 +1,8 @@
|
|||||||
package org.toop.framework.eventbus.subscriber;
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public record DefaultSubscriber<K>(String id, Class<K> event, Consumer<K> handler) implements NamedSubscriber<K> {}
|
public record DefaultSubscriber<K extends EventType>(Class<K> event, Consumer<K> handler) implements Subscriber<K> {}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
|
public interface HasId<ID> {
|
||||||
|
ID id();
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
package org.toop.framework.eventbus.subscriber;
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
public interface IdSubscriber<T> extends Subscriber<Long, T> {}
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
|
||||||
|
public interface IdSubscriber<K extends EventType> extends Subscriber<K>, HasId<Long> {}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package org.toop.framework.eventbus.subscriber;
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public record LongIdSubscriber<K>(Long id, Class<K> event, Consumer<K> handler) implements IdSubscriber<K> {}
|
public record LongIdSubscriber<K extends EventType>(Long id, Class<K> event, Consumer<K> handler)
|
||||||
|
implements IdSubscriber<K> {}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
package org.toop.framework.eventbus.subscriber;
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
public interface NamedSubscriber<T> extends Subscriber<String, T> {}
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
|
||||||
|
public interface NamedSubscriber<K extends EventType> extends Subscriber<K>, HasId<String> {}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
package org.toop.framework.eventbus.subscriber;
|
package org.toop.framework.eventbus.subscriber;
|
||||||
|
|
||||||
|
import org.toop.framework.eventbus.events.EventType;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
public interface Subscriber<ID, K> {
|
public interface Subscriber<K extends EventType> {
|
||||||
ID id();
|
|
||||||
Class<K> event();
|
Class<K> event();
|
||||||
Consumer<K> handler();
|
Consumer<K> handler();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user