KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

22 public class ReferenceCounter
23 {
24     private Map mapIdToRec = new HashMap(11);
25
26     /**
27      * Capture the information about an object.
28      */

29     public class RefRec {
30         public RefRec(Object JavaDoc id, Object JavaDoc value) {
31             this.id = id;
32             this.value = value;
33             addRef();
34         }
35         public Object JavaDoc getId() {
36             return id;
37         }
38         public Object JavaDoc getValue() {
39             return value;
40         }
41         public int addRef() {
42             ++ refCount;
43             return refCount;
44         }
45         public int removeRef() {
46             -- refCount;
47             return refCount;
48         }
49         public int getRef() {
50             return refCount;
51         }
52         public boolean isNotReferenced() {
53             return (refCount <= 0);
54         }
55         public Object JavaDoc id;
56         public Object JavaDoc value;
57         private int refCount;
58     }
59 /**
60  * Creates a new counter.
61  */

62 public ReferenceCounter() {
63     super();
64 }
65 /**
66  * Adds one reference to an object in the counter.
67  *
68  * @param id is a unique ID for the object.
69  * @return the new ref count
70  */

71 public int addRef(Object JavaDoc id) {
72     RefRec rec = (RefRec)mapIdToRec.get(id);
73     if (rec == null)
74         return 0;
75     return rec.addRef();
76 }
77 /**
78  * Returns the object defined by an ID. If the ID is not
79  * found <code>null</code> is returned.
80  *
81  * @return the object or <code>null</code>
82  */

83 public Object JavaDoc get(Object JavaDoc id) {
84     RefRec rec = (RefRec)mapIdToRec.get(id);
85     if (rec == null)
86         return null;
87     return rec.getValue();
88 }
89 /**
90  * Returns a complete list of the keys in the counter.
91  *
92  * @return a Set containing the ID for each.
93  */

94 public Set keySet() {
95     return mapIdToRec.keySet();
96 }
97 /**
98  * Adds an object to the counter for counting and gives
99  * it an initial ref count of 1.
100  *
101  * @param id is a unique ID for the object.
102  * @param value is the object itself.
103  */

104 public void put(Object JavaDoc id, Object JavaDoc value) {
105     RefRec rec = new RefRec(id, value);
106     mapIdToRec.put(id, rec);
107 }
108 /**
109  * Removes one reference from an object in the counter.
110  * If the ref count drops to 0 the object is removed from
111  * the counter completely.
112  *
113  * @param id is a unique ID for the object.
114  * @return the new ref count
115  */

116 public int removeRef(Object JavaDoc id) {
117     RefRec rec = (RefRec)mapIdToRec.get(id);
118     if (rec == null)
119         return 0;
120     int newCount = rec.removeRef();
121     if (newCount <= 0)
122         mapIdToRec.remove(id);
123     return newCount;
124 }
125 /**
126  * Returns a complete list of the values in the counter.
127  *
128  * @return a Collection containing the values.
129  */

130 public List values() {
131     int size = mapIdToRec.size();
132     ArrayList list = new ArrayList(size);
133     Iterator iter = mapIdToRec.values().iterator();
134     while (iter.hasNext()) {
135         RefRec rec = (RefRec)iter.next();
136         list.add(rec.getValue());
137     }
138     return list;
139 }
140 }
141
Popular Tags