From ec39392689496e04efa7ae70d5c08f700e2a3b28 Mon Sep 17 00:00:00 2001 From: yugecin Date: Sun, 15 Jan 2017 22:37:59 +0100 Subject: [PATCH] eventbus --- .../opsudance/core/events/EventBus.java | 58 +++++++++++++++++++ .../opsudance/core/events/EventListener.java | 24 ++++++++ 2 files changed, 82 insertions(+) create mode 100644 src/yugecin/opsudance/core/events/EventBus.java create mode 100644 src/yugecin/opsudance/core/events/EventListener.java diff --git a/src/yugecin/opsudance/core/events/EventBus.java b/src/yugecin/opsudance/core/events/EventBus.java new file mode 100644 index 00000000..4b203b73 --- /dev/null +++ b/src/yugecin/opsudance/core/events/EventBus.java @@ -0,0 +1,58 @@ +/* + * opsu!dance - fork of opsu! with cursordance auto + * Copyright (C) 2017 yugecin + * + * opsu!dance is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * opsu!dance is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with opsu!dance. If not, see . + */ +package yugecin.opsudance.core.events; + +import com.google.inject.Inject; + +import java.util.*; + +@SuppressWarnings("unchecked") +public class EventBus { + + private final List subscribers; + + @Inject + public EventBus() { + subscribers = new LinkedList<>(); + } + + public void subscribe(Class eventType, EventListener eventListener) { + subscribers.add(new Subscriber<>(eventType, eventListener)); + } + + public void post(Object event) { + for (Subscriber s : subscribers) { + if (s.eventType.isInstance(event)) { + s.listener.onEvent(event); + } + } + } + + private class Subscriber { + + private final Class eventType; + private final EventListener listener; + + private Subscriber(Class eventType, EventListener listener) { + this.eventType = eventType; + this.listener = listener; + } + + } + +} diff --git a/src/yugecin/opsudance/core/events/EventListener.java b/src/yugecin/opsudance/core/events/EventListener.java new file mode 100644 index 00000000..e4249347 --- /dev/null +++ b/src/yugecin/opsudance/core/events/EventListener.java @@ -0,0 +1,24 @@ +/* + * opsu!dance - fork of opsu! with cursordance auto + * Copyright (C) 2017 yugecin + * + * opsu!dance is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * opsu!dance is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with opsu!dance. If not, see . + */ +package yugecin.opsudance.core.events; + +public interface EventListener { + + void onEvent(T event); + +}