From 7e84044d077db5f3507a3049f1b14bb020795998 Mon Sep 17 00:00:00 2001 From: hsgamer Date: Sat, 4 May 2024 19:34:37 +0700 Subject: [PATCH] add dedicated classes for listener manager --- .../core/agent/storage/StorageAgent.java | 6 +- .../topper/core/agent/update/UpdateAgent.java | 4 +- .../hsgamer/topper/core/entry/DataEntry.java | 2 +- .../topper/core/holder/DataHolder.java | 80 +++++++++---------- .../core/holder/DataWithAgentHolder.java | 6 +- .../core/listener/EntryListenerManager.java | 27 +++++++ .../topper/core/listener/ListenerManager.java | 24 ++++++ 7 files changed, 96 insertions(+), 53 deletions(-) create mode 100644 core/src/main/java/me/hsgamer/topper/core/listener/EntryListenerManager.java create mode 100644 core/src/main/java/me/hsgamer/topper/core/listener/ListenerManager.java diff --git a/core/src/main/java/me/hsgamer/topper/core/agent/storage/StorageAgent.java b/core/src/main/java/me/hsgamer/topper/core/agent/storage/StorageAgent.java index 496421b..e167a94 100644 --- a/core/src/main/java/me/hsgamer/topper/core/agent/storage/StorageAgent.java +++ b/core/src/main/java/me/hsgamer/topper/core/agent/storage/StorageAgent.java @@ -57,17 +57,17 @@ public void loadIfExist(UUID uuid) { @Override public void start() { - holder.addCreateListener(entry -> { + holder.getCreateListenerManager().add(entry -> { saveQueue.add(entry.getUuid()); if (loadOnCreate) { load(entry); } }); - holder.addRemoveListener(entry -> { + holder.getRemoveListenerManager().add(entry -> { save(entry); saveQueue.remove(entry.getUuid()); }); - holder.addUpdateListener(entry -> entry.addFlag(NEED_SAVING)); + holder.getUpdateListenerManager().add(entry -> entry.addFlag(NEED_SAVING)); storage.onRegister(); storage.load() .whenComplete((entries, throwable) -> { diff --git a/core/src/main/java/me/hsgamer/topper/core/agent/update/UpdateAgent.java b/core/src/main/java/me/hsgamer/topper/core/agent/update/UpdateAgent.java index 399617b..38e4420 100644 --- a/core/src/main/java/me/hsgamer/topper/core/agent/update/UpdateAgent.java +++ b/core/src/main/java/me/hsgamer/topper/core/agent/update/UpdateAgent.java @@ -54,8 +54,8 @@ public void start() { if (updateFunction == null) { throw new IllegalStateException("Update function is not set"); } - holder.addCreateListener(entry -> updateQueue.add(entry.getUuid())); - holder.addRemoveListener(entry -> updateQueue.remove(entry.getUuid())); + holder.getCreateListenerManager().add(entry -> updateQueue.add(entry.getUuid())); + holder.getRemoveListenerManager().add(entry -> updateQueue.remove(entry.getUuid())); super.start(); } diff --git a/core/src/main/java/me/hsgamer/topper/core/entry/DataEntry.java b/core/src/main/java/me/hsgamer/topper/core/entry/DataEntry.java index b59174b..db8dd56 100644 --- a/core/src/main/java/me/hsgamer/topper/core/entry/DataEntry.java +++ b/core/src/main/java/me/hsgamer/topper/core/entry/DataEntry.java @@ -36,7 +36,7 @@ public void setValue(T value) { public void setValue(T value, boolean notify) { if (Objects.equals(this.value.get(), value)) return; this.value.set(value); - if (notify) holder.notifyUpdateEntry(this); + if (notify) holder.getUpdateListenerManager().notifyListeners(this); } public DataHolder getHolder() { diff --git a/core/src/main/java/me/hsgamer/topper/core/holder/DataHolder.java b/core/src/main/java/me/hsgamer/topper/core/holder/DataHolder.java index d67cd51..5f19302 100644 --- a/core/src/main/java/me/hsgamer/topper/core/holder/DataHolder.java +++ b/core/src/main/java/me/hsgamer/topper/core/holder/DataHolder.java @@ -1,19 +1,23 @@ package me.hsgamer.topper.core.holder; import me.hsgamer.topper.core.entry.DataEntry; +import me.hsgamer.topper.core.listener.EntryListenerManager; +import me.hsgamer.topper.core.listener.ListenerManager; -import java.util.*; +import java.util.Collections; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Consumer; public class DataHolder { private final Map> entryMap = new ConcurrentHashMap<>(); - private final List>> removeListeners = new ArrayList<>(); - private final List>> createListeners = new ArrayList<>(); - private final List>> updateListeners = new ArrayList<>(); - private final List registerListeners = new ArrayList<>(); - private final List beforeUnregisterListeners = new ArrayList<>(); - private final List unregisterListeners = new ArrayList<>(); + private final EntryListenerManager removeListenerManager = new EntryListenerManager<>(); + private final EntryListenerManager createListenerManager = new EntryListenerManager<>(); + private final EntryListenerManager updateListenerManager = new EntryListenerManager<>(); + private final ListenerManager registerListenerManager = new ListenerManager(); + private final ListenerManager beforeUnregisterListenerManager = new ListenerManager(); + private final ListenerManager unregisterListenerManager = new ListenerManager(); private final String name; protected DataHolder(String name) { @@ -24,60 +28,48 @@ public T getDefaultValue() { return null; } - public final void notifyCreateEntry(DataEntry entry) { - createListeners.forEach(listener -> listener.accept(entry)); + public final EntryListenerManager getRemoveListenerManager() { + return removeListenerManager; } - public final void notifyRemoveEntry(DataEntry entry) { - removeListeners.forEach(listener -> listener.accept(entry)); + public final EntryListenerManager getCreateListenerManager() { + return createListenerManager; } - public final void notifyUpdateEntry(DataEntry entry) { - updateListeners.forEach(listener -> listener.accept(entry)); + public final EntryListenerManager getUpdateListenerManager() { + return updateListenerManager; } - public final void addCreateListener(Consumer> listener) { - createListeners.add(listener); + public final ListenerManager getRegisterListenerManager() { + return registerListenerManager; } - public final void addRemoveListener(Consumer> listener) { - removeListeners.add(listener); + public final ListenerManager getBeforeUnregisterListenerManager() { + return beforeUnregisterListenerManager; } - public final void addUpdateListener(Consumer> listener) { - updateListeners.add(listener); - } - - public final void addRegisterListener(Runnable listener) { - registerListeners.add(listener); - } - - public final void addBeforeUnregisterListener(Runnable listener) { - beforeUnregisterListeners.add(listener); - } - - public final void addUnregisterListener(Runnable listener) { - unregisterListeners.add(listener); + public final ListenerManager getUnregisterListenerManager() { + return unregisterListenerManager; } public final void register() { - registerListeners.forEach(Runnable::run); + registerListenerManager.notifyListeners(); } public final void unregister() { - beforeUnregisterListeners.forEach(Runnable::run); + beforeUnregisterListenerManager.notifyListeners(); - entryMap.values().forEach(this::notifyRemoveEntry); + entryMap.values().forEach(removeListenerManager::notifyListeners); entryMap.clear(); - unregisterListeners.forEach(Runnable::run); + unregisterListenerManager.notifyListeners(); - createListeners.clear(); - removeListeners.clear(); - updateListeners.clear(); - registerListeners.clear(); - beforeUnregisterListeners.clear(); - unregisterListeners.clear(); + createListenerManager.clear(); + removeListenerManager.clear(); + updateListenerManager.clear(); + registerListenerManager.clear(); + beforeUnregisterListenerManager.clear(); + unregisterListenerManager.clear(); } public final String getName() { @@ -95,12 +87,12 @@ public final Optional> getEntry(UUID uuid) { public final DataEntry getOrCreateEntry(UUID uuid) { return entryMap.computeIfAbsent(uuid, u -> { DataEntry entry = new DataEntry<>(u, this); - notifyCreateEntry(entry); + createListenerManager.notifyListeners(entry); return entry; }); } public final void removeEntry(UUID uuid) { - Optional.ofNullable(entryMap.remove(uuid)).ifPresent(this::notifyRemoveEntry); + Optional.ofNullable(entryMap.remove(uuid)).ifPresent(removeListenerManager::notifyListeners); } } diff --git a/core/src/main/java/me/hsgamer/topper/core/holder/DataWithAgentHolder.java b/core/src/main/java/me/hsgamer/topper/core/holder/DataWithAgentHolder.java index 47f87ea..4719531 100644 --- a/core/src/main/java/me/hsgamer/topper/core/holder/DataWithAgentHolder.java +++ b/core/src/main/java/me/hsgamer/topper/core/holder/DataWithAgentHolder.java @@ -10,9 +10,9 @@ public class DataWithAgentHolder extends DataHolder { protected DataWithAgentHolder(String name) { super(name); - addRegisterListener(() -> agentList.forEach(Agent::start)); - addBeforeUnregisterListener(() -> agentList.forEach(Agent::beforeStop)); - addUnregisterListener(() -> { + getRegisterListenerManager().add(() -> agentList.forEach(Agent::start)); + getBeforeUnregisterListenerManager().add(() -> agentList.forEach(Agent::beforeStop)); + getUnregisterListenerManager().add(() -> { agentList.forEach(Agent::stop); agentList.clear(); }); diff --git a/core/src/main/java/me/hsgamer/topper/core/listener/EntryListenerManager.java b/core/src/main/java/me/hsgamer/topper/core/listener/EntryListenerManager.java new file mode 100644 index 0000000..2f619e0 --- /dev/null +++ b/core/src/main/java/me/hsgamer/topper/core/listener/EntryListenerManager.java @@ -0,0 +1,27 @@ +package me.hsgamer.topper.core.listener; + +import me.hsgamer.topper.core.entry.DataEntry; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; + +public class EntryListenerManager { + private final List>> listeners = new ArrayList<>(); + + public void add(Consumer> listener) { + listeners.add(listener); + } + + public void remove(Consumer> listener) { + listeners.remove(listener); + } + + public void clear() { + listeners.clear(); + } + + public void notifyListeners(DataEntry entry) { + listeners.forEach(listener -> listener.accept(entry)); + } +} diff --git a/core/src/main/java/me/hsgamer/topper/core/listener/ListenerManager.java b/core/src/main/java/me/hsgamer/topper/core/listener/ListenerManager.java new file mode 100644 index 0000000..38feb95 --- /dev/null +++ b/core/src/main/java/me/hsgamer/topper/core/listener/ListenerManager.java @@ -0,0 +1,24 @@ +package me.hsgamer.topper.core.listener; + +import java.util.ArrayList; +import java.util.List; + +public class ListenerManager { + private final List listeners = new ArrayList<>(); + + public void add(Runnable listener) { + listeners.add(listener); + } + + public void remove(Runnable listener) { + listeners.remove(listener); + } + + public void clear() { + listeners.clear(); + } + + public void notifyListeners() { + listeners.forEach(Runnable::run); + } +}