Package io.servicetalk.context.api
Interface ContextMap
- All Known Implementing Classes:
DefaultContextMap
public interface ContextMap
The key-value entry map for different types of the value, defined by the
ContextMap.Key
.-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic final class
A key identifies a specific object in aContextMap
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clear the contents of thisContextMap
.default <T> T
computeIfAbsent
(ContextMap.Key<T> key, Function<ContextMap.Key<T>, T> computeFunction) Computes a new entry for thisContextMap
if this map does not already contain thiskey
or is mapped tonull
.default <T> boolean
contains
(ContextMap.Key<T> key, T value) boolean
containsKey
(ContextMap.Key<?> key) Determine if thisContextMap
contains an entry corresponding to thekey
.boolean
containsValue
(Object value) Determine if thisContextMap
contains an entry with the specifiedvalue
.copy()
Create an isolated copy of the current map.boolean
Determines if the specified object is equal to thisContextMap
.forEach
(BiPredicate<ContextMap.Key<?>, Object> consumer) Iterate over the entries contained in thisContextMap
.<T> T
get
(ContextMap.Key<T> key) Get the value associated with thekey
, ornull
if no value is associated.default <T> T
getOrDefault
(ContextMap.Key<T> key, T defaultValue) Get the value associated with thekey
, ordefaultValue
if no value is associated.int
hashCode()
Returns the hash code value for thisContextMap
.default boolean
isEmpty()
Determine if there are no entries in thisContextMap
.<T> T
put
(ContextMap.Key<T> key, T value) Put a new entry into thisContextMap
.default void
putAll
(ContextMap map) Put all the entries into thisContextMap
.default void
putAll
(Map<ContextMap.Key<?>, Object> map) Put all the entries into thisContextMap
.default <T> T
putIfAbsent
(ContextMap.Key<T> key, T value) Put a new entry into thisContextMap
if this map does not already contain thiskey
or is mapped tonull
.<T> T
remove
(ContextMap.Key<T> key) Remove an entry from thisContextMap
, and get the previous value (if one exists).default boolean
removeAll
(Iterable<ContextMap.Key<?>> keys) Remove all entries from thisContextMap
associated with the keys from the passedIterable
.int
size()
Determine the number ofContextMap.Key
-value pairs in thisContextMap
.
-
Method Details
-
size
int size()Determine the number ofContextMap.Key
-value pairs in thisContextMap
.- Returns:
- the number of
ContextMap.Key
-value pairs in thisContextMap
.
-
isEmpty
default boolean isEmpty()Determine if there are no entries in thisContextMap
.- Returns:
true
if there are no entries in thisContextMap
.
-
containsKey
Determine if thisContextMap
contains an entry corresponding to thekey
.- Parameters:
key
- TheContextMap.Key
to lookup.- Returns:
true
if thisContextMap
contains an entry corresponding to thekey
,false
otherwise.- Throws:
NullPointerException
- (optional behavior) ifkey
isnull
and the implementation doesn't supportnull
keys.
-
containsValue
Determine if thisContextMap
contains an entry with the specifiedvalue
.- Parameters:
value
- Thevalue
to lookup.- Returns:
true
if thisContextMap
contains one or more entries with the specifiedvalue
,false
otherwise.- Throws:
NullPointerException
- (optional behavior) ifvalue
isnull
and the implementation doesn't supportnull
values.
-
contains
- Type Parameters:
T
- The anticipated type of object associated with thekey
.- Parameters:
key
- TheContextMap.Key
to lookup.value
- The value to match.- Returns:
true
if thisContextMap
contains an entry matching the passedkey
andvalue
,false
otherwise.- Throws:
NullPointerException
- (optional behavior) ifkey
orvalue
isnull
and the implementation doesn't supportnull
keys or values.
-
get
Get the value associated with thekey
, ornull
if no value is associated.- Type Parameters:
T
- The anticipated type of object associated with thekey
.- Parameters:
key
- TheContextMap.Key
to lookup.- Returns:
- The value associated with the
key
, ornull
if no value is associated.null
can also indicate the value associated with thekey
isnull
(ifnull
values are supported by the implementation). - Throws:
NullPointerException
- (optional behavior) ifkey
isnull
and the implementation doesn't supportnull
keys.
-
getOrDefault
Get the value associated with thekey
, ordefaultValue
if no value is associated.- Type Parameters:
T
- The anticipated type of object associated with thekey
.- Parameters:
key
- TheContextMap.Key
to lookup.defaultValue
- The value to return if no value is associated with thekey
.- Returns:
- The value associated with the
key
(can returnnull
ifnull
values are supported by the implementation), ordefaultValue
if no value is associated. - Throws:
NullPointerException
- (optional behavior) ifkey
isnull
and the implementation doesn't supportnull
keys.
-
put
Put a new entry into thisContextMap
.- Type Parameters:
T
- The type of object associated with thekey
.- Parameters:
key
- TheContextMap.Key
used to index thevalue
.value
- The value to put.- Returns:
- The previous value associated with the
key
, ornull
if there was none.null
can also indicate the value associated with thekey
wasnull
(ifnull
values are supported by the implementation). - Throws:
NullPointerException
- (optional behavior) ifkey
orvalue
isnull
and the implementation doesn't supportnull
keys or values.UnsupportedOperationException
- if this method is not supported.
-
putIfAbsent
Put a new entry into thisContextMap
if this map does not already contain thiskey
or is mapped tonull
.- Type Parameters:
T
- The type of object associated with thekey
.- Parameters:
key
- TheContextMap.Key
used to index thevalue
.value
- The value to put.- Returns:
- The previous value associated with the
key
, ornull
if there was none.null
can also indicate the value associated with thekey
wasnull
(ifnull
values are supported by the implementation). - Throws:
NullPointerException
- (optional behavior) ifkey
orvalue
isnull
and the implementation doesn't supportnull
keys or values.UnsupportedOperationException
- if this method is not supported.
-
computeIfAbsent
@Nullable default <T> T computeIfAbsent(ContextMap.Key<T> key, Function<ContextMap.Key<T>, T> computeFunction) Computes a new entry for thisContextMap
if this map does not already contain thiskey
or is mapped tonull
.- Type Parameters:
T
- The type of object associated with thekey
.- Parameters:
key
- TheContextMap.Key
used to index a new value.computeFunction
- The function to compute a new value. Implementation may invoke this function multiple times if concurrent threads attempt modifying this context map, result is expected to be idempotent.- Returns:
- The current (existing or computed) value associated with the
key
, ornull
if the computed value isnull
. - Throws:
NullPointerException
- (optional behavior) ifkey
or computedvalue
isnull
and the implementation doesn't supportnull
keys or values.UnsupportedOperationException
- if this method is not supported.
-
putAll
Put all the entries into thisContextMap
.- Parameters:
map
- The entries to insert into thisContextMap
.- Throws:
IllegalArgumentException
- if any value type does not match with its correspondingContextMap.Key.type()
.NullPointerException
- (optional behavior) if any of themap
entries has anull
key
orvalue
and the implementation doesn't supportnull
keys or values.ConcurrentModificationException
- done on a best effort basis if the passedmap
is detected to be modified while attempting to put all entries.UnsupportedOperationException
- if this method is not supported.
-
putAll
Put all the entries into thisContextMap
.- Parameters:
map
- The entries to insert into thisContextMap
.- Throws:
IllegalArgumentException
- if any value type does not match with its correspondingContextMap.Key.type()
.NullPointerException
- (optional behavior) if any of themap
entries has anull
key
orvalue
and the implementation doesn't supportnull
keys or values.ConcurrentModificationException
- done on a best effort basis if the passedmap
is detected to be modified while attempting to put all entries.UnsupportedOperationException
- if this method is not supported.
-
remove
Remove an entry from thisContextMap
, and get the previous value (if one exists).- Type Parameters:
T
- The type of object associated with thekey
.- Parameters:
key
- TheContextMap.Key
which identifies an entry for removal.- Returns:
- The previous value associated with the
key
, ornull
if there was none.null
can also indicate the value associated with thekey
wasnull
(ifnull
values are supported by the implementation). If theContextMap
implementation is immutable this may be a new object. - Throws:
NullPointerException
- (optional behavior) ifkey
isnull
and the implementation doesn't supportnull
keys.UnsupportedOperationException
- if this method is not supported.
-
removeAll
Remove all entries from thisContextMap
associated with the keys from the passedIterable
.- Parameters:
keys
- TheContextMap.Key
s that identify entries for removal.- Returns:
true
if this map has changed as a result of this operation.- Throws:
NullPointerException
- (optional behavior) if any of thekeys
isnull
and the implementation doesn't supportnull
keys.ConcurrentModificationException
- Done on a best effort basis ifentries
is detected to be modified while attempting to remove all entries.UnsupportedOperationException
- if this method is not supported.
-
clear
void clear()Clear the contents of thisContextMap
.- Throws:
UnsupportedOperationException
- if this method is not supported.
-
forEach
Iterate over the entries contained in thisContextMap
.- Parameters:
consumer
- Each entry will be passed as key and value arguments to thisBiPredicate
. A consumer predicate should returntrue
if it wants to keep iterating orfalse
to stop iteration at the current entry.- Returns:
null
ifconsumer
iterated through all entries or theContextMap.Key
at which the iteration stopped.- Throws:
NullPointerException
- ifconsumer
isnull
.
-
copy
ContextMap copy()Create an isolated copy of the current map. The return value contents are the same as thisContextMap
but modifications to thisContextMap
are not visible in the return value, and visa-versa.- Returns:
- an isolated copy of the current map. The contents are the same as this
ContextMap
but modifications to thisContextMap
are not visible in the return value, and visa-versa.
-
equals
Determines if the specified object is equal to thisContextMap
.- Overrides:
equals
in classObject
- Parameters:
o
- object to be compared for equality with thisContextMap
.- Returns:
true
if the passed object is aContextMap
and has the same key-value mapping entries.- See Also:
-
hashCode
int hashCode()Returns the hash code value for thisContextMap
.- Overrides:
hashCode
in classObject
- Returns:
- the hash code value for this
ContextMap
, taking into account the hash codes of each key-value mapping entries thisContextMap
contains. - See Also:
-