com.google.common.collect
Interface CustomConcurrentHashMap.ComputingStrategy<K,V,E>

All Superinterfaces:
CustomConcurrentHashMap.Strategy<K,V,E>
Enclosing class:
CustomConcurrentHashMap

public static interface CustomConcurrentHashMap.ComputingStrategy<K,V,E>
extends CustomConcurrentHashMap.Strategy<K,V,E>

Extends CustomConcurrentHashMap.Strategy to add support for computing values on-demand. Implementations should typically intialize the entry's value to a placeholder value in CustomConcurrentHashMap.Strategy.newEntry(Object, int, Object) so that waitForValue(Object) can tell the difference between a pre-intialized value and an in-progress computation. CustomConcurrentHashMap.Strategy.copyEntry(Object, Object, Object) must detect and handle the case where an entry is copied in the middle of a computation. Implementations can throw UnsupportedOperationException in CustomConcurrentHashMap.Strategy.setValue(Object, Object) if they wish to prevent users from setting values directly.

See Also:
CustomConcurrentHashMap.Builder.buildComputingMap(CustomConcurrentHashMap.ComputingStrategy, Function)

Method Summary
 V compute(K key, E entry, Function<? super K,? extends V> computer)
          Computes a value for the given key and stores it in the given entry.
 V waitForValue(E entry)
          Gets a value from an entry waiting for the value to be set by compute(K, E, com.google.common.base.Function) if necessary.
 
Methods inherited from interface com.google.common.collect.CustomConcurrentHashMap.Strategy
copyEntry, equalKeys, equalValues, getHash, getKey, getNext, getValue, hashKey, newEntry, setInternals, setValue
 

Method Detail

compute

V compute(K key,
          E entry,
          Function<? super K,? extends V> computer)
Computes a value for the given key and stores it in the given entry. Called as a result of Map.get(java.lang.Object). If this method throws an exception, CustomConcurrentHashMap will remove the entry and retry the computation on subsequent requests.

Parameters:
entry - that was created
computer - passed to CustomConcurrentHashMap.Builder.buildMap(com.google.common.collect.CustomConcurrentHashMap.Strategy)
Returns:
the computed value
Throws:
ComputationException - if the computation threw an exception
java.lang.NullPointerException - if the computer returned null

waitForValue

V waitForValue(E entry)
               throws java.lang.InterruptedException
Gets a value from an entry waiting for the value to be set by compute(K, E, com.google.common.base.Function) if necessary. Returns null if a value isn't available at which point CustomConcurrentHashMap tries to compute a new value.

Parameters:
entry - to return value from
Returns:
stored value or null if the value isn't available
Throws:
java.lang.InterruptedException - if the thread was interrupted while waiting


Copyright © 2007-2009 Google. All Rights Reserved.