Improved DataTracker, added registry #19
51
Core/src/main/java/fr/altarik/toolbox/core/Registry.java
Normal file
51
Core/src/main/java/fr/altarik/toolbox/core/Registry.java
Normal 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,6 @@
|
|||||||
package fr.altarik.toolbox.core.data;
|
package fr.altarik.toolbox.core.data;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
public class DataTracker {
|
public class DataTracker {
|
||||||
|
|
||||||
@ -13,7 +11,13 @@ public class DataTracker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void startTracking(TrackedData data) {
|
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) {
|
public String getOrDefault(TrackedData data) {
|
||||||
@ -23,15 +27,18 @@ public class DataTracker {
|
|||||||
public void set(TrackedData data, String value) {
|
public void set(TrackedData data, String value) {
|
||||||
String v = trackedData.get(data);
|
String v = trackedData.get(data);
|
||||||
if(v != null) {
|
if(v != null) {
|
||||||
trackedData.putIfAbsent(data, value);
|
trackedData.put(data, value);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Data " + data.name() + " is not tracked, please initialize it with DataTracker#startTracking(TrackedData, String) first");
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
"schemaVersion": 1,
|
"schemaVersion": 1,
|
||||||
"id": "toolbox-core",
|
"id": "toolbox-core",
|
||||||
"version": "${version}",
|
"version": "${version}",
|
||||||
"name": "Core",
|
"name": "Altarik Toolbox Core",
|
||||||
"description": "",
|
"description": "Dependency of some of altarik toolbox mods",
|
||||||
"authors": [
|
"authors": [
|
||||||
"Altarik"
|
"Altarik"
|
||||||
],
|
],
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
"schemaVersion": 1,
|
"schemaVersion": 1,
|
||||||
"id": "toolbox-database",
|
"id": "toolbox-database",
|
||||||
"version": "${version}",
|
"version": "${version}",
|
||||||
"name": "Database",
|
"name": "Altarik Toolbox Database",
|
||||||
"description": "",
|
"description": "A set of sql tools",
|
||||||
"authors": [
|
"authors": [
|
||||||
"Altarik"
|
"Altarik"
|
||||||
],
|
],
|
||||||
@ -24,6 +24,7 @@
|
|||||||
"fabricloader": "^0.14.12",
|
"fabricloader": "^0.14.12",
|
||||||
"fabric-api": "*",
|
"fabric-api": "*",
|
||||||
"minecraft": "1.19.3",
|
"minecraft": "1.19.3",
|
||||||
"java": ">=17"
|
"java": ">=17",
|
||||||
|
"toolbox-core": "${version}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
"schemaVersion": 1,
|
"schemaVersion": 1,
|
||||||
"id": "toolbox-pagination",
|
"id": "toolbox-pagination",
|
||||||
"version": "${version}",
|
"version": "${version}",
|
||||||
"name": "Pagination",
|
"name": "Altarik Toolbox Pagination",
|
||||||
"description": "A mod to use to paginate long result to player in chat",
|
"description": "A mod to use to paginate long result to player in chat",
|
||||||
"authors": [
|
"authors": [
|
||||||
"Altarik"
|
"Altarik"
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
"schemaVersion": 1,
|
"schemaVersion": 1,
|
||||||
"id": "toolbox-task",
|
"id": "toolbox-task",
|
||||||
"version": "${version}",
|
"version": "${version}",
|
||||||
"name": "Task",
|
"name": "Altarik Toolbox Task",
|
||||||
"description": "A mod to use as a dependency for others to schedule tasks",
|
"description": "A mod to use as a dependency for others to schedule tasks",
|
||||||
"authors": [
|
"authors": [
|
||||||
"Altarik"
|
"Altarik"
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
org.gradle.jvmargs=-Xmx2G
|
org.gradle.jvmargs=-Xmx3G
|
||||||
|
|
||||||
|
|
||||||
junit_version=5.9.0
|
junit_version=5.9.0
|
||||||
@ -8,5 +8,5 @@ loader_version=0.14.14
|
|||||||
fabric_version=0.75.1+1.19.3
|
fabric_version=0.75.1+1.19.3
|
||||||
|
|
||||||
maven_group=fr.altarik.toolbox
|
maven_group=fr.altarik.toolbox
|
||||||
maven_version=4.1.0-SNAPSHOT
|
maven_version=4.3.0-SNAPSHOT
|
||||||
repo_username=Altarik
|
repo_username=Altarik
|
||||||
|
Loading…
Reference in New Issue
Block a user