1 32 package com.imagero.uio.buffer; 33 34 import java.util.Enumeration ; 35 import java.util.Hashtable ; 36 import java.util.Random ; 37 import java.util.Vector ; 38 39 49 public abstract class MemoryAccessManager { 50 51 public static final int DROP_NEVER = 0; 52 public static final int DROP_IMMEDIATELY = -1; 53 public static final int DROP_RANDOM = 1; 54 public static final int DROP_LRU = 2; 55 public static final int DROP_FIFO = 3; 56 57 private static int defaultStrategy = DROP_FIFO; 58 59 public static int getDefaultStrategy() { 60 return defaultStrategy; 61 } 62 63 public static void setDefaultStrategy(int defaultStrategy) { 64 MemoryAccessManager.defaultStrategy = defaultStrategy; 65 } 66 67 public static MemoryAccessManager createMemoryAccessManager() { 68 return createMemoryAccessManager(getDefaultStrategy()); 69 } 70 71 public static MemoryAccessManager createMemoryAccessManager(int strategy) { 72 switch (strategy) { 73 case DROP_NEVER: 74 return new DNMemoryAccessManager(); 75 case DROP_IMMEDIATELY: 76 return new DIMemoryAccessManager(); 77 case DROP_RANDOM: 78 return new DRMemoryAccessManager(); 79 case DROP_LRU: 80 return new LRUMemoryAccessManager(); 81 case DROP_FIFO: 82 return new FIFOMemoryAccessManager(); 83 default: 84 throw new IllegalArgumentException ("unknown strategy:" + strategy); 85 } 86 } 87 88 int maxBufferCount = 10; 89 Hashtable ht = new Hashtable (); 90 91 public abstract Buffer get(Object key); 92 93 public abstract void put(Object key, Buffer b); 94 95 public void add(Buffer b) { 96 put(new Integer (ht.size()), b); 97 } 98 99 public Buffer get(int i) { 100 return get(new Integer (i)); 101 } 102 103 public int getCount() { 104 return ht.size(); 105 } 106 107 public int getBufferLength(int i) { 108 return getBufferLength(new Integer (i)); 109 } 110 111 public int getBufferLength(Object key) { 112 Buffer b = get(key); 113 if (b != null) { 114 return b.length(); 115 } 116 return 0; 117 } 118 119 public Buffer drop(Object key) { 120 return (Buffer) ht.remove(key); 121 } 122 123 public int getMaxBufferCount() { 124 return maxBufferCount; 125 } 126 127 public void setMaxBufferCount(int maxBufferCount) { 128 this.maxBufferCount = maxBufferCount; 129 } 130 131 public void clear() { 132 ht.clear(); 133 } 134 135 public boolean isDirty(int index) { 136 Buffer b = (Buffer) ht.get(new Integer (index)); 137 if (b != null) { 138 return b.isDirty(); 139 } 140 return false; 141 } 142 143 public boolean isDirty(Object key) { 144 Buffer b = (Buffer) ht.get(key); 145 if (b != null) { 146 return b.isDirty(); 147 } 148 return false; 149 } 150 151 public Enumeration keys() { 152 return ht.keys(); 153 } 154 155 static class DNMemoryAccessManager extends MemoryAccessManager { 156 157 public Buffer get(Object key) { 158 return (Buffer) ht.get(key); 159 } 160 161 public void put(Object key, Buffer b) { 162 ht.put(key, b); 163 } 164 } 165 166 static class DIMemoryAccessManager extends MemoryAccessManager { 167 public Buffer get(Object key) { 168 return (Buffer) ht.get(key); 169 } 170 171 public void put(Object key, Buffer b) { 172 ht.clear(); 173 ht.put(key, b); 174 } 175 } 176 177 static class DRMemoryAccessManager extends MemoryAccessManager { 178 Random random = new Random (System.currentTimeMillis()); 179 180 public Buffer get(Object key) { 181 return (Buffer) ht.get(key); 182 } 183 184 public void put(Object key, Buffer b) { 185 int size = ht.size(); 186 if (size >= maxBufferCount) { 187 int r = Math.abs(random.nextInt()) % size; 188 Enumeration keys = ht.keys(); 189 for (int i = 0; i < r; i++) { 190 if (keys.hasMoreElements()) { 191 keys.nextElement(); 192 } 193 } 194 boolean dropped = false; 195 while (keys.hasMoreElements()) { 197 Object k = keys.nextElement(); 198 Buffer b0 = get(k); 199 if (!b0.isDirty()) { 200 drop(k); 201 dropped = true; 202 break; 203 } 204 } 205 if (!dropped) { 206 keys = ht.keys(); 207 while (keys.hasMoreElements()) { 209 Object k = keys.nextElement(); 210 Buffer b0 = get(k); 211 if (!b0.isDirty()) { 212 drop(k); 213 break; 214 } 215 } 216 } 217 } 218 ht.put(key, b); 219 } 220 221 protected void drop(int index) { 222 ht.remove(new Integer (index)); 223 } 224 } 225 226 static class FIFOMemoryAccessManager extends MemoryAccessManager { 227 Vector fifo = new Vector (); 228 229 public Buffer get(Object key) { 230 return (Buffer) ht.get(key); 231 } 232 233 public void put(Object key, Buffer b) { 234 int size = ht.size(); 235 if (size >= maxBufferCount) { 236 for (int i = 0; i < fifo.size(); i++) { 237 Object k = fifo.elementAt(i); 238 Buffer b0 = get(k); 239 if (!b0.isDirty()) { 240 drop(k); 241 break; 242 } 243 } 244 } 245 fifo.addElement(key); 246 ht.put(key, b); 247 } 248 249 protected void drop(int index) { 250 Object k = fifo.elementAt(index); 251 fifo.removeElementAt(index); 252 ht.remove(k); 253 } 254 255 public Buffer drop(Object key) { 256 fifo.removeElement(key); 257 return super.drop(key); 258 } 259 260 public void clear() { 261 ht.clear(); 262 fifo.removeAllElements(); 263 } 264 } 265 266 static class LRUMemoryAccessManager extends MemoryAccessManager { 267 Vector lru = new Vector (); 268 269 public Buffer get(Object key) { 270 lru.removeElement(key); 271 lru.addElement(key); 272 return (Buffer) ht.get(key); 273 } 274 275 public void put(Object key, Buffer b) { 276 lru.removeElement(key); 277 lru.addElement(key); 278 if (lru.size() >= maxBufferCount) { 279 for (int i = 0; i < lru.size(); i++) { 280 Object k = lru.elementAt(i); 281 Buffer b0 = get(k); 282 if (!b0.isDirty()) { 283 drop(k); 284 break; 285 } 286 } 287 } 288 ht.put(key, b); 289 } 290 291 protected void drop(int index) { 292 Object k = lru.elementAt(index); 293 lru.removeElementAt(index); 294 ht.remove(k); 295 } 296 297 public Buffer drop(Object key) { 298 lru.removeElement(key); 299 return super.drop(key); 300 } 301 302 public void clear() { 303 ht.clear(); 304 lru.removeAllElements(); 305 } 306 } 307 } 308 | Popular Tags |