Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SLF4J-522 - Adds NDC.contains() Method #267

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
51 changes: 44 additions & 7 deletions slf4j-ext/src/main/java/org/slf4j/NDC.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
/**
* Copyright (c) 2004-2011 QOS.ch
* All rights reserved.
*
* <p>
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* <p>
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j;

Expand All @@ -29,9 +28,9 @@ public class NDC {

private static int size() {
int i = 0;
while (true) {
while(true) {
String val = MDC.get(PREFIX + i);
if (val != null) {
if(val != null) {
i++;
} else {
break;
Expand All @@ -40,14 +39,28 @@ private static int size() {
return i;
}

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

/**
* Removes the last added value from the current thread's nested diagnostic context and returns it.
* If the nested diagnostic context is empty, an empty String will be returned.
*
* @return the nested diagnostic context value removed, or an empty String if the nested diagnostic context is
* empty.
*/
public static String pop() {
int next = size();
if (next == 0) {
if(next == 0) {
return "";
}
int last = next - 1;
Expand All @@ -57,4 +70,28 @@ public static String pop() {
return val;
}

/**
* Returns whether or not the provided context value is already included in the current thread's
* nested diagnostic context. A <code>null</code> value will always return <code>false</code>.
*
* @param context the context to check for in the nested diagnostic context.
* @return <code>true</code> if the provided context exists within the nested diagnostic context,
* <code>false</code> if it does not exist or the provided context value was <code>null</code>.
*/
public static boolean contains(String context) {
if(context == null) {
return false;
}

int i = 0;
String val;
while((val = MDC.get(PREFIX + i)) != null) {
if(val.equals(context)) {
return true;
}
i++;
}
return false;
}

}
29 changes: 27 additions & 2 deletions slf4j-ext/src/test/java/org/slf4j/NDCTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
*/
package org.slf4j;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class NDCTest {

@Before
Expand Down Expand Up @@ -57,4 +57,29 @@ public void testSmoke2() {
assertEquals("b", result1);
assertEquals("a", result0);
}

@Test
public void testContains() {
assertFalse("nested context does not contain null value", NDC.contains(null));
assertFalse("default nested context does not contain \"a\" value", NDC.contains("a"));

NDC.push("a");

assertTrue("nested context contains \"a\" after being pushed", NDC.contains("a"));
assertFalse("nested context does not contain \"b\" before being pushed", NDC.contains("b"));

NDC.push("b");

assertTrue("nested context still contains \"a\" after \"b\" is pushed", NDC.contains("a"));
assertTrue("nested context contains \"b\" after being pushed", NDC.contains("b"));

NDC.pop();

assertFalse("nested context no longer contains \"b\" after being popped", NDC.contains("b"));
assertTrue("nested context still contains \"a\" after \"b\" is popped", NDC.contains("a"));

NDC.pop();

assertFalse("nested context no longer contains \"a\" after being popped", NDC.contains("a"));
}
}