Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* @author Ceki Gulcu
* @author Maarten Bosteels
* @author Lukasz Cwik
*
*
* @since 1.5.0
*/
public class BasicMDCAdapter implements MDCAdapter {
Expand Down Expand Up @@ -156,9 +156,14 @@ public void pushByKey(String key, String value) {

@Override
public String popByKey(String key) {
return threadLocalMapOfDeques.popByKey(key);
return threadLocalMapOfDeques.popByKey(key);
}

@Override
public String peekByKey(String key) {
return threadLocalMapOfDeques.peekByKey(key);
}

@Override
public Deque<String> getCopyOfDequeByKey(String key) {
return threadLocalMapOfDeques.getCopyOfDequeByKey(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* A simple implementation of ThreadLocal backed Map containing values of type
* Deque<String>.
*
*
* @author Ceki Guuml;c&uuml;
* @since 2.0.0
*/
Expand Down Expand Up @@ -40,50 +40,53 @@ public void pushByKey(String key, String value) {
}

public String popByKey(String key) {
if (key == null)
Deque<String> deque = getDeque(key);
if (deque == null)
return null;

Map<String, Deque<String>> map = tlMapOfStacks.get();
if (map == null)
return null;
Deque<String> deque = map.get(key);
return deque.pop();
}

public String peekByKey(String key) {
Deque<String> deque = getDeque(key);
if (deque == null)
return null;
return deque.pop();

return deque.peek();
}

public Deque<String> getCopyOfDequeByKey(String key) {
Deque<String> deque = getDeque(key);
if (deque == null)
return null;

return new ArrayDeque<String>(deque);
}

private Deque<String> getDeque(String key) {
if (key == null)
return null;

Map<String, Deque<String>> map = tlMapOfStacks.get();
if (map == null)
return null;
Deque<String> deque = map.get(key);
if (deque == null)
return null;

return new ArrayDeque<String>(deque);
return map.get(key);
}

/**
* Clear the deque(stack) referenced by 'key'.
*
* Clear the deque(stack) referenced by 'key'.
*
* @param key identifies the stack
*
*
* @since 2.0.0
*/
public void clearDequeByKey(String key) {
if (key == null)
return;

Map<String, Deque<String>> map = tlMapOfStacks.get();
if (map == null)
return;
Deque<String> deque = map.get(key);
Deque<String> deque = getDeque(key);
if (deque == null)
return;

deque.clear();
}

}
}
13 changes: 13 additions & 0 deletions slf4j-api/src/main/java/org/slf4j/spi/MDCAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ public interface MDCAdapter {
*/
public String popByKey(String key);

/**
* peek the stack referenced by 'key' and return the value possibly null.
*
* @param key identifies the deque(stack)
* @return the value just peeked. May be null/
* @since 2.0.18
*/
default public String peekByKey(String key){
Deque<String> deque = getCopyOfDequeByKey(key);
if(deque == null)
return null;
return deque.peek();
}
/**
* Returns a copy of the deque(stack) referenced by 'key'. May be null.
*
Expand Down
27 changes: 22 additions & 5 deletions slf4j-api/src/test/java/org/slf4j/helpers/MDCAdapterTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,16 @@

package org.slf4j.helpers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.Map;
import java.util.NoSuchElementException;

import org.junit.After;
import org.junit.Test;
import org.slf4j.spi.MDCAdapter;

import static org.junit.Assert.*;

/**
* Tests for {@link BasicMDCAdapter}
*
Expand Down Expand Up @@ -64,6 +62,25 @@ public void testSettingAndGettingWithMDC() {
assertEquals(mdc.get("testKey"), "testValue");
}

@Test
public void testPushingAndPoppingWithMDC() {
assertNull(mdc.popByKey("testKey"));
mdc.pushByKey("testKey", "testValue");
mdc.pushByKey("testKey", "differentTestValue");
assertEquals(mdc.popByKey("testKey"), "differentTestValue");
assertEquals(mdc.popByKey("testKey"), "testValue");
assertThrows(NoSuchElementException.class, () -> mdc.popByKey("testKey"));
}

@Test
public void testPushingAndPeekingWithMDC() {
assertNull(mdc.peekByKey("testKey"));
mdc.pushByKey("testKey", "testValue");
mdc.pushByKey("testKey", "differentTestValue");
assertEquals(mdc.peekByKey("testKey"), "differentTestValue");
assertEquals(mdc.peekByKey("testKey"), "differentTestValue");
}

@Test
public void testOverwritingAKeyInMDC() {
assertNull(mdc.get("testKey"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ public String popByKey(String key) {
return threadLocalMapOfDeques.popByKey(key);
}

@Override
public String peekByKey(String key) {
return threadLocalMapOfDeques.peekByKey(key);
}

@Override
public Deque<String> getCopyOfDequeByKey(String key) {
return threadLocalMapOfDeques.getCopyOfDequeByKey(key);
Expand Down