View Javadoc

1   /*
2    *  File: WatchableArrayList.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Common Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/cpl-v10.html
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   * @author Peter Kliem
21   * @version $id$
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       * (non-Javadoc)
65       * 
66       * @see java.util.List#size()
67       */
68      public int size() {
69          return _content.size();
70      }
71  
72      /*
73       * (non-Javadoc)
74       * 
75       * @see java.util.List#isEmpty()
76       */
77      public boolean isEmpty() {
78          return _content.isEmpty();
79      }
80  
81      /*
82       * (non-Javadoc)
83       * 
84       * @see java.util.List#contains(java.lang.Object)
85       */
86      public boolean contains(Object o) {
87          return _content.contains(o);
88      }
89  
90      /*
91       * (non-Javadoc)
92       * 
93       * @see java.util.List#iterator()
94       */
95      public Iterator iterator() {
96          return _content.iterator();
97      }
98  
99      /*
100      * (non-Javadoc)
101      * 
102      * @see java.util.List#toArray()
103      */
104     public Object[] toArray() {
105         return _content.toArray();
106     }
107 
108     /*
109      * (non-Javadoc)
110      * 
111      * @see java.util.List#toArray(java.lang.Object[])
112      */
113     public Object[] toArray(Object[] a) {
114         return _content.toArray(a);
115     }
116 
117     /*
118      * (non-Javadoc)
119      * 
120      * @see java.util.List#add(java.lang.Object)
121      */
122     public boolean add(Object o) {
123         boolean result = _content.add(o);
124         fireElementAdded(o);
125         return result;
126     }
127 
128     /*
129      * (non-Javadoc)
130      * 
131      * @see java.util.List#remove(java.lang.Object)
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      * (non-Javadoc)
144      * 
145      * @see java.util.List#containsAll(java.util.Collection)
146      */
147     public boolean containsAll(Collection c) {
148         return _content.containsAll(c);
149     }
150 
151     /*
152      * (non-Javadoc)
153      * 
154      * @see java.util.List#addAll(java.util.Collection)
155      */
156     public boolean addAll(Collection c) {
157         // TODO listener
158         return _content.addAll(c);
159     }
160 
161     /*
162      * (non-Javadoc)
163      * 
164      * @see java.util.List#addAll(int, java.util.Collection)
165      */
166     public boolean addAll(int index, Collection c) {
167         // TODO listener
168         return _content.addAll(index, c);
169     }
170 
171     /*
172      * (non-Javadoc)
173      * 
174      * @see java.util.List#removeAll(java.util.Collection)
175      */
176     public boolean removeAll(Collection c) {
177         // TODO listener
178         return _content.removeAll(c);
179     }
180 
181     /*
182      * (non-Javadoc)
183      * 
184      * @see java.util.List#retainAll(java.util.Collection)
185      */
186     public boolean retainAll(Collection c) {
187         // TODO listener
188         return _content.retainAll(c);
189     }
190 
191     /*
192      * (non-Javadoc)
193      * 
194      * @see java.util.List#clear()
195      */
196     public void clear() {
197         // TODO listener
198         _content.clear();
199 
200     }
201 
202     /*
203      * (non-Javadoc)
204      * 
205      * @see java.util.List#get(int)
206      */
207     public Object get(int index) {
208         return _content.get(index);
209     }
210 
211     /*
212      * (non-Javadoc)
213      * 
214      * @see java.util.List#set(int, java.lang.Object)
215      */
216     public Object set(int index, Object element) {
217         // TODO listener
218         return _content.set(index, element);
219     }
220 
221     /*
222      * (non-Javadoc)
223      * 
224      * @see java.util.List#add(int, java.lang.Object)
225      */
226     public void add(int index, Object element) {
227         _content.add(index, element);
228         fireElementAdded(element);
229     }
230 
231     /*
232      * (non-Javadoc)
233      * 
234      * @see java.util.List#remove(int)
235      */
236     public Object remove(int index) {
237         Object o = _content.remove(index);
238         fireElementRemoved(o);
239         return o;
240     }
241 
242     /*
243      * (non-Javadoc)
244      * 
245      * @see java.util.List#indexOf(java.lang.Object)
246      */
247     public int indexOf(Object o) {
248         return _content.indexOf(o);
249     }
250 
251     /*
252      * (non-Javadoc)
253      * 
254      * @see java.util.List#lastIndexOf(java.lang.Object)
255      */
256     public int lastIndexOf(Object o) {
257         return _content.lastIndexOf(o);
258     }
259 
260     /*
261      * (non-Javadoc)
262      * 
263      * @see java.util.List#listIterator()
264      */
265     public ListIterator listIterator() {
266         return _content.listIterator();
267     }
268 
269     /*
270      * (non-Javadoc)
271      * 
272      * @see java.util.List#listIterator(int)
273      */
274     public ListIterator listIterator(int index) {
275         return _content.listIterator(index);
276     }
277 
278     /*
279      * (non-Javadoc)
280      * 
281      * @see java.util.List#subList(int, int)
282      */
283     public List subList(int fromIndex, int toIndex) {
284         return _content.subList(fromIndex, toIndex);
285     }
286 
287 }