Top Description Methods
reactor.util.context

public Interface Context

extends ContextView
Known Direct Subinterfaces
reactor.util.context.CoreContext
Imports
java.util.*, reactor.util.annotation.Nullable

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, which could be more costly. Past five user key/value pair, the Context will use a copy-on-write implementation backed by a new java.util.Map on each put.

Author
Stephane Maldini

Method Summary

Modifier and TypeMethod and Description
public Context

Returns:

a new Context that doesn't include the provided key
delete
(Object
the key to remove.
key
)

Return a new Context that will resolve all existing keys except the removed one, key.

public static Context

Returns:

an empty Context
empty
()

Return an empty Context

public static Context

Returns:

a Context with a single entry.
of
(Object
the key to initialize.
key
,
Object
the value for the key.
value
)

Create a Context pre-initialized with one key-value pair.

public static Context

Returns:

a Context with two entries.
of
(Object
the first key to initialize.
key1
,
Object
the value for the first key.
value1
,
Object
the second key to initialize.
key2
,
Object
the value for the second key.
value2
)

Create a Context pre-initialized with two key-value pairs.

public static Context

Returns:

a Context with three entries.
of
(Object
the first key to initialize.
key1
,
Object
the value for the first key.
value1
,
Object
the second key to initialize.
key2
,
Object
the value for the second key.
value2
,
Object
the third key to initialize.
key3
,
Object
the value for the third key.
value3
)

Create a Context pre-initialized with three key-value pairs.

public static Context

Returns:

a Context with four entries.
of
(Object
the first key to initialize.
key1
,
Object
the value for the first key.
value1
,
Object
the second key to initialize.
key2
,
Object
the value for the second key.
value2
,
Object
the third key to initialize.
key3
,
Object
the value for the third key.
value3
,
Object
the fourth key to initialize.
key4
,
Object
the value for the fourth key.
value4
)

Create a Context pre-initialized with four key-value pairs.

public static Context

Returns:

a Context with five entries.
of
(Object
the first key to initialize.
key1
,
Object
the value for the first key.
value1
,
Object
the second key to initialize.
key2
,
Object
the value for the second key.
value2
,
Object
the third key to initialize.
key3
,
Object
the value for the third key.
value3
,
Object
the fourth key to initialize.
key4
,
Object
the value for the fourth key.
value4
,
Object
the fifth key to initialize.
key5
,
Object
the value for the fifth key.
value5
)

Create a Context pre-initialized with five key-value pairs.

public static Context
of(Map<?, ?> map)

Create a Context out of a Map.

public static Context

Returns:

the converted Context for further modifications
of
(ContextView
the ContextView to convert (or cast) to Context
contextView
)

Create a Context out of a ContextView, enabling write API on top of the read-only view.

public Context

Returns:

a new Context including the provided key/value
put
(Object
the key to add/update in the new Context
key
,
Object
the value to associate to the key in the new Context
value
)

Create a new Context that contains all current key/value pairs plus the given key/value pair.

public default Context

Returns:

a new Context with a merge of the entries from this context and the given context.
putAll
(ContextView
the other ContextView from which to copy entries
other
)

Create a new Context by merging the content of this context and a given ContextView.

public default Context

Returns:

a new Context with a merge of the entries from this context and the given context.
putAll
(Context
the Context from which to copy entries
context
)

Deprecated will be removed in 3.5, kept for backward compatibility with 3.3.
See putAll(ContextView).
public default Context

Returns:

a new Context with a merge of the entries from this context and the given Map.
putAllMap
(Map<?, ?>
the Map from which to include entries in the resulting Context.
from
)

Create a new Context by merging the content of this context and a given Map.

public default Context

Returns:

a new Context including the provided key/value, or the same Context if value is null
putNonNull
(Object
the key to add/update in the new Context
key
,
Object
the value to associate to the key in the new Context, null to ignore the operation
valueOrNull
)

Create a new Context that contains all current key/value pairs plus the given key/value pair only if the value is not null.

public default ContextView

Returns:

the ContextView of this context
readOnly
()

Switch to the ContextView interface, which only allows reading from the context.

Inherited from reactor.util.context.ContextView:
forEachgetgetgetOrDefaultgetOrEmptyhasKeyisEmptysizestream

Method Detail

deleteback to summary
public Context delete(Object key)

Return a new Context that will resolve all existing keys except the removed one, key.

Note that if this Context doesn't contain the key, this method simply returns this same instance.

Parameters
key:Object

the key to remove.

Returns:Context

a new Context that doesn't include the provided key

emptyback to summary
public static Context empty()

Return an empty Context

Returns:Context

an empty Context

ofback to summary
public static Context of(Object key, Object value)

Create a Context pre-initialized with one key-value pair.

Parameters
key:Object

the key to initialize.

value:Object

the value for the key.

Returns:Context

a Context with a single entry.

Exceptions
NullPointerException:
if either key or value are null
ofback to summary
public static Context of(Object key1, Object value1, Object key2, Object value2)

Create a Context pre-initialized with two key-value pairs.

Parameters
key1:Object

the first key to initialize.

value1:Object

the value for the first key.

key2:Object

the second key to initialize.

value2:Object

the value for the second key.

Returns:Context

a Context with two entries.

Exceptions
NullPointerException:
if any key or value is null
ofback to summary
public static Context of(Object key1, Object value1, Object key2, Object value2, Object key3, Object value3)

Create a Context pre-initialized with three key-value pairs.

Parameters
key1:Object

the first key to initialize.

value1:Object

the value for the first key.

key2:Object

the second key to initialize.

value2:Object

the value for the second key.

key3:Object

the third key to initialize.

value3:Object

the value for the third key.

Returns:Context

a Context with three entries.

Exceptions
NullPointerException:
if any key or value is null
ofback to summary
public static Context of(Object key1, Object value1, Object key2, Object value2, Object key3, Object value3, Object key4, Object value4)

Create a Context pre-initialized with four key-value pairs.

Parameters
key1:Object

the first key to initialize.

value1:Object

the value for the first key.

key2:Object

the second key to initialize.

value2:Object

the value for the second key.

key3:Object

the third key to initialize.

value3:Object

the value for the third key.

key4:Object

the fourth key to initialize.

value4:Object

the value for the fourth key.

Returns:Context

a Context with four entries.

Exceptions
NullPointerException:
if any key or value is null
ofback to summary
public static Context of(Object key1, Object value1, Object key2, Object value2, Object key3, Object value3, Object key4, Object value4, Object key5, Object value5)

Create a Context pre-initialized with five key-value pairs.

Parameters
key1:Object

the first key to initialize.

value1:Object

the value for the first key.

key2:Object

the second key to initialize.

value2:Object

the value for the second key.

key3:Object

the third key to initialize.

value3:Object

the value for the third key.

key4:Object

the fourth key to initialize.

value4:Object

the value for the fourth key.

key5:Object

the fifth key to initialize.

value5:Object

the value for the fifth key.

Returns:Context

a Context with five entries.

Exceptions
NullPointerException:
if any key or value is null
ofback to summary
public static Context of(Map<?, ?> map)

Create a Context out of a Map. Prefer this method if you're somehow incapable of checking keys are all distinct in other of(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.

ofback to summary
public static Context of(ContextView contextView)

Create a Context out of a ContextView, enabling write API on top of the read-only view. If the ContextView is already a Context, return the same instance.

Parameters
contextView:ContextView

the ContextView to convert (or cast) to Context

Returns:Context

the converted Context for further modifications

putback to summary
public Context put(Object key, Object value)

Create a new Context that 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 resulting Context.

Parameters
key:Object

the key to add/update in the new Context

value:Object

the value to associate to the key in the new Context

Returns:Context

a new Context including the provided key/value

Exceptions
NullPointerException:
if either the key or value are null
putAllback to summary
public default Context putAll(ContextView other)

Create a new Context by merging the content of this context and a given ContextView. If the other context is empty, the same Context instance is returned.

Parameters
other:ContextView

the other ContextView from which to copy entries

Returns:Context

a new Context with a merge of the entries from this context and the given context.

putAllback to summary
public default Context putAll(Context context)

Deprecated

will be removed in 3.5, kept for backward compatibility with 3.3. Until then if you need to work around the deprecation, use putAll(ContextView) combined with readOnly()

See putAll(ContextView).

Parameters
context:Context

the Context from which to copy entries

Returns:Context

a new Context with a merge of the entries from this context and the given context.

Annotations
@Deprecated
putAllMapback to summary
public default Context putAllMap(Map<?, ?> from)

Create a new Context by merging the content of this context and a given Map. If the Map is empty, the same Context instance is returned.

Parameters
from:Map<?, ?>

the Map from which to include entries in the resulting Context.

Returns:Context

a new Context with a merge of the entries from this context and the given Map.

putNonNullback to summary
public default Context putNonNull(Object key, Object valueOrNull)

Create a new Context that 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 resulting Context.

Parameters
key:Object

the key to add/update in the new Context

valueOrNull:Object

the value to associate to the key in the new Context, null to ignore the operation

Returns:Context

a new Context including the provided key/value, or the same Context if value is null

Exceptions
NullPointerException:
if the key is null
readOnlyback to summary
public default ContextView readOnly()

Switch to the ContextView interface, which only allows reading from the context.

Returns:ContextView

the ContextView of this context