public class ConcurrentUtils extends Object
An utility class providing functionality related to the java.util.concurrent
package.
Modifier and Type | Method and Description |
---|---|
static <T> Future<T> |
constantFuture(T value)
Gets an implementation of
Future that is immediately done
and returns the specified constant value. |
static <K,V> V |
createIfAbsent(ConcurrentMap<K,V> map,
K key,
ConcurrentInitializer<V> init)
Checks if a concurrent map contains a key and creates a corresponding
value if not.
|
static <K,V> V |
createIfAbsentUnchecked(ConcurrentMap<K,V> map,
K key,
ConcurrentInitializer<V> init)
Checks if a concurrent map contains a key and creates a corresponding
value if not, suppressing checked exceptions.
|
static ConcurrentException |
extractCause(ExecutionException ex)
Inspects the cause of the specified
ExecutionException and
creates a ConcurrentException with the checked cause if
necessary. |
static ConcurrentRuntimeException |
extractCauseUnchecked(ExecutionException ex)
Inspects the cause of the specified
ExecutionException and
creates a ConcurrentRuntimeException with the checked cause if
necessary. |
static void |
handleCause(ExecutionException ex)
Handles the specified
ExecutionException . |
static void |
handleCauseUnchecked(ExecutionException ex)
Handles the specified
ExecutionException and transforms it into a
runtime exception. |
static <T> T |
initialize(ConcurrentInitializer<T> initializer)
Invokes the specified
ConcurrentInitializer and returns the
object produced by the initializer. |
static <T> T |
initializeUnchecked(ConcurrentInitializer<T> initializer)
Invokes the specified
ConcurrentInitializer and transforms
occurring exceptions to runtime exceptions. |
static <K,V> V |
putIfAbsent(ConcurrentMap<K,V> map,
K key,
V value)
Puts a value in the specified
ConcurrentMap if the key is not yet
present. |
public static ConcurrentException extractCause(ExecutionException ex)
ExecutionException
and
creates a ConcurrentException
with the checked cause if
necessary. This method performs the following checks on the cause of the
passed in exception:
ConcurrentException
, initializes it with the cause, and
returns it.ex
- the exception to be processedConcurrentException
with the checked causepublic static ConcurrentRuntimeException extractCauseUnchecked(ExecutionException ex)
ExecutionException
and
creates a ConcurrentRuntimeException
with the checked cause if
necessary. This method works exactly like
extractCause(ExecutionException)
. The only difference is that
the cause of the specified ExecutionException
is extracted as a
runtime exception. This is an alternative for client code that does not
want to deal with checked exceptions.ex
- the exception to be processedConcurrentRuntimeException
with the checked causepublic static void handleCause(ExecutionException ex) throws ConcurrentException
ExecutionException
. This method calls
extractCause(ExecutionException)
for obtaining the cause of the
exception - which might already cause an unchecked exception or an error
being thrown. If the cause is a checked exception however, it is wrapped
in a ConcurrentException
, which is thrown. If the passed in
exception is null or has no cause, the method simply returns
without throwing an exception.ex
- the exception to be handledConcurrentException
- if the cause of the ExecutionException
is a checked exceptionpublic static void handleCauseUnchecked(ExecutionException ex)
ExecutionException
and transforms it into a
runtime exception. This method works exactly like
handleCause(ExecutionException)
, but instead of a
ConcurrentException
it throws a
ConcurrentRuntimeException
. This is an alternative for client
code that does not want to deal with checked exceptions.ex
- the exception to be handledConcurrentRuntimeException
- if the cause of the ExecutionException
is a checked exception; this exception is then
wrapped in the thrown runtime exceptionpublic static <T> T initialize(ConcurrentInitializer<T> initializer) throws ConcurrentException
ConcurrentInitializer
and returns the
object produced by the initializer. This method just invokes the get()
method of the given ConcurrentInitializer
. It is
null-safe: if the argument is null, result is also
null.T
- the type of the object produced by the initializerinitializer
- the ConcurrentInitializer
to be invokedConcurrentInitializer
ConcurrentException
- if the ConcurrentInitializer
throws
an exceptionpublic static <T> T initializeUnchecked(ConcurrentInitializer<T> initializer)
ConcurrentInitializer
and transforms
occurring exceptions to runtime exceptions. This method works like
initialize(ConcurrentInitializer)
, but if the ConcurrentInitializer
throws a ConcurrentException
, it is
caught, and the cause is wrapped in a ConcurrentRuntimeException
.
So client code does not have to deal with checked exceptions.T
- the type of the object produced by the initializerinitializer
- the ConcurrentInitializer
to be invokedConcurrentInitializer
ConcurrentRuntimeException
- if the initializer throws an exceptionpublic static <K,V> V putIfAbsent(ConcurrentMap<K,V> map, K key, V value)
Puts a value in the specified ConcurrentMap
if the key is not yet
present. This method works similar to the putIfAbsent()
method of
the ConcurrentMap
interface, but the value returned is different.
Basically, this method is equivalent to the following code fragment:
if (!map.containsKey(key)) { map.put(key, value); return value; } else { return map.get(key); }
except that the action is performed atomically. So this method always returns the value which is stored in the map.
This method is null-safe: It accepts a null map as input without throwing an exception. In this case the return value is null, too.
K
- the type of the keys of the mapV
- the type of the values of the mapmap
- the map to be modifiedkey
- the key of the value to be addedvalue
- the value to be addedpublic static <K,V> V createIfAbsent(ConcurrentMap<K,V> map, K key, ConcurrentInitializer<V> init) throws ConcurrentException
get()
method of the passed in ConcurrentInitializer
is called. With the resulting object
putIfAbsent(ConcurrentMap, Object, Object)
is called. This
handles the case that in the meantime another thread has added the key to
the map. Both the map and the initializer can be null; in this
case this method simply returns null.K
- the type of the keys of the mapV
- the type of the values of the mapmap
- the map to be modifiedkey
- the key of the value to be addedinit
- the ConcurrentInitializer
for creating the valueConcurrentInitializer
ConcurrentException
- if the initializer throws an exceptionpublic static <K,V> V createIfAbsentUnchecked(ConcurrentMap<K,V> map, K key, ConcurrentInitializer<V> init)
createIfAbsent()
. If a ConcurrentException
is thrown, it
is caught and re-thrown as a ConcurrentRuntimeException
.K
- the type of the keys of the mapV
- the type of the values of the mapmap
- the map to be modifiedkey
- the key of the value to be addedinit
- the ConcurrentInitializer
for creating the valueConcurrentInitializer
ConcurrentRuntimeException
- if the initializer throws an exceptionpublic static <T> Future<T> constantFuture(T value)
Gets an implementation of Future
that is immediately done
and returns the specified constant value.
This can be useful to return a simple constant immediately from the concurrent processing, perhaps as part of avoiding nulls. A constant future can also be useful in testing.
T
- the type of the value used by this Future
objectvalue
- the constant value to return, may be nullCopyright © 2001–2019 The Apache Software Foundation. All rights reserved.