Package reactor.util.context
Interface Context
- All Superinterfaces:
ContextView
A key/value store that is propagated between components such as operators via the
context protocol. Contexts are ideal to transport orthogonal information such as
tracing or security tokens.
Context implementations are thread-safe and immutable: mutative operations like
put(Object, Object) will in fact return a new Context instance.
Note that contexts are optimized for low cardinality key/value storage, and a user
might want to associate a dedicated mutable structure to a single key to represent his
own context instead of using multiple put(java.lang.Object, java.lang.Object), which could be more costly.
Past five user key/value pair, the Context will use a copy-on-write
implementation backed by a new Map on each put(java.lang.Object, java.lang.Object).
- Author:
- Stephane Maldini
-
Method Summary
Modifier and TypeMethodDescriptionReturn a newContextthat will resolve all existing keys except the removed one,key.static Contextempty()Return an emptyContextstatic ContextCreate aContextpre-initialized with one key-value pair.static ContextCreate aContextpre-initialized with two key-value pairs.static ContextCreate aContextpre-initialized with three key-value pairs.static Contextof(Object key1, Object value1, Object key2, Object value2, Object key3, Object value3, Object key4, Object value4) Create aContextpre-initialized with four key-value pairs.static Contextof(Object key1, Object value1, Object key2, Object value2, Object key3, Object value3, Object key4, Object value4, Object key5, Object value5) Create aContextpre-initialized with five key-value pairs.static Contextstatic Contextof(ContextView contextView) Create aContextout of aContextView, enabling write API on top of the read-only view.Create a newContextthat contains all current key/value pairs plus the given key/value pair.default ContextDeprecated.will be removed in 3.5, kept for backward compatibility with 3.3.default ContextputAll(ContextView other) Create a newContextby merging the content of this context and a givenContextView.default Contextdefault ContextputNonNull(Object key, @Nullable Object valueOrNull) Create a newContextthat contains all current key/value pairs plus the given key/value pair only if the value is not null.default ContextViewreadOnly()Switch to theContextViewinterface, which only allows reading from the context.Methods inherited from interface reactor.util.context.ContextView
forEach, get, get, getOrDefault, getOrEmpty, hasKey, isEmpty, size, stream
-
Method Details
-
empty
Return an emptyContext- Returns:
- an empty
Context
-
of
Create aContextpre-initialized with one key-value pair.- Parameters:
key- the key to initialize.value- the value for the key.- Returns:
- a
Contextwith a single entry. - Throws:
NullPointerException- if either key or value are null
-
of
Create aContextpre-initialized with two key-value pairs.- Parameters:
key1- the first key to initialize.value1- the value for the first key.key2- the second key to initialize.value2- the value for the second key.- Returns:
- a
Contextwith two entries. - Throws:
NullPointerException- if any key or value is null
-
of
static Context of(Object key1, Object value1, Object key2, Object value2, Object key3, Object value3) Create aContextpre-initialized with three key-value pairs.- Parameters:
key1- the first key to initialize.value1- the value for the first key.key2- the second key to initialize.value2- the value for the second key.key3- the third key to initialize.value3- the value for the third key.- Returns:
- a
Contextwith three entries. - Throws:
NullPointerException- if any key or value is null
-
of
static Context of(Object key1, Object value1, Object key2, Object value2, Object key3, Object value3, Object key4, Object value4) Create aContextpre-initialized with four key-value pairs.- Parameters:
key1- the first key to initialize.value1- the value for the first key.key2- the second key to initialize.value2- the value for the second key.key3- the third key to initialize.value3- the value for the third key.key4- the fourth key to initialize.value4- the value for the fourth key.- Returns:
- a
Contextwith four entries. - Throws:
NullPointerException- if any key or value is null
-
of
static Context of(Object key1, Object value1, Object key2, Object value2, Object key3, Object value3, Object key4, Object value4, Object key5, Object value5) Create aContextpre-initialized with five key-value pairs.- Parameters:
key1- the first key to initialize.value1- the value for the first key.key2- the second key to initialize.value2- the value for the second key.key3- the third key to initialize.value3- the value for the third key.key4- the fourth key to initialize.value4- the value for the fourth key.key5- the fifth key to initialize.value5- the value for the fifth key.- Returns:
- a
Contextwith five entries. - Throws:
NullPointerException- if any key or value is null
-
of
Create aContextout of aMap. Prefer this method if you're somehow incapable of checking keys are all distinct in otherof(Object, Object, Object, Object, Object, Object, Object, Object)implementations.- Implementation Note:
- this method compacts smaller maps into a relevant fields-based implementation when map size is less than 6.
-
of
Create aContextout of aContextView, enabling write API on top of the read-only view. If theContextViewis already aContext, return the same instance.- Parameters:
contextView- theContextViewto convert (or cast) toContext- Returns:
- the converted
Contextfor further modifications
-
readOnly
Switch to theContextViewinterface, which only allows reading from the context.- Returns:
- the
ContextViewof this context
-
put
Create a newContextthat contains all current key/value pairs plus the given key/value pair. If that key existed in the current Context, its associated value is replaced in the resultingContext.- Parameters:
key- the key to add/update in the newContextvalue- the value to associate to the key in the newContext- Returns:
- a new
Contextincluding the provided key/value - Throws:
NullPointerException- if either the key or value are null
-
putNonNull
Create a newContextthat contains all current key/value pairs plus the given key/value pair only if the value is not null. If that key existed in the current Context, its associated value is replaced in the resultingContext.- Parameters:
key- the key to add/update in the newContextvalueOrNull- the value to associate to the key in the newContext, null to ignore the operation- Returns:
- a new
Contextincluding the provided key/value, or the sameContextif value is null - Throws:
NullPointerException- if the key is null
-
delete
Return a newContextthat will resolve all existing keys except the removed one,key.Note that if this
Contextdoesn't contain the key, this method simply returns this same instance.- Parameters:
key- the key to remove.- Returns:
- a new
Contextthat doesn't include the provided key
-
putAll
Create a newContextby merging the content of this context and a givenContextView. If the other context is empty, the sameContextinstance is returned.- Parameters:
other- the otherContextViewfrom which to copy entries- Returns:
- a new
Contextwith a merge of the entries from this context and the given context.
-
putAllMap
-
putAll
Deprecated.will be removed in 3.5, kept for backward compatibility with 3.3. Until then if you need to work around the deprecation, useputAll(ContextView)combined withreadOnly()SeeputAll(ContextView).
-