Package pi.resources

Class ResourcesContainer<T>

java.lang.Object
pi.resources.ResourcesContainer<T>
Type Parameters:
T - Der Datentyp der Ressource, die in dieser Instanz enthalten ist.
All Implemented Interfaces:
Container<T>
Direct Known Subclasses:
FontContainer, ImageContainer, SoundContainer

public abstract class ResourcesContainer<T> extends Object implements Container<T>
Eine abstrakte Implementierung für Unterklassen, die einen bestimmten Typ von Ressourcen (z. b. Bilder, Klänge) bereitstellen wollen. Diese Klasse bietet Methoden zum Verwalten der Ressourcen an.

Die Ressourcen werden von dieser Klasse im Speicher gehalten. Es handelt sich also um einen Cache.

Author:
Steffen Wilke, Matthias Wilke
See Also:
  • Constructor Details

    • ResourcesContainer

      public ResourcesContainer()
  • Method Details

    • addContainerListener

      public ResourcesContainerListener<T> addContainerListener(ResourcesContainerListener<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:
    • addManipulator

      public void addManipulator(ResourceManipulator<T> manipulator)
      Fügt einen Ressourcen-Manipulator zu diesem Ressourcenspeicher hinzu.
    • removeManipulator

      public void removeManipulator()
      Entfernt den Ressourcen-Manipulator aus diesem Ressourcenspeicher.
    • add

      public T add(String name, T resource)
      Fügt die angegebene Ressource zu diesem Speicher hinzu.
      Das hinzugefügte Element kann später aus dem Speicher abgerufen werden, indem get(resourceName) aufgerufen wird.

      Verwenden Sie diese Methode, um eine Ressource während der Laufzeit über diesen Speicher zugänglich zu machen.

      Specified by:
      add in interface Container<T>
      Parameters:
      name - Der Name, unter dem die Ressource verwaltet wird.
      resource - Die Ressourceninstanz.
      See Also:
    • add

      public T add(URL name, T resource)
    • clear

      public void clear()
      Leert den Ressourcenspeicher, indem alle zuvor geladenen Ressourcen entfernt werden.
      Specified by:
      clear in interface Container<T>
      See Also:
    • contains

      public boolean contains(String name)
      Checks if this instance contains a resource with the specified name.

      Note that the name is not case-sensitive.

      Parameters:
      name - Der Name, unter dem die Ressource verwaltet wird.
      Returns:
      True if this container contains a resource with the specified name; otherwise false.
      See Also:
    • contains

      public boolean contains(URL name)
    • 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.
      Specified by:
      count in interface Container<T>
      Returns:
      The amount of resources in this container.
    • get

      public Collection<T> get(Predicate<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 name)
      Ruft die Ressource mit dem angegebenen Name oder Dateipfad auf.

      Dies ist die gängigste (und bevorzugte) Methode, um Ressourcen aus einem Speicher abzurufen.

      Wenn die Ressource nicht zuvor geladen wurde, versucht diese Methode, sie sofort zu laden, andernfalls wird sie aus dem Cache abgerufen.

      Specified by:
      get in interface Container<T>
      Parameters:
      name - Der Name oder Dateipfad, unter dem die Ressource verwaltet wird.
      Returns:
      Die Ressource mit dem angegebenen Namen oder null, wenn sie nicht gefunden wird.
    • get

      public T get(URL name)
    • get

      public T get(String name, 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:
      name - Der Name, unter dem die Ressource verwaltet wird.
      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 name, Supplier<? extends T> loadCallback)
    • get

      public T get(String name, 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:
      name - Der Name, unter dem die Ressource verwaltet wird.
      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 name, boolean forceLoad)
    • getMultiple

      public T[] getMultiple(String[] names)
    • 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 - Der Name, unter dem die Ressource verwaltet wird.
      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 name)
      Removes the resource with the specified name from this container.
      Parameters:
      name - Der Name, unter dem die Ressource verwaltet wird.
      Returns:
      The removed resource.
    • remove

      public T remove(URL name)
    • tryGet

      public Optional<T> tryGet(String name)
      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:
      name - Der Name, unter dem die Ressource verwaltet wird.
      Returns:
      An Optional instance that holds the resource instance, if present on this container.
      See Also:
    • tryGet

      public Optional<T> tryGet(URL name)
    • load

      protected abstract T load(URL name) throws Exception
      Throws:
      Exception
    • getAlias

      protected String getAlias(String name, T resource)
      Gets an alias for the specified resourceName. Note that the process of providing an alias is up to the ResourceContainer implementation.
      Parameters:
      name - Der Name, unter dem die Ressource verwaltet wird.
      resource - The resource.
      Returns:
      An alias for the specified resource.
    • getResources

      protected Map<String,T> getResources()
    • loadResource

      protected T loadResource(String identifier)