Skip to content

Commit

Permalink
add dedicated classes for listener manager
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed May 4, 2024
1 parent 1d83b6a commit 7e84044
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> getHolder() {
Expand Down
80 changes: 36 additions & 44 deletions core/src/main/java/me/hsgamer/topper/core/holder/DataHolder.java
Original file line number Diff line number Diff line change
@@ -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<T> {
private final Map<UUID, DataEntry<T>> entryMap = new ConcurrentHashMap<>();
private final List<Consumer<DataEntry<T>>> removeListeners = new ArrayList<>();
private final List<Consumer<DataEntry<T>>> createListeners = new ArrayList<>();
private final List<Consumer<DataEntry<T>>> updateListeners = new ArrayList<>();
private final List<Runnable> registerListeners = new ArrayList<>();
private final List<Runnable> beforeUnregisterListeners = new ArrayList<>();
private final List<Runnable> unregisterListeners = new ArrayList<>();
private final EntryListenerManager<T> removeListenerManager = new EntryListenerManager<>();
private final EntryListenerManager<T> createListenerManager = new EntryListenerManager<>();
private final EntryListenerManager<T> 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) {
Expand All @@ -24,60 +28,48 @@ public T getDefaultValue() {
return null;
}

public final void notifyCreateEntry(DataEntry<T> entry) {
createListeners.forEach(listener -> listener.accept(entry));
public final EntryListenerManager<T> getRemoveListenerManager() {
return removeListenerManager;
}

public final void notifyRemoveEntry(DataEntry<T> entry) {
removeListeners.forEach(listener -> listener.accept(entry));
public final EntryListenerManager<T> getCreateListenerManager() {
return createListenerManager;
}

public final void notifyUpdateEntry(DataEntry<T> entry) {
updateListeners.forEach(listener -> listener.accept(entry));
public final EntryListenerManager<T> getUpdateListenerManager() {
return updateListenerManager;
}

public final void addCreateListener(Consumer<DataEntry<T>> listener) {
createListeners.add(listener);
public final ListenerManager getRegisterListenerManager() {
return registerListenerManager;
}

public final void addRemoveListener(Consumer<DataEntry<T>> listener) {
removeListeners.add(listener);
public final ListenerManager getBeforeUnregisterListenerManager() {
return beforeUnregisterListenerManager;
}

public final void addUpdateListener(Consumer<DataEntry<T>> 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() {
Expand All @@ -95,12 +87,12 @@ public final Optional<DataEntry<T>> getEntry(UUID uuid) {
public final DataEntry<T> getOrCreateEntry(UUID uuid) {
return entryMap.computeIfAbsent(uuid, u -> {
DataEntry<T> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class DataWithAgentHolder<T> extends DataHolder<T> {

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();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> {
private final List<Consumer<DataEntry<T>>> listeners = new ArrayList<>();

public void add(Consumer<DataEntry<T>> listener) {
listeners.add(listener);
}

public void remove(Consumer<DataEntry<T>> listener) {
listeners.remove(listener);
}

public void clear() {
listeners.clear();
}

public void notifyListeners(DataEntry<T> entry) {
listeners.forEach(listener -> listener.accept(entry));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.hsgamer.topper.core.listener;

import java.util.ArrayList;
import java.util.List;

public class ListenerManager {
private final List<Runnable> 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);
}
}

0 comments on commit 7e84044

Please sign in to comment.