Top Description Inners Fields Constructors Methods
org.glassfish.hk2.utilities.cache

public Class Cache<K, V>

extends Object
implements Computable<K, V>
Class Inheritance
All Implemented Interfaces
org.glassfish.hk2.utilities.cache.Computable
Type Parameters
<K>
The type of the key of the cache
<V>
The type of the values in the cache
Imports
java.util.concurrent.Callable, .ConcurrentHashMap, .ExecutionException, .Future, .FutureTask, .TimeUnit, .TimeoutException

Cache implementation that relies on FutureTask. Desired value will only be computed once and computed value stored in the cache. The implementation is based on an example from the "Java Concurrency in Practice" book authored by Brian Goetz and company.
Author
Jakub Podlesak (jakub.podlesak at oracle.com)

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
Cache.CycleHandler<
Key type.
K
>

Should a cycle be detected during computation of a value for given key, this interface allows client code to register a callback that would get invoked in such a case.

private class
Cache.OriginThreadAwareFuture

Helper class, that remembers the future task origin thread, so that cycles could be detected.

Field Summary

Modifier and TypeField and Description
private final ConcurrentHashMap<K, Cache<K, V>.OriginThreadAwareFuture>
private final Computable<K, V>
private final Cache.CycleHandler<K>
private static final Cache.CycleHandler<Object>

Constructor Summary

AccessConstructor and Description
public
Cache(Computable<K, V> computable)

Create new cache with given computable to compute values.

public
Cache(Computable<K, V> computable, Cache.CycleHandler<K> cycleHandler)

Create new cache with given computable and cycle handler.

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Empty cache.

public V
compute(final K key)

Implements org.glassfish.hk2.utilities.cache.Computable.compute.

Defines an expensive computation to retrieve value V from key K.

public boolean

Returns:

true if given key is present in the cache.
containsKey
(final K key)

Returns true if the key has already been cached.

public void
remove(final K
item key.
key
)

Remove item from the cache.

public int

Returns:

The number of elements in the cache
size
()

Returns the size of the cache

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cacheback to summary
private final ConcurrentHashMap<K, Cache<K, V>.OriginThreadAwareFuture> cache
computableback to summary
private final Computable<K, V> computable
cycleHandlerback to summary
private final Cache.CycleHandler<K> cycleHandler
EMPTY_HANDLERback to summary
private static final Cache.CycleHandler<Object> EMPTY_HANDLER

Constructor Detail

Cacheback to summary
public Cache(Computable<K, V> computable)

Create new cache with given computable to compute values. Detected cycles will be ignored as there is a no-op cycle handler registered by default.

Annotations
@SuppressWarnings:unchecked
Cacheback to summary
public Cache(Computable<K, V> computable, Cache.CycleHandler<K> cycleHandler)

Create new cache with given computable and cycle handler.

Method Detail

clearback to summary
public void clear()

Empty cache.

computeback to summary
public V compute(final K key)

Implements org.glassfish.hk2.utilities.cache.Computable.compute.

Doc from org.glassfish.hk2.utilities.cache.Computable.compute.

Defines an expensive computation to retrieve value V from key K.

Annotations
@Override
containsKeyback to summary
public boolean containsKey(final K key)

Returns true if the key has already been cached.

Returns:boolean

true if given key is present in the cache.

removeback to summary
public void remove(final K key)

Remove item from the cache.

Parameters
key:K

item key.

sizeback to summary
public int size()

Returns the size of the cache

Returns:int

The number of elements in the cache

org.glassfish.hk2.utilities.cache back to summary

public Interface Cache.CycleHandler<K>

Type Parameters
<K>
Key type.

Should a cycle be detected during computation of a value for given key, this interface allows client code to register a callback that would get invoked in such a case.

Method Summary

Modifier and TypeMethod and Description
public void
handleCycle(K
instance that caused the cycle.
key
)

Handle cycle that was detected while computing a cache value for given key.

Method Detail

handleCycleback to summary
public void handleCycle(K key)

Handle cycle that was detected while computing a cache value for given key. This method would typically just throw a runtime exception.

Parameters
key:K

instance that caused the cycle.

org.glassfish.hk2.utilities.cache back to summary

private Class Cache.OriginThreadAwareFuture

extends Object
implements Future<V>
Class Inheritance
  • java.lang.Object
  • org.glassfish.hk2.utilities.cache.Cache.OriginThreadAwareFuture
All Implemented Interfaces
java.util.concurrent.Future

Helper class, that remembers the future task origin thread, so that cycles could be detected. If any thread starts computation for given key and the same thread requests the computed value before the computation stops, a cycle is detected and registered cycle handler is called.

Field Summary

Modifier and TypeField and Description
private final FutureTask<V>
private volatile long

Constructor Summary

AccessConstructor and Description
pack-priv
OriginThreadAwareFuture(Cache<K, V> cache, final K key)

Method Summary

Modifier and TypeMethod and Description
public boolean
cancel(boolean mayInterruptIfRunning)

Implements java.util.concurrent.Future.cancel.

Attempts to cancel execution of this task.

public boolean
equals(Object obj)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public V
get()

Implements java.util.concurrent.Future.get.

Waits if necessary for the computation to complete, and then retrieves its result.

public V
get(long timeout, TimeUnit unit)

Implements java.util.concurrent.Future.get.

Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public boolean
isCancelled()

Implements java.util.concurrent.Future.isCancelled.

Returns true if this task was cancelled before it completed normally.

public boolean
isDone()

Implements java.util.concurrent.Future.isDone.

Returns true if this task completed.

public void
run()

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

futureback to summary
private final FutureTask<V> future
threadIdback to summary
private volatile long threadId

Constructor Detail

OriginThreadAwareFutureback to summary
pack-priv OriginThreadAwareFuture(Cache<K, V> cache, final K key)

Method Detail

cancelback to summary
public boolean cancel(boolean mayInterruptIfRunning)

Implements java.util.concurrent.Future.cancel.

Doc from java.util.concurrent.Future.cancel.

Attempts to cancel execution of this task. This method has no effect if the task is already completed or cancelled, or could not be cancelled for some other reason. Otherwise, if this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task (when known by the implementation) is interrupted in an attempt to stop the task.

The return value from this method does not necessarily indicate whether the task is now cancelled; use isCancelled.

Annotations
@Override
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Annotations
@SuppressWarnings:unchecked
@Override
getback to summary
public V get() throws InterruptedException, ExecutionException

Implements java.util.concurrent.Future.get.

Doc from java.util.concurrent.Future.get.

Waits if necessary for the computation to complete, and then retrieves its result.

Annotations
@Override
Exceptions
InterruptedException:
if the current thread was interrupted while waiting
ExecutionException:
if the computation threw an exception
getback to summary
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException

Implements java.util.concurrent.Future.get.

Doc from java.util.concurrent.Future.get.

Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.

Annotations
@Override
Exceptions
InterruptedException:
if the current thread was interrupted while waiting
ExecutionException:
if the computation threw an exception
TimeoutException:
if the wait timed out
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Annotations
@Override
isCancelledback to summary
public boolean isCancelled()

Implements java.util.concurrent.Future.isCancelled.

Doc from java.util.concurrent.Future.isCancelled.

Returns true if this task was cancelled before it completed normally.

Annotations
@Override
isDoneback to summary
public boolean isDone()

Implements java.util.concurrent.Future.isDone.

Doc from java.util.concurrent.Future.isDone.

Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true.

Annotations
@Override
runback to summary
public void run()