T
- The type of the resource that is contained by this instance.public abstract class ResourcesContainer<T>
extends java.lang.Object
ResourcesContainerListener
Constructor and Description |
---|
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 |
add(java.net.URL resourceName,
T resource) |
void |
addClearedListener(ResourcesContainerClearedListener listener)
Add a new container listener to this instance that observes whenever this instance is cleared.
|
void |
addContainerListener(ResourcesContainerListener<? super T> listener)
Add a new container listener to this instance in order to observe resource life cycles.
|
void |
clear()
Clears the resource 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.
|
boolean |
contains(java.net.URL resourceName) |
int |
count()
Gets the amount of resources that this container holds.
|
java.util.Collection<T> |
get(java.util.function.Predicate<? super 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<? extends T> loadCallback)
Gets the resource with the specified name.
|
T |
get(java.net.URL resourceName) |
T |
get(java.net.URL resourceName,
boolean forceLoad) |
T |
get(java.net.URL resourceName,
java.util.function.Supplier<? extends T> loadCallback) |
protected java.lang.String |
getAlias(java.lang.String resourceName,
T resource)
Gets an alias for the specified resourceName.
|
java.util.Collection<T> |
getAll()
Gets all loaded resources from this container.
|
java.util.concurrent.Future<T> |
getAsync(java.lang.String name)
Eventually gets the resource with the specified location.
|
java.util.concurrent.Future<T> |
getAsync(java.net.URL location)
Eventually gets the resource with the specified location.
|
protected java.util.Map<java.lang.String,T> |
getResources() |
protected abstract T |
load(java.net.URL resourceName) |
T |
remove(java.lang.String resourceName)
Removes the resource with the specified name from this container.
|
T |
remove(java.net.URL resourceName) |
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.
|
java.util.Optional<T> |
tryGet(java.net.URL resourceName) |
public void addContainerListener(ResourcesContainerListener<? super 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 add(java.net.URL resourceName, T resource)
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(java.net.URL resourceName)
public boolean contains(T resource)
resource
- The resource.public int count()
public java.util.Collection<T> get(java.util.function.Predicate<? super 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.net.URL resourceName)
public T get(java.lang.String resourceName, java.util.function.Supplier<? extends 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 T get(java.net.URL resourceName, boolean forceLoad)
public java.util.concurrent.Future<T> getAsync(java.net.URL location)
Future
object returned by this method once loaded.location
- The location of the resourceFuture
object that can be used to retrieve the resource once it is finished loadingpublic java.util.concurrent.Future<T> getAsync(java.lang.String name)
Future
object returned by this method once loaded.name
- The name or location of the resourceFuture
object that can be used to retrieve the resource once it is finished loadingpublic java.util.Collection<T> getAll()
public T remove(java.lang.String resourceName)
resourceName
- The name of the resource that should be removed.public T remove(java.net.URL resourceName)
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)
public java.util.Optional<T> tryGet(java.net.URL resourceName)
protected abstract T load(java.net.URL resourceName) throws java.lang.Exception
java.lang.Exception
protected java.lang.String getAlias(java.lang.String resourceName, T resource)
resourceName
- The original name of the resource.resource
- The resource.protected java.util.Map<java.lang.String,T> getResources()