Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an empty statistic bundle implementation #196

Open
wants to merge 1 commit into
base: 1.18
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/game/GameSpaceStatistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@
* key for their namespace in the form <code>statistic.bundle.[namespace]</code>
*/
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<UUID, StatisticMap> players = new Object2ObjectOpenHashMap<>();
private final StatisticMap global = new StatisticMap();

Expand Down
42 changes: 42 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/game/stats/StatisticMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Double> key, double amount) {
return;
}

@Override
public void increment(StatisticKey<Float> key, float amount) {
return;
}

@Override
public void increment(StatisticKey<Integer> key, int amount) {
return;
}

@Override
public <T extends Number> T get(StatisticKey<T> key, T defaultValue) {
return defaultValue;
}

@Override
public void set(StatisticKey<Double> key, double value) {
return;
}

@Override
public void set(StatisticKey<Float> key, float value) {
return;
}

@Override
public void set(StatisticKey<Integer> key, int value) {
return;
}

@Override
public boolean isEmpty() {
return true;
}
};

private final Object2ObjectMap<StatisticKey<?>, Number> values = new Object2ObjectOpenHashMap<>();

public void increment(StatisticKey<Double> key, double amount) {
Expand Down