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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 432 - (hide annotations) (download)
Sat Oct 10 09:56:10 2009 UTC (14 years, 7 months ago) by torben
File size: 2164 byte(s)
Fixed a bug, which caused a null pointer exception
1 torben 387 package dk.thoerup.traininfoservice.banedk;
2    
3 torben 430 import java.util.ArrayList;
4 torben 428 import java.util.Collection;
5     import java.util.Map;
6     import java.util.Set;
7 torben 430 import java.util.concurrent.ConcurrentHashMap;
8 torben 387
9    
10 torben 429
11 torben 428 public class TimeoutMap<K,V> implements Map<K,V>{
12 torben 387
13     class CacheItem<T> {
14    
15     public CacheItem(T v) {
16     value = v;
17     lastupdate = System.currentTimeMillis();
18     }
19    
20     public long lastupdate;
21     public T value;
22     }
23    
24     private ConcurrentHashMap<K,CacheItem<V>> cache = new ConcurrentHashMap<K,CacheItem<V>>();
25     private long timeout;
26    
27 torben 428 public TimeoutMap(int timeout) {
28 torben 387 this.timeout = timeout;
29     }
30 torben 428
31    
32     @Override
33     public void clear() {
34     cache.clear();
35    
36 torben 387 }
37 torben 428
38     @Override
39     public boolean containsKey(Object arg0) {
40     CacheItem<V> item = cache.get(arg0);
41    
42     return (item != null);
43     }
44    
45     @Override
46     public boolean containsValue(Object arg0) {
47 torben 430 return values().contains(arg0);
48 torben 428 }
49    
50     @Override
51     public Set<java.util.Map.Entry<K, V>> entrySet() {
52     //TODO someday implement this
53     throw new UnsupportedOperationException();
54     }
55    
56     @Override
57     public boolean isEmpty() {
58     return cache.isEmpty();
59     }
60    
61     @Override
62     public Set<K> keySet() {
63     return cache.keySet();
64     }
65    
66     @Override
67     public void putAll(Map<? extends K, ? extends V> arg0) {
68 torben 430 for(K key : arg0.keySet()) {
69     this.put(key, arg0.get(key) );
70     }
71 torben 428 }
72    
73     @Override
74     public V remove(Object arg0) {
75     return cache.remove(arg0).value;
76     }
77    
78     @Override
79     public int size() {
80     return cache.size();
81     }
82    
83     @Override
84     public Collection<V> values() {
85 torben 430 ArrayList<V> values = new ArrayList<V>();
86     for (CacheItem<V> item : cache.values()) {
87     values.add( item.value );
88     }
89    
90     return values;
91    
92 torben 428 }
93    
94     @Override
95     public V get(Object key) {
96 torben 387 long now = System.currentTimeMillis();
97    
98 torben 428 CacheItem<V> item = cache.get(key);
99 torben 387
100    
101     if (item != null) {
102     if ( (item.lastupdate+timeout) < now) { //item too old
103     return null;
104     } else {
105     return item.value; //item still good
106     }
107     } else {
108     return null; // no item found
109     }
110 torben 428 }
111    
112     @Override
113     public V put(K key, V value) {
114     CacheItem<V> item= new CacheItem<V>(value);
115 torben 387
116 torben 432 item = cache.put(key, item);
117    
118     if (item != null)
119     return item.value;
120     else
121     return null;
122 torben 387 }
123    
124     }

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20