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( _cache.get(i)!=null && ((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( _cache.get(i)!=null && ((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       if( _cache.get(i) != null &&
57         ((Entry)_cache.get(i)).getKey(key) != null &&
58         ((Entry)_cache.get(i)).getKey(key).equals(key) ) {
59         System.out.println("test2: "+((Entry)_cache.get(i)).getKey(key));
60         return ((Entry)_cache.get(i)).getValue();
61       }
62     }
63     return null;
64   }
65   
66   public synchronized boolean remove(String key){
67     int i = containsKey(key);
68     if(i==-1){
69       return false;
70     }
71     _cache.remove(i);
72     _cache.trimToSize();
73     _counter --;
74     return true;
75   }
76   
77   public int size(){
78     return _counter;
79   }
80   
81   private class Entry {
82     private String _key;
83     private Object _value;
84     
85     public Entry(String i_key, Object i_value){
86       _key = i_key;
87       _value = i_value;
88     }
89         
90     public void put(String i_key, Object i_value){
91       _key = i_key;
92       _value = i_value;
93     }
94     
95     public Object getValue(String i_key){
96       if(i_key.equals(_key)){
97         return _value;
98       } else {
99         return null;
100       }
101     }
102     
103     public Object getValue(){
104       return _value;
105     }
106     
107     public String getKey(Object i_o){
108       if(i_o.equals(_value)){
109         return _key;
110       } else {
111         return null;
112       }
113     }
114
115     public String getKey(){
116         return _key;
117     }
118   }//Entry
119
120 }
121
122