Module aeonics.core

Class Registry<T extends Entity>

  • Type Parameters:
    T - the entity category
    All Implemented Interfaces:
    Exportable, java.lang.Iterable<T>

    public class Registry<T extends Entity>
    extends java.lang.Object
    implements java.lang.Iterable<T>, Exportable
    The registry contains the list of all Entity that have been populated based on user input. The registry is organised by Entity.category().
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <U extends Entity>
      U
      add​(U entity)
      Adds the specified entity to its matching registry Entity.category().
      static java.lang.Iterable<Registry<? extends Entity>> all()
      Returns an iterator over all registries
      java.lang.String category()
      Returns the registry category.
      void clear()
      Removes all entities from this registry.
      void clear​(java.util.function.Predicate<T> comparator)
      Removes all entities from this registry matching the specified criteria.
      boolean contains​(java.lang.String id)
      Checks whether the specified entity exists in this registry.
      Data export()
      Renders this class instance to a simple data structure for rendering client-side.
      <U extends T>
      U
      get​(java.lang.String id)
      Fetches the entity based on its id or `name` property.
      <U extends T>
      U
      get​(java.util.function.Predicate<T> comparator)
      Fetches the first entity that matches the specified criteria.
      static boolean has​(java.lang.String type)
      Checks if the registry of the given entity category exists.
      java.util.Iterator<T> iterator()
      Returns an iterator over all entities registered in this registry
      static <U extends Entity>
      Registry<U>
      of​(java.lang.Class<? extends Item<U>> category)
      Fetches the registry of the given entity category.
      static <U extends Entity>
      Registry<U>
      of​(java.lang.String type)
      Fetches the registry of the given entity category.
      Callback<Entity,​Registry<T>> onAdd()
      Event callback called every time an entity is added to this registry.
      Callback<Entity,​Registry<T>> onRemove()
      Event callback called every time an entity is removed from this registry.
      <U extends T>
      U
      put​(U entity)
      Adds the specified entity into this registry.
      <U extends T>
      U
      remove​(Entity entity)
      Removes the specified entity.
      <U extends T>
      U
      remove​(java.lang.String id)
      Removes the specified entity.
      <U extends T>
      U
      remove​(java.util.function.Predicate<T> comparator)
      Removes the first entity matching the specified criteria.
      int size()
      Returns the number of entities in this registry.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Method Detail

      • of

        public static <U extends EntityRegistry<U> of​(java.lang.Class<? extends Item<U>> category)
        Fetches the registry of the given entity category. This is the same as calling of(StringUtils.toLowerCase(category))
        Type Parameters:
        U - The entity category
        Parameters:
        category - The entity category
        Returns:
        The registry containing all Entity for the specified category
      • of

        public static <U extends EntityRegistry<U> of​(java.lang.String type)
        Fetches the registry of the given entity category.
        Type Parameters:
        U - The entity category
        Parameters:
        type - The entity category (should be or will be converted to lower case)
        Returns:
        The registry containing all Entity for the specified category
      • has

        public static boolean has​(java.lang.String type)
        Checks if the registry of the given entity category exists.
        Parameters:
        type - The entity category (should be or will be converted to lower case)
        Returns:
        true if the registry of the given entity category exists
      • add

        public static <U extends Entity> U add​(U entity)
        Adds the specified entity to its matching registry Entity.category().
        Type Parameters:
        U - the entity category
        Parameters:
        entity - the entity
        Returns:
        the entity
      • all

        public static java.lang.Iterable<Registry<? extends Entity>> all()
        Returns an iterator over all registries
        Returns:
        a registry iterator
      • category

        public java.lang.String category()
        Returns the registry category.
        Returns:
        the registry category
      • contains

        public boolean contains​(java.lang.String id)
        Checks whether the specified entity exists in this registry. The id is checked first and is a very cheap lookup. If nothing matches, all entities are checked to find the first `name` property that matches.
        Parameters:
        id - the entity id
        Returns:
        true if the entity is found, false otherwise
      • get

        public <U extends T> U get​(java.lang.String id)
        Fetches the entity based on its id or `name` property. The id is checked first and is a very cheap lookup. If nothing matches, all entities are checked to find the first `name` property that matches.
        Type Parameters:
        U - the entity type
        Parameters:
        id - the entity id
        Returns:
        the matching entity or null if no such entity is found
      • get

        public <U extends T> U get​(java.util.function.Predicate<T> comparator)
        Fetches the first entity that matches the specified criteria.
        Type Parameters:
        U - the entity type
        Parameters:
        comparator - the matching criteria
        Returns:
        the matching entity or null if no such entity is found
      • put

        public <U extends T> U put​(U entity)
        Adds the specified entity into this registry. The Entity.category() should match this registry's category. If an existing entity is already registered with the same id, it is replaced with the one provided.

        Note that internal entities cannot be replaced. If you need to replace an internal entity, remove its internal flag first.

        Type Parameters:
        U - the entity type
        Parameters:
        entity - the entity
        Returns:
        the entity
        Throws:
        java.lang.IllegalArgumentException - if the entity is null or if the category does not match
      • remove

        public <U extends T> U remove​(Entity entity)
        Removes the specified entity. If the entity is Closeable, it is closed.

        Note that internal entities cannot be removed. If you need to remove an internal entity, remove its internal flag first.

        Type Parameters:
        U - the entity type
        Parameters:
        entity - the entity id
        Returns:
        the removed entity or null if no entity was found
      • remove

        public <U extends T> U remove​(java.lang.String id)
        Removes the specified entity. The id is checked first and is a very cheap lookup. If nothing matches, all entities are checked to find the first `name` property that matches. If the entity is Closeable, it is closed.

        Note that internal entities cannot be removed. If you need to remove an internal entity, remove its internal flag first.

        Type Parameters:
        U - the entity type
        Parameters:
        id - the entity id
        Returns:
        the removed entity or null if no entity was found
      • remove

        public <U extends T> U remove​(java.util.function.Predicate<T> comparator)
        Removes the first entity matching the specified criteria. If the entity is Closeable, it is closed.

        Note that internal entities cannot be removed. If you need to remove an internal entity, remove its internal flag first.

        Type Parameters:
        U - the entity type
        Parameters:
        comparator - the matching criteria
        Returns:
        the removed entity or null if no entity was found
      • clear

        public void clear()
        Removes all entities from this registry. If the entity is Closeable, it is closed.
      • clear

        public void clear​(java.util.function.Predicate<T> comparator)
        Removes all entities from this registry matching the specified criteria. If the entity is Closeable, it is closed.

        Note that internal entities cannot be removed. If you need to remove an internal entity, remove its internal flag first.

        Parameters:
        comparator - the matching criteria
      • size

        public int size()
        Returns the number of entities in this registry.
        Returns:
        the number of entities in this registry
      • iterator

        public java.util.Iterator<T> iterator()
        Returns an iterator over all entities registered in this registry
        Specified by:
        iterator in interface java.lang.Iterable<T extends Entity>
        Returns:
        an entity iterator
      • export

        public Data export()
        Description copied from interface: Exportable
        Renders this class instance to a simple data structure for rendering client-side.
        Specified by:
        export in interface Exportable
        Returns:
        a public data representation of this class instance