Browse Source

Remove WeakList et al.

pull/41/head
Chris Smith 9 years ago
parent
commit
e7c2ff7d8f

+ 0
- 65
src/com/dmdirc/util/collections/EquatableWeakReference.java View File

@@ -1,65 +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.lang.ref.Reference;
26
-import java.lang.ref.WeakReference;
27
-
28
-/**
29
- * An extension of WeakReference that implements a sane equals and hashcode
30
- * method.
31
- *
32
- * @param <T> The type of object that this reference contains
33
- */
34
-public class EquatableWeakReference<T> extends WeakReference<T> {
35
-
36
-    /**
37
-     * Creates a new instance of EquatableWeakReference.
38
-     *
39
-     * @param referent The object that this weak reference should reference.
40
-     */
41
-    public EquatableWeakReference(final T referent) {
42
-        super(referent);
43
-    }
44
-
45
-    @Override
46
-    public boolean equals(final Object obj) {
47
-        if (get() == null) {
48
-            return obj == null;
49
-        }
50
-        if (obj instanceof Reference<?>) {
51
-            return get().equals(((Reference<?>) obj).get());
52
-        } else {
53
-            return get().equals(obj);
54
-        }
55
-    }
56
-
57
-    @Override
58
-    public int hashCode() {
59
-        if (get() == null) {
60
-            return 0;
61
-        }
62
-        return get().hashCode();
63
-    }
64
-
65
-}

+ 0
- 227
src/com/dmdirc/util/collections/WeakList.java View File

@@ -1,227 +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.lang.ref.WeakReference;
26
-import java.util.ArrayList;
27
-import java.util.Collection;
28
-import java.util.Iterator;
29
-import java.util.List;
30
-import java.util.ListIterator;
31
-import java.util.stream.Collectors;
32
-
33
-import javax.annotation.Nonnull;
34
-
35
-/**
36
- * Implements a list of weak references. The weak references (and subsequent
37
- * garbage collection) are handled transparently.
38
- *
39
- * @param <T> The type of object that this list will contain.
40
- */
41
-public class WeakList<T> implements List<T> {
42
-
43
-    /** The items in this list. */
44
-    private final List<WeakReference<T>> list = new ArrayList<>();
45
-
46
-    /**
47
-     * Removes any entries from the list that have been GC'd.
48
-     */
49
-    private void cleanUp() {
50
-        for (int i = 0; i < list.size(); i++) {
51
-            if (list.get(i).get() == null) {
52
-                list.remove(i--);
53
-            }
54
-        }
55
-    }
56
-
57
-    /**
58
-     * Dereferences the specified list of WeakReferences to get a plain List.
59
-     *
60
-     * @param list The list to be dereferenced
61
-     * @return A list containing the items referenced by the specified list
62
-     */
63
-    private List<T> dereferenceList(final List<WeakReference<T>> list) {
64
-        return list.stream().filter(item -> item.get() != null)
65
-                .map(WeakReference::get)
66
-                .collect(Collectors.toList());
67
-    }
68
-
69
-    /**
70
-     * Creates a new collection of weak references to elements in the specified
71
-     * collection.
72
-     *
73
-     * @param c The collection whose elements should be referenced
74
-     * @return A copy of the specified collection, with each item wrapped in
75
-     * a weak reference.
76
-     */
77
-    @SuppressWarnings("unchecked")
78
-    private Collection<WeakReference<T>> referenceCollection(
79
-            final Collection<?> c) {
80
-        final Collection<WeakReference<T>> res = new ArrayList<>();
81
-
82
-        for (Object item : c) {
83
-            res.add(new EquatableWeakReference<>((T) item));
84
-        }
85
-
86
-        return res;
87
-    }
88
-
89
-    @Override
90
-    public int size() {
91
-        cleanUp();
92
-
93
-        return list.size();
94
-    }
95
-
96
-    @Override
97
-    public boolean isEmpty() {
98
-        cleanUp();
99
-
100
-        return list.isEmpty();
101
-    }
102
-
103
-    @Override
104
-    @SuppressWarnings("unchecked")
105
-    public boolean contains(final Object o) {
106
-        return list.contains(new EquatableWeakReference<>((T) o));
107
-    }
108
-
109
-    @Nonnull
110
-    @Override
111
-    public Iterator<T> iterator() {
112
-        return dereferenceList(list).iterator();
113
-    }
114
-
115
-    @Nonnull
116
-    @Override
117
-    public Object[] toArray() {
118
-        return dereferenceList(list).toArray();
119
-    }
120
-
121
-    @Nonnull
122
-    @Override
123
-    public <S> S[] toArray(@Nonnull final S[] a) {
124
-        return dereferenceList(list).toArray(a);
125
-    }
126
-
127
-    @Override
128
-    public boolean add(final T e) {
129
-        return list.add(new EquatableWeakReference<>(e));
130
-    }
131
-
132
-    @Override
133
-    @SuppressWarnings("unchecked")
134
-    public boolean remove(final Object o) {
135
-        return list.remove(new EquatableWeakReference<>((T) o));
136
-    }
137
-
138
-    @Override
139
-    public boolean containsAll(@Nonnull final Collection<?> c) {
140
-        return dereferenceList(list).containsAll(c);
141
-    }
142
-
143
-    @Override
144
-    public boolean addAll(@Nonnull final Collection<? extends T> c) {
145
-        return list.addAll(referenceCollection(c));
146
-    }
147
-
148
-    @Override
149
-    public boolean addAll(final int index, @Nonnull final Collection<? extends T> c) {
150
-        return list.addAll(index, referenceCollection(c));
151
-    }
152
-
153
-    @Override
154
-    public boolean removeAll(@Nonnull final Collection<?> c) {
155
-        return list.removeAll(referenceCollection(c));
156
-    }
157
-
158
-    @Override
159
-    public boolean retainAll(@Nonnull final Collection<?> c) {
160
-        return list.retainAll(referenceCollection(c));
161
-    }
162
-
163
-    @Override
164
-    public void clear() {
165
-        list.clear();
166
-    }
167
-
168
-    @Override
169
-    public T get(final int index) {
170
-        cleanUp();
171
-
172
-        return list.get(index).get();
173
-    }
174
-
175
-    @Override
176
-    public T set(final int index, final T element) {
177
-        list.set(index, new EquatableWeakReference<>(element));
178
-
179
-        return element;
180
-    }
181
-
182
-    @Override
183
-    public void add(final int index, final T element) {
184
-        list.add(index, new EquatableWeakReference<>(element));
185
-    }
186
-
187
-    @Override
188
-    public T remove(final int index) {
189
-        return list.remove(index).get();
190
-    }
191
-
192
-    @Override
193
-    public int indexOf(final Object o) {
194
-        cleanUp();
195
-
196
-        return dereferenceList(list).indexOf(o);
197
-    }
198
-
199
-    @Override
200
-    public int lastIndexOf(final Object o) {
201
-        cleanUp();
202
-
203
-        return dereferenceList(list).lastIndexOf(o);
204
-    }
205
-
206
-    @Nonnull
207
-    @Override
208
-    public ListIterator<T> listIterator() {
209
-        cleanUp();
210
-
211
-        return dereferenceList(list).listIterator();
212
-    }
213
-
214
-    @Nonnull
215
-    @Override
216
-    public ListIterator<T> listIterator(final int index) {
217
-        cleanUp();
218
-
219
-        return dereferenceList(list).listIterator(index);
220
-    }
221
-
222
-    @Nonnull
223
-    @Override
224
-    public List<T> subList(final int fromIndex, final int toIndex) {
225
-        return dereferenceList(list.subList(fromIndex, toIndex));
226
-    }
227
-}

+ 0
- 51
test/com/dmdirc/util/collections/EquatableWeakReferenceTest.java View File

@@ -1,51 +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
-package com.dmdirc.util.collections;
23
-
24
-import java.lang.ref.Reference;
25
-import java.lang.ref.WeakReference;
26
-import org.junit.Test;
27
-import static org.junit.Assert.*;
28
-
29
-public class EquatableWeakReferenceTest {
30
-
31
-    @Test
32
-    public void testEquals() {
33
-        final Object myObject = "moo";
34
-        final Reference<Object> myRef = new WeakReference<>(myObject);
35
-        final EquatableWeakReference<Object> ewf = new EquatableWeakReference<>(myObject);
36
-
37
-        assertTrue(ewf.equals(myObject));
38
-        assertTrue(ewf.equals(myRef));
39
-        assertFalse("bar".equals(ewf));
40
-        assertFalse(ewf.equals(new WeakReference<Object>("bar")));
41
-    }
42
-
43
-    @Test
44
-    public void testHashCode() {
45
-        final Object myObject = "moo";
46
-        final EquatableWeakReference<Object> ewf = new EquatableWeakReference<>(myObject);
47
-        
48
-        assertEquals(myObject.hashCode(), ewf.hashCode());
49
-    }
50
-
51
-}

+ 0
- 283
test/com/dmdirc/util/collections/WeakListTest.java View File

@@ -1,283 +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.Iterator;
28
-import java.util.List;
29
-import java.util.ListIterator;
30
-
31
-import org.junit.Test;
32
-
33
-import static org.junit.Assert.*;
34
-
35
-public class WeakListTest {
36
-
37
-    @Test
38
-    public void testIsEmpty() {
39
-        final List<String> instance = new WeakList<>();
40
-        assertTrue(instance.isEmpty());
41
-        instance.add("test1");
42
-        assertFalse(instance.isEmpty());
43
-    }
44
-
45
-    @Test
46
-    public void testAdd() {
47
-        final List<String> instance = new WeakList<>();
48
-        assertTrue(instance.add("test1"));
49
-        assertFalse(instance.isEmpty());
50
-    }
51
-
52
-    @Test
53
-    public void testAddInt() {
54
-        final List<String> instance = new WeakList<>();
55
-        assertTrue(instance.add("test1"));
56
-        instance.add(0, "test2");
57
-        assertEquals("test2", instance.get(0));
58
-    }
59
-
60
-    @Test
61
-    public void testRemove() {
62
-        final List<String> instance = new WeakList<>();
63
-        instance.add("test1");
64
-        assertFalse(instance.isEmpty());
65
-        assertTrue(instance.remove("test1"));
66
-        assertFalse(instance.remove("test1"));
67
-    }
68
-
69
-    @Test
70
-    public void testRemoveInt() {
71
-        final List<String> instance = new WeakList<>();
72
-        assertTrue(instance.isEmpty());
73
-        instance.add("test1");
74
-        instance.add("test2");
75
-        assertEquals(2, instance.size());
76
-        instance.remove(1);
77
-        assertTrue(instance.contains("test1"));
78
-        assertFalse(instance.contains("test2"));
79
-    }
80
-
81
-    @Test
82
-    public void testGet() {
83
-        final List<String> instance = new WeakList<>();
84
-        instance.add("test1");
85
-        instance.add("test2");
86
-        assertEquals("test1", instance.get(0));
87
-        assertEquals("test2", instance.get(1));
88
-    }
89
-
90
-    @Test
91
-    public void testContains() {
92
-        final List<String> instance = new WeakList<>();
93
-        assertFalse(instance.contains("test1"));
94
-        instance.add("test1");
95
-        assertTrue(instance.contains("test1"));
96
-    }
97
-
98
-    /**
99
-     * Test of toArray method, of class WeakList.
100
-     */
101
-    @Test
102
-    public void testToArray0args() {
103
-        final List<String> instance = new WeakList<>();
104
-        assertEquals(0, instance.toArray().length);
105
-        instance.add("test1");
106
-        instance.add("test2");
107
-        final Object[] result = instance.toArray();
108
-        assertEquals(2, result.length);
109
-        assertEquals("test1", result[0]);
110
-        assertEquals("test2", result[1]);
111
-    }
112
-
113
-    /**
114
-     * Test of toArray method, of class WeakList.
115
-     */
116
-    @Test
117
-    public void testToArrayGenericType() {
118
-        final List<String> instance = new WeakList<>();
119
-        assertEquals(0, instance.toArray(new String[instance.size()]).length);
120
-        instance.add("test1");
121
-        instance.add("test2");
122
-        final Object[] result = instance.toArray(new String[2]);
123
-        assertEquals(2, result.length);
124
-        assertEquals("test1", result[0]);
125
-        assertEquals("test2", result[1]);
126
-    }
127
-
128
-    /**
129
-     * Test of containsAll method, of class WeakList.
130
-     */
131
-    @Test
132
-    public void testContainsAllAddAll() {
133
-        final Collection<String> list = new ArrayList<>();
134
-        list.add("test1");
135
-        list.add("test2");
136
-        final List<String> instance = new WeakList<>();
137
-        assertFalse(instance.containsAll(list));
138
-        instance.addAll(list);
139
-        assertTrue(instance.contains("test1"));
140
-        assertTrue(instance.contains("test2"));
141
-        assertTrue(instance.containsAll(list));
142
-    }
143
-
144
-    @Test
145
-    public void testAddAllIntCollection() {
146
-        final Collection<String> list = new ArrayList<>();
147
-        list.add("test1");
148
-        list.add("test2");
149
-        final List<String> instance = new WeakList<>();
150
-        instance.add("test3");
151
-        assertEquals("test3", instance.get(0));
152
-        assertFalse(instance.containsAll(list));
153
-        instance.addAll(0, list);
154
-        assertEquals("test1", instance.get(0));
155
-        assertEquals("test2", instance.get(1));
156
-    }
157
-
158
-    @Test
159
-    public void testIndexOf() {
160
-        final String one = "test1";
161
-        final List<String> instance = new WeakList<>();
162
-        instance.add(one);
163
-        final String two = "test2";
164
-        instance.add(two);
165
-        assertEquals(2, instance.size());
166
-        assertEquals(0, instance.indexOf(one));
167
-        assertEquals(1, instance.indexOf(two));
168
-    }
169
-
170
-    @Test
171
-    public void testLastIndexOf() {
172
-        final String one = "test1";
173
-        final List<String> instance = new WeakList<>();
174
-        instance.add(one);
175
-        final String two = "test2";
176
-        instance.add(two);
177
-        instance.add(one);
178
-        assertEquals(3, instance.size());
179
-        assertEquals(2, instance.lastIndexOf(one));
180
-    }
181
-
182
-    @Test
183
-    public void testRemoveAll() {
184
-        final Collection<String> list = new ArrayList<>();
185
-        list.add("test1");
186
-        list.add("test2");
187
-        final List<String> instance = new WeakList<>();
188
-        instance.addAll(list);
189
-        assertFalse(instance.isEmpty());
190
-        instance.removeAll(list);
191
-        assertTrue(instance.isEmpty());
192
-    }
193
-
194
-    @Test
195
-    public void testRetainAll() {
196
-        final Collection<String> list = new ArrayList<>();
197
-        list.add("test1");
198
-        list.add("test2");
199
-        final List<String> instance = new WeakList<>();
200
-        instance.addAll(list);
201
-        instance.add("test3");
202
-        instance.add("test4");
203
-        assertFalse(list.size() == instance.size());
204
-        instance.retainAll(list);
205
-        assertTrue(list.size() == instance.size());
206
-    }
207
-
208
-    @Test
209
-    public void testClear() {
210
-        final Collection<String> list = new ArrayList<>();
211
-        list.add("test1");
212
-        list.add("test2");
213
-        final List<String> instance = new WeakList<>();
214
-        instance.addAll(list);
215
-        assertFalse(instance.isEmpty());
216
-        instance.clear();
217
-        assertTrue(instance.isEmpty());
218
-    }
219
-
220
-    @Test
221
-    public void testSet() {
222
-        final List<String> instance = new WeakList<>();
223
-        instance.add("test1");
224
-        assertEquals("test1", instance.get(0));
225
-        instance.set(0, "test2");
226
-        assertEquals("test2", instance.get(0));
227
-    }
228
-
229
-    @Test
230
-    public void testIterator() {
231
-        final List<String> instance = new WeakList<>();
232
-        Iterator<String> result = instance.iterator();
233
-        assertFalse(result.hasNext());
234
-        instance.add("test1");
235
-        instance.add("test2");
236
-        result = instance.iterator();
237
-        assertEquals("test1", result.next());
238
-        assertEquals("test2", result.next());
239
-        assertFalse(result.hasNext());
240
-    }
241
-
242
-    @Test
243
-    public void testListIterator0args() {
244
-        final List<String> instance = new WeakList<>();
245
-        Iterator<String> result = instance.listIterator();
246
-        assertFalse(result.hasNext());
247
-        instance.add("test1");
248
-        instance.add("test2");
249
-        result = instance.listIterator();
250
-        assertEquals("test1", result.next());
251
-        assertEquals("test2", result.next());
252
-        assertFalse(result.hasNext());
253
-    }
254
-
255
-    @Test(expected=IndexOutOfBoundsException.class)
256
-    public void testListIteratorInt() {
257
-        final List<String> instance = new WeakList<>();
258
-        ListIterator<String> result = instance.listIterator(1);
259
-        assertFalse(result.hasNext());
260
-        instance.add("test1");
261
-        instance.add("test2");
262
-        instance.add("text3");
263
-        result = instance.listIterator(1);
264
-        assertEquals("test1", result.previous());
265
-        assertEquals("test2", result.next());
266
-        assertEquals("test3", result.next());
267
-        assertFalse(result.hasNext());
268
-    }
269
-
270
-    @Test
271
-    public void testSubList() {
272
-        final List<String> instance = new WeakList<>();
273
-        instance.add("test1");
274
-        instance.add("test2");
275
-        instance.add("test3");
276
-        instance.add("test4");
277
-        final List<String> result = instance.subList(1, 3);
278
-        assertEquals(2, result.size());
279
-        assertTrue(result.contains("test2"));
280
-        assertTrue(result.contains("test3"));
281
-    }
282
-
283
-}

Loading…
Cancel
Save