diff --git a/eco-api/src/main/java/com/willfp/eco/core/registry/Registry.java b/eco-api/src/main/java/com/willfp/eco/core/registry/Registry.java index dceda57b..d4f8e8c4 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/registry/Registry.java +++ b/eco-api/src/main/java/com/willfp/eco/core/registry/Registry.java @@ -5,6 +5,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.regex.Pattern; @@ -14,7 +15,7 @@ import java.util.regex.Pattern; * * @param The type of {@link Registrable}. */ -public class Registry { +public class Registry implements Iterable { /** * The ID pattern. */ @@ -25,6 +26,11 @@ public class Registry { */ private final Map registry = new HashMap<>(); + /** + * If the registry is locked. + */ + private boolean isLocked = false; + /** * Instantiate a new registry. */ @@ -40,6 +46,10 @@ public class Registry { */ @NotNull public T register(@NotNull final T element) { + if (this.isLocked) { + throw new IllegalStateException("Cannot add to locked registry! (ID: " + element.getID() + ")"); + } + Validate.isTrue(ID_PATTERN.matcher(element.getID()).matches(), "ID must match pattern: " + ID_PATTERN.pattern() + " (was " + element.getID() + ")"); registry.put(element.getID(), element); @@ -56,6 +66,10 @@ public class Registry { * @return The element. */ public T remove(@NotNull final T element) { + if (this.isLocked) { + throw new IllegalStateException("Cannot remove from locked registry! (ID: " + element.getID() + ")"); + } + element.onRemove(); registry.remove(element.getID()); @@ -71,6 +85,10 @@ public class Registry { */ @Nullable public T remove(@NotNull final String id) { + if (this.isLocked) { + throw new IllegalStateException("Cannot remove from locked registry! (ID: " + id + ")"); + } + T element = registry.get(id); if (element != null) { @@ -109,6 +127,35 @@ public class Registry { return Set.copyOf(registry.values()); } + /** + * Get if the registry is locked. + * + * @return If the registry is locked. + */ + public boolean isLocked() { + return isLocked; + } + + /** + * Lock the registry. + */ + public void lock() { + isLocked = true; + } + + /** + * Unlock the registry. + */ + public void unlock() { + isLocked = false; + } + + @NotNull + @Override + public Iterator iterator() { + return values().iterator(); + } + /** * Try to fit a string to the ID pattern. *