KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > WeakList


1 /*
2  * Copyright (C) 2002-2003, Simon Nieuviarts
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */

19 package org.objectweb.carol.cmi;
20
21 import java.lang.ref.WeakReference JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.NoSuchElementException JavaDoc;
24
25 /**
26  * Register objects in a list via weak references. It is possible to get an iterator to know
27  * objects still alive.
28  * @author nieuviar
29  *
30  */

31 public class WeakList {
32     private final Object JavaDoc listHead = new Object JavaDoc();
33     private WeakLink listStart;
34
35     private static class WeakLink extends WeakReference JavaDoc {
36         public WeakLink next;
37         public WeakLink prev;
38         WeakLink(Object JavaDoc o) {
39             super(o);
40         }
41     }
42
43     public WeakList() {
44         listStart = new WeakLink(listHead);
45         listStart.next = listStart;
46         listStart.prev = listStart;
47     }
48
49     public void put(Object JavaDoc o) {
50         WeakLink n = new WeakLink(o);
51         synchronized (listHead) {
52             WeakLink prev = listStart;
53             WeakLink next = listStart.next;
54             n.prev = prev;
55             n.next = next;
56             prev.next = n;
57             next.prev = n;
58         }
59     }
60
61     private class ListIterator implements Iterator JavaDoc {
62         private WeakLink link = listStart;
63         private Object JavaDoc obj;
64         private boolean isNext = false;
65
66         public ListIterator() {
67             //pinNext();
68
}
69
70         private void pinNext() {
71             WeakLink l = link.next;
72             Object JavaDoc o = l.get();
73             if (o == null) {
74                 // Remove links with null objects
75
synchronized (listHead) {
76                     l = link;
77                     do {
78                         l = l.next;
79                         o = l.get();
80                     } while (o == null);
81                     link.next = l;
82                     l.prev = link;
83                 }
84             }
85             link = l;
86             obj = o;
87             isNext = true;
88         }
89
90         public boolean hasNext() {
91             if (!isNext) {
92                 pinNext();
93             }
94             return obj != listHead;
95         }
96
97         public Object JavaDoc next() {
98             if (!isNext) {
99                 pinNext();
100             }
101             if (obj == listHead) {
102                 throw new NoSuchElementException JavaDoc();
103             }
104             isNext = false;
105             return obj;
106         }
107
108         public void remove() {
109             throw new UnsupportedOperationException JavaDoc();
110         }
111     }
112
113     public Iterator JavaDoc iterator() {
114         return new ListIterator();
115     }
116 }
117
Popular Tags