Bladeren bron

Merge pull request #39 from csmith/master

Put MapList to rest.
pull/41/head
Greg Holmes 9 jaren geleden
bovenliggende
commit
9094ae8a84

+ 0
- 261
src/com/dmdirc/util/collections/MapList.java Bestand weergeven

@@ -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
-}

+ 0
- 53
src/com/dmdirc/util/collections/WeakMapList.java Bestand weergeven

@@ -1,53 +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.List;
26
-
27
-/**
28
- * Wraps a Map&lt;S, List&lt;T&gt;&gt; with various convenience methods for
29
- * accessing the data. Implements a Map-like interface for easier transition.
30
- * This implementation uses WeakLists (i.e., lists of weak references) - all
31
- * references to values are wrapped in WeakReferences.
32
- *
33
- * @param <S> the type of keys maintained by this map
34
- * @param <T> the type of mapped values
35
- */
36
-public class WeakMapList<S,T> extends MapList<S, T> {
37
-
38
-    /**
39
-     * Retrieves the list of values associated with the specified key, creating
40
-     * the key if neccessary.
41
-     *
42
-     * @param key The key to retrieve
43
-     * @return A list of the specified key's values
44
-     */
45
-    @Override
46
-    public List<T> safeGet(final S key) {
47
-        if (!map.containsKey(key)) {
48
-            map.put(key, new WeakList<>());
49
-        }
50
-
51
-        return map.get(key);
52
-    }
53
-}

+ 22
- 8
src/com/dmdirc/util/io/ConfigFile.java Bestand weergeven

@@ -22,8 +22,6 @@
22 22
 
23 23
 package com.dmdirc.util.io;
24 24
 
25
-import com.dmdirc.util.collections.MapList;
26
-
27 25
 import java.io.IOException;
28 26
 import java.io.InputStream;
29 27
 import java.nio.charset.Charset;
@@ -46,7 +44,7 @@ public class ConfigFile extends TextFile {
46 44
     private final Collection<String> domains = new ArrayList<>();
47 45
 
48 46
     /** The values associated with each flat domain. */
49
-    private final MapList<String, String> flatdomains = new MapList<>();
47
+    private final Map<String, List<String>> flatdomains = new HashMap<>();
50 48
 
51 49
     /** The key/value sets associated with each key domain. */
52 50
     private final Map<String, Map<String, String>> keydomains = new HashMap<>();
@@ -117,12 +115,12 @@ public class ConfigFile extends TextFile {
117 115
                 domains.add(domain);
118 116
 
119 117
                 keydomain = keydomains.containsKey(domain)
120
-                        || flatdomains.containsValue("keysections", domain);
118
+                        || hasFlatDomainValue("keysections", domain);
121 119
 
122 120
                 if (keydomain && !keydomains.containsKey(domain)) {
123 121
                     keydomains.put(domain, new HashMap<>());
124 122
                 } else if (!keydomain && !flatdomains.containsKey(domain)) {
125
-                    flatdomains.add(domain);
123
+                    flatdomains.put(domain, new ArrayList<>());
126 124
                 }
127 125
             } else if (domain != null && keydomain
128 126
                     && (offset = findEquals(tline)) != -1) {
@@ -131,7 +129,7 @@ public class ConfigFile extends TextFile {
131 129
 
132 130
                 keydomains.get(domain).put(key, value);
133 131
             } else if (domain != null && !keydomain) {
134
-                flatdomains.add(domain, unescape(tline));
132
+                addFlatDomainValue(domain, unescape(tline));
135 133
             } else {
136 134
                 throw new InvalidConfigFileException("Unknown or unexpected"
137 135
                         + " line encountered: " + tline);
@@ -150,7 +148,7 @@ public class ConfigFile extends TextFile {
150 148
                     + "that isn't writable");
151 149
         }
152 150
 
153
-        final List<String> lines = new ArrayList<>();
151
+        final Collection<String> lines = new ArrayList<>();
154 152
 
155 153
         lines.add("# This is a DMDirc configuration file.");
156 154
         lines.add("# Written on: " + new GregorianCalendar().getTime());
@@ -275,7 +273,12 @@ public class ConfigFile extends TextFile {
275 273
      */
276 274
     public void addDomain(final String name, final Collection<String> data) {
277 275
         domains.add(name);
278
-        flatdomains.add(name, data);
276
+
277
+        if (flatdomains.containsKey(name)) {
278
+            flatdomains.get(name).addAll(data);
279
+        } else {
280
+            flatdomains.put(name, new ArrayList<>(data));
281
+        }
279 282
     }
280 283
 
281 284
     /**
@@ -289,6 +292,17 @@ public class ConfigFile extends TextFile {
289 292
         keydomains.put(name, data);
290 293
     }
291 294
 
295
+    private boolean hasFlatDomainValue(final String domain, final String value) {
296
+        return flatdomains.containsKey(domain) && flatdomains.get(domain).contains(value);
297
+    }
298
+
299
+    private void addFlatDomainValue(final String domain, final String value) {
300
+        if (!flatdomains.containsKey(domain)) {
301
+            flatdomains.put(domain, new ArrayList<>());
302
+        }
303
+        flatdomains.get(domain).add(value);
304
+    }
305
+
292 306
     /**
293 307
      * Unescapes any escaped characters in the specified input string.
294 308
      *

+ 0
- 157
test/com/dmdirc/util/collections/MapListTest.java Bestand weergeven

@@ -1,157 +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 org.junit.Test;
26
-
27
-import java.util.ArrayList;
28
-import java.util.Collection;
29
-
30
-import static org.junit.Assert.*;
31
-
32
-public class MapListTest {
33
-
34
-    @Test
35
-    public void testIsEmpty() {
36
-        final MapList<String, String> test = new MapList<>();
37
-        assertTrue(test.isEmpty());
38
-
39
-        test.add("a", "b");
40
-        assertFalse(test.isEmpty());
41
-        test.removeFromAll("b");
42
-        assertTrue(test.isEmpty());
43
-    }
44
-
45
-    @Test
46
-    public void testAddCollection() {
47
-        final MapList<String, String> test = new MapList<>();
48
-        final Collection<String> testList = new ArrayList<>();
49
-        testList.add("d");
50
-        testList.add("e");
51
-        test.add("key", testList);
52
-
53
-        assertTrue(test.containsKey("key"));
54
-        assertTrue(test.containsValue("key", "d"));
55
-        assertTrue(test.containsValue("key", "e"));
56
-    }
57
-
58
-    @Test
59
-    public void testClear() {
60
-        final MapList<String, String> test = new MapList<>();
61
-        test.add("a", "b");
62
-        test.add("d", "e");
63
-        test.clear();
64
-        assertTrue(test.isEmpty());
65
-    }
66
-
67
-    @Test
68
-    public void testClearKey() {
69
-        final MapList<String, String> test = new MapList<>();
70
-        test.add("a", "b");
71
-        test.add("d", "e");
72
-        test.clear("a");
73
-        assertTrue(test.values("a").isEmpty());
74
-        assertFalse(test.isEmpty());
75
-    }
76
-
77
-    @Test
78
-    public void testRemove() {
79
-        final MapList<String, String> test = new MapList<>();
80
-        test.add("a", "b");
81
-        test.add("d", "e");
82
-        test.remove("z", "b");
83
-
84
-        assertEquals(2, test.keySet().size());
85
-        assertEquals(1, test.values("a").size());
86
-        assertEquals(1, test.values("d").size());
87
-
88
-        test.remove("a", "b");
89
-        assertEquals(2, test.keySet().size());
90
-        assertEquals(0, test.values("a").size());
91
-        assertEquals(1, test.values("d").size());
92
-    }
93
-
94
-    @Test
95
-    public void testRemoveKey() {
96
-        final MapList<String, String> instance = new MapList<>();
97
-        instance.add("a", "b");
98
-        assertTrue(instance.containsKey("a"));
99
-        instance.remove("a");
100
-        assertFalse(instance.containsKey("a"));
101
-    }
102
-
103
-    @Test
104
-    public void testKeySet() {
105
-        final MapList<String, String> test = new MapList<>();
106
-        test.add("a", "b");
107
-        test.add("d", "e");
108
-        assertEquals(2, test.keySet().size());
109
-        assertTrue(test.keySet().contains("a"));
110
-        assertTrue(test.keySet().contains("d"));
111
-    }
112
-
113
-    @Test
114
-    public void testContainsKey() {
115
-        final MapList<String, String> test = new MapList<>();
116
-        test.add("a", "b");
117
-        assertTrue(test.containsKey("a"));
118
-    }
119
-
120
-    @Test
121
-    public void testContainsValue() {
122
-        final MapList<String, String> test = new MapList<>();
123
-        test.add("a", "b");
124
-        assertTrue(test.containsValue("a", "b"));
125
-    }
126
-
127
-    @Test
128
-    public void testGet() {
129
-        final MapList<String, String> test = new MapList<>();
130
-        test.add("a", "b");
131
-        assertEquals(1, test.get("a").size());
132
-        assertEquals("b", test.get("a").get(0));
133
-        assertEquals("b", test.get("a", 0));
134
-    }
135
-
136
-    @Test
137
-    public void testInherit() {
138
-        final MapList<String, String> test1 = new MapList<>();
139
-        test1.add("a", "b");
140
-
141
-        final MapList<String, String> test2 = new MapList<>(test1);
142
-        assertEquals(1, test2.get("a").size());
143
-        assertEquals("b", test2.get("a").get(0));
144
-        assertEquals("b", test2.get("a", 0));
145
-    }
146
-
147
-    @Test
148
-    public void testGetMap() {
149
-        final MapList<String, String> test1 = new MapList<>();
150
-        test1.add("a", "b");
151
-        assertNotSame(test1.getMap(), test1.getMap());
152
-
153
-        final MapList<String, String> test2 = new MapList<>(test1);
154
-        assertEquals(test1.getMap(), test2.getMap());
155
-    }
156
-
157
-}

Laden…
Annuleren
Opslaan