1
2
3
4
5
6
7
8
9
10
11 package de.jaret.util.collections;
12
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.ListIterator;
18
19
20
21
22
23 public class WatchableArrayList implements List {
24 private List<ListWatcher> _listeners;
25 private List _content;
26
27
28
29
30 public WatchableArrayList() {
31 _content = new ArrayList();
32 }
33
34 public void addListWatcher(ListWatcher watcher) {
35 if (_listeners == null) {
36 _listeners = new ArrayList<ListWatcher>();
37 }
38 _listeners.add(watcher);
39 }
40
41 public void removeListWatcher(ListWatcher watcher) {
42 if (_listeners != null) {
43 _listeners.remove(watcher);
44 }
45 }
46
47 private void fireElementAdded(Object o) {
48 if (_listeners != null) {
49 for (ListWatcher watcher : _listeners) {
50 watcher.elementAdded(o);
51 }
52 }
53 }
54
55 private void fireElementRemoved(Object o) {
56 if (_listeners != null) {
57 for (ListWatcher watcher : _listeners) {
58 watcher.elementRemoved(o);
59 }
60 }
61 }
62
63
64
65
66
67
68 public int size() {
69 return _content.size();
70 }
71
72
73
74
75
76
77 public boolean isEmpty() {
78 return _content.isEmpty();
79 }
80
81
82
83
84
85
86 public boolean contains(Object o) {
87 return _content.contains(o);
88 }
89
90
91
92
93
94
95 public Iterator iterator() {
96 return _content.iterator();
97 }
98
99
100
101
102
103
104 public Object[] toArray() {
105 return _content.toArray();
106 }
107
108
109
110
111
112
113 public Object[] toArray(Object[] a) {
114 return _content.toArray(a);
115 }
116
117
118
119
120
121
122 public boolean add(Object o) {
123 boolean result = _content.add(o);
124 fireElementAdded(o);
125 return result;
126 }
127
128
129
130
131
132
133 public boolean remove(Object o) {
134 boolean wasInList = _content.contains(o);
135 boolean result = _content.remove(o);
136 if (wasInList) {
137 fireElementRemoved(o);
138 }
139 return result;
140 }
141
142
143
144
145
146
147 public boolean containsAll(Collection c) {
148 return _content.containsAll(c);
149 }
150
151
152
153
154
155
156 public boolean addAll(Collection c) {
157
158 return _content.addAll(c);
159 }
160
161
162
163
164
165
166 public boolean addAll(int index, Collection c) {
167
168 return _content.addAll(index, c);
169 }
170
171
172
173
174
175
176 public boolean removeAll(Collection c) {
177
178 return _content.removeAll(c);
179 }
180
181
182
183
184
185
186 public boolean retainAll(Collection c) {
187
188 return _content.retainAll(c);
189 }
190
191
192
193
194
195
196 public void clear() {
197
198 _content.clear();
199
200 }
201
202
203
204
205
206
207 public Object get(int index) {
208 return _content.get(index);
209 }
210
211
212
213
214
215
216 public Object set(int index, Object element) {
217
218 return _content.set(index, element);
219 }
220
221
222
223
224
225
226 public void add(int index, Object element) {
227 _content.add(index, element);
228 fireElementAdded(element);
229 }
230
231
232
233
234
235
236 public Object remove(int index) {
237 Object o = _content.remove(index);
238 fireElementRemoved(o);
239 return o;
240 }
241
242
243
244
245
246
247 public int indexOf(Object o) {
248 return _content.indexOf(o);
249 }
250
251
252
253
254
255
256 public int lastIndexOf(Object o) {
257 return _content.lastIndexOf(o);
258 }
259
260
261
262
263
264
265 public ListIterator listIterator() {
266 return _content.listIterator();
267 }
268
269
270
271
272
273
274 public ListIterator listIterator(int index) {
275 return _content.listIterator(index);
276 }
277
278
279
280
281
282
283 public List subList(int fromIndex, int toIndex) {
284 return _content.subList(fromIndex, toIndex);
285 }
286
287 }