K - The key type.V - The value type.E - the element typepublic abstract class SingletonMap<K,V,E extends java.lang.Exception>
extends java.lang.Object
ConcurrentMap on demand, based on a key value. Works the same as
concurrentMap.computeIfAbsent(key, key -> newInstance(key)), except that it also works on JDK 7.| Modifier and Type | Class and Description |
|---|---|
static class |
SingletonMap.NewInstanceException
Thrown when
newInstance(Object, LogNode) throws an exception. |
static class |
SingletonMap.NullSingletonException
Thrown when
newInstance(Object, LogNode) returns null. |
| Constructor and Description |
|---|
SingletonMap() |
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Clear the map.
|
java.util.List<java.util.Map.Entry<K,V>> |
entries()
Get the map entries.
|
V |
get(K key,
LogNode log)
Check if the given key is in the map, and if so, return the value of
newInstance(Object, LogNode)
for that key, or block on the result of newInstance(Object, LogNode) if another thread is currently
creating the new instance. |
boolean |
isEmpty()
Returns true if the map is empty.
|
abstract V |
newInstance(K key,
LogNode log)
Construct a new singleton instance.
|
V |
remove(K key)
Remove the singleton for a given key.
|
java.util.List<V> |
values()
Get all valid singleton values in the map.
|
public abstract V newInstance(K key, LogNode log) throws E extends java.lang.Exception, java.lang.InterruptedException
key - The key for the singleton.log - The log.E - If something goes wrong while instantiating the new object instance.java.lang.InterruptedException - if the thread was interrupted while instantiating the singleton.E extends java.lang.Exceptionpublic V get(K key, LogNode log) throws E extends java.lang.Exception, java.lang.InterruptedException, SingletonMap.NullSingletonException, SingletonMap.NewInstanceException
newInstance(Object, LogNode)
for that key, or block on the result of newInstance(Object, LogNode) if another thread is currently
creating the new instance.
If the given key is not currently in the map, store a placeholder in the map for this key, then run
newInstance(Object, LogNode) for the key, store the result in the placeholder (which unblocks any
other threads waiting for the value), and then return the new instance.key - The key for the singleton.log - The log.newInstance(Object, LogNode) returned a non-null
instance on this call or a previous call, otherwise throws NullPointerException if this call
or a previous call to newInstance(Object, LogNode) returned null.E - If newInstance(Object, LogNode) threw an exception.java.lang.InterruptedException - if the thread was interrupted while waiting for the singleton to be instantiated by another
thread.SingletonMap.NullSingletonException - if newInstance(Object, LogNode) returned null.SingletonMap.NewInstanceException - if newInstance(Object, LogNode) threw an exception.E extends java.lang.Exceptionpublic java.util.List<V> values() throws java.lang.InterruptedException
java.lang.InterruptedException - If getting the values was interrupted.public boolean isEmpty()
public java.util.List<java.util.Map.Entry<K,V>> entries() throws java.lang.InterruptedException
java.lang.InterruptedException - if interrupted.public V remove(K key) throws java.lang.InterruptedException
key - the keyjava.lang.InterruptedException - if interrupted.public void clear()