public interface ContextSet extends Iterable<Map.Entry<String,String>>
Context in the most basic sense simply means the circumstances where something will apply.
A single "context" consists of a key and a value, both strings. The key represents the type of context, and the value represents the setting of the context key.
Contexts can be combined with each other to form so called "context sets" - simply a collection of context pairs.
Context keys and values are case-insensitive, and will be converted to
lowercase by all implementations.
Context keys and values may not be null or empty. A key/value will be deemed empty if it's length is zero, or if it consists of only space characters.
Two default ContextSet implementations are provided.
MutableContextSet allows the addition and removal of context keys
after construction, and ImmutableContextSet does not.
| Modifier and Type | Method and Description |
|---|---|
boolean |
containsKey(@NonNull String key)
Returns if the
ContextSet contains at least one value for the
given key. |
static @NonNull ImmutableContextSet |
empty()
Returns an empty
ImmutableContextSet. |
static @NonNull ImmutableContextSet |
fromEntries(@NonNull Iterable<? extends Map.Entry<String,String>> iterable)
|
static @NonNull ImmutableContextSet |
fromMap(@NonNull Map<String,String> map)
Creates an
ImmutableContextSet from an existing Map. |
static @NonNull ImmutableContextSet |
fromMultimap(@NonNull Multimap<String,String> multimap)
Creates an
ImmutableContextSet from an existing Multimap. |
static @NonNull ImmutableContextSet |
fromSet(@NonNull ContextSet contextSet)
Creates an new
ImmutableContextSet from an existing Set. |
default @NonNull Optional<String> |
getAnyValue(@NonNull String key)
Returns any value from this
ContextSet matching the key, if present. |
@NonNull Set<String> |
getValues(@NonNull String key)
Returns a
Set of the values mapped to the given key. |
default boolean |
has(Map.Entry<String,String> entry)
Returns if the
ContextSet contains a given context pairing. |
boolean |
has(@NonNull String key,
@NonNull String value)
Returns if the
ContextSet contains a given context pairing. |
boolean |
isEmpty()
Returns if the
ContextSet is empty. |
boolean |
isImmutable()
Gets if this
ContextSet is immutable. |
default boolean |
isSatisfiedBy(@NonNull ContextSet other)
Returns if this
ContextSet is fully "satisfied" by another set. |
@NonNull Iterator<Map.Entry<String,String>> |
iterator()
Returns an
Iterator over each of the context pairs in this set. |
@NonNull ImmutableContextSet |
makeImmutable()
Returns an immutable representation of this
ContextSet. |
@NonNull MutableContextSet |
mutableCopy()
Creates a mutable copy of this
ContextSet. |
static @NonNull ImmutableContextSet |
of(@NonNull String key1,
@NonNull String value1,
@NonNull String key2,
@NonNull String value2)
Creates an
ImmutableContextSet from two context pairs. |
static @NonNull ImmutableContextSet |
singleton(@NonNull String key,
@NonNull String value)
Creates an
ImmutableContextSet from a context pair. |
int |
size()
Gets the number of context pairs in the
ContextSet. |
@NonNull Map<String,String> |
toMap()
Deprecated.
because the resultant map may not contain all data in the ContextSet
|
@NonNull Multimap<String,String> |
toMultimap()
Returns a
Multimap representing the current state of this
ContextSet. |
@NonNull Set<Map.Entry<String,String>> |
toSet()
|
forEach, spliteratorstatic @NonNull ImmutableContextSet singleton(@NonNull String key, @NonNull String value)
ImmutableContextSet from a context pair.key - the keyvalue - the valueNullPointerException - if key or value is nullstatic @NonNull ImmutableContextSet of(@NonNull String key1, @NonNull String value1, @NonNull String key2, @NonNull String value2)
ImmutableContextSet from two context pairs.key1 - the first keyvalue1 - the first valuekey2 - the second keyvalue2 - the second valueNullPointerException - if any of the keys or values are nullstatic @NonNull ImmutableContextSet fromEntries(@NonNull Iterable<? extends Map.Entry<String,String>> iterable)
iterable - the iterable to copy fromNullPointerException - if the iterable is nullstatic @NonNull ImmutableContextSet fromMap(@NonNull Map<String,String> map)
ImmutableContextSet from an existing Map.map - the map to copy fromNullPointerException - if the map is nullstatic @NonNull ImmutableContextSet fromMultimap(@NonNull Multimap<String,String> multimap)
ImmutableContextSet from an existing Multimap.multimap - the multimap to copy fromNullPointerException - if the multimap is nullstatic @NonNull ImmutableContextSet fromSet(@NonNull ContextSet contextSet)
ImmutableContextSet from an existing Set.
Only really useful for converting between mutable and immutable types.
contextSet - the context set to copy fromNullPointerException - if contextSet is nullstatic @NonNull ImmutableContextSet empty()
ImmutableContextSet.boolean isImmutable()
ContextSet is immutable.
The state of immutable instances will never change.
@NonNull ImmutableContextSet makeImmutable()
ContextSet.
If the set is already immutable, the same object will be returned. If the set is mutable, an immutable copy will be made.
@NonNull MutableContextSet mutableCopy()
ContextSet.
A new copy is returned regardless of the
mutability of this set.
@NonNull Set<Map.Entry<String,String>> toSet()
Set of Map.Entrys representing the current
state of this ContextSet.
The returned set is immutable, and is a copy of the current set. (will not update live)
@Deprecated @NonNull Map<String,String> toMap()
Map loosely representing the current state of
this ContextSet.
The returned map is immutable, and is a copy of the current set. (will not update live)
As a single context key can be mapped to multiple values, this method may not be a true representation of the set.
If you need a representation of the set in a Java collection instance,
use toSet() or toMultimap() followed by
Multimap.asMap().
@NonNull Multimap<String,String> toMultimap()
Multimap representing the current state of this
ContextSet.
The returned multimap is immutable, and is a copy of the current set. (will not update live)
@NonNull Iterator<Map.Entry<String,String>> iterator()
Iterator over each of the context pairs in this set.
The returned iterator represents the state of the set at the time of creation. It is not updated as the set changes.
The iterator does not support Iterator.remove() calls.
boolean containsKey(@NonNull String key)
ContextSet contains at least one value for the
given key.key - the key to check forNullPointerException - if the key is null@NonNull Set<String> getValues(@NonNull String key)
Set of the values mapped to the given key.
The returned set is immutable, and only represents the current state
of the ContextSet. (will not update live)
key - the key to get values forNullPointerException - if the key is nulldefault @NonNull Optional<String> getAnyValue(@NonNull String key)
ContextSet matching the key, if present.
Note that context keys can be mapped to multiple values.
Use getValues(String) to retrieve all associated values.
key - the key to find values forboolean has(@NonNull String key, @NonNull String value)
ContextSet contains a given context pairing.key - the key to look forvalue - the value to look forNullPointerException - if the key or value is nulldefault boolean has(Map.Entry<String,String> entry)
ContextSet contains a given context pairing.entry - the entry to look forNullPointerException - if the key or value is nulldefault boolean isSatisfiedBy(@NonNull ContextSet other)
ContextSet is fully "satisfied" by another set.
For a context set to "satisfy" another, it must itself contain all of the context pairings in the other set.
Mathematically, this method returns true if this set is a subset of the other.
other - the other set to checkboolean isEmpty()
ContextSet is empty.int size()
ContextSet.