37 lines
889 B
Java
37 lines
889 B
Java
package dev.kske.chess.event;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Project: <strong>Chess</strong><br>
|
|
* File: <strong>EventBus.java</strong><br>
|
|
* Created: <strong>7 Aug 2019</strong><br>
|
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
*/
|
|
public class EventBus {
|
|
|
|
private List<Subscribable> subscribers;
|
|
|
|
private static EventBus instance;
|
|
|
|
public static EventBus getInstance() {
|
|
if (instance == null) instance = new EventBus();
|
|
return instance;
|
|
}
|
|
|
|
private EventBus() {
|
|
subscribers = new ArrayList<>();
|
|
}
|
|
|
|
public void register(Subscribable subscribable) {
|
|
subscribers.add(subscribable);
|
|
}
|
|
|
|
public void dispatch(Event<?> event) {
|
|
subscribers.stream().filter(e -> e.supports().contains(event.getClass())).forEach(e -> e.handle(event));
|
|
}
|
|
|
|
public List<Subscribable> getSubscribers() { return subscribers; }
|
|
}
|