From c74ae48156da0b19e4c9bfd6f1147b867eafd036 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 21 Jun 2023 18:15:12 +0200 Subject: [PATCH] Add Registry --- .../fr/altarik/toolbox/core/Registry.java | 51 +++++++++++++++++++ gradle.properties | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 Core/src/main/java/fr/altarik/toolbox/core/Registry.java diff --git a/Core/src/main/java/fr/altarik/toolbox/core/Registry.java b/Core/src/main/java/fr/altarik/toolbox/core/Registry.java new file mode 100644 index 0000000..85d37ac --- /dev/null +++ b/Core/src/main/java/fr/altarik/toolbox/core/Registry.java @@ -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 { + + protected final HashMap 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 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)); + } + +} + diff --git a/gradle.properties b/gradle.properties index f7f7fb2..9dcf69e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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.2.1-SNAPSHOT +maven_version=4.3.0-SNAPSHOT repo_username=Altarik