From d2073bece8310017914e1ff65d7107b2d4869c7d Mon Sep 17 00:00:00 2001 From: ceki Date: Mon, 24 Nov 2025 17:51:51 +0100 Subject: [PATCH] minor refactoring, javadoc changes Signed-off-by: ceki --- slf4j-api/src/main/java/org/slf4j/MDC.java | 48 ++++++++----------- .../main/java/org/slf4j/spi/MDCAdapter.java | 8 ++-- 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/slf4j-api/src/main/java/org/slf4j/MDC.java b/slf4j-api/src/main/java/org/slf4j/MDC.java index 8625af3b..dc46d2e0 100644 --- a/slf4j-api/src/main/java/org/slf4j/MDC.java +++ b/slf4j-api/src/main/java/org/slf4j/MDC.java @@ -63,7 +63,7 @@ import org.slf4j.spi.SLF4JServiceProvider; public class MDC { static final String NULL_MDCA_URL = "http://www.slf4j.org/codes.html#null_MDCA"; - private static final String MDC_APAPTER_CANNOT_BE_NULL_MESSAGE = "MDCAdapter cannot be null. See also " + NULL_MDCA_URL; + private static final String MDC_ADAPTER_CANNOT_BE_NULL_MESSAGE = "MDCAdapter cannot be null. See also " + NULL_MDCA_URL; static final String NO_STATIC_MDC_BINDER_URL = "http://www.slf4j.org/codes.html#no_static_mdc_binder"; static MDCAdapter MDC_ADAPTER; @@ -132,7 +132,7 @@ public class MDC { throw new IllegalArgumentException("key parameter cannot be null"); } if (getMDCAdapter() == null) { - throw new IllegalStateException(MDC_APAPTER_CANNOT_BE_NULL_MESSAGE); + throw new IllegalStateException(MDC_ADAPTER_CANNOT_BE_NULL_MESSAGE); } getMDCAdapter().put(key, val); } @@ -187,9 +187,7 @@ public class MDC { throw new IllegalArgumentException("key parameter cannot be null"); } - if (getMDCAdapter() == null) { - throw new IllegalStateException(MDC_APAPTER_CANNOT_BE_NULL_MESSAGE); - } + mdcAdapterNullCheck(); return getMDCAdapter().get(key); } @@ -208,9 +206,7 @@ public class MDC { throw new IllegalArgumentException("key parameter cannot be null"); } - if (getMDCAdapter() == null) { - throw new IllegalStateException(MDC_APAPTER_CANNOT_BE_NULL_MESSAGE); - } + mdcAdapterNullCheck(); getMDCAdapter().remove(key); } @@ -218,9 +214,7 @@ public class MDC { * Clear all entries in the MDC of the underlying implementation. */ public static void clear() { - if (getMDCAdapter() == null) { - throw new IllegalStateException(MDC_APAPTER_CANNOT_BE_NULL_MESSAGE); - } + mdcAdapterNullCheck(); getMDCAdapter().clear(); } @@ -232,9 +226,7 @@ public class MDC { * @since 1.5.1 */ public static Map getCopyOfContextMap() { - if (getMDCAdapter() == null) { - throw new IllegalStateException(MDC_APAPTER_CANNOT_BE_NULL_MESSAGE); - } + mdcAdapterNullCheck(); return getMDCAdapter().getCopyOfContextMap(); } @@ -250,9 +242,7 @@ public class MDC { * @since 1.5.1 */ public static void setContextMap(Map contextMap) { - if (getMDCAdapter() == null) { - throw new IllegalStateException(MDC_APAPTER_CANNOT_BE_NULL_MESSAGE); - } + mdcAdapterNullCheck(); getMDCAdapter().setContextMap(contextMap); } @@ -280,7 +270,7 @@ public class MDC { */ static void setMDCAdapter(MDCAdapter anMDCAdapter) { if(anMDCAdapter == null) { - throw new IllegalStateException(MDC_APAPTER_CANNOT_BE_NULL_MESSAGE); + throw new IllegalStateException(MDC_ADAPTER_CANNOT_BE_NULL_MESSAGE); } MDC_ADAPTER = anMDCAdapter; } @@ -293,28 +283,24 @@ public class MDC { * @since 2.0.0 */ static public void pushByKey(String key, String value) { - if (getMDCAdapter() == null) { - throw new IllegalStateException(MDC_APAPTER_CANNOT_BE_NULL_MESSAGE); - } + mdcAdapterNullCheck(); getMDCAdapter().pushByKey(key, value); } /** - * Pop the stack referenced by 'key' and return the value possibly null. + * Pop the stack referenced by 'key' and return the value possibly null. * * @param key identifies the deque(stack) * @return the value just popped. May be null/ * @since 2.0.0 */ static public String popByKey(String key) { - if (getMDCAdapter() == null) { - throw new IllegalStateException(MDC_APAPTER_CANNOT_BE_NULL_MESSAGE); - } + mdcAdapterNullCheck(); return getMDCAdapter().popByKey(key); } /** - * Returns a copy of the deque(stack) referenced by 'key'. May be null. + * Returns a copy of the deque(stack) referenced by 'key'. May be null. * * @param key identifies the stack * @return copy of stack referenced by 'key'. May be null. @@ -322,9 +308,13 @@ public class MDC { * @since 2.0.0 */ public Deque getCopyOfDequeByKey(String key) { - if (getMDCAdapter() == null) { - throw new IllegalStateException(MDC_APAPTER_CANNOT_BE_NULL_MESSAGE); - } + mdcAdapterNullCheck(); return getMDCAdapter().getCopyOfDequeByKey(key); } + + private static void mdcAdapterNullCheck() { + if (getMDCAdapter() == null) { + throw new IllegalStateException(MDC_ADAPTER_CANNOT_BE_NULL_MESSAGE); + } + } } diff --git a/slf4j-api/src/main/java/org/slf4j/spi/MDCAdapter.java b/slf4j-api/src/main/java/org/slf4j/spi/MDCAdapter.java index 924849d1..d5b5fdec 100644 --- a/slf4j-api/src/main/java/org/slf4j/spi/MDCAdapter.java +++ b/slf4j-api/src/main/java/org/slf4j/spi/MDCAdapter.java @@ -93,7 +93,7 @@ public interface MDCAdapter { public void setContextMap(Map contextMap); /** - * Push a value into the deque(stack) referenced by 'key'. + * Push a value into the deque(stack) referenced by 'key'. * * @param key identifies the appropriate stack * @param value the value to push into the stack @@ -102,7 +102,7 @@ public interface MDCAdapter { public void pushByKey(String key, String value); /** - * Pop the stack referenced by 'key' and return the value possibly null. + * Pop the stack referenced by 'key' and return the value possibly null. * * @param key identifies the deque(stack) * @return the value just popped. May be null/ @@ -111,7 +111,7 @@ public interface MDCAdapter { public String popByKey(String key); /** - * Returns a copy of the deque(stack) referenced by 'key'. May be null. + * Returns a copy of the deque(stack) referenced by 'key'. May be null. * * @param key identifies the stack * @return copy of stack referenced by 'key'. May be null. @@ -122,7 +122,7 @@ public interface MDCAdapter { /** - * Clear the deque(stack) referenced by 'key'. + * Clear the deque(stack) referenced by 'key'. * * @param key identifies the stack *