From 50b9f5df111d09f0bd60240228a1e8b866e82976 Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Wed, 6 Dec 2023 20:54:40 +0100 Subject: [PATCH] rename MDCHelper as MDCAmbit, rename removeSet() method as clear() Signed-off-by: Ceki Gulcu --- .../src/main/java/org/slf4j/MDCAmbit.java | 40 +++++++++---------- .../src/test/java/org/slf4j/MDCAmbitTest.java | 18 ++++----- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/slf4j-api/src/main/java/org/slf4j/MDCAmbit.java b/slf4j-api/src/main/java/org/slf4j/MDCAmbit.java index 3797da972..3cf023e7a 100644 --- a/slf4j-api/src/main/java/org/slf4j/MDCAmbit.java +++ b/slf4j-api/src/main/java/org/slf4j/MDCAmbit.java @@ -35,32 +35,32 @@ *

Typical Usage example:

* *
- *  MDCHelper mdch = new MDCHelper();
+ *  MDCAmbit mdca = new MDCAmbit();
  *  try {
- *    mdch.put("k0", "v0");
+ *    mdca.put("k0", "v0");
  *    throw new RuntimeException();
  *  } catch (RuntimeException e) {
  *    // here MDC.get("k0") would return "v0"
  *  } finally {
  *    // MDC remove "k0"
- *    mdch.removeSet();
+ *    mdca.clear();
  *  }
  * 
* *

It is also possible to chain {@link #put} invocations. For example:

*
- *   MDCHelper mdch = new MDCHelper();
+ *   MDCAmbit mdca = new MDCAmbit();
  *   try {
  *     // assume "k0" was added to MDC at an earlier stage
- *     mdch.addKey("k0").put("k1", "v1").put("k2, "v2");
+ *     mdca.addKey("k0").put("k1", "v1").put("k2, "v2");
  *   } finally {
- *     // MDC remove "k0", "k1", "k2"
- *     mdch.removeSet();
+ *     // MDC remove "k0", "k1", "k2", clear the set of tracked keys
+ *     mdch.clear();
  *   }
  * 
- * @since 2.0.10 + * @since 2.1.0 */ -public class MDCHelper { +public class MDCAmbit { /** @@ -70,44 +70,44 @@ public class MDCHelper { MDCAdapter mdcAdapter; - public MDCHelper() { + public MDCAmbit() { mdcAdapter = MDC.getMDCAdapter(); } - MDCHelper(MDCAdapter mdcAdapter) { + MDCAmbit(MDCAdapter mdcAdapter) { this.mdcAdapter = mdcAdapter; } /** * Put the key/value couple in the MDC and keep track of the key for later - * removal by a call to {@link #removeSet()}. + * removal by a call to {@link #()}. * * @param key * @param value * @return this instance */ - public MDCHelper put(String key, String value) { + public MDCAmbit put(String key, String value) { mdcAdapter.put(key, value); keySet.add(key); return this; } /** - * Keep track of a key for later removal by a call to {@link #removeSet()}. + * Keep track of a key for later removal by a call to {@link #()}. * @param key * @return this instance */ - public MDCHelper addKey(String key) { + public MDCAmbit addKey(String key) { keySet.add(key); return this; } /** - * Keep track of several keys for later removal by a call to {@link #removeSet()}. + * Keep track of several keys for later removal by a call to {@link #()}. * @param keys * @return this instance */ - public MDCHelper addKeys(String... keys) { + public MDCAmbit addKeys(String... keys) { if(keys == null) return this; @@ -119,15 +119,15 @@ public MDCHelper addKeys(String... keys) { /** - * Remove tracked keys by calling {@link MDC#remove} on each key. + * Clear tracked keys by calling {@link MDC#remove} on each key. * - *

The set of tracked keys is cleared.

+ *

In addition, the set of tracked keys is cleared.

* *

This method is usually called from within finally statement of a * try/catch/finally block

* */ - public void removeSet() { + public void clear() { for(String key: keySet) { mdcAdapter.remove(key); } diff --git a/slf4j-api/src/test/java/org/slf4j/MDCAmbitTest.java b/slf4j-api/src/test/java/org/slf4j/MDCAmbitTest.java index 944df1574..3da224c40 100644 --- a/slf4j-api/src/test/java/org/slf4j/MDCAmbitTest.java +++ b/slf4j-api/src/test/java/org/slf4j/MDCAmbitTest.java @@ -30,11 +30,11 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -public class MDCHelperTest { +public class MDCAmbitTest { BasicMDCAdapter mdcAdapter = new BasicMDCAdapter(); - MDCHelper mdch = new MDCHelper(mdcAdapter); + MDCAmbit mdca = new MDCAmbit(mdcAdapter); String k0 = "k0"; String v0 = "v0"; @@ -51,14 +51,14 @@ void throwRuntimeException() { @Test public void smoke() { try { - mdch.put(k0, v0); + mdca.put(k0, v0); assertEquals(v0, mdcAdapter.get(k0)); assertNull(mdcAdapter.get(kUnused)); throwRuntimeException(); } catch (RuntimeException e) { assertEquals(v0, mdcAdapter.get(k0)); } finally { - mdch.removeSet(); + mdca.clear(); } assertNull(mdcAdapter.get(k0)); } @@ -68,7 +68,7 @@ public void empty() { try { assertNull(mdcAdapter.get(kUnused)); } finally { - mdch.removeSet(); + mdca.clear(); } assertNull(mdcAdapter.get(k0)); } @@ -77,14 +77,14 @@ public void empty() { public void addKeyTest() { mdcAdapter.put(k0, v0); try { - mdch.addKey(k0); + mdca.addKey(k0); assertEquals(v0, mdcAdapter.get(k0)); assertNull(mdcAdapter.get(kUnused)); throwRuntimeException(); } catch (RuntimeException e) { assertEquals(v0, mdcAdapter.get(k0)); } finally { - mdch.removeSet(); + mdca.clear(); } assertNull(mdcAdapter.get(k0)); } @@ -93,7 +93,7 @@ public void addKeyTest() { public void combinedPut_addKeyTest() { mdcAdapter.put(k0, v0); try { - mdch.addKey(k0).put(k1, v1).put(k2, v2); + mdca.addKey(k0).put(k1, v1).put(k2, v2); assertEquals(v0, mdcAdapter.get(k0)); assertEquals(v1, mdcAdapter.get(k1)); assertEquals(v2, mdcAdapter.get(k2)); @@ -104,7 +104,7 @@ public void combinedPut_addKeyTest() { assertEquals(v1, mdcAdapter.get(k1)); assertEquals(v2, mdcAdapter.get(k2)); } finally { - mdch.removeSet(); + mdca.clear(); } assertNull(mdcAdapter.get(k0)); assertNull(mdcAdapter.get(k1));