From 0a024d65d9db1f96dccae4b62ef239a5d700d3b6 Mon Sep 17 00:00:00 2001 From: jmp Date: Tue, 15 Dec 2020 04:09:29 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20set,=20contains,=20and=20asMa?= =?UTF-8?q?p=20methods=20to=20CommandContext?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../context/CommandContext.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java b/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java index 14adf180..0e9fee76 100644 --- a/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java +++ b/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java @@ -171,6 +171,46 @@ public final class CommandContext { 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. + *

+ * 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 Value type + * @since 1.3.0 + */ + public 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()} * if no value is stored with the given key