KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > config > CfModList


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on 07.11.2004
33  */

34 package com.nightlabs.config;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.ListIterator JavaDoc;
41
42 import org.apache.log4j.Logger;
43
44 import com.nightlabs.util.RWLockable;
45
46 /**
47  * This implementation of List should be used within a config module instead of a usual
48  * ArrayList, because it notifies its owner ConfigModule, if it gets changed. Furthermore,
49  * it uses the RWLockable ability of its owner to ensure thread safety and implements
50  * RWLockable itself (delegating locking to the config module).
51  * <p>
52  * Warning: You still need to think about synchronization using RWLockable when using
53  * iterator() or similar methods that return an object which should only be used while having
54  * a read lock set! These methods don't acquire a read lock, because it would be in vain anyway.
55  * Read the documentation of the methods you use to avoid thread problems.
56  *
57  * @author Marco Schulze - marco at nightlabs dot de
58  * @author Marc Klinger - marc at nightlabs dot de (API documentation fixes)
59  */

60 public class CfModList extends ArrayList JavaDoc implements RWLockable
61 {
62     public static final Logger LOGGER = Logger.getLogger(CfModList.class);
63
64     protected ConfigModule ownerCfMod = null;
65
66     public CfModList() { }
67
68     public CfModList(int initialCapacity)
69     {
70         super(initialCapacity);
71     }
72
73     public CfModList(Collection JavaDoc c)
74     {
75         super(c);
76     }
77
78     public CfModList(ConfigModule ownerCfMod)
79     {
80         this.ownerCfMod = ownerCfMod;
81     }
82
83     /**
84      * @return Returns the ownerCfMod.
85      */

86     public ConfigModule getOwnerCfMod()
87     {
88         return ownerCfMod;
89     }
90     /**
91      * @param ownerCfMod The ownerCfMod to set.
92      */

93     public void setOwnerCfMod(ConfigModule ownerCfMod)
94     {
95         this.ownerCfMod = ownerCfMod;
96     }
97     
98     protected void setChanged()
99     {
100         if (ownerCfMod == null) {
101             LOGGER.warn("Owner ConfigModule is null! Cannot notify change!");
102             return;
103         }
104         ownerCfMod.setChanged();
105     }
106
107     public void acquireReadLock()
108     {
109         if (ownerCfMod == null) {
110             LOGGER.warn("Owner ConfigModule is null! Cannot lock! Operation is not thread-safe!!!");
111             return;
112         }
113         ownerCfMod.acquireReadLock();
114     }
115
116     public void acquireWriteLock()
117     {
118         if (ownerCfMod == null) {
119             LOGGER.warn("Owner ConfigModule is null! Cannot lock! Operation is not thread-safe!!!");
120             return;
121         }
122         ownerCfMod.acquireReadLock();
123     }
124
125     public void releaseLock()
126     {
127         if (ownerCfMod == null) {
128             LOGGER.warn("Owner ConfigModule is null! Cannot lock! Operation is not thread-safe!!!");
129             return;
130         }
131         ownerCfMod.releaseLock();
132     }
133     
134     /**
135      * @see java.util.List#add(int, java.lang.Object)
136      */

137     public void add(int index, Object JavaDoc element)
138     {
139         acquireWriteLock();
140         try {
141             super.add(index, element);
142             setChanged();
143         } finally {
144             releaseLock();
145         }
146     }
147     /**
148      * @see java.util.Collection#add(java.lang.Object)
149      */

150     public boolean add(Object JavaDoc o)
151     {
152         acquireWriteLock();
153         try {
154             boolean res = super.add(o);
155             setChanged();
156             return res;
157         } finally {
158             releaseLock();
159         }
160     }
161     /**
162      * @see java.util.Collection#addAll(java.util.Collection)
163      */

164     public boolean addAll(Collection JavaDoc c)
165     {
166         acquireWriteLock();
167         try {
168             boolean res = super.addAll(c);
169             setChanged();
170             return res;
171         } finally {
172             releaseLock();
173         }
174     }
175     /**
176      * @see java.util.List#addAll(int, java.util.Collection)
177      */

178     public boolean addAll(int index, Collection JavaDoc c)
179     {
180         acquireWriteLock();
181         try {
182             boolean res = super.addAll(index, c);
183             setChanged();
184             return res;
185         } finally {
186             releaseLock();
187         }
188     }
189     /**
190      * @see java.util.Collection#clear()
191      */

192     public void clear()
193     {
194         acquireWriteLock();
195         try {
196             super.clear();
197             setChanged();
198         } finally {
199             releaseLock();
200         }
201     }
202     /**
203      * @see java.util.List#remove(int)
204      */

205     public Object JavaDoc remove(int index)
206     {
207         acquireWriteLock();
208         try {
209             Object JavaDoc res = super.remove(index);
210             setChanged();
211             return res;
212         } finally {
213             releaseLock();
214         }
215     }
216     /**
217      * @see java.util.AbstractList#removeRange(int, int)
218      */

219     protected void removeRange(int fromIndex, int toIndex)
220     {
221         acquireWriteLock();
222         try {
223             super.removeRange(fromIndex, toIndex);
224             setChanged();
225         } finally {
226             releaseLock();
227         }
228     }
229     /**
230      * @see java.util.List#set(int, java.lang.Object)
231      */

232     public Object JavaDoc set(int index, Object JavaDoc element)
233     {
234         acquireWriteLock();
235         try {
236             Object JavaDoc res = super.set(index, element);
237             setChanged();
238             return res;
239         } finally {
240             releaseLock();
241         }
242     }
243     /**
244      * @see java.util.ArrayList#trimToSize()
245      */

246     public void trimToSize()
247     {
248         acquireWriteLock();
249         try {
250             super.trimToSize();
251             setChanged();
252         } finally {
253             releaseLock();
254         }
255     }
256     /**
257      * @see java.util.Collection#remove(java.lang.Object)
258      */

259     public boolean remove(Object JavaDoc o)
260     {
261         acquireWriteLock();
262         try {
263             boolean res = super.remove(o);
264             setChanged();
265             return res;
266         } finally {
267             releaseLock();
268         }
269     }
270     /**
271      * @see java.util.Collection#removeAll(java.util.Collection)
272      */

273     public boolean removeAll(Collection JavaDoc c)
274     {
275         acquireWriteLock();
276         try {
277             boolean res = super.removeAll(c);
278             setChanged();
279             return res;
280         } finally {
281             releaseLock();
282         }
283     }
284     /**
285      * @see java.util.Collection#retainAll(java.util.Collection)
286      */

287     public boolean retainAll(Collection JavaDoc c)
288     {
289         acquireWriteLock();
290         try {
291             boolean res = super.retainAll(c);
292             setChanged();
293             return res;
294         } finally {
295             releaseLock();
296         }
297     }
298     
299     
300     /**
301      * @see java.lang.Object#clone()
302      */

303     public Object JavaDoc clone()
304     {
305         acquireReadLock();
306         try {
307             return super.clone();
308         } finally {
309             releaseLock();
310         }
311     }
312     /**
313      * @see java.util.Collection#contains(java.lang.Object)
314      */

315     public boolean contains(Object JavaDoc elem)
316     {
317         acquireReadLock();
318         try {
319             return super.contains(elem);
320         } finally {
321             releaseLock();
322         }
323     }
324     /**
325      * @see java.util.ArrayList#ensureCapacity(int)
326      */

327     public void ensureCapacity(int minCapacity)
328     {
329         acquireReadLock();
330         try {
331             super.ensureCapacity(minCapacity);
332         } finally {
333             releaseLock();
334         }
335     }
336     /**
337      * @see java.util.List#get(int)
338      */

339     public Object JavaDoc get(int index)
340     {
341         acquireReadLock();
342         try {
343             return super.get(index);
344         } finally {
345             releaseLock();
346         }
347     }
348     /**
349      * @see java.util.List#indexOf(java.lang.Object)
350      */

351     public int indexOf(Object JavaDoc elem)
352     {
353         acquireReadLock();
354         try {
355             return super.indexOf(elem);
356         } finally {
357             releaseLock();
358         }
359     }
360     /**
361      * @see java.util.Collection#isEmpty()
362      */

363     public boolean isEmpty()
364     {
365         acquireReadLock();
366         try {
367             return super.isEmpty();
368         } finally {
369             releaseLock();
370         }
371     }
372     /**
373      * @see java.util.List#lastIndexOf(java.lang.Object)
374      */

375     public int lastIndexOf(Object JavaDoc elem)
376     {
377         acquireReadLock();
378         try {
379             return super.lastIndexOf(elem);
380         } finally {
381             releaseLock();
382         }
383     }
384     /**
385      * @see java.util.Collection#size()
386      */

387     public int size()
388     {
389         acquireReadLock();
390         try {
391             return super.size();
392         } finally {
393             releaseLock();
394         }
395     }
396     /**
397      * @see java.util.Collection#toArray()
398      */

399     public Object JavaDoc[] toArray()
400     {
401         acquireReadLock();
402         try {
403             return super.toArray();
404         } finally {
405             releaseLock();
406         }
407     }
408     /**
409      * @see java.util.Collection#toArray(java.lang.Object[])
410      */

411     public Object JavaDoc[] toArray(Object JavaDoc[] a)
412     {
413         acquireReadLock();
414         try {
415             return super.toArray(a);
416         } finally {
417             releaseLock();
418         }
419     }
420     /**
421      * @see java.lang.Object#equals(java.lang.Object)
422      */

423     public boolean equals(Object JavaDoc o)
424     {
425         acquireReadLock();
426         try {
427             return super.equals(o);
428         } finally {
429             releaseLock();
430         }
431     }
432     /**
433      * @see java.lang.Object#hashCode()
434      */

435     public int hashCode()
436     {
437         acquireReadLock();
438         try {
439             return super.hashCode();
440         } finally {
441             releaseLock();
442         }
443     }
444     /**
445      * This method does NOT acquire a read lock, because the returned Iterator
446      * must be thread protected the whole time it is used. Thus, it is your duty,
447      * to call acquireReadLock() and releaseLock()!
448      *
449      * @see java.util.Collection#iterator()
450      */

451     public Iterator JavaDoc iterator()
452     {
453         return super.iterator();
454     }
455     /**
456      * This method does NOT acquire a read lock, because YOU must protect
457      * the returned ListIterator the whole time, you use it. YOU MUST call
458      * acquireReadLock() before calling this method and releaseLock() after
459      * you finished using the returned ListIterator!
460      *
461      * @see java.util.List#listIterator()
462      */

463     public ListIterator JavaDoc listIterator()
464     {
465         return super.listIterator();
466     }
467     /**
468      * This method does NOT acquire a read lock! YOU must call acquireReadLock()
469      * before calling this method and releaseLock() after you finished using the returned
470      * ListIterator!
471      *
472      * @see java.util.List#listIterator(int)
473      */

474     public ListIterator JavaDoc listIterator(int index)
475     {
476         return super.listIterator(index);
477     }
478     /**
479      * @see java.util.List#subList(int, int)
480      */

481     public List JavaDoc subList(int fromIndex, int toIndex)
482     {
483         acquireReadLock();
484         try {
485             return super.subList(fromIndex, toIndex);
486         } finally {
487             releaseLock();
488         }
489     }
490     /**
491      * @see java.util.Collection#containsAll(java.util.Collection)
492      */

493     public boolean containsAll(Collection JavaDoc c)
494     {
495         acquireReadLock();
496         try {
497             return super.containsAll(c);
498         } finally {
499             releaseLock();
500         }
501     }
502     /**
503      * @see java.lang.Object#toString()
504      */

505     public String JavaDoc toString()
506     {
507         acquireReadLock();
508         try {
509             return super.toString();
510         } finally {
511             releaseLock();
512         }
513     }
514 }
515
Popular Tags