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
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classA key identifies a specific object in aContextMap. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clear the contents of thisContextMap.default <T> TcomputeIfAbsent(ContextMap.Key<T> key, Function<ContextMap.Key<T>, T> computeFunction) Computes a new entry for thisContextMapif this map does not already contain thiskeyor is mapped tonull.default <T> booleancontains(ContextMap.Key<T> key, T value) booleancontainsKey(ContextMap.Key<?> key) Determine if thisContextMapcontains an entry corresponding to thekey.booleancontainsValue(Object value) Determine if thisContextMapcontains an entry with the specifiedvalue.copy()Create an isolated copy of the current map.booleanDetermines if the specified object is equal to thisContextMap.forEach(BiPredicate<ContextMap.Key<?>, Object> consumer) Iterate over the entries contained in thisContextMap.<T> Tget(ContextMap.Key<T> key) Get the value associated with thekey, ornullif no value is associated.default <T> TgetOrDefault(ContextMap.Key<T> key, T defaultValue) Get the value associated with thekey, ordefaultValueif no value is associated.inthashCode()Returns the hash code value for thisContextMap.default booleanisEmpty()Determine if there are no entries in thisContextMap.<T> Tput(ContextMap.Key<T> key, T value) Put a new entry into thisContextMap.default voidputAll(ContextMap map) Put all the entries into thisContextMap.default voidputAll(Map<ContextMap.Key<?>, Object> map) Put all the entries into thisContextMap.default <T> TputIfAbsent(ContextMap.Key<T> key, T value) Put a new entry into thisContextMapif this map does not already contain thiskeyor is mapped tonull.<T> Tremove(ContextMap.Key<T> key) Remove an entry from thisContextMap, and get the previous value (if one exists).default booleanremoveAll(Iterable<ContextMap.Key<?>> keys) Remove all entries from thisContextMapassociated with the keys from the passedIterable.intsize()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:
trueif there are no entries in thisContextMap.
-
containsKey
Determine if thisContextMapcontains an entry corresponding to thekey.- Parameters:
key- TheContextMap.Keyto lookup.- Returns:
trueif thisContextMapcontains an entry corresponding to thekey,falseotherwise.- Throws:
NullPointerException- (optional behavior) ifkeyisnulland the implementation doesn't supportnullkeys.
-
containsValue
Determine if thisContextMapcontains an entry with the specifiedvalue.- Parameters:
value- Thevalueto lookup.- Returns:
trueif thisContextMapcontains one or more entries with the specifiedvalue,falseotherwise.- Throws:
NullPointerException- (optional behavior) ifvalueisnulland the implementation doesn't supportnullvalues.
-
contains
- Type Parameters:
T- The anticipated type of object associated with thekey.- Parameters:
key- TheContextMap.Keyto lookup.value- The value to match.- Returns:
trueif thisContextMapcontains an entry matching the passedkeyandvalue,falseotherwise.- Throws:
NullPointerException- (optional behavior) ifkeyorvalueisnulland the implementation doesn't supportnullkeys or values.
-
get
Get the value associated with thekey, ornullif no value is associated.- Type Parameters:
T- The anticipated type of object associated with thekey.- Parameters:
key- TheContextMap.Keyto lookup.- Returns:
- The value associated with the
key, ornullif no value is associated.nullcan also indicate the value associated with thekeyisnull(ifnullvalues are supported by the implementation). - Throws:
NullPointerException- (optional behavior) ifkeyisnulland the implementation doesn't supportnullkeys.
-
getOrDefault
Get the value associated with thekey, ordefaultValueif no value is associated.- Type Parameters:
T- The anticipated type of object associated with thekey.- Parameters:
key- TheContextMap.Keyto lookup.defaultValue- The value to return if no value is associated with thekey.- Returns:
- The value associated with the
key(can returnnullifnullvalues are supported by the implementation), ordefaultValueif no value is associated. - Throws:
NullPointerException- (optional behavior) ifkeyisnulland the implementation doesn't supportnullkeys.
-
put
Put a new entry into thisContextMap.- Type Parameters:
T- The type of object associated with thekey.- Parameters:
key- TheContextMap.Keyused to index thevalue.value- The value to put.- Returns:
- The previous value associated with the
key, ornullif there was none.nullcan also indicate the value associated with thekeywasnull(ifnullvalues are supported by the implementation). - Throws:
NullPointerException- (optional behavior) ifkeyorvalueisnulland the implementation doesn't supportnullkeys or values.UnsupportedOperationException- if this method is not supported.
-
putIfAbsent
Put a new entry into thisContextMapif this map does not already contain thiskeyor is mapped tonull.- Type Parameters:
T- The type of object associated with thekey.- Parameters:
key- TheContextMap.Keyused to index thevalue.value- The value to put.- Returns:
- The previous value associated with the
key, ornullif there was none.nullcan also indicate the value associated with thekeywasnull(ifnullvalues are supported by the implementation). - Throws:
NullPointerException- (optional behavior) ifkeyorvalueisnulland the implementation doesn't supportnullkeys 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 thisContextMapif this map does not already contain thiskeyor is mapped tonull.- Type Parameters:
T- The type of object associated with thekey.- Parameters:
key- TheContextMap.Keyused 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, ornullif the computed value isnull. - Throws:
NullPointerException- (optional behavior) ifkeyor computedvalueisnulland the implementation doesn't supportnullkeys 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 themapentries has anullkeyorvalueand the implementation doesn't supportnullkeys or values.ConcurrentModificationException- done on a best effort basis if the passedmapis 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 themapentries has anullkeyorvalueand the implementation doesn't supportnullkeys or values.ConcurrentModificationException- done on a best effort basis if the passedmapis 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.Keywhich identifies an entry for removal.- Returns:
- The previous value associated with the
key, ornullif there was none.nullcan also indicate the value associated with thekeywasnull(ifnullvalues are supported by the implementation). If theContextMapimplementation is immutable this may be a new object. - Throws:
NullPointerException- (optional behavior) ifkeyisnulland the implementation doesn't supportnullkeys.UnsupportedOperationException- if this method is not supported.
-
removeAll
Remove all entries from thisContextMapassociated with the keys from the passedIterable.- Parameters:
keys- TheContextMap.Keys that identify entries for removal.- Returns:
trueif this map has changed as a result of this operation.- Throws:
NullPointerException- (optional behavior) if any of thekeysisnulland the implementation doesn't supportnullkeys.ConcurrentModificationException- Done on a best effort basis ifentriesis 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 returntrueif it wants to keep iterating orfalseto stop iteration at the current entry.- Returns:
nullifconsumeriterated through all entries or theContextMap.Keyat which the iteration stopped.- Throws:
NullPointerException- ifconsumerisnull.
-
copy
ContextMap copy()Create an isolated copy of the current map. The return value contents are the same as thisContextMapbut modifications to thisContextMapare not visible in the return value, and visa-versa.- Returns:
- an isolated copy of the current map. The contents are the same as this
ContextMapbut modifications to thisContextMapare not visible in the return value, and visa-versa.
-
equals
Determines if the specified object is equal to thisContextMap.- Overrides:
equalsin classObject- Parameters:
o- object to be compared for equality with thisContextMap.- Returns:
trueif the passed object is aContextMapand has the same key-value mapping entries.- See Also:
-
hashCode
int hashCode()Returns the hash code value for thisContextMap.- Overrides:
hashCodein classObject- Returns:
- the hash code value for this
ContextMap, taking into account the hash codes of each key-value mapping entries thisContextMapcontains. - See Also:
-