annotate Registry methods with nullability and contract

This commit is contained in:
dfsek
2021-12-01 10:15:02 -07:00
parent 2d7cf5151f
commit 7a665d6f9c
7 changed files with 75 additions and 36 deletions

View File

@@ -9,6 +9,8 @@ package com.dfsek.terra.api.registry;
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
import org.jetbrains.annotations.NotNull;
public interface CheckedRegistry<T> extends Registry<T> {
/**
@@ -19,6 +21,5 @@ public interface CheckedRegistry<T> extends Registry<T> {
*
* @throws DuplicateEntryException If an entry with the same identifier is already present.
*/
void register(String identifier, T value) throws DuplicateEntryException;
void register(@NotNull String identifier, @NotNull T value) throws DuplicateEntryException;
}

View File

@@ -9,6 +9,8 @@ package com.dfsek.terra.api.registry;
import com.dfsek.terra.api.registry.exception.DuplicateEntryException;
import org.jetbrains.annotations.NotNull;
public interface OpenRegistry<T> extends Registry<T> {
/**
@@ -17,7 +19,7 @@ public interface OpenRegistry<T> extends Registry<T> {
* @param identifier Identifier to assign value.
* @param value Value to register.
*/
boolean register(String identifier, T value);
boolean register(@NotNull String identifier, @NotNull T value);
/**
* Add a value to this registry, checking whether it is present first.
@@ -27,7 +29,7 @@ public interface OpenRegistry<T> extends Registry<T> {
*
* @throws DuplicateEntryException If an entry with the same identifier is already present.
*/
void registerChecked(String identifier, T value) throws DuplicateEntryException;
void registerChecked(@NotNull String identifier, @NotNull T value) throws DuplicateEntryException;
/**
* Clears all entries from the registry.

View File

@@ -9,6 +9,11 @@ package com.dfsek.terra.api.registry;
import com.dfsek.tectonic.loading.TypeLoader;
import com.dfsek.terra.api.registry.exception.NoSuchEntryException;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.Set;
import java.util.function.BiConsumer;
@@ -23,7 +28,9 @@ public interface Registry<T> extends TypeLoader<T> {
*
* @return Value matching the identifier, {@code null} if no value is present.
*/
T get(String identifier);
@NotNull
@Contract(pure = true)
T get(@NotNull String identifier) throws NoSuchEntryException;
/**
* Check if the registry contains a value.
@@ -32,27 +39,30 @@ public interface Registry<T> extends TypeLoader<T> {
*
* @return Whether the registry contains the value.
*/
boolean contains(String identifier);
@Contract(pure = true)
boolean contains(@NotNull String identifier);
/**
* Perform the given action for every value in the registry.
*
* @param consumer Action to perform on value.
*/
void forEach(Consumer<T> consumer);
void forEach(@NotNull Consumer<T> consumer);
/**
* Perform an action for every key-value pair in the registry.
*
* @param consumer Action to perform on pair.
*/
void forEach(BiConsumer<String, T> consumer);
void forEach(@NotNull BiConsumer<String, T> consumer);
/**
* Get the entries of this registry as a {@link Set}.
*
* @return Set containing all entries.
*/
@NotNull
@Contract(pure = true)
Collection<T> entries();
/**
@@ -60,5 +70,7 @@ public interface Registry<T> extends TypeLoader<T> {
*
* @return Keys in registry
*/
@NotNull
@Contract(pure = true)
Set<String> keys();
}

View File

@@ -0,0 +1,11 @@
package com.dfsek.terra.api.registry.exception;
public class NoSuchEntryException extends RuntimeException {
public NoSuchEntryException(String message) {
super(message);
}
public NoSuchEntryException(String message, Throwable cause) {
super(message, cause);
}
}