Interface ContextSet
-
- All Known Subinterfaces:
ImmutableContextSet,MutableContextSet
public interface ContextSet extends Iterable<Context>
A set of contexts.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 describes the type of the context, and the value represents what the key is set to.
For example, a context with
key=worldandvalue=world_netherdescribes that a subject is in the "world_nether" world.Contexts are exposed to end users and manipulated when managing permissions. For this reason, context keys should strike a balance between being descriptive and succinct.
Context keys and values are case-insensitive, and will be automatically converted to
lowercasewhen added to a context set. Keys and values cannot be null or empty (len=0 or consisting of only whitespace).If a context key is formed of more than one word, the parts should be separated by the '
-' character. (e.g. "server-type")If a context key is likely to conflict with another plugin, it should be appropriately namespaced using the name of the plugin providing the context. The namespace should be at the start of the context key, and separated using the '
:' character. (e.g. "worldguard:region" for WorldGuard regions)Contexts can be combined with each other to form so called "context sets" - simply a collection of context pairs.
Two default ContextSet implementations are provided.
MutableContextSetallows the addition and removal of context keys after construction, andImmutableContextSetdoes not.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Deprecated Methods Modifier and Type Method Description booleancontains(@NonNull String key, @NonNull String value)Returns if theContextSetcontains a given context pairing.default booleancontains(@NonNull Context entry)Returns if theContextSetcontains a given context pairing.default booleancontainsAny(@NonNull String key, @NonNull Iterable<String> values)Returns if theContextSetcontains any of the given context pairings.booleancontainsKey(@NonNull String key)Returns if theContextSetcontains at least one value for the given key.default @NonNull Optional<String>getAnyValue(@NonNull String key)Returns any value from thisContextSetmatching the key, if present.@NonNull @Unmodifiable Set<String>getValues(@NonNull String key)Returns aSetof the values mapped to the given key.@NonNull ImmutableContextSetimmutableCopy()Returns an immutable representation of thisContextSet.booleanisEmpty()Returns if theContextSetis empty.booleanisImmutable()Gets if thisContextSetis immutable.default booleanisSatisfiedBy(@NonNull ContextSet other)Returns if thisContextSetis "satisfied" by another set.booleanisSatisfiedBy(@NonNull ContextSet other, @NonNull ContextSatisfyMode mode)Returns if thisContextSetis "satisfied" by another set, according to the givenmode.@NonNull @Unmodifiable Iterator<Context>iterator()Returns anIteratorover each of the context pairs in this set.@NonNull MutableContextSetmutableCopy()Creates a mutable copy of thisContextSet.intsize()Gets the number of context pairs in theContextSet.@NonNull @Unmodifiable Map<String,String>toFlattenedMap()Deprecated.Deprecated because the returned map may not contain all data in the ContextSet@NonNull @Unmodifiable Map<String,Set<String>>toMap()Returns aMaprepresenting the current state of thisContextSet.@NonNull @Unmodifiable Set<Context>toSet()-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
isImmutable
boolean isImmutable()
Gets if thisContextSetis immutable.The state of immutable instances will never change.
- Returns:
- true if the set is immutable
-
immutableCopy
@NonNull ImmutableContextSet immutableCopy()
Returns an immutable representation of thisContextSet.If the set is already immutable, the same object will be returned. If the set is mutable, an immutable copy will be made.
- Returns:
- an immutable representation of this set
-
mutableCopy
@NonNull MutableContextSet mutableCopy()
Creates a mutable copy of thisContextSet.A new copy is returned regardless of the
mutabilityof this set.- Returns:
- a mutable ContextSet
-
toSet
@NonNull @Unmodifiable Set<Context> toSet()
Returns aSetofContexts representing the current state of thisContextSet.The returned set is immutable, and is a copy of the current set. (will not update live)
- Returns:
- an immutable set
-
toMap
@NonNull @Unmodifiable Map<String,Set<String>> toMap()
Returns aMaprepresenting the current state of thisContextSet.The returned set is immutable, and is a copy of the current set. (will not update live)
- Returns:
- a map
-
toFlattenedMap
@Deprecated @NonNull @Unmodifiable Map<String,String> toFlattenedMap()
Deprecated.Deprecated because the returned map may not contain all data in the ContextSetReturns aMaploosely representing the current state of thisContextSet.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.
- Returns:
- an immutable map
-
iterator
@NonNull @Unmodifiable Iterator<Context> iterator()
Returns anIteratorover 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.
-
containsKey
boolean containsKey(@NonNull String key)
Returns if theContextSetcontains at least one value for the given key.- Parameters:
key- the key to check for- Returns:
- true if the set contains a value for the key
- Throws:
NullPointerException- if the key is null
-
getValues
@NonNull @Unmodifiable Set<String> getValues(@NonNull String key)
Returns aSetof 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)- Parameters:
key- the key to get values for- Returns:
- a set of values
- Throws:
NullPointerException- if the key is null
-
getAnyValue
default @NonNull Optional<String> getAnyValue(@NonNull String key)
Returns any value from thisContextSetmatching the key, if present.Note that context keys can be mapped to multiple values. Use
getValues(String)to retrieve all associated values.- Parameters:
key- the key to find values for- Returns:
- an optional containing any match
-
contains
boolean contains(@NonNull String key, @NonNull String value)
Returns if theContextSetcontains a given context pairing.- Parameters:
key- the key to look forvalue- the value to look for- Returns:
- true if the set contains the context pair
- Throws:
NullPointerException- if the key or value is null
-
contains
default boolean contains(@NonNull Context entry)
Returns if theContextSetcontains a given context pairing.- Parameters:
entry- the entry to look for- Returns:
- true if the set contains the context pair
- Throws:
NullPointerException- if the key or value is null
-
containsAny
default boolean containsAny(@NonNull String key, @NonNull Iterable<String> values)
Returns if theContextSetcontains any of the given context pairings.- Parameters:
key- the key to look forvalues- the values to look for- Returns:
- true if the set contains any of the pairs
- Since:
- 5.2
-
isSatisfiedBy
default boolean isSatisfiedBy(@NonNull ContextSet other)
Returns if thisContextSetis "satisfied" by another set.ContextSatisfyMode.AT_LEAST_ONE_VALUE_PER_KEYis the mode used by this method.- Parameters:
other- the other set- Returns:
- true if this context set is satisfied by the other
-
isSatisfiedBy
boolean isSatisfiedBy(@NonNull ContextSet other, @NonNull ContextSatisfyMode mode)
Returns if thisContextSetis "satisfied" by another set, according to the givenmode.- Parameters:
other- the other setmode- the mode to use- Returns:
- true if this context set is satisfied by the other
- Since:
- 5.2
-
isEmpty
boolean isEmpty()
Returns if theContextSetis empty.- Returns:
- true if the set is empty
-
size
int size()
Gets the number of context pairs in theContextSet.- Returns:
- the size of the set
-
-