T - The type of the resource that is contained by this instance.public abstract class ResourcesContainer<T>
extends java.lang.Object
ResourcesContainerListener| Modifier | Constructor and Description |
|---|---|
protected |
ResourcesContainer() |
| Modifier and Type | Method and Description |
|---|---|
void |
add(java.lang.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). |
void |
addClearedListener(ResourcesContainerClearedListener listener)
Add a new container listener to this instance that observes whenever this instance is cleared.
|
void |
addContainerListener(ResourcesContainerListener<T> listener)
Add a new container listener to this instance in order to observe resource life cycles.
|
void |
clear()
Clears the resources container by removing all previously loaded resources.
|
boolean |
contains(java.lang.String resourceName)
Checks if this instance contains a resource with the specified name.
|
boolean |
contains(T resource)
Checks if the specified resource is contained by this instance.
|
int |
count()
Gets the amount of resources that this container holds.
|
java.util.Collection<T> |
get(java.util.function.Predicate<T> pred)
Gets all resources that match the specified condition.
|
T |
get(java.lang.String resourceName)
Gets the resource with the specified name.
|
T |
get(java.lang.String resourceName,
boolean forceLoad)
Gets the resource with the specified name.
|
T |
get(java.lang.String resourceName,
java.util.function.Supplier<T> loadCallback)
Gets the resource with the specified name.
|
protected java.lang.String |
getAlias(java.lang.String resourceName,
T resource) |
java.util.Collection<T> |
getAll()
Gets all loaded resources from this container.
|
protected java.util.Map<java.lang.String,T> |
getResources() |
protected abstract T |
load(java.lang.String resourceName) |
T |
remove(java.lang.String resourceName)
Removes the resource with the specified name from this container.
|
void |
removeClearedListener(ResourcesContainerClearedListener listener)
Remove the specified listener from this container.
|
void |
removeContainerListener(ResourcesContainerListener<T> listener)
Remove the specified listener from this container.
|
java.util.Optional<T> |
tryGet(java.lang.String resourceName)
Tries to get a resource with the specified name from this container.
|
public void addContainerListener(ResourcesContainerListener<T> listener)
listener - The container listener instance that will receive call backs from this container.removeContainerListener(ResourcesContainerListener)public void removeContainerListener(ResourcesContainerListener<T> listener)
listener - The listener instance that was previously added to this container.addContainerListener(ResourcesContainerListener)public void addClearedListener(ResourcesContainerClearedListener listener)
listener - The container listener instance.removeClearedListener(ResourcesContainerClearedListener)public void removeClearedListener(ResourcesContainerClearedListener listener)
listener - The listener instance that was previously added to this container.addClearedListener(ResourcesContainerClearedListener)public void add(java.lang.String resourceName,
T resource)
get(resourceName).
Use this method to make a resource accessible over this container during runtime.
resourceName - The name that the resource is managed by.resource - The resource instance.get(Predicate),
get(String),
get(String, boolean),
remove(String),
tryGet(String)public void clear()
public boolean contains(java.lang.String resourceName)
Note that the name is not case-sensitive.
resourceName - The resource's name.contains(Object)public boolean contains(T resource)
resource - The resource.public int count()
public java.util.Collection<T> get(java.util.function.Predicate<T> pred)
pred - The condition that a resource must fulfill in order to be returned.public T get(java.lang.String resourceName)
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.
resourceName - The resource's name.public T get(java.lang.String resourceName, java.util.function.Supplier<T> loadCallback)
If no such resource is currently present on the container, it will be loaded with the specified loadCallback and added to this
container.
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.public T get(java.lang.String resourceName, boolean forceLoad)
If not previously loaded, this method attempts to load the resource on the fly otherwise it will be retrieved from the cache.
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.public java.util.Collection<T> getAll()
public T remove(java.lang.String resourceName)
resourceName - The name of the resource that should be removed.public java.util.Optional<T> tryGet(java.lang.String resourceName)
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.
resourceName - The name of the resource.Optional,
contains(String),
get(String)protected abstract T load(java.lang.String resourceName) throws java.lang.Exception
java.lang.Exceptionprotected java.lang.String getAlias(java.lang.String resourceName,
T resource)
protected java.util.Map<java.lang.String,T> getResources()