KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > util > CopyOnWriteMap


1 package hudson.util;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Collections JavaDoc;
5 import java.util.Comparator JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.Set JavaDoc;
9 import java.util.TreeMap JavaDoc;
10
11 /**
12  * {@link Map} that has copy-on-write semantics.
13  *
14  * <p>
15  * This class is suitable where highly concurrent access is needed, yet
16  * the write operation is relatively uncommon.
17  *
18  * @author Kohsuke Kawaguchi
19  */

20 public abstract class CopyOnWriteMap<K,V> implements Map JavaDoc<K,V> {
21     protected volatile Map JavaDoc<K,V> core;
22     /**
23      * Read-only view of {@link #core}.
24      */

25     private volatile Map JavaDoc<K,V> view;
26
27     protected CopyOnWriteMap(Map JavaDoc<K, V> core) {
28         update(core);
29     }
30
31     protected CopyOnWriteMap() {
32         update(Collections.<K,V>emptyMap());
33     }
34
35     private void update(Map JavaDoc<K, V> m) {
36         core = m;
37         view = Collections.unmodifiableMap(core);
38     }
39
40     public int size() {
41         return core.size();
42     }
43
44     public boolean isEmpty() {
45         return core.isEmpty();
46     }
47
48     public boolean containsKey(Object JavaDoc key) {
49         return core.containsKey(key);
50     }
51
52     public boolean containsValue(Object JavaDoc value) {
53         return core.containsValue(value);
54     }
55
56     public V get(Object JavaDoc key) {
57         return core.get(key);
58     }
59
60     public synchronized V put(K key, V value) {
61         Map JavaDoc<K,V> m = copy();
62         V r = m.put(key,value);
63         update(m);
64
65         return r;
66     }
67
68     public synchronized V remove(Object JavaDoc key) {
69         Map JavaDoc<K,V> m = copy();
70         V r = m.remove(key);
71         update(m);
72
73         return r;
74     }
75
76     public synchronized void putAll(Map JavaDoc<? extends K, ? extends V> t) {
77         Map JavaDoc<K,V> m = copy();
78         m.putAll(t);
79         update(m);
80     }
81
82     protected abstract Map JavaDoc<K,V> copy();
83
84     public synchronized void clear() {
85         update(Collections.<K,V>emptyMap());
86     }
87
88     /**
89      * This method will return a read-only {@link Set}.
90      */

91     public Set JavaDoc<K> keySet() {
92         return view.keySet();
93     }
94
95     /**
96      * This method will return a read-only {@link Collection}.
97      */

98     public Collection JavaDoc<V> values() {
99         return view.values();
100     }
101
102     /**
103      * This method will return a read-only {@link Set}.
104      */

105     public Set JavaDoc<Entry<K,V>> entrySet() {
106         return view.entrySet();
107     }
108
109     /**
110      * {@link CopyOnWriteMap} backed by {@link HashMap}.
111      */

112     public static final class Hash<K,V> extends CopyOnWriteMap<K,V> {
113         public Hash(Map JavaDoc<K, V> core) {
114             super(new HashMap JavaDoc<K,V>(core));
115         }
116
117         public Hash() {
118         }
119
120         protected Map JavaDoc<K,V> copy() {
121             return new HashMap JavaDoc<K,V>(core);
122         }
123     }
124
125     /**
126      * {@link CopyOnWriteMap} backed by {@link TreeMap}.
127      */

128     public static final class Tree<K,V> extends CopyOnWriteMap<K,V> {
129         private final Comparator JavaDoc<K> comparator;
130
131         public Tree(Map JavaDoc<K,V> core, Comparator JavaDoc<K> comparator) {
132             this(comparator);
133             putAll(core);
134         }
135
136         public Tree(Comparator JavaDoc<K> comparator) {
137             this.comparator = comparator;
138         }
139
140         public Tree() {
141             this(null);
142         }
143
144         protected Map JavaDoc<K,V> copy() {
145             TreeMap JavaDoc<K, V> m = new TreeMap JavaDoc<K, V>(comparator);
146             m.putAll(core);
147             return m;
148         }
149     }
150 }
151
Popular Tags