KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > collection > WeakReferenceCollection


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.collection;
25
26 import java.lang.ref.Reference JavaDoc;
27 import java.lang.ref.WeakReference JavaDoc;
28 import java.util.AbstractCollection JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 /**
37  * Collection whose values are stored as {@link WeakReference weak references}.
38  *
39  * @author Felix Gnass [fgnass at neteye dot de]
40  * @since 6.4
41  */

42 public class WeakReferenceCollection extends AbstractCollection JavaDoc {
43
44     private static final Log log = LogFactory
45             .getLog(WeakReferenceCollection.class);
46     
47     private Collection JavaDoc references = new ArrayList JavaDoc();
48     
49     /**
50      * Returns an iterator over all non-cleared values.
51      */

52     public Iterator JavaDoc iterator() {
53         return new ReferentIterator();
54     }
55
56     /**
57      * Returns the number of references. The number may include references that
58      * have already been cleared. To get an exact count call
59      * {@link #purge()} before.
60      */

61     public int size() {
62         return references.size();
63     }
64     
65     /**
66      * Adds a {@link WeakReference} referring to the given value to the internal
67      * collection of references.
68      */

69     public boolean add(Object JavaDoc value) {
70         references.add(new WeakReference JavaDoc(value));
71         return true;
72     }
73     
74     /**
75      * Clears the collection.
76      */

77     public void clear() {
78         references.clear();
79     }
80     
81     /**
82      * Removes all cleared references from the internal collection.
83      */

84     public void purge() {
85         Iterator JavaDoc it = iterator();
86         while (it.hasNext()) {
87             it.next();
88         }
89     }
90
91     private class ReferentIterator implements Iterator JavaDoc {
92
93         private Iterator JavaDoc it = references.iterator();
94         
95         private Object JavaDoc nextValue = null;
96         
97         public boolean hasNext() {
98             while (it.hasNext()) {
99                 Reference JavaDoc ref = (Reference JavaDoc) it.next();
100                 nextValue = ref.get();
101                 if (nextValue == null) {
102                     log.debug("Item cleared, removing reference.");
103                     it.remove();
104                 }
105                 else {
106                     return true;
107                 }
108             }
109             return false;
110         }
111
112         public Object JavaDoc next() {
113             return nextValue;
114         }
115
116         public void remove() {
117             it.remove();
118         }
119         
120     }
121 }
122
Popular Tags