@Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof ThreadContextStack)) { return false; } final ThreadContextStack other = (ThreadContextStack) obj; final List<String> otherAsList = other.asList(); if (this.list == null) { if (otherAsList != null) { return false; } } else if (!this.list.equals(otherAsList)) { return false; } return true; }
/** * Returns a copy of this thread's stack. * * @return A copy of this thread's stack. */ public static ContextStack cloneStack() { return contextStack.copy(); }
@Test public void testCopy() { final DefaultThreadContextStack stack = createStack(); final ThreadContextStack copy = stack.copy(); assertEquals(3, copy.size()); assertTrue(copy.containsAll(Arrays.asList("msg1", "msg2", "msg3"))); // clearing stack does not affect copy stack.clear(); assertTrue(stack.isEmpty()); assertEquals(3, copy.size()); // not affected assertTrue(copy.containsAll(Arrays.asList("msg1", "msg2", "msg3"))); // adding to copy does not affect stack copy.add("other"); assertEquals(4, copy.size()); // not affected assertTrue(stack.isEmpty()); // adding to stack does not affect copy stack.push("newStackMsg"); assertEquals(1, stack.size()); assertEquals(4, copy.size()); // not affected // clearing copy does not affect stack copy.clear(); assertTrue(copy.isEmpty()); assertEquals(1, stack.size()); }
/** * Clears the stack for this thread. */ public static void clearStack() { contextStack.clear(); }
/** * Pushes new diagnostic context information for the current thread. * * <p> * The contents of the <code>message</code> parameter is determined solely by the client. * </p> * * @param message The new diagnostic context information. */ public static void push(final String message) { contextStack.push(message); }
/** * Looks at the last diagnostic context at the top of this NDC without removing it. * * <p> * The returned value is the value that was pushed last. If no context is available, then the empty string "" is * returned. * </p> * * @return String The innermost diagnostic context. */ public static String peek() { return contextStack.peek(); }
/** * Gets an immutable copy of this current thread's context stack. * * @return an immutable copy of the ThreadContext stack. */ public static ContextStack getImmutableStack() { final ContextStack result = contextStack.getImmutableStackOrNull(); return result == null ? EMPTY_STACK : result; }
/** * Gets the current nesting depth of this thread's stack. * * @return the number of items in the stack. * * @see #trim */ public static int getDepth() { return contextStack.getDepth(); }
/** * Returns the value of the last item placed on the stack. * * <p> * The returned value is the value that was pushed last. If no context is available, then the empty string "" is * returned. * </p> * * @return String The innermost diagnostic context. */ public static String pop() { return contextStack.pop(); }
contextStack.trim(depth);
@Test public void testCopy() { final MutableThreadContextStack stack = createStack(); final ThreadContextStack copy = stack.copy(); assertEquals(3, copy.size()); assertTrue(copy.containsAll(Arrays.asList("msg1", "msg2", "msg3"))); // clearing stack does not affect copy stack.clear(); assertTrue(stack.isEmpty()); assertEquals(3, copy.size()); // not affected assertTrue(copy.containsAll(Arrays.asList("msg1", "msg2", "msg3"))); // adding to copy does not affect stack copy.add("other"); assertEquals(4, copy.size()); // not affected assertTrue(stack.isEmpty()); // adding to stack does not affect copy stack.push("newStackMsg"); assertEquals(1, stack.size()); assertEquals(4, copy.size()); // not affected // clearing copy does not affect stack copy.clear(); assertTrue(copy.isEmpty()); assertEquals(1, stack.size()); }
/** * Removes the diagnostic context for this thread. * * <p> * Each thread that created a diagnostic context by calling {@link #push} should call this method before exiting. * Otherwise, the memory used by the <b>thread</b> cannot be reclaimed by the VM. * </p> * * <p> * As this is such an important problem in heavy duty systems and because it is difficult to always guarantee that * the remove method is called before exiting a thread, this method has been augmented to lazily remove references * to dead threads. In practice, this means that you can be a little sloppy and occasionally forget to call * {@link #remove} before exiting a thread. However, you must call <code>remove</code> sometime. If you never call * it, then your application is sure to run out of memory. * </p> */ public static void removeStack() { contextStack.clear(); }
/** * Pushes new diagnostic context information for the current thread. * * <p> * The contents of the <code>message</code> and args parameters are determined solely by the client. The message * will be treated as a format String and tokens will be replaced with the String value of the arguments in * accordance with ParameterizedMessage. * </p> * * @param message The new diagnostic context information. * @param args Parameters for the message. */ public static void push(final String message, final Object... args) { contextStack.push(ParameterizedMessage.format(message, args)); }
/** * Looks at the last diagnostic context at the top of this NDC without removing it. * * <p> * The returned value is the value that was pushed last. If no context is available, then the empty string "" is * returned. * </p> * * @return String The innermost diagnostic context. */ public static String peek() { return contextStack.peek(); }
/** * Gets an immutable copy of this current thread's context stack. * * @return an immutable copy of the ThreadContext stack. */ public static ContextStack getImmutableStack() { final ContextStack result = contextStack.getImmutableStackOrNull(); return result == null ? EMPTY_STACK : result; }
/** * Gets the current nesting depth of this thread's stack. * * @return the number of items in the stack. * * @see #trim */ public static int getDepth() { return contextStack.getDepth(); }
/** * Returns the value of the last item placed on the stack. * * <p> * The returned value is the value that was pushed last. If no context is available, then the empty string "" is * returned. * </p> * * @return String The innermost diagnostic context. */ public static String pop() { return contextStack.pop(); }
contextStack.trim(depth);