1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 import java.util.Set ; 19 20 27 public class ReferenceCounter { 28 private Map mapIdToRec = new HashMap (11); 29 30 33 public class RefRec { 34 public RefRec(Object id, Object value) { 35 this.id = id; 36 this.value = value; 37 addRef(); 38 } 39 40 public Object getId() { 41 return id; 42 } 43 44 public Object 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 id; 67 68 public Object value; 69 70 private int refCount; 71 } 72 73 76 public ReferenceCounter() { 77 super(); 78 } 79 80 86 public int addRef(Object id) { 87 RefRec rec = (RefRec) mapIdToRec.get(id); 88 if (rec == null) { 89 return 0; 90 } 91 return rec.addRef(); 92 } 93 94 100 public Object get(Object id) { 101 RefRec rec = (RefRec) mapIdToRec.get(id); 102 if (rec == null) { 103 return null; 104 } 105 return rec.getValue(); 106 } 107 108 113 public Set keySet() { 114 return mapIdToRec.keySet(); 115 } 116 117 124 public void put(Object id, Object value) { 125 RefRec rec = new RefRec(id, value); 126 mapIdToRec.put(id, rec); 127 } 128 129 133 public int getRef(Object id) { 134 RefRec rec = (RefRec) mapIdToRec.get(id); 135 if (rec == null) { 136 return 0; 137 } 138 return rec.refCount; 139 } 140 141 149 public int removeRef(Object 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 166 public List values() { 167 int size = mapIdToRec.size(); 168 ArrayList list = new ArrayList (size); 169 Iterator 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 |