/[projects]/android/TrainInfo/src/dk/thoerup/traininfo/util/AndroidTimeoutCache.java
ViewVC logotype

Diff of /android/TrainInfo/src/dk/thoerup/traininfo/util/AndroidTimeoutCache.java

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

revision 1027 by torben, Wed Sep 8 06:03:45 2010 UTC revision 1033 by torben, Wed Sep 8 10:23:44 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfo.util;  package dk.thoerup.traininfo.util;
2    
3  import java.util.HashMap;  import java.util.HashMap;
4    import java.util.Iterator;
5    import java.util.Set;
6    
7    import android.util.Log;
8    
9    
10  public class AndroidTimeoutCache<K,V> {  public class AndroidTimeoutCache<K,V> {
# Line 12  public class AndroidTimeoutCache<K,V> { Line 16  public class AndroidTimeoutCache<K,V> {
16                          lastupdate = android.os.SystemClock.elapsedRealtime();                          lastupdate = android.os.SystemClock.elapsedRealtime();
17                  }                  }
18                                    
19                    public boolean isExpired(long now) {
20                            return ( (lastupdate+timeout) < now);
21                    }
22                    
23                  public long lastupdate;                  public long lastupdate;
24                  public T value;                  public T value;
25          }          }
# Line 26  public class AndroidTimeoutCache<K,V> { Line 34  public class AndroidTimeoutCache<K,V> {
34          public void purgeOldEntries() {          public void purgeOldEntries() {
35                                    
36                  long now = android.os.SystemClock.elapsedRealtime();                  long now = android.os.SystemClock.elapsedRealtime();
37                    //Log.e("Purge","Purge");
38                                    
39                  for (K key : cache.keySet()) {                  Set<K> keyset = cache.keySet();
40                    
41                    for ( Iterator<K> it = keyset.iterator(); it.hasNext() ;) {
42                            K key = it.next();
43                            
44                          CacheItem<V> item = cache.get(key);                          CacheItem<V> item = cache.get(key);
45                          if ( (item.lastupdate+timeout) < now) { //item too old                          if ( item.isExpired(now)) { //item too old
46                                  cache.remove(key);                                  it.remove();
47                          }                                  //Log.e("Purge", "removing");
48                  }                          }                      
49                    }      
50                    
51          }          }
52                    
53          public void put(K k, V v) {          public void put(K k, V v) {
# Line 47  public class AndroidTimeoutCache<K,V> { Line 62  public class AndroidTimeoutCache<K,V> {
62                                                                    
63                                    
64                  if (item != null) {                  if (item != null) {
65                          if ( (item.lastupdate+timeout) < now) { //item too old                          if ( item.isExpired(now) ) { //item too old
66                                  return null;                                  return null;
67                          } else {                          } else {
68                                  return item.value; //item still good                                  return item.value; //item still good

Legend:
Removed from v.1027  
changed lines
  Added in v.1033

  ViewVC Help
Powered by ViewVC 1.1.20