public class SynchronizationWrapper extends Object implements Synchronization
Constructor and Description |
---|
SynchronizationWrapper(String originator,
TaskId taskId,
SynchronizationInternal internalAPI) |
Modifier and Type | Method and Description |
---|---|
boolean |
channelExists(String name)
Check for channel existence
|
void |
clear(String channel)
Removes all of the mappings from this channel map (optional operation).
|
Serializable |
compute(String channel,
String key,
String remappingFunction)
Attempts to compute a mapping for the specified key and its current
mapped value (or
null if there is no current mapping). |
Serializable |
computeIfAbsent(String channel,
String key,
String mappingFunction)
If the specified key is not already associated with a value (or is mapped
to
null ), attempts to compute its value using the given mapping
function and enters it into this map unless null . |
Serializable |
computeIfPresent(String channel,
String key,
String remappingFunction)
If the value for the specified key is present and non-null, attempts to
compute a new mapping given the key and its current mapped value.
|
PredicateActionResult |
conditionalCompute(String channel,
String key,
String predicate,
String thenRemappingFunction)
Conditional execution on a key based on a predicate result and remapping function.
|
PredicateActionResult |
conditionalCompute(String channel,
String key,
String predicate,
String thenRemappingFunction,
String elseRemappingFunction)
Conditional execution on a key based on a predicate result and two remapping functions.
|
void |
conditionalReplaceAll(String channel,
String predicate,
String thenRemappingFunction)
Replaces each entry's value with the result of invoking the given function on that entry if the given predicate is satisfied.
|
void |
conditionalReplaceAll(String channel,
String predicate,
String thenRemappingFunction,
String elseRemappingFunction)
Replaces each entry's value with the result of invoking the given function on that entry if the given predicate is satisfied.
|
boolean |
containsKey(String channel,
String key)
Returns true if this channel map contains a mapping for the specified
key.
|
boolean |
containsValue(String channel,
Serializable value)
Returns true if this channel map maps one or more keys to the
specified value.
|
boolean |
createChannel(String name,
boolean isPersistent)
Create a channel, overwrite any already existing channel with the same name
|
boolean |
createChannelIfAbsent(String name,
boolean isPersistent)
Create a channel, if no channel with the same name already exists
|
boolean |
deleteChannel(String name)
Delete a channel
|
Set<Map.Entry<String,Serializable>> |
entrySet(String channel)
Returns a
Set view of the mappings contained in this map. |
void |
forEach(String channel,
String action)
Performs the given action for each entry in this channel map until all entries
have been processed or the action throws an exception.
|
Serializable |
get(String channel,
String key)
Returns the value to which the specified key is mapped,
or
null if this channel map contains no mapping for the key. |
Serializable |
getOrDefault(String channel,
String key,
Serializable defaultValue)
Returns the value to which the specified key is mapped, or
defaultValue if this channel map contains no mapping for the key. |
boolean |
isEmpty(String channel)
Returns true if this channel map contains no key-value mappings.
|
Set<String> |
keySet(String channel)
Returns a
Set view of the keys contained in this channel map. |
Serializable |
merge(String channel,
String key,
Serializable value,
String remappingFunction)
If the specified key is not already associated with a value or is
associated with null, associates it with the given non-null value.
|
Serializable |
put(String channel,
String key,
Serializable value)
Associates the specified value with the specified key in this channel map
(optional operation).
|
void |
putAll(String channel,
Map<? extends String,? extends Serializable> m)
Copies all of the mappings from the specified map to this channel map
(optional operation).
|
Serializable |
putIfAbsent(String channel,
String key,
Serializable value)
If the specified key is not already associated with a value (or is mapped
to
null ) associates it with the given value and returns
null , else returns the current value. |
Serializable |
remove(String channel,
String key)
Removes the mapping for a key from this channel map if it is present
(optional operation).
|
boolean |
remove(String channel,
String key,
Serializable value)
Removes the entry for the specified key only if it is currently
mapped to the specified value.
|
Serializable |
replace(String channel,
String key,
Serializable value)
Replaces the entry for the specified key only if it is
currently mapped to some value.
|
boolean |
replace(String channel,
String key,
Serializable oldValue,
Serializable newValue)
Replaces the entry for the specified key only if currently
mapped to the specified value.
|
void |
replaceAll(String channel,
String function)
Replaces each entry's value with the result of invoking the given
function on that entry until all entries have been processed or the
function throws an exception.
|
int |
size(String channel)
Returns the number of key-value mappings in this channel map.
|
Collection<Serializable> |
values(String channel)
Returns a
Collection view of the values contained in this channel map. |
boolean |
waitUntil(String channel,
String key,
String predicate)
Blocking call waiting for a predicate to be met.
|
boolean |
waitUntil(String channel,
String key,
String predicate,
long timeout)
Blocking call waiting for a predicate to be met, with a timeout.
|
PredicateActionResult |
waitUntilThen(String channel,
String key,
String predicate,
long timeout,
String thenRemappingFunction)
Blocking call waiting with timeout for a predicate to be met before executing a remapping function.
|
PredicateActionResult |
waitUntilThen(String channel,
String key,
String predicate,
String thenRemappingFunction)
Blocking call waiting for a predicate to be met before executing a remapping function.
|
public SynchronizationWrapper(String originator, TaskId taskId, SynchronizationInternal internalAPI)
public boolean createChannel(String name, boolean isPersistent) throws IOException
Synchronization
createChannel
in interface Synchronization
name
- new channel nameisPersistent
- controls whether the channel should have a database backup (in case of crash or restart) or is kept in memory onlyIOException
- if an error occurred while persisting the channel on diskpublic boolean createChannelIfAbsent(String name, boolean isPersistent) throws IOException
Synchronization
createChannelIfAbsent
in interface Synchronization
name
- new or existing channel nameisPersistent
- controls whether the channel should have a database backup (in case of crash or restart) or is kept in memory onlyIOException
- if an error occurred while persisting the channel on diskpublic boolean deleteChannel(String name) throws IOException
Synchronization
deleteChannel
in interface Synchronization
name
- existing channel nameIOException
- if an error occurred while persisting the channel on diskpublic boolean channelExists(String name)
Synchronization
channelExists
in interface Synchronization
name
- channel namepublic int size(String channel) throws InvalidChannelException
Synchronization
size
in interface Synchronization
channel
- channel identifierInvalidChannelException
- if the channel does not existpublic boolean isEmpty(String channel) throws InvalidChannelException
Synchronization
isEmpty
in interface Synchronization
channel
- channel identifierInvalidChannelException
- if the channel does not existpublic boolean containsKey(String channel, String key) throws InvalidChannelException
Synchronization
containsKey
in interface Synchronization
channel
- channel identifierkey
- key whose presence in this channel map is to be testedInvalidChannelException
- if the channel does not existpublic boolean containsValue(String channel, Serializable value) throws InvalidChannelException
Synchronization
containsValue
in interface Synchronization
channel
- channel identifiervalue
- value whose presence in this channel map is to be testedInvalidChannelException
- if the channel does not existpublic Serializable get(String channel, String key) throws InvalidChannelException
Synchronization
null
if this channel map contains no mapping for the key.
More formally, if this channel map contains a mapping from a key
k
to a value v
such that (key==null ? k==null :
key.equals(k))
, then this method returns v
; otherwise
it returns null
. (There can be at most one such mapping.)
As this channel map permits null values, a return value of
null
does not necessarily indicate that the channel map
contains no mapping for the key; it's also possible that the channel map
explicitly maps the key to null
. The containsKey
operation may be used to distinguish these two cases.
get
in interface Synchronization
channel
- channel identifierkey
- the key whose associated value is to be returnednull
if this channel map contains no mapping for the keyInvalidChannelException
- if the channel does not existpublic Serializable put(String channel, String key, Serializable value) throws InvalidChannelException, IOException
Synchronization
Synchronization.containsKey(String, String)
m.containsKey(k)} would return
true.)put
in interface Synchronization
channel
- channel identifierkey
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyInvalidChannelException
- if the channel does not existIOException
- if an error occurred when persisting the modified channel on diskpublic Serializable remove(String channel, String key) throws InvalidChannelException, IOException
Synchronization
(key==null ? k==null : key.equals(k))
, that mapping
is removed. (The channel map can contain at most one such mapping.)
Returns the value to which this channel map previously associated the key, or null if the channel map contained no mapping for the key.
As this channel map permits null values, a return value of null does not necessarily indicate that the channel map contained no mapping for the key; it's also possible that the channel map explicitly mapped the key to null.
The channel map will not contain a mapping for the specified key once the call returns.
remove
in interface Synchronization
channel
- channel identifierkey
- key whose mapping is to be removed from the channel mapInvalidChannelException
- if the channel does not existIOException
- if an error occurred when persisting the modified channel on diskpublic void putAll(String channel, Map<? extends String,? extends Serializable> m) throws InvalidChannelException, IOException
Synchronization
Synchronization.put(String, String, Serializable)
put(k, v)} on this channel map once
for each mapping from key k to value v in the
specified map. The behavior of this operation is undefined if the
specified map is modified while the operation is in progress.putAll
in interface Synchronization
channel
- channel identifierm
- mappings to be stored in this channel mapInvalidChannelException
- if the channel does not existIOException
- if an error occurred when persisting the modified channel on diskpublic void clear(String channel) throws InvalidChannelException, IOException
Synchronization
clear
in interface Synchronization
channel
- channel identifierInvalidChannelException
- if the channel does not existIOException
- if an error occurred when persisting the modified channel on diskpublic Set<String> keySet(String channel) throws InvalidChannelException
Synchronization
Set
view of the keys contained in this channel map.
As this operation is performed remotely, the returned set is a remote copy of the server-side channel content.
Any operation on the reported set will not be reflected on the server and can be used only for informative purpose.keySet
in interface Synchronization
channel
- channel identifierInvalidChannelException
- if the channel does not existpublic Collection<Serializable> values(String channel) throws InvalidChannelException
Synchronization
Collection
view of the values contained in this channel map.
As this operation is performed remotely, the returned collection is a remote copy of the server-side channel content.
Any operation on the reported set will not be reflected on the server and can be used only for informative purpose.values
in interface Synchronization
channel
- channel identifierInvalidChannelException
- if the channel does not existpublic Set<Map.Entry<String,Serializable>> entrySet(String channel) throws InvalidChannelException
Synchronization
Set
view of the mappings contained in this map.
As this operation is performed remotely, the returned collection is a remote copy of the server-side channel content.
Any operation on the reported set will not be reflected on the server and can be used only for informative purpose.entrySet
in interface Synchronization
channel
- channel identifierInvalidChannelException
- if the channel does not existpublic Serializable getOrDefault(String channel, String key, Serializable defaultValue) throws InvalidChannelException
Synchronization
defaultValue
if this channel map contains no mapping for the key.
This implementation guarantees that the operation is performed atomically on the map.getOrDefault
in interface Synchronization
channel
- channel identifierkey
- the key whose associated value is to be returneddefaultValue
- the default mapping of the keydefaultValue
if this channel map contains no mapping for the keyInvalidChannelException
- if the channel does not existpublic void forEach(String channel, String action) throws InvalidChannelException, CompilationException
Synchronization
BiConsumer
specification.
The first parameter receives the key, and the second parameter receives the binding associated with this key.
For example:
"{ k, x -> println(k + \":\" + x) }"
This implementation guarantees that the operation is performed atomically on the channel map.forEach
in interface Synchronization
channel
- channel identifieraction
- The action to be performed for each entryInvalidChannelException
- if the channel does not existCompilationException
- if an error occurred during the compilation of the specified actionMap.forEach(BiConsumer)
,
BiConsumer
,
Groovy Closurespublic void replaceAll(String channel, String function) throws InvalidChannelException, CompilationException, IOException
Synchronization
BiFunction
specification.
The first parameter receives the key, and the second parameter receives the binding associated with this key.
For example:
"{ k, x -> x = x + 1 }"
This implementation guarantees that the operation is performed atomically on the channel map.replaceAll
in interface Synchronization
channel
- channel identifierfunction
- the function to apply to each entryInvalidChannelException
- if the channel does not existCompilationException
- if an error occurred during the compilation of the specified actionIOException
- if an error occurred when persisting the modified channel on diskMap.replaceAll(BiFunction)
,
BiFunction
,
Groovy Closurespublic Serializable putIfAbsent(String channel, String key, Serializable value) throws InvalidChannelException, IOException
Synchronization
null
) associates it with the given value and returns
null
, else returns the current value.
The default implementation is equivalent to, for this map
:
V v = map.get(key);
if (v == null)
v = map.put(key, value);
return v;
This implementation guarantees that the operation is performed atomically on the channel map.
putIfAbsent
in interface Synchronization
channel
- channel identifierkey
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keynull
if there was no mapping for the key.
(A null
return can also indicate that the map
previously associated null
with the key,
as this implementation supports null values.)InvalidChannelException
- if the channel does not existIOException
- if an error occurred when persisting the modified channel on diskMap.putIfAbsent(Object, Object)
public boolean remove(String channel, String key, Serializable value) throws InvalidChannelException, IOException
Synchronization
map
:
if (map.containsKey(key) && Objects.equals(map.get(key), value)) {
map.remove(key);
return true;
} else
return false;
This implementation guarantees that the operation is performed atomically on the channel map.
remove
in interface Synchronization
channel
- channel identifierkey
- key with which the specified value is associatedvalue
- value expected to be associated with the specified keytrue
if the value was removedInvalidChannelException
- if the channel does not existIOException
- if an error occurred when persisting the modified channel on diskMap.remove(Object, Object)
public boolean replace(String channel, String key, Serializable oldValue, Serializable newValue) throws InvalidChannelException, IOException
Synchronization
map
:
if (map.containsKey(key) && Objects.equals(map.get(key), value)) {
map.put(key, newValue);
return true;
} else
return false;
This implementation guarantees that the operation is performed atomically on the channel map.
replace
in interface Synchronization
channel
- channel identifierkey
- key with which the specified value is associatedoldValue
- value expected to be associated with the specified keynewValue
- value to be associated with the specified keytrue
if the value was replacedInvalidChannelException
- if the channel does not existIOException
- if an error occurred when persisting the modified channel on diskMap.replace(Object, Object, Object)
public Serializable replace(String channel, String key, Serializable value) throws InvalidChannelException, IOException
Synchronization
map
:
if (map.containsKey(key)) {
return map.put(key, value);
} else
return null;
This implementation guarantees that the operation is performed atomically on the channel map.
replace
in interface Synchronization
channel
- channel identifierkey
- key with which the specified value is associatedvalue
- value to be associated with the specified keynull
if there was no mapping for the key.
(A null
return can also indicate that the channel map
previously associated null
with the key,
as this implementation supports null values.)InvalidChannelException
- if the channel does not existIOException
- if an error occurred when persisting the modified channel on diskMap.replace(Object, Object)
public Serializable computeIfAbsent(String channel, String key, String mappingFunction) throws InvalidChannelException, CompilationException, IOException
Synchronization
null
), attempts to compute its value using the given mapping
function and enters it into this map unless null
.
The function is passed as a String containing a groovy closure definition which matches the Function
specification.
The parameter received contains the key.
For example:
"{ k -> new HashSet() }"
If the function returns null
no mapping is recorded. If
the function itself throws an (unchecked) exception, the
exception is rethrown as a ClosureEvaluationException, and no mapping is recorded.
The most common usage is to construct a new object serving as an initial
mapped value or memoized result, as in:
store.computeIfAbsent(channel, key, "{k -> new Value(k)}");
Or to implement a multi-value map, Map<K,Collection<V>>
,
supporting multiple values per key:
The default implementation is equivalent to the following steps for this
map
, then returning the current value or null
if now
absent:
if (map.get(key) == null) {
V newValue = mappingFunction.apply(key);
if (newValue != null)
map.put(key, newValue);
}
This implementation guarantees that the operation is performed atomically on the channel map.
computeIfAbsent
in interface Synchronization
channel
- channel identifierkey
- key with which the specified value is to be associatedmappingFunction
- the function to compute a valueInvalidChannelException
- if the channel does not existCompilationException
- if an error occurred during the compilation of the specified actionIOException
- if an error occurred when persisting the modified channel on diskMap.computeIfAbsent(Object, Function)
,
Function
,
Groovy Closurespublic Serializable computeIfPresent(String channel, String key, String remappingFunction) throws InvalidChannelException, CompilationException, IOException
Synchronization
If the function returns null
, the mapping is removed. If the
function itself throws an (unchecked) exception, the exception is
rethrown, and the current mapping is left unchanged.
The function is passed as a String containing a groovy closure definition which matches the BiFunction
specification.
The first parameter receives the key, and the second parameter receives the binding associated with this key.
For example:
"{ k, x -> x = x + 1 }"
The default implementation is equivalent to performing the following
steps for this map
, then returning the current value or
null
if now absent:
if (map.get(key) != null) {
V oldValue = map.get(key);
V newValue = remappingFunction.apply(key, oldValue);
if (newValue != null)
map.put(key, newValue);
else
map.remove(key);
}
This implementation guarantees that the operation is performed atomically on the channel map.
computeIfPresent
in interface Synchronization
channel
- channel identifierkey
- key with which the specified value is to be associatedremappingFunction
- the function to compute a valueInvalidChannelException
- if the channel does not existCompilationException
- if an error occurred during the compilation of the specified actionIOException
- if an error occurred when persisting the modified channel on diskMap.computeIfPresent(Object, BiFunction)
,
BiFunction
,
Groovy Closurespublic Serializable compute(String channel, String key, String remappingFunction) throws InvalidChannelException, CompilationException, IOException
Synchronization
null
if there is no current mapping).
The function is passed as a String containing a groovy closure definition which matches the BiFunction
specification.
The first parameter receives the key, and the second parameter receives the binding associated with this key.
For example, to either create or append a String
msg to a value
mapping:
store.compute(channel, key, "{ k, v -> (v == null) ? \"" + msg + "\" : v.concat(\"" + msg + "\")}")
(Method merge()
is often simpler to use for such purposes.)
If the function returns null
, the mapping is removed (or
remains absent if initially absent). If the function itself throws an
(unchecked) exception, the exception is rethrown as a ClosureEvaluationException, and the current mapping
is left unchanged.
The default implementation is equivalent to performing the following
steps for this map
, then returning the current value or
null
if absent:
V oldValue = map.get(key);
V newValue = remappingFunction.apply(key, oldValue);
if (oldValue != null ) {
if (newValue != null)
map.put(key, newValue);
else
map.remove(key);
} else {
if (newValue != null)
map.put(key, newValue);
else
return null;
}
This implementation guarantees that the operation is performed atomically on the channel map.
compute
in interface Synchronization
channel
- channel identifierkey
- key with which the specified value is to be associatedremappingFunction
- the function to compute a valueInvalidChannelException
- if the channel does not existCompilationException
- if an error occurred during the compilation of the specified actionIOException
- if an error occurred when persisting the modified channel on diskMap.compute(Object, BiFunction)
,
BiFunction
,
Groovy Closurespublic Serializable merge(String channel, String key, Serializable value, String remappingFunction) throws InvalidChannelException, CompilationException, IOException
Synchronization
null
. This
method may be of use when combining multiple mapped values for a key.
The function is passed as a String containing a groovy closure definition which matches the BiFunction
specification.
The first parameter receives the key, and the second parameter receives the binding associated with this key.
For example, to either create or append a String msg
to a
value mapping:
store.merge(channel, key, msg, "{k, x -> x.concat(\"" + msg + "\")}")
If the function returns null
the mapping is removed. If the
function itself throws an (unchecked) exception, the exception is
rethrown, and the current mapping is left unchanged.
The default implementation is equivalent to performing the following
steps for this map
, then returning the current value or
null
if absent:
V oldValue = map.get(key);
V newValue = (oldValue == null) ? value :
remappingFunction.apply(oldValue, value);
if (newValue == null)
map.remove(key);
else
map.put(key, newValue);
This implementation guarantees that the operation is performed atomically on the channel map.
merge
in interface Synchronization
channel
- channel identifierkey
- key with which the resulting value is to be associatedvalue
- the non-null value to be merged with the existing value
associated with the key or, if no existing value or a null value
is associated with the key, to be associated with the keyremappingFunction
- the function to recompute a value if presentInvalidChannelException
- if the channel does not existCompilationException
- if an error occurred during the compilation of the specified actionIOException
- if an error occurred when persisting the modified channel on diskMap.merge(Object, Object, BiFunction)
,
BiFunction
,
Groovy Closurespublic PredicateActionResult conditionalCompute(String channel, String key, String predicate, String thenRemappingFunction) throws InvalidChannelException, CompilationException, IOException
Synchronization
BiPredicate
and BiFunction
specifications.
The first parameter receives the key, and the second parameter receives the binding associated with this key.
For example, to append a String
msg to a value only if the value is already defined
mapping:
store.conditionalCompute(channel, key, "{ k, v -> v != null}", "{k, v -> v.concat(\"" + msg + "\")}")
If the function returns null
, the mapping is removed (or
remains absent if initially absent). If the predicate or function itself throw an
(unchecked) exception, the exception is rethrown as a ClosureEvaluationException, and the current mapping
is left unchanged.
The default implementation is equivalent to performing the following
steps for this map
, then returning PredicateActionResult
object containing the current value or
null
if absent:
V oldValue = map.get(key);
if (predicate.test(key, oldValue)) {
map.compute(key, thenRemappingFunction);
}
This implementation guarantees that the operation is performed atomically on the channel map.
conditionalCompute
in interface Synchronization
channel
- channel identifierkey
- key with which the specified value is to be associatedpredicate
- closure used for conditional decisionthenRemappingFunction
- the function to compute a value if the predicate is truePredicateActionResult
object which contains the result of the predicate evaluation and either (true) the new value associated with the key after applying thenRemappingFunction, or (false) the unmodified value associated with the key.InvalidChannelException
- if the channel does not existCompilationException
- if an error occurred during the compilation of the specified actionIOException
- if an error occurred when persisting the modified channel on diskMap.compute(Object, BiFunction)
,
BiPredicate
,
BiFunction
,
Groovy Closures,
PredicateActionResult
public PredicateActionResult conditionalCompute(String channel, String key, String predicate, String thenRemappingFunction, String elseRemappingFunction) throws InvalidChannelException, CompilationException, IOException
Synchronization
BiPredicate
and BiFunction
specifications.
The first parameter receives the key, and the second parameter receives the binding associated with this key.
For example, to append a String
msg to a value if the value is already defined, and otherwise set msg as the new mapping
mapping:
store.conditionalCompute(channel, key, "{ k, v -> v != null}", "{k, v -> v.concat(\"" + msg + "\")}", "{k, v -> \"" + msg + "\""}")
If either function returns null
, the mapping is removed (or
remains absent if initially absent). If the predicate or function itself throw an
(unchecked) exception, the exception is rethrown as a ClosureEvaluationException, and the current mapping
is left unchanged.
The default implementation is equivalent to performing the following
steps for this map
, then returning a PredicateActionResult object containing the current value or
null
if absent:
V oldValue = map.get(key);
if (predicate.test(key, oldValue)) {
map.compute(key, thenRemappingFunction);
} else {
map.compute(key, elseRemappingFunction);
}
This implementation guarantees that the operation is performed atomically on the channel map.
conditionalCompute
in interface Synchronization
channel
- channel identifierkey
- key with which the specified value is to be associatedpredicate
- closure used for conditional decisionthenRemappingFunction
- the function to compute a value if the predicate is trueelseRemappingFunction
- the function to compute a value if the predicate is falsePredicateActionResult
object which contains the result of the predicate evaluation and either (true) the new value associated with the key after applying thenRemappingFunction, or (false) the new value associated with the key after applying elseRemappingFunction.InvalidChannelException
- if the channel does not existCompilationException
- if an error occurred during the compilation of the specified actionIOException
- if an error occurred when persisting the modified channel on diskMap.compute(Object, BiFunction)
,
BiPredicate
,
BiFunction
,
Groovy Closures,
PredicateActionResult
public void conditionalReplaceAll(String channel, String predicate, String thenRemappingFunction) throws InvalidChannelException, CompilationException, IOException
Synchronization
BiPredicate
and BiFunction
specifications.
The first parameter receives the key, and the second parameter receives the binding associated with this key.
For example, to increment a counter only on positive values
store.conditionalCompute(channel, key, "{ k, v -> v >= 0}", "{k, v -> v + 1}")
If the function returns null
, the mapping is removed (or
remains absent if initially absent). If the predicate or function itself throw an
(unchecked) exception, the exception is rethrown as a ClosureEvaluationException, and the current mapping
is left unchanged.
The default implementation is equivalent to performing the following
steps for this map
, then returning PredicateActionResult
object containing the current value or
null
if absent:
for (Map.Entry<String, V> entry : map.entries()) {
if (predicate.test(entry.getKey(), entry.getValue())) {
map.compute(entry.getKey(), thenRemappingFunction);
}
}
This implementation guarantees that the operation is performed atomically on the channel map.
conditionalReplaceAll
in interface Synchronization
channel
- channel identifierpredicate
- closure used for conditional decisionthenRemappingFunction
- the function to compute a value if the predicate is trueInvalidChannelException
- if the channel does not existCompilationException
- if an error occurred during the compilation of the specified actionIOException
- if an error occurred when persisting the modified channel on diskMap.compute(Object, BiFunction)
,
BiPredicate
,
BiFunction
,
Groovy Closurespublic void conditionalReplaceAll(String channel, String predicate, String thenRemappingFunction, String elseRemappingFunction) throws InvalidChannelException, CompilationException, IOException
Synchronization
BiPredicate
and BiFunction
specifications.
The first parameter receives the key, and the second parameter receives the binding associated with this key.
For example, to increment a counter only on positive values
store.conditionalCompute(channel, key, "{ k, v -> v >= 0}", "{k, v -> v + 1}")
If the function returns null
, the mapping is removed (or
remains absent if initially absent). If the predicate or function itself throw an
(unchecked) exception, the exception is rethrown as a ClosureEvaluationException, and the current mapping
is left unchanged.
The default implementation is equivalent to performing the following
steps for this map
, then returning PredicateActionResult object containing the current value or
null
if absent:
for (Map.Entry<String, V> entry : map.entries()) {
if (predicate.test(entry.getKey(), entry.getValue())) {
map.compute(entry.getKey(), thenRemappingFunction);
} else {
map.compute(entry.getKey(), elseRemappingFunction);
}
}
This implementation guarantees that the operation is performed atomically on the channel map.
conditionalReplaceAll
in interface Synchronization
channel
- channel identifierpredicate
- closure used for conditional decisionthenRemappingFunction
- the function to compute a value if the predicate is trueelseRemappingFunction
- the function to compute a value if the predicate is falseInvalidChannelException
- if the channel does not existCompilationException
- if an error occurred during the compilation of the specified actionIOException
- if an error occurred when persisting the modified channel on diskMap.compute(Object, BiFunction)
,
BiPredicate
,
BiFunction
,
Groovy Closurespublic boolean waitUntil(String channel, String key, String predicate) throws InvalidChannelException, CompilationException
Synchronization
BiPredicate
specification.
For example, to wait until a mapping is defined:
store.waitUntil(channel, key, "{ k, v -> v != null}")
The default implementation is equivalent to performing the following
steps for this map
, then returning the result of the predicate, which will always be true;
V oldValue = map.get(key);
while (!predicate.test(key, oldValue)) {
oldValue = map.get(key);
}
This implementation guarantees that the operation is performed atomically on the channel map, which means that as soon as another operation modifies the underlying structure to meet the predicate,
the waitUntil will be unblocked. Subsequent operations based on this condition wait may still be interleaved by other operations performed on the map. This is why it is generally preferable to use waitUntilThen()
to guaranty both predicate and action.
waitUntil
in interface Synchronization
channel
- channel identifierkey
- key with which the specified value is to be associatedpredicate
- closure used for conditional decisionInvalidChannelException
- if the channel does not existCompilationException
- if an error occurred during the compilation of the specified actionsMap.compute(Object, BiFunction)
,
BiPredicate
,
Groovy Closurespublic boolean waitUntil(String channel, String key, String predicate, long timeout) throws InvalidChannelException, CompilationException, TimeoutException
Synchronization
BiPredicate
specification.
For example, to wait for at most one minute until a mapping is defined:
store.waitUntil(channel, key, "{ k, v -> v != null}", 60000)
This implementation guarantees that the operation is performed atomically on the channel map, which means that as soon as another operation modifies the underlying structure to meet the predicate,
the waitUntil will be unblocked. Subsequent operations based on this condition wait may still be interleaved by other operations performed on the map. This is why it is generally preferable to use waitUntilThen()
to guaranty both predicate and action.
waitUntil
in interface Synchronization
channel
- channel identifierkey
- key with which the specified value is to be associatedpredicate
- closure used for conditional decisiontimeout
- maximum time in milliseconds to waitInvalidChannelException
- if the channel does not existCompilationException
- if an error occurred during the compilation of the specified actionsTimeoutException
- if the predicate is not met before the timeout is reachedMap.compute(Object, BiFunction)
,
BiPredicate
,
Groovy Closurespublic PredicateActionResult waitUntilThen(String channel, String key, String predicate, String thenRemappingFunction) throws InvalidChannelException, CompilationException, IOException
Synchronization
null
if there is no current mapping), if the given predicate is met.
If the predicate is not yet met, block the operation until it is satisfied
Both predicate and function are passed as String containing a groovy closure definition which matches the BiPredicate
and BiFunction
specifications.
The first parameter receives the key, and the second parameter receives the binding associated with this key.
For example, to append a String
msg to a value when the value will be defined
mapping:
store.waitUntilThen(channel, key, "{ k, v -> v != null}", "{k, v -> v.concat(\"" + msg + "\")}")
If the function returns null
, the mapping is removed (or
remains absent if initially absent). If the predicate or function itself throw an
(unchecked) exception, the exception is rethrown as a ClosureEvaluationException, and the current mapping
is left unchanged.
The default implementation is equivalent to performing the following
steps for this map
, then returning the current value or
null
if absent:
V oldValue = map.get(key);
while (!predicate.test(key, oldValue)) {
oldValue = map.get(key)
}
map.compute(key, thenRemappingFunction);
This implementation guarantees that the operation is performed atomically on the channel map, which means that as soon as another operation modifies the underlying structure to meet the predicate, the remappingFunction will be executed at once and atomically, without letting another operation to interfere.
waitUntilThen
in interface Synchronization
channel
- channel identifierkey
- key with which the specified value is to be associatedpredicate
- closure used for conditional decisionthenRemappingFunction
- the function to compute a valuePredicateActionResult
object which contains the result of the predicate evaluation and the new value associated with the key after applying thenRemappingFunctionInvalidChannelException
- if the channel does not existCompilationException
- if an error occurred during the compilation of the specified actionIOException
- if an error occurred when persisting the modified channel on diskMap.compute(Object, BiFunction)
,
BiPredicate
,
BiFunction
,
Groovy Closurespublic PredicateActionResult waitUntilThen(String channel, String key, String predicate, long timeout, String thenRemappingFunction) throws InvalidChannelException, CompilationException, IOException, TimeoutException
Synchronization
null
if there is no current mapping), if the given predicate is met.
If the predicate is not yet met, block the operation until it is satisfied or until the specified timeout is reached.
Both predicate and function are passed as String containing a groovy closure definition which matches the BiPredicate
and BiFunction
specifications.
The first parameter receives the key, and the second parameter receives the binding associated with this key.
For example, to append a String
msg to a value when the value will be defined
mapping:
store.waitUntilThen(channel, key, "{ k, v -> v != null}", "{k, v -> v.concat(\"" + msg + "\")}")
If the function returns null
, the mapping is removed (or
remains absent if initially absent). If the predicate or function itself throw an
(unchecked) exception, the exception is rethrown as a ClosureEvaluationException, and the current mapping
is left unchanged.
This implementation guarantees that the operation is performed atomically on the channel map, which means that as soon as another operation modifies the underlying structure to meet the predicate, the remappingFunction will be executed at once and atomically, without letting another operation to interfere.
waitUntilThen
in interface Synchronization
channel
- channel identifierkey
- key with which the specified value is to be associatedpredicate
- closure used for conditional decisiontimeout
- maximum time in milliseconds to waitthenRemappingFunction
- the function to compute a valuePredicateActionResult
object which contains the result of the predicate evaluation and the new value associated with the key after applying thenRemappingFunctionInvalidChannelException
- if the channel does not existCompilationException
- if an error occurred during the compilation of the specified actionIOException
- if an error occurred when persisting the modified channel on diskTimeoutException
- if the predicate is not met before the timeout is reachedMap.compute(Object, BiFunction)
,
BiPredicate
,
BiFunction
,
Groovy Closures