| Method from com.opensymphony.xwork2.inject.util.ReferenceCache Detail: |
protected void cancel() {
Future< V > future = localFuture.get();
if (future == null) {
throw new IllegalStateException("Not in create().");
}
future.cancel(false);
}
|
abstract protected V create(K key)
Override to lazy load values. Use as an alternative to #put(Object,Object) . Invoked by getter if value isn't already cached.
Must not return {@code null}. This method will not be called again until
the garbage collector reclaims the returned value. |
public V get(Object key) {
V value = super.get(key);
return (value == null)
? internalCreate((K) key)
: value;
}
{@inheritDoc}
If this map does not contain an entry for the given key and #create(Object) has been overridden, this method will create a new
value, put it in the map, and return it. |
V internalCreate(K key) {
try {
FutureTask< V > futureTask = new FutureTask< V >(
new CallableCreate(key));
// use a reference so we get the same equality semantics.
Object keyReference = referenceKey(key);
Future< V > future = futures.putIfAbsent(keyReference, futureTask);
if (future == null) {
// winning thread.
try {
if (localFuture.get() != null) {
throw new IllegalStateException(
"Nested creations within the same cache are not allowed.");
}
localFuture.set(futureTask);
futureTask.run();
V value = futureTask.get();
putStrategy().execute(this,
keyReference, referenceValue(keyReference, value));
return value;
} finally {
localFuture.remove();
futures.remove(keyReference);
}
} else {
// wait for winning thread.
return future.get();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else if (cause instanceof Error) {
throw (Error) cause;
}
throw new RuntimeException(cause);
}
}
|
public static ReferenceCache<K, V> of(ReferenceType keyReferenceType,
ReferenceType valueReferenceType,
Function<? super K, ? extends V> function) {
ensureNotNull(function);
return new ReferenceCache< K, V >(keyReferenceType, valueReferenceType) {
@Override
protected V create(K key) {
return function.apply(key);
}
private static final long serialVersionUID = 0;
};
}
Returns a {@code ReferenceCache} delegating to the specified {@code
function}. The specified function must not return {@code null}. |