| This class represents a map which can be temporarily modified
without impacting other threads (
LocalContext locally scoped changes).
Operation on instances of this class are completely thread-safe.
For example:
public class XMLFormat {
static LocalMap CLASS_TO_FORMAT = new LocalMap();
public static void setFormat(Class forClass, XMLFormat that) {
CLASS_TO_FORMAT.put(forClass, that); // No synchronization required.
}
public static XMLFormat getInstance(Class forClass) {
return CLASS_TO_FORMAT.get(forClass); // No synchronization required.
}
}
public void main(String[] args) {
// Sets default (global settings).
XMLFormat.setFormat(Foo.class, xFormat);
XMLFormat.setFormat(Bar.class, yFormat);
}
... // Another thread.
LocalContext.enter();
try { // Use of local context to avoid impacting other threads.
XMLFormat.setFormat(Foo.class, zFormat);
XMLFormat.getInstance(Foo.class); // Returns zFormat
XMLFormat.getInstance(Bar.class); // Returns yFormat (inherited)
} finally {
LocalContext.exit();
}
getInstance(Foo.class); // Returns xFormat
[/code]
Note: Because key-value mappings are inherited, the semantic of
LocalMap.remove and
LocalMap.clear is slightly modified (associate
null values instead of removing the entries).
author: Jean-Marie Dautelle version: 3.7, January 27, 2005 |