Browse Source

Added EquatableWeakReference to make up for Sun's crap (Weak)Reference implementation

Added unit tests for MapList and WeakList

git-svn-id: http://svn.dmdirc.com/trunk@2872 00569f92-eb28-0410-84fd-f71c24880f
tags/0.6
Chris Smith 16 years ago
parent
commit
0342f857ef

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

@@ -0,0 +1,56 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
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;
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
+ * @author chris
33
+ */
34
+public class EquatableWeakReference<T> extends WeakReference<T> {
35
+    
36
+    public EquatableWeakReference(T referent) {
37
+        super(referent);
38
+    }
39
+
40
+    /** {@inheritDoc} */
41
+    @Override
42
+    public boolean equals(Object obj) {
43
+        if (obj instanceof Reference) {
44
+            return get().equals(((Reference) obj).get());
45
+        } else {
46
+            return get().equals(obj);
47
+        }
48
+    }
49
+
50
+    /** {@inheritDoc} */
51
+    @Override
52
+    public int hashCode() {
53
+        return get().hashCode();
54
+    }
55
+    
56
+}

+ 9
- 9
src/com/dmdirc/util/WeakList.java View File

@@ -67,7 +67,7 @@ public class WeakList<T> implements List<T> {
67 67
         final Collection<WeakReference<T>> res = new ArrayList<WeakReference<T>>();
68 68
 
69 69
         for (Object item : c) {
70
-            res.add(new WeakReference(item));
70
+            res.add(new EquatableWeakReference(item));
71 71
         }
72 72
 
73 73
         return res;
@@ -90,9 +90,9 @@ public class WeakList<T> implements List<T> {
90 90
     }
91 91
 
92 92
     /** {@inheritDoc} */
93
-    @Override @SuppressWarnings(value = "unchecked")
93
+    @Override @SuppressWarnings("unchecked")
94 94
     public boolean contains(Object o) {
95
-        return list.contains(new WeakReference(o));
95
+        return list.contains(new EquatableWeakReference(o));
96 96
     }
97 97
 
98 98
     /** {@inheritDoc} */
@@ -116,13 +116,13 @@ public class WeakList<T> implements List<T> {
116 116
     /** {@inheritDoc} */
117 117
     @Override
118 118
     public boolean add(T e) {
119
-        return list.add(new WeakReference<T>(e));
119
+        return list.add(new EquatableWeakReference<T>(e));
120 120
     }
121 121
 
122 122
     /** {@inheritDoc} */
123 123
     @Override @SuppressWarnings(value = "unchecked")
124 124
     public boolean remove(Object o) {
125
-        return list.remove(new WeakReference(o));
125
+        return list.remove(new EquatableWeakReference(o));
126 126
     }
127 127
 
128 128
     /** {@inheritDoc} */
@@ -172,7 +172,7 @@ public class WeakList<T> implements List<T> {
172 172
     /** {@inheritDoc} */
173 173
     @Override
174 174
     public T set(int index, T element) {
175
-        list.set(index, new WeakReference<T>(element));
175
+        list.set(index, new EquatableWeakReference<T>(element));
176 176
 
177 177
         return element;
178 178
     }
@@ -180,7 +180,7 @@ public class WeakList<T> implements List<T> {
180 180
     /** {@inheritDoc} */
181 181
     @Override
182 182
     public void add(int index, T element) {
183
-        list.add(index, new WeakReference<T>(element));
183
+        list.add(index, new EquatableWeakReference<T>(element));
184 184
     }
185 185
 
186 186
     /** {@inheritDoc} */
@@ -194,7 +194,7 @@ public class WeakList<T> implements List<T> {
194 194
     public int indexOf(Object o) {
195 195
         cleanUp();
196 196
 
197
-        return list.indexOf(new WeakReference(o));
197
+        return list.indexOf(o);
198 198
     }
199 199
 
200 200
     /** {@inheritDoc} */
@@ -202,7 +202,7 @@ public class WeakList<T> implements List<T> {
202 202
     public int lastIndexOf(Object o) {
203 203
         cleanUp();
204 204
 
205
-        return list.lastIndexOf(new WeakReference(o));
205
+        return list.lastIndexOf(o);
206 206
     }
207 207
 
208 208
     /** {@inheritDoc} */

+ 63
- 0
test/com/dmdirc/util/MapListTest.java View File

@@ -0,0 +1,63 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
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;
24
+
25
+import org.junit.Test;
26
+import static org.junit.Assert.*;
27
+
28
+public class MapListTest extends junit.framework.TestCase {
29
+
30
+    @Test
31
+    public void testIsEmpty() {
32
+        final MapList<String, String> test = new MapList<String, String>();
33
+        assertTrue(test.isEmpty());
34
+
35
+        test.add("a", "b");
36
+        test.removeFromAll("b");
37
+        assertTrue(test.isEmpty());
38
+    }
39
+
40
+    @Test
41
+    public void testContainsKey() {
42
+        final MapList<String, String> test = new MapList<String, String>();
43
+        test.add("a", "b");
44
+        assertTrue(test.containsKey("a"));
45
+    }
46
+
47
+    @Test
48
+    public void testContainsValue() {
49
+        final MapList<String, String> test = new MapList<String, String>();
50
+        test.add("a", "b");
51
+        assertTrue(test.containsValue("a", "b"));
52
+    }
53
+
54
+    @Test
55
+    public void testGet() {
56
+        final MapList<String, String> test = new MapList<String, String>();
57
+        test.add("a", "b");
58
+        assertTrue(test.get("a").size() == 1);
59
+        assertTrue(test.get("a").get(0).equals("b"));
60
+        assertTrue(test.get("a", 0).equals("b"));
61
+    }
62
+
63
+}

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

@@ -0,0 +1,50 @@
1
+/*
2
+ * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
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;
24
+
25
+import org.junit.Test;
26
+import static org.junit.Assert.*;
27
+
28
+public class WeakListTest extends junit.framework.TestCase {
29
+
30
+    @Test
31
+    public void testBasics() {
32
+        final WeakList<String> test = new WeakList<String>();
33
+        assertTrue(test.isEmpty());
34
+        
35
+        test.add("abcdef");
36
+        test.add("123");
37
+        
38
+        assertEquals(2, test.size());
39
+        assertTrue(test.get(0).equals("abcdef"));
40
+        assertTrue(test.contains("123"));
41
+        assertFalse(test.isEmpty());
42
+        
43
+        test.remove("abcdef");
44
+        assertFalse(test.contains("abcdef"));
45
+        
46
+        test.remove("123");
47
+        assertTrue(test.isEmpty());
48
+    }
49
+
50
+}

Loading…
Cancel
Save