Skip to content

Commit 1aea4ee

Browse files
committed
SLF4J-522 - Adds NDC.contains() Method
Updates the NDC class in slf4j-ext to include a static contains() method that can check the existing nested diagnostic context for the presence of a value. Added unit tests to test the new NDC.contains() method.
1 parent cf09b2e commit 1aea4ee

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed
Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
/**
22
* Copyright (c) 2004-2011 QOS.ch
33
* All rights reserved.
4-
*
4+
* <p>
55
* Permission is hereby granted, free of charge, to any person obtaining
66
* a copy of this software and associated documentation files (the
77
* "Software"), to deal in the Software without restriction, including
88
* without limitation the rights to use, copy, modify, merge, publish,
99
* distribute, sublicense, and/or sell copies of the Software, and to
1010
* permit persons to whom the Software is furnished to do so, subject to
1111
* the following conditions:
12-
*
12+
* <p>
1313
* The above copyright notice and this permission notice shall be
1414
* included in all copies or substantial portions of the Software.
15-
*
15+
* <p>
1616
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1717
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1818
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1919
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
2020
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
2121
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2222
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23-
*
2423
*/
2524
package org.slf4j;
2625

@@ -29,9 +28,9 @@ public class NDC {
2928

3029
private static int size() {
3130
int i = 0;
32-
while (true) {
31+
while(true) {
3332
String val = MDC.get(PREFIX + i);
34-
if (val != null) {
33+
if(val != null) {
3534
i++;
3635
} else {
3736
break;
@@ -40,14 +39,28 @@ private static int size() {
4039
return i;
4140
}
4241

42+
/**
43+
* Adds a new value to the current thread's nested diagnostic context. The
44+
* <code>context</code> parameter cannot be <code>null</code>.
45+
*
46+
* @param val a non-null value to add to the nested diagnostic context
47+
* @throws IllegalArgumentException in case the <code>val</code> parameter is null
48+
*/
4349
public static void push(String val) {
4450
int next = size();
4551
MDC.put(PREFIX + next, val);
4652
}
4753

54+
/**
55+
* Removes the last added value from the current thread's nested diagnostic context and returns it.
56+
* If the nested diagnostic context is empty, an empty String will be returned.
57+
*
58+
* @return the nested diagnostic context value removed, or an empty String if the nested diagnostic context is
59+
* empty.
60+
*/
4861
public static String pop() {
4962
int next = size();
50-
if (next == 0) {
63+
if(next == 0) {
5164
return "";
5265
}
5366
int last = next - 1;
@@ -57,4 +70,28 @@ public static String pop() {
5770
return val;
5871
}
5972

73+
/**
74+
* Returns whether or not the provided context value is already included in the current thread's
75+
* nested diagnostic context. A <code>null</code> value will always return <code>false</code>.
76+
*
77+
* @param context the context to check for in the nested diagnostic context.
78+
* @return <code>true</code> if the provided context exists within the nested diagnostic context,
79+
* <code>false</code> if it does not exist or the provided context value was <code>null</code>.
80+
*/
81+
public static boolean contains(String context) {
82+
if(context == null) {
83+
return false;
84+
}
85+
86+
int i = 0;
87+
String val;
88+
while((val = MDC.get(PREFIX + i)) != null) {
89+
if(val.equals(context)) {
90+
return true;
91+
}
92+
i++;
93+
}
94+
return false;
95+
}
96+
6097
}

slf4j-ext/src/test/java/org/slf4j/NDCTest.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
*/
2525
package org.slf4j;
2626

27-
import static org.junit.Assert.assertEquals;
28-
2927
import org.junit.Before;
3028
import org.junit.Test;
3129

30+
import static org.junit.Assert.*;
31+
3232
public class NDCTest {
3333

3434
@Before
@@ -57,4 +57,29 @@ public void testSmoke2() {
5757
assertEquals("b", result1);
5858
assertEquals("a", result0);
5959
}
60+
61+
@Test
62+
public void testContains() {
63+
assertFalse("nested context does not contain null value", NDC.contains(null));
64+
assertFalse("default nested context does not contain \"a\" value", NDC.contains("a"));
65+
66+
NDC.push("a");
67+
68+
assertTrue("nested context contains \"a\" after being pushed", NDC.contains("a"));
69+
assertFalse("nested context does not contain \"b\" before being pushed", NDC.contains("b"));
70+
71+
NDC.push("b");
72+
73+
assertTrue("nested context still contains \"a\" after \"b\" is pushed", NDC.contains("a"));
74+
assertTrue("nested context contains \"b\" after being pushed", NDC.contains("b"));
75+
76+
NDC.pop();
77+
78+
assertFalse("nested context no longer contains \"b\" after being popped", NDC.contains("b"));
79+
assertTrue("nested context still contains \"a\" after \"b\" is popped", NDC.contains("a"));
80+
81+
NDC.pop();
82+
83+
assertFalse("nested context no longer contains \"a\" after being popped", NDC.contains("a"));
84+
}
6085
}

0 commit comments

Comments
 (0)