Top Description Inners Fields Constructors Methods
nonapi.io.github.classgraph.concurrency

public abstract Class SingletonMap<K, V, E extends Exception>

extends Object
Class Inheritance
Type Parameters
<K>
The key type.
<V>
The value type.
<E>
the element type
Imports
java.util.AbstractMap.SimpleEntry, .ArrayList, .List, .Map.Entry, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap, .CountDownLatch, nonapi.io.github.classgraph.utils.LogNode

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 and Inner Type Summary

Modifier and TypeClass and Description
public static class
public static interface
SingletonMap.NewInstanceFactory<
The instance type.
V
, E extends Exception>

Create a new instance.

public static class
private static class
SingletonMap.SingletonHolder<
the singleton type
V
>

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

Modifier and TypeField and Description
private final ConcurrentMap<K, SingletonMap.SingletonHolder<V>>
map

The map.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Clear the map.

public List<Map.Entry<K, V>>

Returns:

the map entries.
entries
()

Get the map entries.

public V

Returns:

The non-null singleton instance, if 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.
get
(final K
The key for the singleton.
key
,
final LogNode
The log.
log
,
final SingletonMap.NewInstanceFactory<V, E>
if non-null, a factory for creating new instances, otherwise if null, then newInstance(Object, LogNode) is called instead (this allows new instance creation to be overridden on a per-instance basis).
newInstanceFactory
)

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.

public V

Returns:

The non-null singleton instance, if 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.
get
(final K
The key for the singleton.
key
,
final LogNode
The log.
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.

public boolean

Returns:

true, if the map is empty
isEmpty
()

Returns true if the map is empty.

public abstract V

Returns:

The singleton instance. This method must either return a non-null value, or throw an exception of type E.
newInstance
(K
The key for the singleton.
key
,
LogNode
The log.
log
)

Construct a new singleton instance.

public V

Returns:

the old singleton from the map, if one was present, otherwise null.
remove
(final K
the key
key
)

Remove the singleton for a given key.

public List<V>

Returns:

the singleton values in the map, skipping over any value for which newInstance() threw an exception or returned null.
values
()

Get all valid singleton values in the map.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

mapback to summary
private final ConcurrentMap<K, SingletonMap.SingletonHolder<V>> map

The map.

Constructor Detail

SingletonMapback to summary
public SingletonMap()

Method Detail

clearback to summary
public void clear()

Clear the map.

entriesback to summary
public List<Map.Entry<K, V>> entries() throws InterruptedException

Get the map entries.

Returns:List<Map.Entry<K, V>>

the map entries.

Exceptions
InterruptedException:
if interrupted.
getback to summary
public V get(final K key, final LogNode log, final SingletonMap.NewInstanceFactory<V, E> newInstanceFactory) throws SingletonMap:E, InterruptedException, NullSingletonException, NewInstanceException

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. 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.

Parameters
key:K

The key for the singleton.

log:LogNode

The log.

newInstanceFactory:SingletonMap.NewInstanceFactory<V, E>

if non-null, a factory for creating new instances, otherwise if null, then newInstance(Object, LogNode) is called instead (this allows new instance creation to be overridden on a per-instance basis).

Returns:V

The non-null singleton instance, if 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.

Exceptions
SingletonMap:E:
If newInstance(Object, LogNode) threw an exception.
InterruptedException:
if the thread was interrupted while waiting for the singleton to be instantiated by another thread.
NullSingletonException:
if newInstance(Object, LogNode) returned null.
NewInstanceException:
if newInstance(Object, LogNode) threw an exception.
getback to summary
public V get(final K key, final LogNode log) throws SingletonMap:E, InterruptedException, NullSingletonException, NewInstanceException

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. 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.

Parameters
key:K

The key for the singleton.

log:LogNode

The log.

Returns:V

The non-null singleton instance, if 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.

Exceptions
SingletonMap:E:
If newInstance(Object, LogNode) threw an exception.
InterruptedException:
if the thread was interrupted while waiting for the singleton to be instantiated by another thread.
NullSingletonException:
if newInstance(Object, LogNode) returned null.
NewInstanceException:
if newInstance(Object, LogNode) threw an exception.
isEmptyback to summary
public boolean isEmpty()

Returns true if the map is empty.

Returns:boolean

true, if the map is empty

newInstanceback to summary
public abstract V newInstance(K key, LogNode log) throws SingletonMap:E, InterruptedException

Construct a new singleton instance.

Parameters
key:K

The key for the singleton.

log:LogNode

The log.

Returns:V

The singleton instance. This method must either return a non-null value, or throw an exception of type E.

Exceptions
SingletonMap:E:
If something goes wrong while instantiating the new object instance.
InterruptedException:
if the thread was interrupted while instantiating the singleton.
removeback to summary
public V remove(final K key) throws InterruptedException

Remove the singleton for a given key.

Parameters
key:K

the key

Returns:V

the old singleton from the map, if one was present, otherwise null.

Annotations
@SuppressWarnings:null
Exceptions
InterruptedException:
if interrupted.
valuesback to summary
public List<V> values() throws InterruptedException

Get all valid singleton values in the map.

Returns:List<V>

the singleton values in the map, skipping over any value for which newInstance() threw an exception or returned null.

Exceptions
InterruptedException:
If getting the values was interrupted.
nonapi.io.github.classgraph.concurrency back to summary

public Class SingletonMap.NewInstanceException

extends Exception
Class Inheritance

Thrown when SingletonMap#newInstance(Object, LogNode) throws an exception.

Field Summary

Modifier and TypeField and Description
pack-priv static final long

Constructor Summary

AccessConstructor and Description
public <
the key type
K
>
NewInstanceException(final K
the key
key
,
final Throwable
the Throwable that was thrown
t
)

Constructor.

Field Detail

serialVersionUIDback to summary
pack-priv static final long serialVersionUID

Hides java.lang.Exception.serialVersionUID.

serialVersionUID.

Constructor Detail

NewInstanceExceptionback to summary
public <K> NewInstanceException(final K key, final Throwable t)

Constructor.

Parameters
<K>
the key type
key:K

the key

t:Throwable

the Throwable that was thrown

nonapi.io.github.classgraph.concurrency back to summary

public Interface SingletonMap.NewInstanceFactory<V, E extends Exception>

Annotations
@FunctionalInterface
Type Parameters
<V>
The instance type.

Create a new instance.

Method Summary

Modifier and TypeMethod and Description
public V

Returns:

The new instance.
newInstance
()

Create a new instance.

Method Detail

newInstanceback to summary
public V newInstance() throws SingletonMap:E, InterruptedException

Create a new instance.

Returns:V

The new instance.

nonapi.io.github.classgraph.concurrency back to summary

public Class SingletonMap.NullSingletonException

extends Exception
Class Inheritance

Thrown when SingletonMap#newInstance(Object, LogNode) returns null.

Field Summary

Modifier and TypeField and Description
pack-priv static final long

Constructor Summary

AccessConstructor and Description
public <
the key type
K
>
NullSingletonException(final K
the key
key
)

Constructor.

Field Detail

serialVersionUIDback to summary
pack-priv static final long serialVersionUID

Hides java.lang.Exception.serialVersionUID.

serialVersionUID.

Constructor Detail

NullSingletonExceptionback to summary
public <K> NullSingletonException(final K key)

Constructor.

Parameters
<K>
the key type
key:K

the key

nonapi.io.github.classgraph.concurrency back to summary

private Class SingletonMap.SingletonHolder<V>

extends Object
Class Inheritance
  • java.lang.Object
  • nonapi.io.github.classgraph.concurrency.SingletonMap.SingletonHolder
Type Parameters
<V>
the singleton type

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

Modifier and TypeField and Description
private final CountDownLatch
initialized

Whether or not the singleton has been initialized (the count will have reached 0 if so).

private volatile V
singleton

The singleton.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
pack-priv V

Returns:

the singleton value.
get
()

Get the singleton value.

pack-priv void
set(final V
the singleton
singleton
)

Set the singleton value, and decreases the countdown latch to 0.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

initializedback to summary
private final CountDownLatch initialized

Whether or not the singleton has been initialized (the count will have reached 0 if so).

singletonback to summary
private volatile V singleton

The singleton.

Annotations
@SuppressWarnings:null

Constructor Detail

SingletonHolderback to summary
private SingletonHolder()

Method Detail

getback to summary
pack-priv V get() throws InterruptedException

Get the singleton value.

Returns:V

the singleton value.

Exceptions
InterruptedException:
if the thread was interrupted while waiting for the value to be set.
setback to summary
pack-priv void set(final V singleton) throws IllegalArgumentException

Set the singleton value, and decreases the countdown latch to 0.

Parameters
singleton:V

the singleton

Exceptions
IllegalArgumentException:
if this method is called more than once (indicating an internal inconsistency).