implement single-parameter Keyed registration methods

This commit is contained in:
dfsek 2021-12-26 19:26:23 -07:00
parent 3792a1ab05
commit 1c93c2bbb4
2 changed files with 17 additions and 0 deletions

View File

@ -7,6 +7,7 @@
package com.dfsek.terra.api.registry; package com.dfsek.terra.api.registry;
import com.dfsek.terra.api.registry.key.Keyed;
import com.dfsek.terra.api.registry.key.RegistryKey; import com.dfsek.terra.api.registry.key.RegistryKey;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -24,4 +25,9 @@ public interface CheckedRegistry<T> extends Registry<T> {
* @throws DuplicateEntryException If an entry with the same identifier is already present. * @throws DuplicateEntryException If an entry with the same identifier is already present.
*/ */
void register(@NotNull RegistryKey identifier, @NotNull T value) throws DuplicateEntryException; void register(@NotNull RegistryKey identifier, @NotNull T value) throws DuplicateEntryException;
@SuppressWarnings("unchecked")
default void register(@NotNull Keyed<? extends T> value) throws DuplicateEntryException {
register(value.getRegistryKey(), (T) value);
}
} }

View File

@ -7,6 +7,7 @@
package com.dfsek.terra.api.registry; package com.dfsek.terra.api.registry;
import com.dfsek.terra.api.registry.key.Keyed;
import com.dfsek.terra.api.registry.key.RegistryKey; import com.dfsek.terra.api.registry.key.RegistryKey;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -23,6 +24,11 @@ public interface OpenRegistry<T> extends Registry<T> {
*/ */
boolean register(@NotNull RegistryKey identifier, @NotNull T value); boolean register(@NotNull RegistryKey identifier, @NotNull T value);
@SuppressWarnings("unchecked")
default boolean register(@NotNull Keyed<? extends T> value) {
return register(value.getRegistryKey(), (T) value);
}
/** /**
* Add a value to this registry, checking whether it is present first. * Add a value to this registry, checking whether it is present first.
* *
@ -33,6 +39,11 @@ public interface OpenRegistry<T> extends Registry<T> {
*/ */
void registerChecked(@NotNull RegistryKey identifier, @NotNull T value) throws DuplicateEntryException; void registerChecked(@NotNull RegistryKey identifier, @NotNull T value) throws DuplicateEntryException;
@SuppressWarnings("unchecked")
default void registerChecked(@NotNull Keyed<? extends T> value) {
registerChecked(value.getRegistryKey(), (T) value);
}
/** /**
* Clears all entries from the registry. * Clears all entries from the registry.
*/ */