Guest User

Untitled

a guest
Oct 21st, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. package com.example;
  2.  
  3. import static java.util.Collections.*;
  4.  
  5. import java.util.Collection;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.Set;
  9. import java.util.concurrent.TimeUnit;
  10.  
  11. /**
  12. * A hash map that expires and removes items if they are older than a given
  13. * time-to-live.
  14. * <p>
  15. * The expiry is a passive process, items aren't removed until they are
  16. * retrieved and deemed to be expired by {@link #get(Object)}.
  17. */
  18. public class TtlHashMap<K, V> implements Map<K, V> {
  19.  
  20. private final HashMap<K, V> store = new HashMap<>();
  21. private final HashMap<K, Long> timestamps = new HashMap<>();
  22. private final long ttl;
  23.  
  24. public TtlHashMap(TimeUnit ttlUnit, long ttlValue) {
  25. this.ttl = ttlUnit.toNanos(ttlValue);
  26. }
  27.  
  28. @Override
  29. public V get(Object key) {
  30. V value = this.store.get(key);
  31.  
  32. if (value != null && expired(key, value)) {
  33. store.remove(key);
  34. timestamps.remove(key);
  35. return null;
  36. } else {
  37. return value;
  38. }
  39. }
  40.  
  41. private boolean expired(Object key, V value) {
  42. return (System.nanoTime() - timestamps.get(key)) > this.ttl;
  43. }
  44.  
  45. @Override
  46. public V put(K key, V value) {
  47. timestamps.put(key, System.nanoTime());
  48. return store.put(key, value);
  49. }
  50.  
  51. @Override
  52. public int size() {
  53. clearExpired();
  54. return store.size();
  55. }
  56.  
  57. @Override
  58. public boolean isEmpty() {
  59. return store.isEmpty();
  60. }
  61.  
  62. @Override
  63. public boolean containsKey(Object key) {
  64. return store.containsKey(key);
  65. }
  66.  
  67. @Override
  68. public boolean containsValue(Object value) {
  69. return store.containsValue(value);
  70. }
  71.  
  72. @Override
  73. public V remove(Object key) {
  74. timestamps.remove(key);
  75. return store.remove(key);
  76. }
  77.  
  78. @Override
  79. public void putAll(Map<? extends K, ? extends V> m) {
  80. for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
  81. this.put(e.getKey(), e.getValue());
  82. }
  83. }
  84.  
  85. @Override
  86. public void clear() {
  87. timestamps.clear();
  88. store.clear();
  89. }
  90.  
  91. @Override
  92. public Set<K> keySet() {
  93. clearExpired();
  94. return unmodifiableSet(store.keySet());
  95. }
  96.  
  97. @Override
  98. public Collection<V> values() {
  99. clearExpired();
  100. return unmodifiableCollection(store.values());
  101. }
  102.  
  103. @Override
  104. public Set<java.util.Map.Entry<K, V>> entrySet() {
  105. clearExpired();
  106. return unmodifiableSet(store.entrySet());
  107. }
  108.  
  109. private void clearExpired() {
  110. for (K k : store.keySet()) {
  111. this.get(k);
  112. }
  113. }
  114. }
Add Comment
Please, Sign In to add comment