|
@@ -0,0 +1,229 @@
|
|
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.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
|
+
|
|
32
|
+/**
|
|
33
|
+ *
|
|
34
|
+ * @param <T>
|
|
35
|
+ * @author chris
|
|
36
|
+ */
|
|
37
|
+public class WeakList<T> implements List<T> {
|
|
38
|
+
|
|
39
|
+ private final List<WeakReference<T>> list = new ArrayList<WeakReference<T>>();
|
|
40
|
+
|
|
41
|
+ public WeakList() {
|
|
42
|
+ super();
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ private void cleanUp() {
|
|
46
|
+ for (int i = 0; i < list.size(); i++) {
|
|
47
|
+ if (list.get(i).get() == null) {
|
|
48
|
+ list.remove(i--);
|
|
49
|
+ }
|
|
50
|
+ }
|
|
51
|
+ }
|
|
52
|
+
|
|
53
|
+ private List<T> dereferenceList(final List<WeakReference<T>> list) {
|
|
54
|
+ final List<T> res = new ArrayList<T>();
|
|
55
|
+
|
|
56
|
+ for (WeakReference<T> item : list) {
|
|
57
|
+ if (item.get() != null) {
|
|
58
|
+ res.add(item.get());
|
|
59
|
+ }
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ return res;
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ @SuppressWarnings(value = "unchecked")
|
|
66
|
+ private Collection<WeakReference<T>> referenceCollection(final Collection<?> c) {
|
|
67
|
+ final Collection<WeakReference<T>> res = new ArrayList<WeakReference<T>>();
|
|
68
|
+
|
|
69
|
+ for (Object item : c) {
|
|
70
|
+ res.add(new WeakReference(item));
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ return res;
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ /** {@inheritDoc} */
|
|
77
|
+ @Override
|
|
78
|
+ public int size() {
|
|
79
|
+ cleanUp();
|
|
80
|
+
|
|
81
|
+ return list.size();
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ /** {@inheritDoc} */
|
|
85
|
+ @Override
|
|
86
|
+ public boolean isEmpty() {
|
|
87
|
+ cleanUp();
|
|
88
|
+
|
|
89
|
+ return list.isEmpty();
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ /** {@inheritDoc} */
|
|
93
|
+ @Override @SuppressWarnings(value = "unchecked")
|
|
94
|
+ public boolean contains(Object o) {
|
|
95
|
+ return list.contains(new WeakReference(o));
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ /** {@inheritDoc} */
|
|
99
|
+ @Override
|
|
100
|
+ public Iterator<T> iterator() {
|
|
101
|
+ return dereferenceList(list).iterator();
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ /** {@inheritDoc} */
|
|
105
|
+ @Override
|
|
106
|
+ public Object[] toArray() {
|
|
107
|
+ return dereferenceList(list).toArray();
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ /** {@inheritDoc} */
|
|
111
|
+ @Override
|
|
112
|
+ public <T> T[] toArray(T[] a) {
|
|
113
|
+ return dereferenceList(list).toArray(a);
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ /** {@inheritDoc} */
|
|
117
|
+ @Override
|
|
118
|
+ public boolean add(T e) {
|
|
119
|
+ return list.add(new WeakReference<T>(e));
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ /** {@inheritDoc} */
|
|
123
|
+ @Override @SuppressWarnings(value = "unchecked")
|
|
124
|
+ public boolean remove(Object o) {
|
|
125
|
+ return list.remove(new WeakReference(o));
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ /** {@inheritDoc} */
|
|
129
|
+ @Override
|
|
130
|
+ public boolean containsAll(Collection<?> c) {
|
|
131
|
+ return dereferenceList(list).containsAll(c);
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ /** {@inheritDoc} */
|
|
135
|
+ @Override
|
|
136
|
+ public boolean addAll(Collection<? extends T> c) {
|
|
137
|
+ return list.addAll(referenceCollection(c));
|
|
138
|
+ }
|
|
139
|
+
|
|
140
|
+ /** {@inheritDoc} */
|
|
141
|
+ @Override
|
|
142
|
+ public boolean addAll(int index, Collection<? extends T> c) {
|
|
143
|
+ return list.addAll(index, referenceCollection(c));
|
|
144
|
+ }
|
|
145
|
+
|
|
146
|
+ /** {@inheritDoc} */
|
|
147
|
+ @Override
|
|
148
|
+ public boolean removeAll(Collection<?> c) {
|
|
149
|
+ return list.removeAll(referenceCollection(c));
|
|
150
|
+ }
|
|
151
|
+
|
|
152
|
+ /** {@inheritDoc} */
|
|
153
|
+ @Override
|
|
154
|
+ public boolean retainAll(Collection<?> c) {
|
|
155
|
+ return list.retainAll(referenceCollection(c));
|
|
156
|
+ }
|
|
157
|
+
|
|
158
|
+ /** {@inheritDoc} */
|
|
159
|
+ @Override
|
|
160
|
+ public void clear() {
|
|
161
|
+ list.clear();
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+ /** {@inheritDoc} */
|
|
165
|
+ @Override
|
|
166
|
+ public T get(int index) {
|
|
167
|
+ cleanUp();
|
|
168
|
+
|
|
169
|
+ return list.get(index).get();
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+ /** {@inheritDoc} */
|
|
173
|
+ @Override
|
|
174
|
+ public T set(int index, T element) {
|
|
175
|
+ list.set(index, new WeakReference<T>(element));
|
|
176
|
+
|
|
177
|
+ return element;
|
|
178
|
+ }
|
|
179
|
+
|
|
180
|
+ /** {@inheritDoc} */
|
|
181
|
+ @Override
|
|
182
|
+ public void add(int index, T element) {
|
|
183
|
+ list.add(index, new WeakReference<T>(element));
|
|
184
|
+ }
|
|
185
|
+
|
|
186
|
+ /** {@inheritDoc} */
|
|
187
|
+ @Override
|
|
188
|
+ public T remove(int index) {
|
|
189
|
+ return list.remove(index).get();
|
|
190
|
+ }
|
|
191
|
+
|
|
192
|
+ /** {@inheritDoc} */
|
|
193
|
+ @Override @SuppressWarnings(value = "unchecked")
|
|
194
|
+ public int indexOf(Object o) {
|
|
195
|
+ cleanUp();
|
|
196
|
+
|
|
197
|
+ return list.indexOf(new WeakReference(o));
|
|
198
|
+ }
|
|
199
|
+
|
|
200
|
+ /** {@inheritDoc} */
|
|
201
|
+ @Override @SuppressWarnings(value = "unchecked")
|
|
202
|
+ public int lastIndexOf(Object o) {
|
|
203
|
+ cleanUp();
|
|
204
|
+
|
|
205
|
+ return list.lastIndexOf(new WeakReference(o));
|
|
206
|
+ }
|
|
207
|
+
|
|
208
|
+ /** {@inheritDoc} */
|
|
209
|
+ @Override
|
|
210
|
+ public ListIterator<T> listIterator() {
|
|
211
|
+ cleanUp();
|
|
212
|
+
|
|
213
|
+ return dereferenceList(list).listIterator();
|
|
214
|
+ }
|
|
215
|
+
|
|
216
|
+ /** {@inheritDoc} */
|
|
217
|
+ @Override
|
|
218
|
+ public ListIterator<T> listIterator(int index) {
|
|
219
|
+ cleanUp();
|
|
220
|
+
|
|
221
|
+ return dereferenceList(list).listIterator(index);
|
|
222
|
+ }
|
|
223
|
+
|
|
224
|
+ /** {@inheritDoc} */
|
|
225
|
+ @Override
|
|
226
|
+ public List<T> subList(int fromIndex, int toIndex) {
|
|
227
|
+ return dereferenceList(list.subList(fromIndex, toIndex));
|
|
228
|
+ }
|
|
229
|
+}
|