Class ResourcesContainer<T>

Type Parameters:
T - The type of the resource that is contained by this instance.
Direct Known Subclasses:
Animations, Blueprints, Fonts, Images, Maps, Sounds, Tilesets

public abstract class ResourcesContainer<T>
An abstract implementation for all classes that provide a certain type of resources. Basically, it's an in-memory cache of the resources and provides access to manage the resources.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(String resourceName, T resource)
    Add the specified resource to this container.
    void
    add(URL resourceName, T resource)
    Adds the specified resource to this container using the given URL as the resource name.
    void
    Add a new container listener to this instance that observes whenever this instance is cleared.
    void
    Add a new container listener to this instance in order to observe resource life cycles.
    void
    Clears the resource container by removing all previously loaded resources.
    boolean
    contains(String resourceName)
    Checks if this instance contains a resource with the specified name.
    boolean
    contains(URL resourceName)
    Checks if this instance contains a resource with the specified URL.
    boolean
    contains(T resource)
    Checks if the specified resource is contained by this instance.
    int
    Gets the amount of resources that this container holds.
    get(String resourceName)
    Gets the resource with the specified name.
    get(String resourceName, boolean forceLoad)
    Gets the resource with the specified name.
    get(String resourceName, Supplier<? extends T> loadCallback)
    Gets the resource with the specified name.
    get(URL resourceName)
    Gets the resource with the specified URL.
    get(URL resourceName, boolean forceLoad)
    Gets the resource with the specified URL.
    get(URL resourceName, Supplier<? extends T> loadCallback)
    Gets the resource with the specified URL.
    get(Predicate<? super T> pred)
    Gets all resources that match the specified condition.
    protected String
    getAlias(String resourceName, T resource)
    Gets an alias for the specified resourceName.
    Gets all loaded resources from this container.
    Eventually gets the resource with the specified location.
    getAsync(URL location)
    Eventually gets the resource with the specified location.
    protected Map<String,T>
    Gets the map of resources contained in this container.
    protected abstract T
    load(URL resourceName)
    Loads the resource with the specified URL.
    remove(String resourceName)
    Removes the resource with the specified name from this container.
    remove(URL resourceName)
    Removes the resource with the specified URL from this container.
    void
    Remove the specified listener from this container.
    void
    Remove the specified listener from this container.
    tryGet(String resourceName)
    Tries to get a resource with the specified name from this container.
    tryGet(URL resourceName)
    Tries to get a resource with the specified URL from this container.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ResourcesContainer

      public ResourcesContainer()
  • Method Details

    • addContainerListener

      public void addContainerListener(ResourcesContainerListener<? super T> listener)
      Add a new container listener to this instance in order to observe resource life cycles. The listener will get notified whenever a resource was added to or removed from this container.
      Parameters:
      listener - The container listener instance that will receive call backs from this container.
      See Also:
    • removeContainerListener

      public void removeContainerListener(ResourcesContainerListener<T> listener)
      Remove the specified listener from this container.
      Parameters:
      listener - The listener instance that was previously added to this container.
      See Also:
    • addClearedListener

      public void addClearedListener(ResourcesContainerClearedListener listener)
      Add a new container listener to this instance that observes whenever this instance is cleared.
      Parameters:
      listener - The container listener instance.
      See Also:
    • removeClearedListener

      public void removeClearedListener(ResourcesContainerClearedListener listener)
      Remove the specified listener from this container.
      Parameters:
      listener - The listener instance that was previously added to this container.
      See Also:
    • add

      public void add(String resourceName, T resource)

      Add the specified resource to this container. The added element can later be retrieved from this container by calling get(resourceName).

      Use this method to make a resource accessible over this container during runtime.

      Parameters:
      resourceName - The name that the resource is managed by.
      resource - The resource instance.
      See Also:
    • add

      public void add(URL resourceName, T resource)
      Adds the specified resource to this container using the given URL as the resource name. The added element can later be retrieved from this container by calling get(resourceName).
      Parameters:
      resourceName - The URL of the resource to be added.
      resource - The resource instance to be added.
    • clear

      public void clear()
      Clears the resource container by removing all previously loaded resources.
    • contains

      public boolean contains(String resourceName)

      Checks if this instance contains a resource with the specified name.

      Note that the name is not case-sensitive.

      Parameters:
      resourceName - The resource's name.
      Returns:
      True if this container contains a resource with the specified name; otherwise false.
      See Also:
    • contains

      public boolean contains(URL resourceName)
      Checks if this instance contains a resource with the specified URL.
      Parameters:
      resourceName - The URL of the resource.
      Returns:
      True if this container contains a resource with the specified URL; otherwise false.
    • contains

      public boolean contains(T resource)
      Checks if the specified resource is contained by this instance.
      Parameters:
      resource - The resource.
      Returns:
      True if this instance contains the specified resource instance; otherwise false.
    • count

      public int count()
      Gets the amount of resources that this container holds.
      Returns:
      The amount of resources in this container.
    • get

      public Collection<T> get(Predicate<? super T> pred)
      Gets all resources that match the specified condition.
      Parameters:
      pred - The condition that a resource must fulfill in order to be returned.
      Returns:
      All resources that match the specified condition.
    • get

      public T get(String resourceName)

      Gets the resource with the specified name.

      This is the most common (and preferred) way to fetch resources from a container.

      If not previously loaded, this method attempts to load the resource on the fly otherwise it will be retrieved from the cache.

      Parameters:
      resourceName - The resource's name.
      Returns:
      The resource with the specified name or null if not found.
    • get

      public T get(URL resourceName)

      Gets the resource with the specified URL.

      If not previously loaded, this method attempts to load the resource on the fly; otherwise, it will be retrieved from the cache.

      Parameters:
      resourceName - The URL of the resource.
      Returns:
      The resource with the specified URL or null if not found.
    • get

      public T get(String resourceName, Supplier<? extends T> loadCallback)

      Gets the resource with the specified name.

      If no such resource is currently present on the container, it will be loaded with the specified loadCallback and added to this container.

      Parameters:
      resourceName - The resource's name.
      loadCallback - The callback that is used to load the resource on-demand if it's not present on this container.
      Returns:
      T The resource with the specified name.
    • get

      public T get(URL resourceName, Supplier<? extends T> loadCallback)

      Gets the resource with the specified URL.

      If no such resource is currently present in the container, it will be loaded with the specified loadCallback and added to this container.

      Parameters:
      resourceName - The URL of the resource.
      loadCallback - The callback that is used to load the resource on-demand if it's not present in this container.
      Returns:
      The resource with the specified URL.
    • get

      public T get(String resourceName, boolean forceLoad)

      Gets the resource with the specified name.

      If not previously loaded, this method attempts to load the resource on the fly otherwise it will be retrieved from the cache.

      Parameters:
      resourceName - The name of the game resource.
      forceLoad - If set to true, cached resource (if existing) will be discarded and the resource will be freshly loaded.
      Returns:
      The game resource or null if not found.
    • get

      public T get(URL resourceName, boolean forceLoad)

      Gets the resource with the specified URL.

      If not previously loaded, this method attempts to load the resource on the fly; otherwise, it will be retrieved from the cache.

      Parameters:
      resourceName - The URL of the resource.
      forceLoad - If set to true, cached resource (if existing) will be discarded and the resource will be freshly loaded.
      Returns:
      The resource with the specified URL or null if not found.
    • getAsync

      public Future<T> getAsync(URL location)
      Eventually gets the resource with the specified location. The resource is loaded asynchronously and can be retrieved from the returned Future object returned by this method once loaded.
      Parameters:
      location - The location of the resource
      Returns:
      A Future object that can be used to retrieve the resource once it is finished loading
    • getAsync

      public Future<T> getAsync(String name)
      Eventually gets the resource with the specified location. The resource is loaded asynchronously and can be retrieved from the returned Future object returned by this method once loaded.
      Parameters:
      name - The name or location of the resource
      Returns:
      A Future object that can be used to retrieve the resource once it is finished loading
    • getAll

      public Collection<T> getAll()
      Gets all loaded resources from this container.
      Returns:
      All loaded resources.
    • remove

      public T remove(String resourceName)
      Removes the resource with the specified name from this container.
      Parameters:
      resourceName - The name of the resource that should be removed.
      Returns:
      The removed resource.
    • remove

      public T remove(URL resourceName)
      Removes the resource with the specified URL from this container.
      Parameters:
      resourceName - The URL of the resource that should be removed.
      Returns:
      The removed resource.
    • tryGet

      public Optional<T> tryGet(String resourceName)

      Tries to get a resource with the specified name from this container.

      This method should be used, if it's not clear whether the resource is present on this container. It is basically a combination of get(String) and contains(String) and allows to check whether a resource is present while also fetching it from the container.

      Parameters:
      resourceName - The name of the resource.
      Returns:
      An Optional instance that holds the resource instance, if present on this container.
      See Also:
    • tryGet

      public Optional<T> tryGet(URL resourceName)

      Tries to get a resource with the specified URL from this container.

      This method should be used if it's not clear whether the resource is present in this container. It is basically a combination of get(URL) and contains(URL) and allows checking whether a resource is present while also fetching it from the container.

      Parameters:
      resourceName - The URL of the resource.
      Returns:
      An Optional instance that holds the resource instance, if present in this container.
    • load

      protected abstract T load(URL resourceName) throws Exception

      Loads the resource with the specified URL.

      This method must be implemented by subclasses to define how a resource is loaded from the given URL.

      Parameters:
      resourceName - The URL of the resource to be loaded.
      Returns:
      The loaded resource.
      Throws:
      Exception - If an error occurs while loading the resource.
    • getAlias

      protected String getAlias(String resourceName, T resource)
      Gets an alias for the specified resourceName. Note that the process of providing an alias is up to the ResourceContainer implementation.
      Parameters:
      resourceName - The original name of the resource.
      resource - The resource.
      Returns:
      An alias for the specified resource.
    • getResources

      protected Map<String,T> getResources()
      Gets the map of resources contained in this container.
      Returns:
      A map where the keys are resource names and the values are the resources.