Package com.mongodb
Interface RequestContext
-
public interface RequestContext
The request context, useful for implementing distributed tracing.- Since:
- 4.4
- See Also:
MongoClientSettings.getContextProvider()
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description void
delete(Object key)
Delete the given key and its associated value from the RequestContext.default <T> T
get(Class<T> key)
Resolve a value given a type key within theRequestContext
.<T> T
get(Object key)
Resolve a value given a key that exists within theRequestContext
, or throw aNoSuchElementException
if the key is not present.default <T> T
getOrDefault(Object key, T defaultValue)
Resolve a value given a key within theRequestContext
.default <T> Optional<T>
getOrEmpty(Object key)
Resolve a value given a key within theRequestContext
.boolean
hasKey(Object key)
Return true if a particular key resolves to a value within theRequestContext
.boolean
isEmpty()
Return true if theRequestContext
is empty.void
put(Object key, Object value)
Modifies this instance with the given key and value.default void
putNonNull(Object key, Object valueOrNull)
Modifies this instance with the given key and value only if the value is not null.int
size()
Return the size of thisRequestContext
, the number of key/value pairs stored inside it.Stream<Map.Entry<Object,Object>>
stream()
Stream key/value pairs from thisRequestContext
-
-
-
Method Detail
-
get
<T> T get(Object key)
Resolve a value given a key that exists within theRequestContext
, or throw aNoSuchElementException
if the key is not present.- Type Parameters:
T
- an unchecked casted generic for fluent typing convenience- Parameters:
key
- a lookup key to resolve the value within the context- Returns:
- the value resolved for this key (throws if key not found)
- Throws:
NoSuchElementException
- when the given key is not present- See Also:
getOrDefault(Object, Object)
,getOrEmpty(Object)
,hasKey(Object)
-
get
default <T> T get(Class<T> key)
Resolve a value given a type key within theRequestContext
.- Type Parameters:
T
- an unchecked casted generic for fluent typing convenience- Parameters:
key
- a type key to resolve the value within the context- Returns:
- the value resolved for this type key (throws if key not found)
- Throws:
NoSuchElementException
- when the given type key is not present- See Also:
getOrDefault(Object, Object)
,getOrEmpty(Object)
-
getOrDefault
@Nullable default <T> T getOrDefault(Object key, @Nullable T defaultValue)
Resolve a value given a key within theRequestContext
. If unresolved return the passed default value.- Type Parameters:
T
- an unchecked casted generic for fluent typing convenience- Parameters:
key
- a lookup key to resolve the value within the contextdefaultValue
- a fallback value if key doesn't resolve- Returns:
- the value resolved for this key, or the given default if not present
-
getOrEmpty
default <T> Optional<T> getOrEmpty(Object key)
Resolve a value given a key within theRequestContext
.- Type Parameters:
T
- an unchecked casted generic for fluent typing convenience- Parameters:
key
- a lookup key to resolve the value within the context- Returns:
- an
Optional
of the value for that key.
-
hasKey
boolean hasKey(Object key)
Return true if a particular key resolves to a value within theRequestContext
.- Parameters:
key
- a lookup key to test for- Returns:
- true if this context contains the given key
-
isEmpty
boolean isEmpty()
Return true if theRequestContext
is empty.- Returns:
- true if the
RequestContext
is empty.
-
put
void put(Object key, Object value)
Modifies this instance with the given key and value. If that key existed in the currentRequestContext
, its associated value is replaced.- Parameters:
key
- the key to add/updatevalue
- the value to associate to the key- Throws:
NullPointerException
- if either the key or value are null
-
putNonNull
default void putNonNull(Object key, @Nullable Object valueOrNull)
Modifies this instance with the given key and value only if the value is not null. If that key existed in the current Context, its associated value is replaced in the resultingRequestContext
.- Parameters:
key
- the key to add/updatevalueOrNull
- the value to associate to the key, null to ignore the operation- Throws:
NullPointerException
- if the key is null
-
delete
void delete(Object key)
Delete the given key and its associated value from the RequestContext.- Parameters:
key
- the key to remove.
-
size
int size()
Return the size of thisRequestContext
, the number of key/value pairs stored inside it.- Returns:
- the size of the
RequestContext
-
stream
Stream<Map.Entry<Object,Object>> stream()
Stream key/value pairs from thisRequestContext
It is not specified whether modification of a
Map.Entry
instance in theStream
results in a modification of the state of theRequestContext
, or whether theMap.Entry
instances are modifiable. That is considered an implementation detail, so users of this method should not rely on the behavior one way or the other unless the implementing class has documented it.- Returns:
- a
Stream
of key/value pairs held by this context
-
-