1 23 24 package org.objectweb.medor.eval.cache.lib; 25 26 29 import org.objectweb.medor.eval.cache.api.CollectionCache; 30 import org.objectweb.medor.tuple.api.Tuple; 31 import org.objectweb.medor.api.MedorException; 32 33 import java.util.Hashtable ; 34 import java.util.Iterator ; 35 36 public class TupleCache implements CollectionCache { 37 38 public TupleCache(long size) { 39 capacity = size; 40 cache = new Hashtable (); 41 } 42 43 private long capacity; 44 private int smallIndex = 1; 45 private boolean canInsert = true; 46 private Hashtable cache; 47 48 public synchronized long getCapacity() { 49 return capacity; 50 } 51 52 public synchronized void setCapacity(int capacity) { 53 this.capacity = capacity; 54 } 55 56 public synchronized Tuple getTuple(int index) throws MedorException { 57 Tuple t = null; 58 if (cache.containsKey(new Integer (index))) { 59 t = (Tuple) cache.get(new Integer (index)); 60 } 61 return t; 62 } 63 64 public synchronized boolean putTuple(int index, Tuple t) throws MedorException { 65 boolean isPuted = false; 66 if (canInsert) { 67 if (!cache.containsKey(new Integer (index))) { 68 if (cache.size() >= capacity) { 69 cache.remove(new Integer (smallIndex)); 70 smallIndex++; 71 } 72 cache.put(new Integer (index), t); isPuted = true; 74 } } return isPuted; 79 } 80 81 public synchronized void initialize() { 82 } 83 84 public synchronized Iterator tupleIndexIterator() { 85 return cache.keySet().iterator(); 86 } 87 88 public synchronized void destroy() { 89 cache.clear(); 90 } 91 92 public synchronized boolean contains(int index) { 93 return cache.containsKey(new Integer (index)); 94 } 95 96 public boolean isCanInsert() { 97 return canInsert; 98 } 99 100 public void setCanInsert(boolean canInsert) { 101 this.canInsert = canInsert; 102 } 103 } 104 | Popular Tags |