com.jme3.asset.cache
Class WeakRefAssetCache

java.lang.Object
  extended by com.jme3.asset.cache.WeakRefAssetCache
All Implemented Interfaces:
AssetCache

public class WeakRefAssetCache
extends java.lang.Object
implements AssetCache

A garbage collector bound asset cache that handles non-clonable objects. This cache assumes that the asset given to the user is the same asset that has been stored in the cache, in other words, AssetProcessor.createClone(java.lang.Object) for that asset returns the same object as the argument. This implementation will remove the asset from the cache once the asset is no longer referenced in user code and memory is low, e.g. the VM feels like purging the weak references for that asset.


Constructor Summary
WeakRefAssetCache()
           
 
Method Summary
<T> void
addToCache(AssetKey<T> key, T obj)
          Adds an asset to the cache.
 void clearCache()
          Deletes all assets from the cache.
 boolean deleteFromCache(AssetKey key)
          Deletes an asset from the cache.
<T> T
getFromCache(AssetKey<T> key)
          Retrieves an asset from the cache.
 void notifyNoAssetClone()
          Notifies the cache that even though the methods AssetCache.addToCache(com.jme3.asset.AssetKey, java.lang.Object) or AssetCache.getFromCache(com.jme3.asset.AssetKey) were used, there won't be a call to AssetCache.registerAssetClone(com.jme3.asset.AssetKey, java.lang.Object) for some reason.
<T> void
registerAssetClone(AssetKey<T> key, T clone)
          This should be called by the asset manager when it has successfully acquired a cached asset (with AssetCache.getFromCache(com.jme3.asset.AssetKey)) and cloned it for use.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WeakRefAssetCache

public WeakRefAssetCache()
Method Detail

addToCache

public <T> void addToCache(AssetKey<T> key,
                           T obj)
Description copied from interface: AssetCache
Adds an asset to the cache. Once added, it should be possible to retrieve the asset by using the AssetCache.getFromCache(com.jme3.asset.AssetKey) method. However the caching criteria may at some point choose that the asset should be removed from the cache to save memory, in that case, AssetCache.getFromCache(com.jme3.asset.AssetKey) will return null.

Thread-Safe

Specified by:
addToCache in interface AssetCache
Type Parameters:
T - The type of the asset to cache.
Parameters:
key - The asset key that can be used to look up the asset.
obj - The asset data to cache.

getFromCache

public <T> T getFromCache(AssetKey<T> key)
Description copied from interface: AssetCache
Retrieves an asset from the cache. It is possible to add an asset to the cache using AssetCache.addToCache(com.jme3.asset.AssetKey, java.lang.Object). The asset may be removed from the cache automatically even if it was added previously, in that case, this method will return null.

Thread-Safe

Specified by:
getFromCache in interface AssetCache
Type Parameters:
T - The type of the asset to retrieve
Parameters:
key - The key used to lookup the asset.
Returns:
The asset that was previously cached, or null if not found.

deleteFromCache

public boolean deleteFromCache(AssetKey key)
Description copied from interface: AssetCache
Deletes an asset from the cache.

Thread-Safe

Specified by:
deleteFromCache in interface AssetCache
Parameters:
key - The asset key to find the asset to delete.
Returns:
True if the asset was successfully found in the cache and removed.

clearCache

public void clearCache()
Description copied from interface: AssetCache
Deletes all assets from the cache.

Thread-Safe

Specified by:
clearCache in interface AssetCache

registerAssetClone

public <T> void registerAssetClone(AssetKey<T> key,
                                   T clone)
Description copied from interface: AssetCache
This should be called by the asset manager when it has successfully acquired a cached asset (with AssetCache.getFromCache(com.jme3.asset.AssetKey)) and cloned it for use.

Thread-Safe

Specified by:
registerAssetClone in interface AssetCache
Type Parameters:
T - The type of the asset to register.
Parameters:
key - The asset key of the loaded asset (used to retrieve from cache)
clone - The clone of the asset retrieved from the cache.

notifyNoAssetClone

public void notifyNoAssetClone()
Description copied from interface: AssetCache
Notifies the cache that even though the methods AssetCache.addToCache(com.jme3.asset.AssetKey, java.lang.Object) or AssetCache.getFromCache(com.jme3.asset.AssetKey) were used, there won't be a call to AssetCache.registerAssetClone(com.jme3.asset.AssetKey, java.lang.Object) for some reason. For example, if an error occurred during loading or if the addToCache/getFromCache were used from user code.

Specified by:
notifyNoAssetClone in interface AssetCache