1
1
/**
2
2
* Copyright (c) 2004-2011 QOS.ch
3
3
* All rights reserved.
4
- *
4
+ * <p>
5
5
* Permission is hereby granted, free of charge, to any person obtaining
6
6
* a copy of this software and associated documentation files (the
7
7
* "Software"), to deal in the Software without restriction, including
8
8
* without limitation the rights to use, copy, modify, merge, publish,
9
9
* distribute, sublicense, and/or sell copies of the Software, and to
10
10
* permit persons to whom the Software is furnished to do so, subject to
11
11
* the following conditions:
12
- *
12
+ * <p>
13
13
* The above copyright notice and this permission notice shall be
14
14
* included in all copies or substantial portions of the Software.
15
- *
15
+ * <p>
16
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
17
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
18
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
19
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
20
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
21
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
22
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
- *
24
23
*/
25
24
package org .slf4j ;
26
25
@@ -29,9 +28,9 @@ public class NDC {
29
28
30
29
private static int size () {
31
30
int i = 0 ;
32
- while (true ) {
31
+ while (true ) {
33
32
String val = MDC .get (PREFIX + i );
34
- if (val != null ) {
33
+ if (val != null ) {
35
34
i ++;
36
35
} else {
37
36
break ;
@@ -40,14 +39,28 @@ private static int size() {
40
39
return i ;
41
40
}
42
41
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
+ */
43
49
public static void push (String val ) {
44
50
int next = size ();
45
51
MDC .put (PREFIX + next , val );
46
52
}
47
53
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
+ */
48
61
public static String pop () {
49
62
int next = size ();
50
- if (next == 0 ) {
63
+ if (next == 0 ) {
51
64
return "" ;
52
65
}
53
66
int last = next - 1 ;
@@ -57,4 +70,28 @@ public static String pop() {
57
70
return val ;
58
71
}
59
72
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
+
60
97
}
0 commit comments