|
| 1 | +package de.geolykt.starloader.api.registry; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.jetbrains.annotations.NotNull; |
| 7 | +import org.jetbrains.annotations.Nullable; |
| 8 | + |
| 9 | +import de.geolykt.starloader.api.NamespacedKey; |
| 10 | + |
| 11 | +/** |
| 12 | + * Registry of enum and/or enum-like objects with additionally capability to add metadata to |
| 13 | + * the key-value pairs. This is added for extension harmony as multiple extensions cannot do these |
| 14 | + * themselves without breaking aspects of the functionality or creating agreements themselves. |
| 15 | + * Since the StarloaderAPI is already one of the first extensions to experiment with such aspects, |
| 16 | + * the StarloaderAPI is taking the authority in this. |
| 17 | + * The metadata is a concept that is introduced since especially non-abstract enums are very state based |
| 18 | + * and a metadata API would make it more data based and increase the modifiability of the behaviour of the game. |
| 19 | + * |
| 20 | + * @param <T> The type the registry is holding |
| 21 | + * @param <U> The metadata container type of the registry entries |
| 22 | + */ |
| 23 | +public abstract class MetadatableRegistry<T, U extends MetadatableRegistry.MetadataEntry> extends Registry<T> { |
| 24 | + |
| 25 | + /** |
| 26 | + * The metadata entry structure. Does nothing on it's own other than looking good; it is up to the implementation |
| 27 | + * to make it more meaningful. |
| 28 | + */ |
| 29 | + public static abstract class MetadataEntry {} |
| 30 | + |
| 31 | + /** |
| 32 | + * Internal map containing the key-metadata entry pairs of the registry for lookup. |
| 33 | + */ |
| 34 | + protected final Map<NamespacedKey, U> metadataEntries = new HashMap<>(); |
| 35 | + |
| 36 | + /** |
| 37 | + * Obtains the metadata entry bound to the given registry key. If no metadata is bound at that key, |
| 38 | + * then null will be returned. |
| 39 | + * |
| 40 | + * @param key The {@link NamespacedKey} that is used for the lookup operation |
| 41 | + * @return The {@link MetadataEntry} |
| 42 | + */ |
| 43 | + public @Nullable U getMetadataEntry(@NotNull NamespacedKey key) { |
| 44 | + return metadataEntries.get(key); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @deprecated This method has no use as it does not specify the metadata. |
| 49 | + * |
| 50 | + * This operation instantly throws an exception |
| 51 | + * |
| 52 | + * @param key irrelevant |
| 53 | + * @param value irrelevant |
| 54 | + */ |
| 55 | + @Override |
| 56 | + @Deprecated(forRemoval = false, since = "1.1.0") |
| 57 | + public final void register(@NotNull NamespacedKey key, @NotNull T value) { |
| 58 | + throw new IllegalArgumentException("The metadatable registry requires to know the metadata entry." |
| 59 | + + "Use the other register method instead."); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Registers the value to the given key; the implementation might be thread-safe, however extensions should always |
| 64 | + * believe that multithreading can be dangerous and as such this method should never be called concurrently as otherwise |
| 65 | + * some other things (such as the values array) might break. |
| 66 | + * |
| 67 | + * @param key The key of the entry to register |
| 68 | + * @param value The value of the entry |
| 69 | + * @param metadata The metadata entry. |
| 70 | + */ |
| 71 | + public abstract void register(@NotNull NamespacedKey key, @NotNull T value, @NotNull U metadata); |
| 72 | +} |
0 commit comments