From d9044fa1b54aea06b5dae3163349b60cfa80ad66 Mon Sep 17 00:00:00 2001
From: haykam821 <24855774+haykam821@users.noreply.github.com>
Date: Thu, 6 Jan 2022 17:51:11 -0500
Subject: [PATCH] Add an empty statistic bundle implementation
---
.../plasmid/game/GameSpaceStatistics.java | 13 ++++++
.../game/stats/GameStatisticBundle.java | 17 ++++++++
.../plasmid/game/stats/StatisticMap.java | 42 +++++++++++++++++++
3 files changed, 72 insertions(+)
diff --git a/src/main/java/xyz/nucleoid/plasmid/game/GameSpaceStatistics.java b/src/main/java/xyz/nucleoid/plasmid/game/GameSpaceStatistics.java
index 759cf482..3406b153 100644
--- a/src/main/java/xyz/nucleoid/plasmid/game/GameSpaceStatistics.java
+++ b/src/main/java/xyz/nucleoid/plasmid/game/GameSpaceStatistics.java
@@ -6,6 +6,8 @@
import java.util.function.BiConsumer;
+import org.jetbrains.annotations.Nullable;
+
/**
* Holds the {@link GameStatisticBundle} instances associated with a {@link GameSpace} instance.
*/
@@ -23,6 +25,17 @@ public GameStatisticBundle bundle(String namespace) {
return this.statistics.computeIfAbsent(namespace, $ -> new GameStatisticBundle());
}
+ /**
+ * Note: bundle namespaces can only contain the characters a-zA-Z0-9_
+ *
+ * @param namespace The statistic namespace to get a bundle for, or {@code null} for an empty bundle
+ * @return the {@link GameStatisticBundle} for the given namespace, or {@linkplain GameStatisticBundle#EMPTY an empty bundle}
+ * @see GameStatisticBundle#bundle(String)
+ */
+ public GameStatisticBundle bundleOrEmpty(@Nullable String namespace) {
+ return namespace == null ? GameStatisticBundle.EMPTY : this.bundle(namespace);
+ }
+
/**
* @param consumer Will be called for every non-empty {@link GameStatisticBundle} in this {@link GameSpace}
*/
diff --git a/src/main/java/xyz/nucleoid/plasmid/game/stats/GameStatisticBundle.java b/src/main/java/xyz/nucleoid/plasmid/game/stats/GameStatisticBundle.java
index ed614586..e7610912 100644
--- a/src/main/java/xyz/nucleoid/plasmid/game/stats/GameStatisticBundle.java
+++ b/src/main/java/xyz/nucleoid/plasmid/game/stats/GameStatisticBundle.java
@@ -15,6 +15,23 @@
* key for their namespace in the form statistic.bundle.[namespace]
*/
public class GameStatisticBundle {
+ public static final GameStatisticBundle EMPTY = new GameStatisticBundle() {
+ @Override
+ public StatisticMap forPlayer(UUID uuid) {
+ return StatisticMap.EMPTY;
+ }
+
+ @Override
+ public StatisticMap global() {
+ return StatisticMap.EMPTY;
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return true;
+ }
+ };
+
private final Object2ObjectMap players = new Object2ObjectOpenHashMap<>();
private final StatisticMap global = new StatisticMap();
diff --git a/src/main/java/xyz/nucleoid/plasmid/game/stats/StatisticMap.java b/src/main/java/xyz/nucleoid/plasmid/game/stats/StatisticMap.java
index 4ff358ab..3df4b406 100644
--- a/src/main/java/xyz/nucleoid/plasmid/game/stats/StatisticMap.java
+++ b/src/main/java/xyz/nucleoid/plasmid/game/stats/StatisticMap.java
@@ -10,6 +10,48 @@
* Stores a mapping of {@link StatisticKey} to their corresponding values
*/
public class StatisticMap {
+ protected static final StatisticMap EMPTY = new StatisticMap() {
+ @Override
+ public void increment(StatisticKey key, double amount) {
+ return;
+ }
+
+ @Override
+ public void increment(StatisticKey key, float amount) {
+ return;
+ }
+
+ @Override
+ public void increment(StatisticKey key, int amount) {
+ return;
+ }
+
+ @Override
+ public T get(StatisticKey key, T defaultValue) {
+ return defaultValue;
+ }
+
+ @Override
+ public void set(StatisticKey key, double value) {
+ return;
+ }
+
+ @Override
+ public void set(StatisticKey key, float value) {
+ return;
+ }
+
+ @Override
+ public void set(StatisticKey key, int value) {
+ return;
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return true;
+ }
+ };
+
private final Object2ObjectMap, Number> values = new Object2ObjectOpenHashMap<>();
public void increment(StatisticKey key, double amount) {