1 11 package org.eclipse.team.internal.ui.synchronize; 12 13 import java.util.*; 14 15 22 public class ReferenceCounter 23 { 24 private Map mapIdToRec = new HashMap(11); 25 26 29 public class RefRec { 30 public RefRec(Object id, Object value) { 31 this.id = id; 32 this.value = value; 33 addRef(); 34 } 35 public Object getId() { 36 return id; 37 } 38 public Object 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 id; 56 public Object value; 57 private int refCount; 58 } 59 62 public ReferenceCounter() { 63 super(); 64 } 65 71 public int addRef(Object id) { 72 RefRec rec = (RefRec)mapIdToRec.get(id); 73 if (rec == null) 74 return 0; 75 return rec.addRef(); 76 } 77 83 public Object get(Object id) { 84 RefRec rec = (RefRec)mapIdToRec.get(id); 85 if (rec == null) 86 return null; 87 return rec.getValue(); 88 } 89 94 public Set keySet() { 95 return mapIdToRec.keySet(); 96 } 97 104 public void put(Object id, Object value) { 105 RefRec rec = new RefRec(id, value); 106 mapIdToRec.put(id, rec); 107 } 108 116 public int removeRef(Object 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 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 |