Package org.apache.sis.util.collection
Interface Cache.Handler<V>
-
- Type Parameters:
V- the type of value objects.
public static interface Cache.Handler<V>The handler returned byCache.lock(K), to be used for unlocking and storing the result. This handler should be used as below (thetry…finallystatements are important):
See theValue V = null; Cache.Handler<V> handler = cache.lock(key); try { value = handler.peek(); if (value == null) { value = createMyObject(key); } } finally { handler.putAndUnlock(value); }Cachejavadoc for a more complete example.- Since:
- 0.3
Defined in the
sis-utilitymodule
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Vpeek()If the value is already in the cache, returns it.voidputAndUnlock(V result)Stores the given value in the cache and release the lock.
-
-
-
Method Detail
-
peek
V peek()
If the value is already in the cache, returns it. Otherwise returnsnull. This method should be invoked after theHandlercreation in case a value has been computed in an other thread.- Returns:
- the value from the cache, or
nullif none.
-
putAndUnlock
void putAndUnlock(V result) throws IllegalStateException
Stores the given value in the cache and release the lock. This method must be invoked in afinallyblock, no matter what the result is.- Parameters:
result- the result to store in the cache, ornullfor removing the entry from the cache. If an entry is removed, a new computation will be attempted the next time a handler is created for the same key.- Throws:
IllegalStateException- may be thrown if this method is not invoked in the pattern described in class javadoc, or if a key collision occurs.
-
-