KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jzonic > jlo > NDC


1 package org.jzonic.jlo;
2
3 import java.util.Map JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Iterator JavaDoc;
6
7 /**
8  * User: Mecky
9  * Date: 19.07.2005
10  * Time: 16:15:14
11  */

12 public class NDC {
13
14     private static final ThreadLocal JavaDoc resources = new ThreadLocal JavaDoc();
15
16     private NDC() {
17     }
18
19     public static void put(String JavaDoc key,Object JavaDoc obj) {
20         Map JavaDoc map = (Map JavaDoc) resources.get();
21         // if no map then create one
22
if (map == null) {
23             map = new HashMap JavaDoc();
24             resources.set(map);
25         }
26         map.put(key,obj);
27     }
28
29     public static Object JavaDoc get(String JavaDoc key) {
30         Map JavaDoc map = (Map JavaDoc) resources.get();
31         if (map == null) {
32             return null;
33         }
34         return map.get(key);
35     }
36     
37     public void remove(String JavaDoc key) {
38         Map JavaDoc map = (Map JavaDoc) resources.get();
39         if (map != null) {
40             if ( map.containsKey(key)) {
41                 map.remove(key);
42             }
43         }
44     }
45
46     public static String JavaDoc getAsString() {
47         Map JavaDoc map = (Map JavaDoc) resources.get();
48         if (map == null) {
49             return null;
50         }
51         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
52         buffer.append("[");
53         Iterator JavaDoc it = map.keySet().iterator();
54         while ( it.hasNext() ) {
55             String JavaDoc key = (String JavaDoc)it.next();
56             if ( buffer.length() > 1 ) {
57                 buffer.append(",");
58             }
59             buffer.append(key);
60             buffer.append("=");
61             buffer.append(map.get(key).toString());
62         }
63         buffer.append("]");
64         return buffer.toString();
65     }
66
67 }
68
Popular Tags