|
@@ -1,261 +0,0 @@
|
1
|
|
-/*
|
2
|
|
- * Copyright (c) 2006-2015 DMDirc Developers
|
3
|
|
- *
|
4
|
|
- * Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
|
- * of this software and associated documentation files (the "Software"), to deal
|
6
|
|
- * in the Software without restriction, including without limitation the rights
|
7
|
|
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
|
- * copies of the Software, and to permit persons to whom the Software is
|
9
|
|
- * furnished to do so, subject to the following conditions:
|
10
|
|
- *
|
11
|
|
- * The above copyright notice and this permission notice shall be included in
|
12
|
|
- * all copies or substantial portions of the Software.
|
13
|
|
- *
|
14
|
|
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
|
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
|
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
|
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
|
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
|
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
20
|
|
- * SOFTWARE.
|
21
|
|
- */
|
22
|
|
-
|
23
|
|
-package com.dmdirc.util.collections;
|
24
|
|
-
|
25
|
|
-import java.util.ArrayList;
|
26
|
|
-import java.util.Collection;
|
27
|
|
-import java.util.Collections;
|
28
|
|
-import java.util.HashMap;
|
29
|
|
-import java.util.List;
|
30
|
|
-import java.util.Map;
|
31
|
|
-import java.util.Set;
|
32
|
|
-
|
33
|
|
-/**
|
34
|
|
- * Wraps a Map<S, List<T>> with various convenience methods for
|
35
|
|
- * accessing the data. Implements a Map-like interface for easier transition.
|
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(List)} method, which requires
|
44
|
|
- * manual synchronization in any code iterating over the values.
|
45
|
|
- *
|
46
|
|
- * @param <S> the type of keys maintained by this map
|
47
|
|
- * @param <T> the type of mapped values
|
48
|
|
- */
|
49
|
|
-public class MapList<S, T> {
|
50
|
|
-
|
51
|
|
- /** Our internal map. */
|
52
|
|
- protected final Map<S, List<T>> map;
|
53
|
|
-
|
54
|
|
- /**
|
55
|
|
- * Creates a new, empty MapList.
|
56
|
|
- */
|
57
|
|
- public MapList() {
|
58
|
|
- map = new HashMap<>();
|
59
|
|
- }
|
60
|
|
-
|
61
|
|
- /**
|
62
|
|
- * Creates a new MapList with the values from the specified list.
|
63
|
|
- *
|
64
|
|
- * @param list The MapList whose values should be used
|
65
|
|
- */
|
66
|
|
- public MapList(final MapList<S, T> list) {
|
67
|
|
- map = list.getMap();
|
68
|
|
- }
|
69
|
|
-
|
70
|
|
- /**
|
71
|
|
- * Determines if this MapList is empty. An empty MapList is one that either
|
72
|
|
- * contains no keys, or contains only keys which have no associated values.
|
73
|
|
- *
|
74
|
|
- * @return True if this MapList is empty, false otherwise
|
75
|
|
- */
|
76
|
|
- public boolean isEmpty() {
|
77
|
|
- for (List<T> list : map.values()) {
|
78
|
|
- if (!list.isEmpty()) {
|
79
|
|
- return false;
|
80
|
|
- }
|
81
|
|
- }
|
82
|
|
-
|
83
|
|
- return true;
|
84
|
|
- }
|
85
|
|
-
|
86
|
|
- /**
|
87
|
|
- * Determines if this MapList contains the specified key.
|
88
|
|
- *
|
89
|
|
- * @param key The key to look for
|
90
|
|
- * @return True if this MapList contains the specified key, false otherwise
|
91
|
|
- */
|
92
|
|
- public boolean containsKey(final S key) {
|
93
|
|
- return map.containsKey(key);
|
94
|
|
- }
|
95
|
|
-
|
96
|
|
- /**
|
97
|
|
- * Determines if this MapList contains the specified value as a child of
|
98
|
|
- * the specified key.
|
99
|
|
- *
|
100
|
|
- * @param key The key to search under
|
101
|
|
- * @param value The value to look for
|
102
|
|
- * @return True if this MapList contains the specified key/value pair,
|
103
|
|
- * false otherwise
|
104
|
|
- */
|
105
|
|
- public boolean containsValue(final S key, final T value) {
|
106
|
|
- return map.containsKey(key) && map.get(key).contains(value);
|
107
|
|
- }
|
108
|
|
-
|
109
|
|
- /**
|
110
|
|
- * Retrieves the list of values associated with the specified key.
|
111
|
|
- *
|
112
|
|
- * @param key The key whose values are being retrieved
|
113
|
|
- * @return The values belonging to the specified key
|
114
|
|
- */
|
115
|
|
- public List<T> get(final S key) {
|
116
|
|
- return map.get(key);
|
117
|
|
- }
|
118
|
|
-
|
119
|
|
- /**
|
120
|
|
- * Retrieves the value at the specified offset of the specified key.
|
121
|
|
- *
|
122
|
|
- * @param key The key whose values are being retrieved
|
123
|
|
- * @param index The index of the value to retrieve
|
124
|
|
- * @return The specified value of the key
|
125
|
|
- */
|
126
|
|
- public T get(final S key, final int index) {
|
127
|
|
- return map.get(key).get(index);
|
128
|
|
- }
|
129
|
|
-
|
130
|
|
- /**
|
131
|
|
- * Retrieves the list of values associated with the specified key, creating
|
132
|
|
- * the key if necessary.
|
133
|
|
- *
|
134
|
|
- * @param key The key to retrieve
|
135
|
|
- * @return A list of the specified key's values
|
136
|
|
- */
|
137
|
|
- public List<T> safeGet(final S key) {
|
138
|
|
- if (!map.containsKey(key)) {
|
139
|
|
- map.put(key, Collections.synchronizedList(new ArrayList<>()));
|
140
|
|
- }
|
141
|
|
-
|
142
|
|
- return map.get(key);
|
143
|
|
- }
|
144
|
|
-
|
145
|
|
- /**
|
146
|
|
- * Adds the specified key to the MapList.
|
147
|
|
- *
|
148
|
|
- * @param key The key to be added
|
149
|
|
- */
|
150
|
|
- public void add(final S key) {
|
151
|
|
- safeGet(key);
|
152
|
|
- }
|
153
|
|
-
|
154
|
|
- /**
|
155
|
|
- * Adds the specified value as a child of the specified key. If the key
|
156
|
|
- * didn't previous exist, it is created.
|
157
|
|
- *
|
158
|
|
- * @param key The key to which the value is being added
|
159
|
|
- * @param value The value to be added
|
160
|
|
- */
|
161
|
|
- public void add(final S key, final T value) {
|
162
|
|
- safeGet(key).add(value);
|
163
|
|
- }
|
164
|
|
-
|
165
|
|
- /**
|
166
|
|
- * Adds the specified set of values to the specified key. If the key
|
167
|
|
- * didn't previous exist, it is created.
|
168
|
|
- *
|
169
|
|
- * @param key The key to which the value is being added
|
170
|
|
- * @param values The values to be added
|
171
|
|
- */
|
172
|
|
- public void add(final S key, final Collection<T> values) {
|
173
|
|
- safeGet(key).addAll(values);
|
174
|
|
- }
|
175
|
|
-
|
176
|
|
- /**
|
177
|
|
- * Removes the specified key and all of its values.
|
178
|
|
- *
|
179
|
|
- * @param key The key to remove
|
180
|
|
- */
|
181
|
|
- public void remove(final S key) {
|
182
|
|
- map.remove(key);
|
183
|
|
- }
|
184
|
|
-
|
185
|
|
- /**
|
186
|
|
- * Removes the specified value from all keys.
|
187
|
|
- *
|
188
|
|
- * @param value The value to remove
|
189
|
|
- */
|
190
|
|
- public void removeFromAll(final T value) {
|
191
|
|
- for (List<T> list : map.values()) {
|
192
|
|
- list.remove(value);
|
193
|
|
- }
|
194
|
|
- }
|
195
|
|
-
|
196
|
|
- /**
|
197
|
|
- * Removes the specified value from the specified key.
|
198
|
|
- *
|
199
|
|
- * @param key The key whose value is being removed
|
200
|
|
- * @param value The value to be removed
|
201
|
|
- */
|
202
|
|
- public void remove(final S key, final T value) {
|
203
|
|
- if (map.containsKey(key)) {
|
204
|
|
- map.get(key).remove(value);
|
205
|
|
- }
|
206
|
|
- }
|
207
|
|
-
|
208
|
|
- /**
|
209
|
|
- * Entirely clears this MapList.
|
210
|
|
- */
|
211
|
|
- public void clear() {
|
212
|
|
- map.clear();
|
213
|
|
- }
|
214
|
|
-
|
215
|
|
- /**
|
216
|
|
- * Clears all values of the specified key.
|
217
|
|
- *
|
218
|
|
- * @param key The key to be cleared
|
219
|
|
- */
|
220
|
|
- public void clear(final S key) {
|
221
|
|
- safeGet(key).clear();
|
222
|
|
- }
|
223
|
|
-
|
224
|
|
- /**
|
225
|
|
- * Returns the set of all keys belonging to this MapList.
|
226
|
|
- *
|
227
|
|
- * @return This MapList's keyset
|
228
|
|
- */
|
229
|
|
- public Set<S> keySet() {
|
230
|
|
- return map.keySet();
|
231
|
|
- }
|
232
|
|
-
|
233
|
|
- /**
|
234
|
|
- * Returns a collection of all values belonging to the specified key.
|
235
|
|
- *
|
236
|
|
- * @param key The key whose values are being sought
|
237
|
|
- * @return A collection of values belonging to the key
|
238
|
|
- */
|
239
|
|
- public Collection<T> values(final S key) {
|
240
|
|
- return map.get(key);
|
241
|
|
- }
|
242
|
|
-
|
243
|
|
- /**
|
244
|
|
- * Retrieves the entry set for this MapList.
|
245
|
|
- *
|
246
|
|
- * @return This MapList's entry set
|
247
|
|
- */
|
248
|
|
- public Set<Map.Entry<S, List<T>>> entrySet() {
|
249
|
|
- return map.entrySet();
|
250
|
|
- }
|
251
|
|
-
|
252
|
|
- /**
|
253
|
|
- * Retrieves the map behind this maplist.
|
254
|
|
- *
|
255
|
|
- * @return This MapList's map.
|
256
|
|
- */
|
257
|
|
- public Map<S, List<T>> getMap() {
|
258
|
|
- return new HashMap<>(map);
|
259
|
|
- }
|
260
|
|
-
|
261
|
|
-}
|