From 90efd93b72037f0a456b22439f652d067a7ae93d Mon Sep 17 00:00:00 2001 From: Leo Galambos Date: Tue, 12 Aug 2025 23:06:19 +0200 Subject: [PATCH] feat: removeContext implemented Signed-off-by: Leo Galambos --- .classpath | 13 ----------- src/main/java/conflux/Ctx.java | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/.classpath b/.classpath index 68d13e3..282ada5 100644 --- a/.classpath +++ b/.classpath @@ -13,19 +13,6 @@ - - - - - - - - - - - - - diff --git a/src/main/java/conflux/Ctx.java b/src/main/java/conflux/Ctx.java index eba3769..98dcb67 100644 --- a/src/main/java/conflux/Ctx.java +++ b/src/main/java/conflux/Ctx.java @@ -85,6 +85,46 @@ public enum Ctx implements CtxInterface { return contexts.computeIfAbsent(name, k -> new CtxInstance()); } + /** + * Removes a named context previously created via {@link #getContext(String)}. + *

+ * The context is atomically removed from the internal registry and then + * {@linkplain CtxInterface#clear() cleared} to release stored values and + * listener references. This operation is thread-safe and idempotent: if the + * specified context does not exist, the method returns {@code false} and + * performs no action. + *

+ *

+ * Calling {@code getContext(name)} again after a successful removal will create + * a brand-new, empty context. The default/global context represented by + * {@link #INSTANCE} is not affected. + *

+ * + * @param name the name of the context to remove; must not be {@code null} or + * blank + * @return {@code true} if a context with the given name existed and was + * removed; {@code false} otherwise + * @throws IllegalArgumentException if {@code name} is {@code null} or blank + * @implNote Uses {@link ConcurrentHashMap#remove(Object)} for atomic removal, + * then invokes {@link CtxInterface#clear()} on the removed instance. + * @since 2025.08 + */ + public boolean removeContext(String name) { + if (name == null || name.isBlank()) { + throw new IllegalArgumentException("name must not be null or blank"); + } + CtxInterface ctx = contexts.remove(name); + final boolean existed = ctx != null; + if (existed) { + try { + ctx.clear(); + } catch (RuntimeException ignore) { // NOPMD + // Best-effort cleanup; deregistration already succeeded. + } + } + return existed; + } + /** * Returns the set of names for all currently registered contexts. *