Fix: 'Type safety: Unchecked cast from ...'

This commit is contained in:
Lars Simon Winzer
2026-03-12 19:26:00 +01:00
parent 7b32fe1147
commit db7892d9c4
@@ -9,6 +9,7 @@ import java.util.function.Consumer;
public class EventBus { public class EventBus {
private final Map<Class<?>, List<Consumer<Object>>> handlers = new ConcurrentHashMap<>(); private final Map<Class<?>, List<Consumer<Object>>> handlers = new ConcurrentHashMap<>();
@SuppressWarnings("unchecked") // This cast is safe, because handlers only get passed the type they subscribed to
public <T extends Event> void subscribe(Class<T> eventType, Consumer<T> handler) { public <T extends Event> void subscribe(Class<T> eventType, Consumer<T> handler) {
handlers.computeIfAbsent(eventType, k -> new CopyOnWriteArrayList<>()) handlers.computeIfAbsent(eventType, k -> new CopyOnWriteArrayList<>())
.add((Consumer<Object>) handler); .add((Consumer<Object>) handler);