debugging DatabaseCache
[mir.git] / source / mir / storage / DatabaseCache.java
1 package mir.storage;
2
3 import java.util.*;
4
5 public class DatabaseCache {
6   private final ArrayList _cache;
7   private int _counter;
8   private final int _max;
9   
10   public DatabaseCache(int i_max){
11     _counter = 0;
12     _max = i_max;
13     _cache = new ArrayList(_max);
14   }
15   
16   public DatabaseCache(){
17     _counter = -1;
18     _max = 100;
19     _cache = new ArrayList(_max);
20   }
21   
22   public synchronized void put(String key, Object value){
23     if(_counter >=_max){
24       _cache.remove(0);
25       _cache.trimToSize();
26       _counter --;
27       System.out.println("put: remove " + _counter);
28     }
29     _cache.add(new Entry(key,value));
30     _counter ++;
31     System.out.println("put: put " + _counter);
32   }
33   
34   public synchronized void clear(){
35     _cache.clear();
36   }
37       
38   public int containsKey(String key){
39     for(int i = 0; i<_cache.size(); i++){
40       if( ((Entry)_cache.get(i)).getKey().equals(key) )
41         return i;
42     }
43     return -1;
44   }
45   
46   public int containsValue(Object o){
47     for(int i = 0; i<_cache.size(); i++){
48       if(((Entry)_cache.get(i)).getValue().equals(o) )
49         return i;
50     }
51     return -1;
52   }
53   
54   public Object get(String key){
55     for(int i = 0; i<_cache.size(); i++){
56       System.out.println("test"+((Entry)_cache.get(i)).getKey(key));
57       if( ((Entry)_cache.get(i)).getKey(key).equals(key) )
58         System.out.println("test2: "+((Entry)_cache.get(i)).getKey(key));
59         return ((Entry)_cache.get(i)).getValue();
60     }
61     return null;
62   }
63   
64   public synchronized boolean remove(String key){
65     int i = containsKey(key);
66     if(i==-1){
67       return false;
68     }
69     _cache.remove(i);
70     _cache.trimToSize();
71     _counter --;
72     return true;
73   }
74   
75   public int size(){
76     return _counter;
77   }
78   
79   private class Entry {
80     private String _key;
81     private Object _value;
82     
83     public Entry(String i_key, Object i_value){
84       _key = i_key;
85       _value = i_value;
86     }
87         
88     public void put(String i_key, Object i_value){
89       _key = i_key;
90       _value = i_value;
91     }
92     
93     public Object getValue(String i_key){
94       if(i_key.equals(_key)){
95         return _value;
96       } else {
97         return null;
98       }
99     }
100     
101     public Object getValue(){
102       return _value;
103     }
104     
105     public String getKey(Object i_o){
106       if(i_o.equals(_value)){
107         return _key;
108       } else {
109         return null;
110       }
111     }
112
113     public String getKey(){
114         return _key;
115     }
116   }//Entry
117
118 }
119
120