public interface Context
Context implementations are thread-safe put(Object, Object) will
usually return a safe new Context object.
Note that contexts are optimized for single key/value storage, and a user might want
to represent his own context instead of using more costly put(java.lang.Object, java.lang.Object).
Past one user key/value pair, the context will use a copy-on-write Context
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 value is resolvable given a key within the
Context. |
default boolean |
isEmpty()
Return true if
Context is empty. |
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)
Inject a key/value pair in a new
Context inheriting current state. |
default Context |
putAll(Context other)
|
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.static 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.static 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.static 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.static 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.<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),
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 present@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 contextboolean hasKey(Object key)
Context.key - a lookup key to resolve the value within the contextdefault boolean isEmpty()
Context is empty.Context is empty.Context 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 keyStream<Map.Entry<Object,Object>> stream()
ContextStream 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