KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ReferenceCounter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 /**
21  * A ReferenceCounter is used to reference counting objects.
22  * Each object is identified by a unique ID. Together they form
23  * an ID - value pair. An object is added to the counter by calling
24  * #put(id, object). From this point on additional refs can be made
25  * by calling #addRef(id) or #removeRef(id).
26  */

27 public class ReferenceCounter {
28     private Map JavaDoc mapIdToRec = new HashMap JavaDoc(11);
29
30     /**
31      * Capture the information about an object.
32      */

33     public class RefRec {
34         public RefRec(Object JavaDoc id, Object JavaDoc value) {
35             this.id = id;
36             this.value = value;
37             addRef();
38         }
39
40         public Object JavaDoc getId() {
41             return id;
42         }
43
44         public Object JavaDoc getValue() {
45             return value;
46         }
47
48         public int addRef() {
49             ++refCount;
50             return refCount;
51         }
52
53         public int removeRef() {
54             --refCount;
55             return refCount;
56         }
57
58         public int getRef() {
59             return refCount;
60         }
61
62         public boolean isNotReferenced() {
63             return (refCount <= 0);
64         }
65
66         public Object JavaDoc id;
67
68         public Object JavaDoc value;
69
70         private int refCount;
71     }
72
73     /**
74      * Creates a new counter.
75      */

76     public ReferenceCounter() {
77         super();
78     }
79
80     /**
81      * Adds one reference to an object in the counter.
82      *
83      * @param id is a unique ID for the object.
84      * @return the new ref count
85      */

86     public int addRef(Object JavaDoc id) {
87         RefRec rec = (RefRec) mapIdToRec.get(id);
88         if (rec == null) {
89             return 0;
90         }
91         return rec.addRef();
92     }
93
94     /**
95      * Returns the object defined by an ID. If the ID is not
96      * found <code>null</code> is returned.
97      *
98      * @return the object or <code>null</code>
99      */

100     public Object JavaDoc get(Object JavaDoc id) {
101         RefRec rec = (RefRec) mapIdToRec.get(id);
102         if (rec == null) {
103             return null;
104         }
105         return rec.getValue();
106     }
107
108     /**
109      * Returns a complete list of the keys in the counter.
110      *
111      * @return a Set containing the ID for each.
112      */

113     public Set JavaDoc keySet() {
114         return mapIdToRec.keySet();
115     }
116
117     /**
118      * Adds an object to the counter for counting and gives
119      * it an initial ref count of 1.
120      *
121      * @param id is a unique ID for the object.
122      * @param value is the object itself.
123      */

124     public void put(Object JavaDoc id, Object JavaDoc value) {
125         RefRec rec = new RefRec(id, value);
126         mapIdToRec.put(id, rec);
127     }
128
129     /**
130      * @param id is a unique ID for the object.
131      * @return the current ref count
132      */

133     public int getRef(Object JavaDoc id) {
134         RefRec rec = (RefRec) mapIdToRec.get(id);
135         if (rec == null) {
136             return 0;
137         }
138         return rec.refCount;
139     }
140
141     /**
142      * Removes one reference from an object in the counter.
143      * If the ref count drops to 0 the object is removed from
144      * the counter completely.
145      *
146      * @param id is a unique ID for the object.
147      * @return the new ref count
148      */

149     public int removeRef(Object JavaDoc id) {
150         RefRec rec = (RefRec) mapIdToRec.get(id);
151         if (rec == null) {
152             return 0;
153         }
154         int newCount = rec.removeRef();
155         if (newCount <= 0) {
156             mapIdToRec.remove(id);
157         }
158         return newCount;
159     }
160     
161     /**
162      * Returns a complete list of the values in the counter.
163      *
164      * @return a Collection containing the values.
165      */

166     public List JavaDoc values() {
167         int size = mapIdToRec.size();
168         ArrayList JavaDoc list = new ArrayList JavaDoc(size);
169         Iterator JavaDoc iter = mapIdToRec.values().iterator();
170         while (iter.hasNext()) {
171             RefRec rec = (RefRec) iter.next();
172             list.add(rec.getValue());
173         }
174         return list;
175     }
176 }
177
Popular Tags