Class RegisteredMap<K,V extends Registered>

java.lang.Object
dev.jaxydog.astral.register.RegisteredMap<K,V>
Type Parameters:
K - The map's key.
V - The map's registerable value.
All Implemented Interfaces:
Registered, Registered.All, Registered.Client, Registered.Common, Registered.Generated, Registered.Server
Direct Known Subclasses:
ArmorMap, DyeMap, PlaceholderMimicItem.Group

public abstract class RegisteredMap<K,V extends Registered> extends Object implements Registered.All, Registered.Generated
A generic map structure, backed by an Object2ObjectOpenHashMap, where all values implement the Registered interface and are generated using a provided computation function.

This is most useful when generating a large amount of values at once. For a small number of items, it may be better to instead just instantiate normally.

Since:
1.5.0
See Also:
  • Field Details

    • innerMap

      protected final Map<K,V extends Registered> innerMap
      The inner hash map that contains all generated keys and values.
      Since:
      2.0.0
  • Constructor Details

    • RegisteredMap

      protected RegisteredMap(@NotNull @NotNull String basePath, BiFunction<@NotNull String,@NotNull K,V> computeCallback)
      Creates a new registered map.
      Parameters:
      basePath - The base identifier path.
      computeCallback - The value computation callback.
      Since:
      2.0.0
  • Method Details

    • keys

      public abstract Set<@NotNull K> keys()
      Returns the set of all expected keys.

      The values within this set are fed into the computation callback to generate the values stored within this map.

      Returns:
      A set containing all expected keys.
      Since:
      2.0.0
    • compareKeys

      protected abstract int compareKeys(@NotNull K left, @NotNull K right)
      Compares the provided keys to determine their registration order.

      If left is lesser than right, a negative value should be returned. If left is greater than right, a positive value should be returned. Otherwise, if both are equal, 0 should be returned.

      For a simple example, assuming <K> is of type Integer, the value can be determined by evaluating right - left for a simple natural ordering.

      Parameters:
      left - The left key.
      right - The right key.
      Returns:
      The result as defined in the method documentation.
      Since:
      2.0.0
    • getPath

      protected abstract String getPath(@NotNull K key)
      Returns an identifier path based off of the given key.
      Parameters:
      key - The value's key.
      Returns:
      A valid identifier path.
      Since:
      2.0.0
    • get

      public Optional<V> get(@NotNull K key)
      Returns a value from within the map if it is assigned to the given key.

      If the given key is present within the map, this is equivalent to calling Optional.ofNullable(map.getNullable(key)).

      Parameters:
      key - The value's expected key.
      Returns:
      A possible value.
      Since:
      2.0.0
    • getNullable

      @Nullable public V getNullable(@NotNull K key)
      Returns a value from within the map.
      Parameters:
      key - The value's expected key.
      Returns:
      A value, or null if the value is not present.
      Since:
      2.0.0
    • getComputed

      public V getComputed(@NotNull K key)
      Returns a value from within the map, creating it if it does not exist.
      Parameters:
      key - The value's expected key.
      Returns:
      A value, or null if the value is not present.
      Since:
      2.0.0
    • entries

      public Set<Map.Entry<K,V>> entries()
      Returns a set of all entries stored within this map.
      Returns:
      A set of all stored entries.
      Since:
      2.0.0
    • values

      public Collection<V> values()
      Returns a collection of the values stored within this map.
      Returns:
      A collection of stored values.
      Since:
      2.0.0
    • computeValuesIfMissing

      public void computeValuesIfMissing()
      Computes all values stored within the map if they have not yet been created.

      If the keys() method returns an empty set, this method does nothing.

      Since:
      2.0.0
    • computeValues

      protected void computeValues()
      Computes all values stored within the map.

      If any keys are stored within the map that are included within the set returned by {}, they will be overwritten.

      If the keys() method returns an empty set, this method does nothing.

      Since:
      2.0.0
    • sortedValuesOfType

      protected List<V> sortedValuesOfType(Class<? extends Registered> type)
      Returns a list of sorted values that match the expected registered type.
      Parameters:
      type - The expected type.
      Returns:
      A list of sorted values.
      Since:
      2.0.0
    • getRegistryPath

      public String getRegistryPath()
      Description copied from interface: Registered
      Returns this value's associated registry identifier path.

      This is by default used to determine the Identifier provided by Registered.getRegistryId(). For example, returning "my_item" will cause the associated identifier to be astral:my_item.

      Specified by:
      getRegistryPath in interface Registered
      Returns:
      An identifier path.
    • registerCommon

      public void registerCommon()
      Description copied from interface: Registered.Common
      Registers this value on both the client and server environments.

      The contained code will run for both environments. If you only want one or the other, see its sister interfaces.

      Specified by:
      registerCommon in interface Registered.Common
      See Also:
    • registerClient

      public void registerClient()
      Description copied from interface: Registered.Client
      Registers this value on the client environment.

      The contained code will only ever run on the game client. If you only want the server or want both, see its sister interfaces.

      Specified by:
      registerClient in interface Registered.Client
      See Also:
    • registerServer

      public void registerServer()
      Description copied from interface: Registered.Server
      Registers this value on the server environment.

      The contained code will only ever run on the game server. If you only want the client or want both, see its sister interfaces.

      Specified by:
      registerServer in interface Registered.Server
      See Also:
    • generate

      public void generate()
      Description copied from interface: Registered.Generated
      Generates game assets or data.

      It should be assumed that the contained code will only ever run on the server environment.

      Specified by:
      generate in interface Registered.Generated