1 11 package org.eclipse.jdi.internal; 12 13 14 import java.lang.ref.Reference ; 15 import java.lang.ref.ReferenceQueue ; 16 import java.lang.ref.SoftReference ; 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.Hashtable ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 34 public class ValueCache { 35 39 private Map cacheTable = new Hashtable (); 40 44 private Map refTable = new Hashtable (); 45 46 50 private ReferenceQueue refQueue = new ReferenceQueue (); 51 52 55 private void cleanup() { 56 Reference ref; 57 while ((ref = refQueue.poll()) != null) { 58 Object key = refTable.get(ref); 59 if (key != null) 60 cacheTable.remove(key); 61 refTable.remove(ref); 62 } 63 } 64 65 68 public void put(Object key, Object value) { 69 cleanup(); 70 SoftReference ref = new SoftReference (value, refQueue); 71 cacheTable.put(key, ref); 72 refTable.put(ref, key); 73 } 74 75 82 public Object get(Object key) { 83 cleanup(); 84 Object value = null; 85 SoftReference ref = (SoftReference )cacheTable.get(key); 86 if (ref != null) { 87 value = ref.get(); 88 } 89 return value; 90 } 91 92 95 public Collection values() { 96 cleanup(); 97 List returnValues = new ArrayList (); 98 synchronized (cacheTable) { 99 Iterator iter = cacheTable.values().iterator(); 100 SoftReference ref; 101 Object value; 102 while (iter.hasNext()) { 103 ref = (SoftReference )iter.next(); 104 value = ref.get(); 105 if (value != null) { 106 returnValues.add(value); 107 } 108 } 109 } 110 return returnValues; 111 } 112 113 117 public Collection valuesWithType(Class type) { 118 cleanup(); 119 List returnValues = new ArrayList (); 120 synchronized (cacheTable) { 121 Iterator iter = cacheTable.values().iterator(); 122 SoftReference ref; 123 Object value; 124 while (iter.hasNext()) { 125 ref = (SoftReference )iter.next(); 126 value = ref.get(); 127 if (value != null && value.getClass().equals(type)) { 128 returnValues.add(value); 129 } 130 } 131 } 132 return returnValues; 133 } 134 135 140 public Object remove(Object key) { 141 cleanup(); 142 Object value = null; 143 SoftReference ref = (SoftReference )cacheTable.get(key); 144 if (ref != null) { 145 value = ref.get(); 146 refTable.remove(ref); 147 } 148 cacheTable.remove(key); 149 return value; 150 } 151 } 152 | Popular Tags |