package io.branch.rnbranch; import java.util.HashMap; import java.util.Iterator; /** * Created by jdee on 3/8/17. */ public class AgingHash { private long mTtlMillis; private HashMap> mHash = new HashMap<>(); public AgingHash(long ttlMillis) { mTtlMillis = ttlMillis; } public long getTtlMillis() { return mTtlMillis; } public void put(KeyType key, ValueType value) { ageItems(); AgingItem item = new AgingItem<>(value); mHash.put(key, item); } public ValueType get(KeyType key) { AgingItem item = mHash.get(key); if (item == null) return null; return item.get(); } public void remove(KeyType key) { mHash.remove(key); } private void ageItems() { long now = System.currentTimeMillis(); Iterator it = mHash.entrySet().iterator(); while (it.hasNext()) { HashMap.Entry pair = (HashMap.Entry) it.next(); AgingItem item = (AgingItem) pair.getValue(); if (now - item.getAccessTime() >= mTtlMillis) { it.remove(); } } } }