Added registry
This commit is contained in:
@@ -0,0 +1,32 @@
|
|||||||
|
package com.willfp.eco.core.registry;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An object that can be registered.
|
||||||
|
*
|
||||||
|
* @see Registry
|
||||||
|
*/
|
||||||
|
public interface Registrable {
|
||||||
|
/**
|
||||||
|
* Get the ID of the element.
|
||||||
|
*
|
||||||
|
* @return The ID.
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
String getID();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the element is registered.
|
||||||
|
*/
|
||||||
|
default void onRegister() {
|
||||||
|
// Do nothing by default.
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the element is removed.
|
||||||
|
*/
|
||||||
|
default void onRemove() {
|
||||||
|
// Do nothing by default.
|
||||||
|
}
|
||||||
|
}
|
||||||
102
eco-api/src/main/java/com/willfp/eco/core/registry/Registry.java
Normal file
102
eco-api/src/main/java/com/willfp/eco/core/registry/Registry.java
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
package com.willfp.eco.core.registry;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A registry for {@link Registrable}s.
|
||||||
|
*
|
||||||
|
* @param <T> The type of {@link Registrable}.
|
||||||
|
*/
|
||||||
|
public abstract class Registry<T extends Registrable> {
|
||||||
|
/**
|
||||||
|
* The registry.
|
||||||
|
*/
|
||||||
|
private final Map<String, T> registry = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiate a new registry.
|
||||||
|
*/
|
||||||
|
protected Registry() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a new element.
|
||||||
|
*
|
||||||
|
* @param element The element to register.
|
||||||
|
* @return The element.
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public T register(@NotNull final T element) {
|
||||||
|
registry.put(element.getID(), element);
|
||||||
|
|
||||||
|
element.onRegister();
|
||||||
|
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove an element.
|
||||||
|
*
|
||||||
|
* @param element The element.
|
||||||
|
* @return The element.
|
||||||
|
*/
|
||||||
|
public T remove(@NotNull final T element) {
|
||||||
|
element.onRemove();
|
||||||
|
|
||||||
|
registry.remove(element.getID());
|
||||||
|
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove an element by ID.
|
||||||
|
*
|
||||||
|
* @param id The ID.
|
||||||
|
* @return The element.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public T remove(@NotNull final String id) {
|
||||||
|
T element = registry.get(id);
|
||||||
|
|
||||||
|
if (element != null) {
|
||||||
|
element.onRemove();
|
||||||
|
}
|
||||||
|
|
||||||
|
return registry.remove(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get an element by ID.
|
||||||
|
*
|
||||||
|
* @param id The ID.
|
||||||
|
* @return The element, or null if not found.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public T get(@NotNull final String id) {
|
||||||
|
return registry.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear the registry.
|
||||||
|
*/
|
||||||
|
public void clear() {
|
||||||
|
for (T value : registry.values()) {
|
||||||
|
remove(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all elements.
|
||||||
|
*
|
||||||
|
* @return All elements.
|
||||||
|
*/
|
||||||
|
public Set<T> values() {
|
||||||
|
return Set.copyOf(registry.values());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user