✨ Add set, contains, and asMap methods to CommandContext
This commit is contained in:
parent
e0216dec9f
commit
0a024d65d9
1 changed files with 40 additions and 0 deletions
|
|
@ -171,6 +171,46 @@ public final class CommandContext<C> {
|
||||||
this.internalStorage.put(key, value);
|
this.internalStorage.put(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store or remove a value in the context map. This will overwrite any existing
|
||||||
|
* value stored with the same key.
|
||||||
|
* <p>
|
||||||
|
* If the provided value is {@code null}, any current value stored for the provided key will be removed.
|
||||||
|
*
|
||||||
|
* @param key Key
|
||||||
|
* @param value Value
|
||||||
|
* @param <T> Value type
|
||||||
|
* @since 1.3.0
|
||||||
|
*/
|
||||||
|
public <T> void set(final @NonNull String key, final @Nullable T value) {
|
||||||
|
if (value != null) {
|
||||||
|
this.store(key, value);
|
||||||
|
} else {
|
||||||
|
this.remove(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the context has a value stored for a key
|
||||||
|
*
|
||||||
|
* @param key Key
|
||||||
|
* @return Whether the context has a value for the provided key
|
||||||
|
* @since 1.3.0
|
||||||
|
*/
|
||||||
|
public boolean contains(final @NonNull String key) {
|
||||||
|
return this.internalStorage.containsKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current state of this command context as a map of String to context value.
|
||||||
|
*
|
||||||
|
* @return An immutable copy of this command context as a map
|
||||||
|
* @since 1.3.0
|
||||||
|
*/
|
||||||
|
public @NonNull Map<@NonNull String, @Nullable ?> asMap() {
|
||||||
|
return Collections.unmodifiableMap(this.internalStorage);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a value from its key. Will return {@link Optional#empty()}
|
* Get a value from its key. Will return {@link Optional#empty()}
|
||||||
* if no value is stored with the given key
|
* if no value is stored with the given key
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue