public interface Context extends ContextView
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).
| Modifier and Type | Method and Description |
|---|---|
Context |
delete(Object key)
Return a new
Context that will resolve all existing keys except the
removed one, key. |
static Context |
empty()
Return an empty
Context |
static Context |
of(ContextView contextView)
Create a
Context out of a ContextView, enabling write API on top of
the read-only view. |
static Context |
of(Map<?,?> map)
|
static Context |
of(Object key,
Object value)
Create a
Context pre-initialized with one key-value pair. |
static Context |
of(Object key1,
Object value1,
Object key2,
Object value2)
Create a
Context pre-initialized with two key-value pairs. |
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. |
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. |
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. |
Context |
put(Object key,
Object value)
Create a new
Context that contains all current key/value pairs plus the
given key/value pair. |
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() |
default Context |
putAll(ContextView other)
Create a new
Context by merging the content of this context and a given
ContextView. |
default Context |
putAllMap(Map<?,?> from)
|
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. |
default ContextView |
readOnly()
Switch to the
ContextView interface, which only allows reading from the
context. |
forEach, get, get, getOrDefault, getOrEmpty, hasKey, isEmpty, size, streamstatic Context of(Object key, Object value)
Context pre-initialized with one key-value pair.key - the key to initialize.value - the value for the key.Context with a single entry.NullPointerException - if either key or value are nullstatic Context of(Object key1, Object value1, Object key2, Object value2)
Context pre-initialized with two key-value pairs.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.Context with two entries.NullPointerException - if any key or value is nullstatic Context of(Object key1, Object value1, Object key2, Object value2, Object key3, Object value3)
Context pre-initialized with three key-value pairs.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.Context with three entries.NullPointerException - if any key or value is nullstatic Context of(Object key1, Object value1, Object key2, Object value2, Object key3, Object value3, Object key4, Object value4)
Context pre-initialized with four key-value pairs.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.Context with four entries.NullPointerException - if any key or value is nullstatic Context of(Object key1, Object value1, Object key2, Object value2, Object key3, Object value3, Object key4, Object value4, Object key5, Object value5)
Context pre-initialized with five key-value pairs.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.Context with five entries.NullPointerException - if any key or value is nullstatic Context of(Map<?,?> map)
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.static Context of(ContextView contextView)
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.contextView - the ContextView to convert (or cast) to ContextContext for further modificationsdefault ContextView readOnly()
ContextView interface, which only allows reading from the
context.ContextView of this contextContext put(Object key, Object value)
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.key - the key to add/update in the new Contextvalue - the value to associate to the key in the new ContextContext including the provided key/valueNullPointerException - if either the key or value are nulldefault Context putNonNull(Object key, @Nullable Object valueOrNull)
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.key - the key to add/update in the new ContextvalueOrNull - the value to associate to the key in the new Context, null to ignore the operationContext including the provided key/value, or the same Context if value is nullNullPointerException - if the key is nullContext delete(Object key)
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.
key - the key to remove.Context that doesn't include the provided keydefault Context putAll(ContextView other)
Context by merging the content of this context and a given
ContextView. If the other context is empty, the same Context instance
is returned.other - the other ContextView from which to copy entriesContext with a merge of the entries from this context and the given context.@Deprecated default Context putAll(Context context)
putAll(ContextView)
combined with readOnly()putAll(ContextView).