Methods Summary |
---|
public static boolean | contains(java.lang.String key)Check whether a value with the given key exists in the stack.
This means that the push method was called with
this key and it was not cancelled by a subsequent
restore . This method is useful when the
get method returns null, to distinguish between
the case where the key exists in the stack but is associated
with a null value, and the case where the key does not exist in
the stack.
return (contextContaining(key) != null);
|
private static com.sun.jmx.snmp.ThreadContext | contextContaining(java.lang.String key)Find the ThreadContext in the stack that contains the given key,
or return null if there is none.
if (key == null)
throw new IllegalArgumentException("null key");
for (ThreadContext context = getContext();
context != null;
context = context.previous) {
if (key.equals(context.key))
return context;
/* Note that "context.key" may be null if "context" is the
sentinel, so don't write "if (context.key.equals(key))"! */
}
return null;
|
public static java.lang.Object | get(java.lang.String key)Get the Object that was most recently pushed with the given key.
ThreadContext context = contextContaining(key);
if (context == null)
return null;
else
return context.value;
|
private static com.sun.jmx.snmp.ThreadContext | getContext()
return (ThreadContext) localContext.get();
|
public static com.sun.jmx.snmp.ThreadContext | getThreadContext()Return an object that can later be supplied to restore
to restore the context stack to its current state. The object can
also be given to setInitialContext .
return getContext();
|
public static com.sun.jmx.snmp.ThreadContext | push(java.lang.String key, java.lang.Object value)Push an object on the context stack with the given key.
This operation can subsequently be undone by calling
restore with the ThreadContext value returned
here.
if (key == null)
throw new IllegalArgumentException("null key");
ThreadContext oldContext = getContext();
if (oldContext == null)
oldContext = new ThreadContext(null, null, null); // make sentinel
ThreadContext newContext = new ThreadContext(oldContext, key, value);
setContext(newContext);
return oldContext;
|
public static void | restore(com.sun.jmx.snmp.ThreadContext oldContext)Restore the context stack to an earlier state. This typically
undoes the effect of one or more push calls.
/* The following test is not strictly necessary in the code as it
stands today, since the reference to "oldContext.key" would
generate a NullPointerException anyway. But if someone
didn't notice that during subsequent changes, they could
accidentally permit restore(null) with the semantics of
trashing the context stack. */
if (oldContext == null)
throw new NullPointerException();
/* Check that the restored context is in the stack. */
for (ThreadContext context = getContext();
context != oldContext;
context = context.previous) {
if (context == null) {
throw new IllegalArgumentException("Restored context is not " +
"contained in current " +
"context");
}
}
/* Discard the sentinel if the stack is empty. This means that it
is an error to call "restore" a second time with the
ThreadContext value that means an empty stack. That's why we
don't say that it is all right to restore the stack to the
state it was already in. */
if (oldContext.key == null)
oldContext = null;
setContext(oldContext);
|
private static void | setContext(com.sun.jmx.snmp.ThreadContext context)
localContext.set(context);
|
public void | setInitialContext(com.sun.jmx.snmp.ThreadContext context)Set the initial context of the calling thread to a context obtained
from another thread. After this call, the calling thread will see
the same results from the get method as the thread
from which the context argument was obtained, at the
time it was obtained.
The context argument must be the result of an earlier
push or getThreadContext call. It is an
error (which may or may not be detected) if this context has been
undone by a restore .
The context stack of the calling thread must be empty before this
call, i.e., there must not have been a push not undone
by a subsequent restore .
/* The following test assumes that we discard sentinels when the
stack is empty. */
if (getContext() != null)
throw new IllegalArgumentException("previous context not empty");
setContext(context);
|