1 19 package org.netbeans.modules.javacore; 20 21 25 public class Cache { 26 private final Object [] instanceCache; 27 28 29 public Cache(int size) { 30 instanceCache = new Object [size]; 31 } 32 33 public void put(Object instance) { 34 Object ce = instance; 35 for (int i = 0, j = 0; i < instanceCache.length; i++) { 36 Object temp = null; 37 while (j < instanceCache.length) { 38 Object tmp2 = instanceCache[j]; 39 j++; 40 if (tmp2 != null && tmp2 != instance) { 41 if (!(tmp2 instanceof CachedElement) || ((CachedElement) tmp2).isValid()) { 42 temp = tmp2; 43 break; 44 } else { 45 ((CachedElement) tmp2).release(); 46 } 47 } 48 } 49 instanceCache[i] = ce; 50 ce = temp; 51 } 52 if (ce != null && (ce instanceof CachedElement)) { 53 ((CachedElement) ce).release(); 54 } 55 } 56 57 public void dump() { 58 System.err.println("******** Cache dump:"); 59 for (int i = 0; i < instanceCache.length; i++) { 60 System.err.println(instanceCache[i].toString()); 61 } 62 System.err.println("*********"); 63 } 64 65 public interface CachedElement { 66 void release(); 67 boolean isValid(); 68 } 69 } 70 | Popular Tags |