KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > collection > CachedCollection


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.collection;
23
24 import java.util.Collection JavaDoc;
25 import java.util.AbstractCollection JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import java.lang.ref.ReferenceQueue JavaDoc;
29
30 import org.jboss.util.SoftObject;
31 import org.jboss.util.Objects;
32
33 /**
34  * A wrapper around a <code>Collection</code> which translates added objects
35  * into {@link SoftObject} references, allowing the VM to garbage collect
36  * objects in the collection when memory is low.
37  *
38  * @version <tt>$Revision: 1958 $</tt>
39  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
40  */

41 public class CachedCollection
42    extends AbstractCollection JavaDoc
43 {
44    /** Reference queue */
45    protected final ReferenceQueue JavaDoc queue = new ReferenceQueue JavaDoc();
46
47    /** Wrapped collection */
48    protected final Collection JavaDoc collection;
49
50    /**
51     * Construct a CachedCollection.
52     *
53     * @param collection Collection to wrap.
54     */

55    public CachedCollection(final Collection JavaDoc collection) {
56       this.collection = collection;
57    }
58
59    /**
60     * Returns an iterator over the elements contained in this collection.
61     *
62     * @return An iterator over the elements contained in this collection.
63     */

64    public Iterator JavaDoc iterator() {
65       maintain();
66       return new MyIterator(collection.iterator());
67    }
68
69    /**
70     * Returns the size of the collection.
71     *
72     * @return The number of elements in the collection.
73     */

74    public int size() {
75       maintain();
76       return collection.size();
77    }
78
79    /**
80     * Add an object to the collection.
81     *
82     * @param obj Object (or <i>null</i> to add to the collection.
83     * @return True if object was added.
84     */

85    public boolean add(final Object JavaDoc obj) {
86       maintain();
87
88       SoftObject soft = SoftObject.create(obj, queue);
89       
90       return collection.add(soft);
91    }
92
93    /**
94     * Maintains the collection by removing garbage collected objects.
95     */

96    private void maintain() {
97       SoftObject obj;
98       int count = 0;
99
100       while ((obj = (SoftObject)queue.poll()) != null) {
101          count++;
102          collection.remove(obj);
103       }
104
105       if (count != 0) {
106          // some temporary debugging fluff
107
System.err.println("vm reclaimed " + count + " objects");
108       }
109    }
110
111
112    /////////////////////////////////////////////////////////////////////////
113
// De-Referencing Iterator //
114
/////////////////////////////////////////////////////////////////////////
115

116    /**
117     * A dereferencing iterator.
118     */

119    private final class MyIterator
120       implements Iterator JavaDoc
121    {
122       private final Iterator JavaDoc iter;
123
124       public MyIterator(final Iterator JavaDoc iter) {
125          this.iter = iter;
126       }
127
128       public boolean hasNext() {
129          maintain();
130          return iter.hasNext();
131       }
132
133       private Object JavaDoc nextObject() {
134          Object JavaDoc obj = iter.next();
135
136          return Objects.deref(obj);
137       }
138
139       public Object JavaDoc next() {
140          maintain();
141          return nextObject();
142       }
143
144       public void remove() {
145          maintain();
146          iter.remove();
147       }
148    }
149 }
150
Popular Tags