Class SingletonMap<K,V>
- java.lang.Object
-
- io.github.lukehutch.fastclasspathscanner.utils.SingletonMap<K,V>
-
public abstract class SingletonMap<K,V> extends Object
A map from keys to singleton instances.
-
-
Constructor Summary
Constructors Constructor Description SingletonMap()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
clear()
Clear the map.boolean
createSingleton(K key, LogNode log)
Initialize a value object for this key and return true, if this is the first time this key has been seen, otherwise return false.V
get(K key)
Get the singleton for a given key.V
getOrCreateSingleton(K key, LogNode log)
Check if the given key is in the map, and if so, return it.abstract V
newInstance(K key, LogNode log)
Construct a new singleton instance.List<V>
values()
Get all singletons in the map.
-
-
-
Method Detail
-
createSingleton
public boolean createSingleton(K key, LogNode log) throws Exception
Initialize a value object for this key and return true, if this is the first time this key has been seen, otherwise return false.- Throws:
Exception
- if newInstance(key) throws an exception.IllegalArgumentException
- if newInstance(key) returns null.
-
getOrCreateSingleton
public V getOrCreateSingleton(K key, LogNode log) throws Exception
Check if the given key is in the map, and if so, return it. If not, create a singleton value for that key, and return the created value.- Throws:
Exception
- if newInstance(key) throws an exception.IllegalArgumentException
- if newInstance(key) returns null.
-
newInstance
public abstract V newInstance(K key, LogNode log) throws Exception
Construct a new singleton instance.- Throws:
Exception
-
get
public V get(K key) throws InterruptedException
Get the singleton for a given key.- Returns:
- the new singleton instance, initialized by calling newInstance, or null if createSingleton() or getOrCreateSingleton() has not yet been called. Also returns null if newInstance() threw an exception or returned null while calling either of these methods.
- Throws:
InterruptedException
-
values
public List<V> values() throws InterruptedException
Get all singletons in the map.- Throws:
InterruptedException
-
clear
public void clear()
Clear the map.
-
-