Class DefaultContextMap
java.lang.Object
io.servicetalk.concurrent.internal.DefaultContextMap
- All Implemented Interfaces:
ContextMap
Default implementation of
ContextMap
.
Note: it's not thread-safe!
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.servicetalk.context.api.ContextMap
ContextMap.Key<T>
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clear the contents of thisContextMap
.<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
.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.<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
.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
.void
putAll
(ContextMap map) Put all the entries into thisContextMap
.void
putAll
(Map<ContextMap.Key<?>, Object> map) Put all the entries into thisContextMap
.<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).int
size()
Determine the number ofContextMap.Key
-value pairs in thisContextMap
.toString()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface io.servicetalk.context.api.ContextMap
contains, removeAll
-
Constructor Details
-
DefaultContextMap
public DefaultContextMap()Creates a new instance.
-
-
Method Details
-
size
public int size()Description copied from interface:ContextMap
Determine the number ofContextMap.Key
-value pairs in thisContextMap
.- Specified by:
size
in interfaceContextMap
- Returns:
- the number of
ContextMap.Key
-value pairs in thisContextMap
.
-
isEmpty
public boolean isEmpty()Description copied from interface:ContextMap
Determine if there are no entries in thisContextMap
.- Specified by:
isEmpty
in interfaceContextMap
- Returns:
true
if there are no entries in thisContextMap
.
-
containsKey
Description copied from interface:ContextMap
Determine if thisContextMap
contains an entry corresponding to thekey
.- Specified by:
containsKey
in interfaceContextMap
- Parameters:
key
- TheContextMap.Key
to lookup.- Returns:
true
if thisContextMap
contains an entry corresponding to thekey
,false
otherwise.
-
containsValue
Description copied from interface:ContextMap
Determine if thisContextMap
contains an entry with the specifiedvalue
.- Specified by:
containsValue
in interfaceContextMap
- Parameters:
value
- Thevalue
to lookup.- Returns:
true
if thisContextMap
contains one or more entries with the specifiedvalue
,false
otherwise.
-
get
Description copied from interface:ContextMap
Get the value associated with thekey
, ornull
if no value is associated.- Specified by:
get
in interfaceContextMap
- 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).
-
getOrDefault
Description copied from interface:ContextMap
Get the value associated with thekey
, ordefaultValue
if no value is associated.- Specified by:
getOrDefault
in interfaceContextMap
- 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.
-
put
Description copied from interface:ContextMap
Put a new entry into thisContextMap
.- Specified by:
put
in interfaceContextMap
- 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).
-
putIfAbsent
Description copied from interface:ContextMap
Put a new entry into thisContextMap
if this map does not already contain thiskey
or is mapped tonull
.- Specified by:
putIfAbsent
in interfaceContextMap
- 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).
-
computeIfAbsent
@Nullable public <T> T computeIfAbsent(ContextMap.Key<T> key, Function<ContextMap.Key<T>, T> computeFunction) Description copied from interface:ContextMap
Computes a new entry for thisContextMap
if this map does not already contain thiskey
or is mapped tonull
.- Specified by:
computeIfAbsent
in interfaceContextMap
- 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
.
-
putAll
Description copied from interface:ContextMap
Put all the entries into thisContextMap
.- Specified by:
putAll
in interfaceContextMap
- Parameters:
map
- The entries to insert into thisContextMap
.
-
putAll
Description copied from interface:ContextMap
Put all the entries into thisContextMap
.- Specified by:
putAll
in interfaceContextMap
- Parameters:
map
- The entries to insert into thisContextMap
.
-
remove
Description copied from interface:ContextMap
Remove an entry from thisContextMap
, and get the previous value (if one exists).- Specified by:
remove
in interfaceContextMap
- 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.
-
clear
public void clear()Description copied from interface:ContextMap
Clear the contents of thisContextMap
.- Specified by:
clear
in interfaceContextMap
-
forEach
Description copied from interface:ContextMap
Iterate over the entries contained in thisContextMap
.- Specified by:
forEach
in interfaceContextMap
- 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.
-
copy
Description copied from interface:ContextMap
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.- Specified by:
copy
in interfaceContextMap
- 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.
-
hashCode
public int hashCode()Description copied from interface:ContextMap
Returns the hash code value for thisContextMap
.- Specified by:
hashCode
in interfaceContextMap
- 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:
-
equals
Description copied from interface:ContextMap
Determines if the specified object is equal to thisContextMap
.- Specified by:
equals
in interfaceContextMap
- 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:
-
toString
-