Kaynağa Gözat

MapList now uses synchronised lists.

Added explicit documentation on synchronisation.
Minor code tidying and typo fixes.

Fixes issue 4344.

Change-Id: I92b691de235cb5da1ca83133cb6eac451c5973bd
Reviewed-on: http://gerrit.dmdirc.com/1452
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
tags/0.6.5
Chris Smith 14 yıl önce
ebeveyn
işleme
2bc887dacc
1 değiştirilmiş dosya ile 52 ekleme ve 42 silme
  1. 52
    42
      src/com/dmdirc/util/MapList.java

+ 52
- 42
src/com/dmdirc/util/MapList.java Dosyayı Görüntüle

24
 
24
 
25
 import java.util.ArrayList;
25
 import java.util.ArrayList;
26
 import java.util.Collection;
26
 import java.util.Collection;
27
+import java.util.Collections;
27
 import java.util.HashMap;
28
 import java.util.HashMap;
28
 import java.util.List;
29
 import java.util.List;
29
 import java.util.Map;
30
 import java.util.Map;
32
 /**
33
 /**
33
  * Wraps a Map&lt;S, List&lt;T&gt;&gt; with various convenience methods for
34
  * Wraps a Map&lt;S, List&lt;T&gt;&gt; with various convenience methods for
34
  * accessing the data. Implements a Map-like interface for easier transition.
35
  * accessing the data. Implements a Map-like interface for easier transition.
35
- * 
36
+ * <p>
37
+ * <strong>Note that this implementation is not synchronized.</strong> If
38
+ * multiple threads access a <code>MapList</code> instance concurrently, and
39
+ * at least one of the threads modifies the map, it <em>must</em> be
40
+ * synchronized externally.
41
+ * <p>
42
+ * The <code>List</code>s used to back this map are synchronized using the
43
+ * {@link Collections#synchronizedList(java.util.List)} method, which requires
44
+ * manual synchronization in any code iterating over the values.
45
+ *
36
  * @param <S> the type of keys maintained by this map
46
  * @param <S> the type of keys maintained by this map
37
  * @param <T> the type of mapped values
47
  * @param <T> the type of mapped values
38
  * @author chris
48
  * @author chris
39
  */
49
  */
40
-public class MapList<S,T> {
41
-    
50
+public class MapList<S, T> {
51
+
42
     /** Our internal map. */
52
     /** Our internal map. */
43
     protected final Map<S, List<T>> map;
53
     protected final Map<S, List<T>> map;
44
 
54
 
51
 
61
 
52
     /**
62
     /**
53
      * Creates a new MapList with the values from the specified list.
63
      * Creates a new MapList with the values from the specified list.
54
-     * 
64
+     *
55
      * @param list The MapList whose values should be used
65
      * @param list The MapList whose values should be used
56
      */
66
      */
57
-    public MapList(final MapList<S,T> list) {
67
+    public MapList(final MapList<S, T> list) {
58
         map = list.getMap();
68
         map = list.getMap();
59
     }
69
     }
60
 
70
 
61
     /**
71
     /**
62
      * Determines if this MapList is empty. An empty MapList is one that either
72
      * Determines if this MapList is empty. An empty MapList is one that either
63
      * contains no keys, or contains only keys which have no associated values.
73
      * contains no keys, or contains only keys which have no associated values.
64
-     * 
74
+     *
65
      * @return True if this MapList is empty, false otherwise
75
      * @return True if this MapList is empty, false otherwise
66
      */
76
      */
67
     public boolean isEmpty() {
77
     public boolean isEmpty() {
70
                 return false;
80
                 return false;
71
             }
81
             }
72
         }
82
         }
73
-        
83
+
74
         return true;
84
         return true;
75
     }
85
     }
76
 
86
 
77
     /**
87
     /**
78
      * Determines if this MapList contains the specified key.
88
      * Determines if this MapList contains the specified key.
79
-     * 
89
+     *
80
      * @param key The key to look for
90
      * @param key The key to look for
81
      * @return True if this MapList contains the specified key, false otherwise
91
      * @return True if this MapList contains the specified key, false otherwise
82
      */
92
      */
87
     /**
97
     /**
88
      * Determines if this MapList contains the specified value as a child of
98
      * Determines if this MapList contains the specified value as a child of
89
      * the specified key.
99
      * the specified key.
90
-     * 
100
+     *
91
      * @param key The key to search under
101
      * @param key The key to search under
92
      * @param value The value to look for
102
      * @param value The value to look for
93
-     * @return True if this MapList contains the specified key/value pair, 
103
+     * @return True if this MapList contains the specified key/value pair,
94
      * false otherwise
104
      * false otherwise
95
      */
105
      */
96
     public boolean containsValue(final S key, final T value) {
106
     public boolean containsValue(final S key, final T value) {
99
 
109
 
100
     /**
110
     /**
101
      * Retrieves the list of values associated with the specified key.
111
      * Retrieves the list of values associated with the specified key.
102
-     * 
112
+     *
103
      * @param key The key whose values are being retrieved
113
      * @param key The key whose values are being retrieved
104
      * @return The values belonging to the specified key
114
      * @return The values belonging to the specified key
105
      */
115
      */
106
     public List<T> get(final S key) {
116
     public List<T> get(final S key) {
107
         return map.get(key);
117
         return map.get(key);
108
     }
118
     }
109
-    
119
+
110
     /**
120
     /**
111
      * Retrieves the value at the specified offset of the specified key.
121
      * Retrieves the value at the specified offset of the specified key.
112
-     * 
122
+     *
113
      * @param key The key whose values are being retrieved
123
      * @param key The key whose values are being retrieved
114
      * @param index The index of the value to retrieve
124
      * @param index The index of the value to retrieve
115
      * @return The specified value of the key
125
      * @return The specified value of the key
116
-     */    
126
+     */
117
     public T get(final S key, final int index) {
127
     public T get(final S key, final int index) {
118
         return map.get(key).get(index);
128
         return map.get(key).get(index);
119
-    }    
120
-    
129
+    }
130
+
121
     /**
131
     /**
122
      * Retrieves the list of values associated with the specified key, creating
132
      * Retrieves the list of values associated with the specified key, creating
123
-     * the key if neccessary.
124
-     * 
133
+     * the key if necessary.
134
+     *
125
      * @param key The key to retrieve
135
      * @param key The key to retrieve
126
      * @return A list of the specified key's values
136
      * @return A list of the specified key's values
127
      */
137
      */
128
     public List<T> safeGet(final S key) {
138
     public List<T> safeGet(final S key) {
129
         if (!map.containsKey(key)) {
139
         if (!map.containsKey(key)) {
130
-            map.put(key, new ArrayList<T>());
140
+            map.put(key, Collections.synchronizedList(new ArrayList<T>()));
131
         }
141
         }
132
-        
142
+
133
         return map.get(key);
143
         return map.get(key);
134
     }
144
     }
135
-    
145
+
136
     /**
146
     /**
137
      * Adds the specified key to the MapList.
147
      * Adds the specified key to the MapList.
138
-     * 
148
+     *
139
      * @param key The key to be added
149
      * @param key The key to be added
140
      */
150
      */
141
     public void add(final S key) {
151
     public void add(final S key) {
142
         safeGet(key);
152
         safeGet(key);
143
-    }    
153
+    }
144
 
154
 
145
     /**
155
     /**
146
      * Adds the specified value as a child of the specified key. If the key
156
      * Adds the specified value as a child of the specified key. If the key
147
      * didn't previous exist, it is created.
157
      * didn't previous exist, it is created.
148
-     * 
158
+     *
149
      * @param key The key to which the value is being added
159
      * @param key The key to which the value is being added
150
      * @param value The value to be added
160
      * @param value The value to be added
151
      */
161
      */
156
     /**
166
     /**
157
      * Adds the specified set of values to the specified key. If the key
167
      * Adds the specified set of values to the specified key. If the key
158
      * didn't previous exist, it is created.
168
      * didn't previous exist, it is created.
159
-     * 
169
+     *
160
      * @param key The key to which the value is being added
170
      * @param key The key to which the value is being added
161
      * @param values The values to be added
171
      * @param values The values to be added
162
-     */    
172
+     */
163
     public void add(final S key, final Collection<T> values) {
173
     public void add(final S key, final Collection<T> values) {
164
         safeGet(key).addAll(values);
174
         safeGet(key).addAll(values);
165
-    }    
175
+    }
166
 
176
 
167
     /**
177
     /**
168
      * Removes the specified key and all of its values.
178
      * Removes the specified key and all of its values.
169
-     * 
179
+     *
170
      * @param key The key to remove
180
      * @param key The key to remove
171
-     */    
181
+     */
172
     public void remove(final S key) {
182
     public void remove(final S key) {
173
         map.remove(key);
183
         map.remove(key);
174
     }
184
     }
175
-    
185
+
176
     /**
186
     /**
177
      * Removes the specified value from all keys.
187
      * Removes the specified value from all keys.
178
-     * 
188
+     *
179
      * @param value The value to remove
189
      * @param value The value to remove
180
      */
190
      */
181
     public void removeFromAll(final T value) {
191
     public void removeFromAll(final T value) {
186
 
196
 
187
     /**
197
     /**
188
      * Removes the specified value from the specified key.
198
      * Removes the specified value from the specified key.
189
-     * 
199
+     *
190
      * @param key The key whose value is being removed
200
      * @param key The key whose value is being removed
191
      * @param value The value to be removed
201
      * @param value The value to be removed
192
      */
202
      */
194
         if (map.containsKey(key)) {
204
         if (map.containsKey(key)) {
195
             map.get(key).remove(value);
205
             map.get(key).remove(value);
196
         }
206
         }
197
-    }    
207
+    }
198
 
208
 
199
     /**
209
     /**
200
      * Entirely clears this MapList.
210
      * Entirely clears this MapList.
202
     public void clear() {
212
     public void clear() {
203
         map.clear();
213
         map.clear();
204
     }
214
     }
205
-    
215
+
206
     /**
216
     /**
207
      * Clears all values of the specified key.
217
      * Clears all values of the specified key.
208
-     * 
218
+     *
209
      * @param key The key to be cleared
219
      * @param key The key to be cleared
210
      */
220
      */
211
     public void clear(final S key) {
221
     public void clear(final S key) {
212
         safeGet(key).clear();
222
         safeGet(key).clear();
213
-    }    
223
+    }
214
 
224
 
215
     /**
225
     /**
216
      * Returns the set of all keys belonging to this MapList.
226
      * Returns the set of all keys belonging to this MapList.
217
-     * 
227
+     *
218
      * @return This MapList's keyset
228
      * @return This MapList's keyset
219
      */
229
      */
220
     public Set<S> keySet() {
230
     public Set<S> keySet() {
223
 
233
 
224
     /**
234
     /**
225
      * Returns a collection of all values belonging to the specified key.
235
      * Returns a collection of all values belonging to the specified key.
226
-     * 
236
+     *
227
      * @param key The key whose values are being sought
237
      * @param key The key whose values are being sought
228
      * @return A collection of values belonging to the key
238
      * @return A collection of values belonging to the key
229
      */
239
      */
230
     public Collection<T> values(final S key) {
240
     public Collection<T> values(final S key) {
231
         return map.get(key);
241
         return map.get(key);
232
     }
242
     }
233
-    
243
+
234
     /**
244
     /**
235
      * Retrieves the entry set for this MapList.
245
      * Retrieves the entry set for this MapList.
236
-     * 
246
+     *
237
      * @return This MapList's entry set
247
      * @return This MapList's entry set
238
      */
248
      */
239
     public Set<Map.Entry<S, List<T>>> entrySet() {
249
     public Set<Map.Entry<S, List<T>>> entrySet() {
240
         return map.entrySet();
250
         return map.entrySet();
241
     }
251
     }
242
-    
252
+
243
     /**
253
     /**
244
      * Retrieves the map behind this maplist.
254
      * Retrieves the map behind this maplist.
245
-     * 
255
+     *
246
      * @return This MapList's map.
256
      * @return This MapList's map.
247
      */
257
      */
248
     public Map<S, List<T>> getMap() {
258
     public Map<S, List<T>> getMap() {

Loading…
İptal
Kaydet