KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > eval > cache > lib > TupleCache


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

23
24 package org.objectweb.medor.eval.cache.lib;
25
26 /**
27  * This class
28  */

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 JavaDoc;
34 import java.util.Iterator JavaDoc;
35
36 public class TupleCache implements CollectionCache {
37
38     public TupleCache(long size) {
39         capacity = size;
40         cache = new Hashtable JavaDoc();
41     }
42
43     private long capacity;
44     private int smallIndex = 1;
45     private boolean canInsert = true;
46     private Hashtable JavaDoc 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 JavaDoc(index))) {
59             t = (Tuple) cache.get(new Integer JavaDoc(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 JavaDoc(index))) {
68                 if (cache.size() >= capacity) {
69                     cache.remove(new Integer JavaDoc(smallIndex));
70                     smallIndex++;
71                 }
72                 cache.put(new Integer JavaDoc(index), t); // Make sure that t is cloned and not a reference
73
isPuted = true;
74             } //else
75
//System.out.println("Not puted in the cache because already existe: " + index);
76
} //else
77
//System.out.println("Not puted in the cache because cache full: " + index);
78
return isPuted;
79     }
80
81     public synchronized void initialize() {
82     }
83
84     public synchronized Iterator JavaDoc 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 JavaDoc(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