Class SingletonMap<K,V,E extends Exception>
java.lang.Object
nonapi.io.github.classgraph.concurrency.SingletonMap<K,V,E>
- Type Parameters:
K
- The key type.V
- The value type.E
- the element type
A map from keys to singleton instances. Allows you to create object instance singletons and add them to a
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.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Thrown whennewInstance(Object, LogNode)
throws an exception.static interface
SingletonMap.NewInstanceFactory<V,
E extends Exception> Create a new instance.static class
Thrown whennewInstance(Object, LogNode)
returns null.private static class
Wrapper to allow an object instance to be put into a ConcurrentHashMap using putIfAbsent() without requiring the instance to be initialized first, so that putIfAbsent can be performed without wrapping it with a synchronized lock, and so that initialization work is not wasted if an object is already in the map for the key. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ConcurrentMap
<K, SingletonMap.SingletonHolder<V>> The map. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clear the map.entries()
Get the map entries.Check if the given key is in the map, and if so, return the value ofnewInstance(Object, LogNode)
for that key, or block on the result ofnewInstance(Object, LogNode)
if another thread is currently creating the new instance.Check if the given key is in the map, and if so, return the value ofnewInstance(Object, LogNode)
for that key, or block on the result ofnewInstance(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.Remove the singleton for a given key.values()
Get all valid singleton values in the map.
-
Field Details
-
map
The map.
-
-
Constructor Details
-
SingletonMap
public SingletonMap()
-
-
Method Details
-
newInstance
Construct a new singleton instance.- Parameters:
key
- The key for the singleton.log
- The log.- Returns:
- The singleton instance. This method must either return a non-null value, or throw an exception of type E.
- Throws:
E
- If something goes wrong while instantiating the new object instance.InterruptedException
- if the thread was interrupted while instantiating the singleton.
-
get
public V get(K key, LogNode log, SingletonMap.NewInstanceFactory<V, E> newInstanceFactory) throws E, InterruptedException, SingletonMap.NullSingletonException, SingletonMap.NewInstanceExceptionCheck if the given key is in the map, and if so, return the value ofnewInstance(Object, LogNode)
for that key, or block on the result ofnewInstance(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 runnewInstance(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.- Parameters:
key
- The key for the singleton.log
- The log.newInstanceFactory
- if non-null, a factory for creating new instances, otherwise if null, thennewInstance(Object, LogNode)
is called instead (this allows new instance creation to be overridden on a per-instance basis).- Returns:
- The non-null singleton instance, if
newInstance(Object, LogNode)
returned a non-null instance on this call or a previous call, otherwise throwsNullPointerException
if this call or a previous call tonewInstance(Object, LogNode)
returned null. - Throws:
E
- IfnewInstance(Object, LogNode)
threw an exception.InterruptedException
- if the thread was interrupted while waiting for the singleton to be instantiated by another thread.SingletonMap.NullSingletonException
- ifnewInstance(Object, LogNode)
returned null.SingletonMap.NewInstanceException
- ifnewInstance(Object, LogNode)
threw an exception.
-
get
public V get(K key, LogNode log) throws E, InterruptedException, SingletonMap.NullSingletonException, SingletonMap.NewInstanceException Check if the given key is in the map, and if so, return the value ofnewInstance(Object, LogNode)
for that key, or block on the result ofnewInstance(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 runnewInstance(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.- Parameters:
key
- The key for the singleton.log
- The log.- Returns:
- The non-null singleton instance, if
newInstance(Object, LogNode)
returned a non-null instance on this call or a previous call, otherwise throwsNullPointerException
if this call or a previous call tonewInstance(Object, LogNode)
returned null. - Throws:
E
- IfnewInstance(Object, LogNode)
threw an exception.InterruptedException
- if the thread was interrupted while waiting for the singleton to be instantiated by another thread.SingletonMap.NullSingletonException
- ifnewInstance(Object, LogNode)
returned null.SingletonMap.NewInstanceException
- ifnewInstance(Object, LogNode)
threw an exception.
-
values
Get all valid singleton values in the map.- Returns:
- the singleton values in the map, skipping over any value for which newInstance() threw an exception or returned null.
- Throws:
InterruptedException
- If getting the values was interrupted.
-
isEmpty
public boolean isEmpty()Returns true if the map is empty.- Returns:
- true, if the map is empty
-
entries
Get the map entries.- Returns:
- the map entries.
- Throws:
InterruptedException
- if interrupted.
-
remove
Remove the singleton for a given key.- Parameters:
key
- the key- Returns:
- the old singleton from the map, if one was present, otherwise null.
- Throws:
InterruptedException
- if interrupted.
-
clear
public void clear()Clear the map.
-