Improved DataTracker, added registry #19

Merged
quentinlegot merged 5 commits from dev into master 2023-06-21 18:39:49 +02:00
7 changed files with 75 additions and 16 deletions

View File

@ -0,0 +1,51 @@
package fr.altarik.toolbox.core;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Objects;
public abstract class Registry<T, U> {
protected final HashMap<T, U> registry = new HashMap<>();
/**
* Return a set view of the keys contained in the registry.
* @return An iterator view of the keys contained in the registry
*/
public Iterator<T> keySet() {
return registry.keySet().iterator();
}
/**
* Returns true if this registry contains the key.
* @param key the key whose presence in the registry is to be tested
* @return Returns true if this registry contains the key, false otherwise
*/
public boolean containsKey(T key) {
return registry.containsKey(key);
}
/**
* Returns the containing value represented by a specific key, or {@code null} if the registry do not contain the key
* @param key the key whose associated value is to be returned
* @return The value to which the specified key is represented, or {@code null} if the registry do not contain the key
*/
public @Nullable U getValue(T key) {
return registry.get(key);
}
/**
* If the specified key is not already associated with a value, associate it with the given value.
* @param key Key to which the specified value is to be associated
* @param value value to be associated with the specified key
* @throws NullPointerException if key or value is null
*/
public void register(@NotNull T key, @NotNull U value) {
registry.putIfAbsent(Objects.requireNonNull(key), Objects.requireNonNull(value));
}
}

View File

@ -1,8 +1,6 @@
package fr.altarik.toolbox.core.data;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;
public class DataTracker {
@ -13,7 +11,13 @@ public class DataTracker {
}
public void startTracking(TrackedData data) {
trackedData.put(data, data.defaultValue());
String v = trackedData.get(data);
if(v == null) {
trackedData.put(data, data.defaultValue());
} else {
throw new IllegalArgumentException("Data " + data.name() + " has already been initialized");
}
}
public String getOrDefault(TrackedData data) {
@ -23,15 +27,18 @@ public class DataTracker {
public void set(TrackedData data, String value) {
String v = trackedData.get(data);
if(v != null) {
trackedData.putIfAbsent(data, value);
trackedData.put(data, value);
} else {
throw new IllegalArgumentException("Data " + data.name() + " is not tracked, please initialize it with DataTracker#startTracking(TrackedData, String) first");
}
}
public void saveToDb() {
public Iterator<TrackedData> getTrackedDataIterator() {
return trackedData.keySet().iterator();
}
public Iterator<String> getTrackedDataValueIterator() {
return trackedData.values().iterator();
}
}

View File

@ -2,8 +2,8 @@
"schemaVersion": 1,
"id": "toolbox-core",
"version": "${version}",
"name": "Core",
"description": "",
"name": "Altarik Toolbox Core",
"description": "Dependency of some of altarik toolbox mods",
"authors": [
"Altarik"
],

View File

@ -2,8 +2,8 @@
"schemaVersion": 1,
"id": "toolbox-database",
"version": "${version}",
"name": "Database",
"description": "",
"name": "Altarik Toolbox Database",
"description": "A set of sql tools",
"authors": [
"Altarik"
],
@ -24,6 +24,7 @@
"fabricloader": "^0.14.12",
"fabric-api": "*",
"minecraft": "1.19.3",
"java": ">=17"
"java": ">=17",
"toolbox-core": "${version}"
}
}

View File

@ -2,7 +2,7 @@
"schemaVersion": 1,
"id": "toolbox-pagination",
"version": "${version}",
"name": "Pagination",
"name": "Altarik Toolbox Pagination",
"description": "A mod to use to paginate long result to player in chat",
"authors": [
"Altarik"

View File

@ -2,7 +2,7 @@
"schemaVersion": 1,
"id": "toolbox-task",
"version": "${version}",
"name": "Task",
"name": "Altarik Toolbox Task",
"description": "A mod to use as a dependency for others to schedule tasks",
"authors": [
"Altarik"

View File

@ -1,4 +1,4 @@
org.gradle.jvmargs=-Xmx2G
org.gradle.jvmargs=-Xmx3G
junit_version=5.9.0
@ -8,5 +8,5 @@ loader_version=0.14.14
fabric_version=0.75.1+1.19.3
maven_group=fr.altarik.toolbox
maven_version=4.1.0-SNAPSHOT
maven_version=4.3.0-SNAPSHOT
repo_username=Altarik