✨ Add keyed values to CommandContext and do some cleanup
This commit is contained in:
parent
9276a919d3
commit
bb9bc4e579
1 changed files with 41 additions and 0 deletions
|
|
@ -194,6 +194,19 @@ public final class CommandContext<C> {
|
||||||
* @param value Value
|
* @param value Value
|
||||||
* @param <T> Value type
|
* @param <T> Value type
|
||||||
*/
|
*/
|
||||||
|
public <T> void store(final @NonNull CommandArgument<C, T> keyHolder, final @NonNull T value) {
|
||||||
|
this.store((CloudKeyHolder<T>) keyHolder, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a value in the context map. This will overwrite any existing
|
||||||
|
* value stored with the same key
|
||||||
|
*
|
||||||
|
* @param keyHolder Holder of the identifying key
|
||||||
|
* @param value Value
|
||||||
|
* @param <T> Value type
|
||||||
|
* @since 1.4.0
|
||||||
|
*/
|
||||||
public <T> void store(final @NonNull CloudKeyHolder<T> keyHolder, final @NonNull T value) {
|
public <T> void store(final @NonNull CloudKeyHolder<T> keyHolder, final @NonNull T value) {
|
||||||
this.internalStorage.put(keyHolder.getKey(), value);
|
this.internalStorage.put(keyHolder.getKey(), value);
|
||||||
}
|
}
|
||||||
|
|
@ -316,6 +329,20 @@ public final class CommandContext<C> {
|
||||||
* @return Value
|
* @return Value
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
|
public <T> @NonNull Optional<T> getOptional(final @NonNull CommandArgument<C, T> keyHolder) {
|
||||||
|
return this.getOptional((CloudKeyHolder<T>) keyHolder);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a value from its key. Will return {@link Optional#empty()}
|
||||||
|
* if no value is stored with the given key
|
||||||
|
*
|
||||||
|
* @param keyHolder Holder of the key
|
||||||
|
* @param <T> Value type
|
||||||
|
* @return Value
|
||||||
|
* @since 1.4.0
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unused")
|
||||||
public <T> @NonNull Optional<T> getOptional(final @NonNull CloudKeyHolder<T> keyHolder) {
|
public <T> @NonNull Optional<T> getOptional(final @NonNull CloudKeyHolder<T> keyHolder) {
|
||||||
final Object value = this.internalStorage.get(keyHolder.getKey());
|
final Object value = this.internalStorage.get(keyHolder.getKey());
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
|
|
@ -391,6 +418,20 @@ public final class CommandContext<C> {
|
||||||
* @return Stored value
|
* @return Stored value
|
||||||
* @throws NullPointerException If no such value is stored
|
* @throws NullPointerException If no such value is stored
|
||||||
*/
|
*/
|
||||||
|
public <T> @NonNull T get(final @NonNull CommandArgument<C, T> keyHolder) {
|
||||||
|
return this.get(keyHolder.getKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a required argument from the context. This will thrown an exception
|
||||||
|
* if there's no value associated with the given argument
|
||||||
|
*
|
||||||
|
* @param keyHolder Holder of the identifying key
|
||||||
|
* @param <T> Argument type
|
||||||
|
* @return Stored value
|
||||||
|
* @throws NullPointerException If no such value is stored
|
||||||
|
* @since 1.4.0
|
||||||
|
*/
|
||||||
public <T> @NonNull T get(final @NonNull CloudKeyHolder<T> keyHolder) {
|
public <T> @NonNull T get(final @NonNull CloudKeyHolder<T> keyHolder) {
|
||||||
return this.get(keyHolder.getKey());
|
return this.get(keyHolder.getKey());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue