KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.AbstractList 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>List</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 CachedList
42    extends AbstractList JavaDoc
43 {
44    /** Reference queue. */
45    protected final ReferenceQueue JavaDoc queue = new ReferenceQueue JavaDoc();
46
47    /** Wrapped list. */
48    protected final List JavaDoc list;
49
50    /**
51     * Construct a <tt>CachedList</tt>.
52     *
53     * @param list List to wrap.
54     */

55    public CachedList(final List JavaDoc list) {
56       this.list = list;
57    }
58
59    /**
60     * Construct a <tt>CachedList</tt> using a <tt>LinkedList</tt> for
61     * storage.
62     */

63    public CachedList() {
64       this(new LinkedList JavaDoc());
65    }
66
67    /**
68     * Dereference the object at the given index.
69     */

70    private Object JavaDoc getObject(final int index) {
71       Object JavaDoc obj = list.get(index);
72
73       return Objects.deref(obj);
74    }
75
76    /**
77     * Returns the element at the specified position in this list.
78     *
79     * @param index Index of element to return.
80     * @return The element at the specified position.
81     */

82    public Object JavaDoc get(final int index) {
83       maintain();
84       return getObject(index);
85    }
86
87    /**
88     * Return the size of the list.
89     *
90     * @return The number of elements in the list.
91     */

92    public int size() {
93       maintain();
94       return list.size();
95    }
96
97    /**
98     * Replaces the element at the specified position in this list with the
99     * specified element.
100     *
101     * @param index Index of element to replace.
102     * @param obj Element to be stored at the specified postion.
103     * @return The previous element at the given index.
104     */

105    public Object JavaDoc set(final int index, final Object JavaDoc obj) {
106       maintain();
107       
108       SoftObject soft = SoftObject.create(obj, queue);
109       soft = (SoftObject)list.set(index, soft);
110
111       return Objects.deref(soft);
112    }
113
114    /**
115     * Inserts the specified element at the specified position in this list
116     * (optional operation). Shifts the element currently at that position
117     * (if any) and any subsequent elements to the right (adds one to their
118     * indices).
119     *
120     * @param index Index at which the specified element is to be inserted.
121     * @param obj Element to be inserted.
122     */

123    public void add(final int index, final Object JavaDoc obj) {
124       maintain();
125
126       SoftObject soft = SoftObject.create(obj, queue);
127       list.add(index, soft);
128    }
129
130    /**
131     * Removes the element at the specified position in this list (optional
132     * operation). Shifts any subsequent elements to the left (subtracts one
133     * from their indices). Returns the element that was removed from the list.
134     *
135     * @param index The index of the element to remove.
136     * @return The element previously at the specified position.
137     */

138    public Object JavaDoc remove(final int index) {
139       maintain();
140
141       Object JavaDoc obj = list.remove(index);
142       return Objects.deref(obj);
143    }
144
145    /**
146     * Maintains the collection by removing garbage collected objects.
147     */

148    private void maintain() {
149       SoftObject obj;
150       int count = 0;
151
152       while ((obj = (SoftObject)queue.poll()) != null) {
153          count++;
154          list.remove(obj);
155       }
156
157       if (count != 0) {
158          // some temporary debugging fluff
159
System.err.println("vm reclaimed " + count + " objects");
160       }
161    }
162 }
163
Popular Tags