1 24 package org.objectweb.jalisto.se.impl.cache; 25 26 import org.objectweb.jalisto.se.api.cache.JalistoCache; 27 import org.objectweb.jalisto.se.impl.trace.Trace; 28 29 import java.io.PrintStream ; 30 import java.util.*; 31 32 public class GenericCache implements JalistoCache { 33 34 public GenericCache() { 35 table = new HashMap(); 36 } 37 38 public void init(int size, String name, double clearPourcent) { 39 this.maxSize = size; 40 this.name = name; 41 this.cacheClearPourcent = clearPourcent; 42 } 43 44 public void setTrace(Trace trace) { 45 this.trace = trace; 46 } 47 48 public boolean isEmpty() { 49 return table.isEmpty(); 50 } 51 52 public void clear() { 53 trace.println(Trace.CACHE, "{0} : clear()", name); 54 table.clear(); 55 firstCell = null; 56 lastCell = null; 57 } 58 59 public boolean containsKey(Object key) { 60 return table.containsKey(key); 61 } 62 63 public boolean containsValue(Object value) { 64 throw new UnsupportedOperationException ("not used in this implementation"); 65 } 66 67 public Set entrySet() { 68 throw new UnsupportedOperationException ("not used in this implementation"); 69 } 70 71 public Object get(Object key) { 72 trace.println(Trace.CACHE, "{0} : get({1})", name, key); 73 Object result = table.get(key); 74 if (result != null) { 75 return ((CacheCell) result).getObject(); 76 } 77 return null; 78 } 79 80 public Set keySet() { 81 return table.keySet(); 82 } 83 84 public Object put(Object key, Object value) { 85 trace.println(Trace.CACHE, "{0} : put({1}, {2})", name, key, value); 86 if (containsKey(key)) { 87 ((CacheCell) table.get(key)).setObject(value); 88 } else { 89 trace.println(Trace.CACHE, "{0} : put -> add"); 90 if (table.size() >= maxSize) { 91 makeSpace(); 92 } 93 CacheCell availableCacheCell = CacheCell.newInstance(key, value); 94 if (lastCell != null) { 95 lastCell.setNextCell(availableCacheCell); 96 availableCacheCell.setPrecedingCell(lastCell); 97 lastCell = availableCacheCell; 98 } else { 99 lastCell = availableCacheCell; 100 firstCell = availableCacheCell; 101 } 102 table.put(key, availableCacheCell); 103 } 104 return null; 105 } 106 107 public void putAll(Map map) { 108 Iterator keys = map.keySet().iterator(); 109 while (keys.hasNext()) { 110 Object key = keys.next(); 111 put(key, map.get(key)); 112 } 113 } 114 115 public Object remove(Object key) { 116 trace.println(Trace.CACHE, "{0} : remove({1})", name, key); 117 if (containsKey(key)) { 118 synchronized (table) { 119 CacheCell cell = (CacheCell) table.remove(key); 120 if (cell.equals(firstCell)) { 121 firstCell = cell.getNextCell(); 122 } 123 if (cell.equals(lastCell)) { 124 lastCell = cell.getPrecedingCell(); 125 } 126 Object result = cell.getObject(); 127 cell.delink(); 128 if (table.isEmpty()) { 129 lastCell = null; 130 firstCell = null; 131 } 132 return result; 133 } 134 } 135 return null; 136 } 137 138 public int size() { 139 return table.size(); 140 } 141 142 public synchronized void setMaxSize(int size) { 143 maxSize = size; 144 CacheCell myFirst = firstCell; 145 synchronized (table) { 146 while (table.size() > maxSize) { 147 CacheCell mySecond = myFirst.getNextCell(); 148 table.remove(myFirst.getId()); 149 myFirst.delink(); 150 myFirst = mySecond; 151 } 152 if (table.isEmpty()) { 153 lastCell = null; 154 firstCell = null; 155 } 156 firstCell = myFirst; 157 } 158 } 159 160 public int getMaxSize() { 161 return maxSize; 162 } 163 164 public Collection values() { 165 throw new UnsupportedOperationException ("not used in this implementation"); 166 } 167 168 169 private void makeSpace() { 170 int nbrToRemove = (new Double (maxSize * cacheClearPourcent)).intValue(); 171 trace.println(Trace.CACHE, "{0} : makeSpace() : clear {1} objects", name, new Integer (nbrToRemove)); 172 173 CacheCell myFirst = firstCell; 174 synchronized (table) { 175 for (int i = 0; i < nbrToRemove; i++) { 176 CacheCell mySecond = myFirst.getNextCell(); 177 if (mySecond == null) { 178 throw new NullPointerException (); 179 } 180 table.remove(myFirst.getId()); 181 myFirst.delink(); 182 myFirst = mySecond; 183 } 184 } 185 firstCell = myFirst; 186 } 187 188 public void print(PrintStream out) { 189 out.println(table.toString()); 190 } 191 192 public String toString() { 193 return table.toString(); 194 } 195 196 197 private CacheCell firstCell; 198 private CacheCell lastCell; 199 private HashMap table; 200 private String name; 201 private double cacheClearPourcent; 202 private int maxSize; 203 private Trace trace; 204 } 205 | Popular Tags |