public interface Context
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 |
default <T> T |
get(Class<T> key)
Resolve a value given a type key within the
Context . |
<T> T |
get(Object key)
Resolve a value given a key that exists within the
Context , or throw
a NoSuchElementException if the key is not present. |
default <T> T |
getOrDefault(Object key,
T defaultValue)
Resolve a value given a key within the
Context . |
default <T> Optional<T> |
getOrEmpty(Object key)
Resolve a value given a key within the
Context . |
boolean |
hasKey(Object key)
Return true if a particular key resolves to a value within the
Context . |
default boolean |
isEmpty()
Return true if the
Context is empty. |
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 other)
|
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. |
int |
size()
Return the size of this
Context , the number of immutable key/value pairs stored inside it. |
Stream<Map.Entry<Object,Object>> |
stream()
Stream key/value pairs from this
Context |
static 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.<T> T get(Object key)
Context
, or throw
a NoSuchElementException
if the key is not present.T
- an unchecked casted generic for fluent typing conveniencekey
- a lookup key to resolve the value within the contextNoSuchElementException
- when the given key is not presentgetOrDefault(Object, Object)
,
getOrEmpty(Object)
,
hasKey(Object)
default <T> T get(Class<T> key)
Context
.T
- an unchecked casted generic for fluent typing conveniencekey
- a type key to resolve the value within the contextNoSuchElementException
- when the given type key is not presentgetOrDefault(Object, Object)
,
getOrEmpty(Object)
@Nullable default <T> T getOrDefault(Object key, @Nullable T defaultValue)
Context
. If unresolved return the
passed default value.key
- a lookup key to resolve the value within the contextdefaultValue
- a fallback value if key doesn't resolvedefault <T> Optional<T> getOrEmpty(Object key)
Context
.key
- a lookup key to resolve the value within the contextOptional
of the value for that key.boolean hasKey(Object key)
Context
.key
- a lookup key to test fordefault boolean isEmpty()
Context
is empty.Context
is empty.Context 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 Context
value
- the value to associate to the key in the new Context
Context
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 Context
valueOrNull
- 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 keyint size()
Context
, the number of immutable key/value pairs stored inside it.Context
Stream<Map.Entry<Object,Object>> stream()
Context
Stream
of key/value pairs held by this contextdefault Context putAll(Context other)
Context
by merging the content of this context and a given
Context
. If the other context is empty, the same Context
instance
is returned.other
- the other Context to get values from