/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimeoutMap.java
ViewVC logotype

Diff of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimeoutMap.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimeoutCache.java revision 387 by torben, Fri Oct 2 15:06:08 2009 UTC android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimeoutMap.java revision 804 by torben, Mon Jun 7 12:13:10 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice.banedk;  package dk.thoerup.traininfoservice.banedk;
2    
3    import java.util.ArrayList;
4    import java.util.Collection;
5    import java.util.Map;
6    import java.util.Set;
7  import java.util.concurrent.ConcurrentHashMap;  import java.util.concurrent.ConcurrentHashMap;
8    
9    
10  public class TimeoutCache<K,V> {  
11    public class TimeoutMap<K,V> implements Map<K,V>{
12    
13          class CacheItem<T> {          class CacheItem<T> {
14                                    
# Line 19  public class TimeoutCache<K,V> { Line 24  public class TimeoutCache<K,V> {
24          private ConcurrentHashMap<K,CacheItem<V>> cache = new ConcurrentHashMap<K,CacheItem<V>>();          private ConcurrentHashMap<K,CacheItem<V>> cache = new ConcurrentHashMap<K,CacheItem<V>>();
25          private long timeout;          private long timeout;
26                    
27          public TimeoutCache(int timeout) {          public TimeoutMap(int timeout) {
28                  this.timeout = timeout;                  this.timeout = timeout;
29          }          }
30            
31          public void put(K k, V v) {  
32                  CacheItem<V> item= new CacheItem<V>(v);          @Override
33                  cache.put(k, item);          public void clear() {
34                    cache.clear();
35                    
36          }          }
37            
38          public V get(K k) {          @Override
39            public boolean containsKey(Object key) {
40                    return cache.containsKey(key);
41            }
42    
43            @Override
44            public boolean containsValue(Object arg0) {
45                    return values().contains(arg0);        
46            }
47    
48            @Override
49            public Set<java.util.Map.Entry<K, V>> entrySet() {
50                    //TODO someday implement this
51                    throw new UnsupportedOperationException();
52            }
53    
54            @Override
55            public boolean isEmpty() {
56                    return cache.isEmpty();
57            }
58    
59            @Override
60            public Set<K> keySet() {
61                    return cache.keySet();
62            }
63    
64            @Override
65            public void putAll(Map<? extends K, ? extends V> arg0) {
66                    for(K key : arg0.keySet()) {
67                            this.put(key, arg0.get(key) );
68                    }
69            }
70    
71            @Override
72            public V remove(Object arg0) {
73                    return cache.remove(arg0).value;
74            }
75    
76            @Override
77            public int size() {
78                    return cache.size();
79            }
80    
81            @Override
82            public Collection<V> values() {
83                    ArrayList<V> values = new ArrayList<V>();
84                    for (CacheItem<V> item : cache.values()) {
85                            values.add( item.value );
86                    }
87                    
88                    return values;
89    
90            }
91    
92            @Override
93            public V get(Object key) {
94                  long now = System.currentTimeMillis();                  long now = System.currentTimeMillis();
95                                    
96                  CacheItem<V> item = cache.get(k);                  CacheItem<V> item = cache.get(key);
97                                                                    
98                                    
99                  if (item != null) {                  if (item != null) {
# Line 43  public class TimeoutCache<K,V> { Line 105  public class TimeoutCache<K,V> {
105                  } else {                  } else {
106                          return null; // no item found                          return null; // no item found
107                  }                  }
108            }
109    
110            @Override
111            public V put(K key, V value) {          
112                    CacheItem<V> item= new CacheItem<V>(value);
113                    
114                    item = cache.put(key, item);
115                                    
116                    if (item != null)
117                            return item.value;
118                    else
119                            return null;
120          }          }
121                    
122  }  }

Legend:
Removed from v.387  
changed lines
  Added in v.804

  ViewVC Help
Powered by ViewVC 1.1.20