/** * This returns a set of entries. Our LRUMapEntry is used since the value stored in the * underlying map is a node in the double linked list. We wouldn't want to return this to the * client, so we construct a new entry with the payload of the node. * <p> * TODO we should return out own set wrapper, so we can avoid the extra object creation if it * isn't necessary. * <p> * @see java.util.Map#entrySet() */ @Override public Set<Map.Entry<K, V>> entrySet() { lock.lock(); try { // TODO we should return a defensive copy Set<Map.Entry<K, LRUElementDescriptor<K, V>>> entries = map.entrySet(); Set<Map.Entry<K, V>> unWrapped = new HashSet<Map.Entry<K, V>>(); for (Map.Entry<K, LRUElementDescriptor<K, V>> pre : entries) { Map.Entry<K, V> post = new LRUMapEntry<K, V>(pre.getKey(), pre.getValue().getPayload()); unWrapped.add(post); } return unWrapped; } finally { lock.unlock(); } }
/** * This returns a set of entries. Our LRUMapEntry is used since the value stored in the * underlying map is a node in the double linked list. We wouldn't want to return this to the * client, so we construct a new entry with the payload of the node. * <p> * TODO we should return out own set wrapper, so we can avoid the extra object creation if it * isn't necessary. * <p> * @see java.util.Map#entrySet() */ @Override public Set<Map.Entry<K, V>> entrySet() { lock.lock(); try { // todo, we should return a defensive copy Set<Map.Entry<K, LRUElementDescriptor<K, V>>> entries = map.entrySet(); Set<Map.Entry<K, V>> unWrapped = new HashSet<Map.Entry<K, V>>(); for (Map.Entry<K, LRUElementDescriptor<K, V>> pre : entries) { Map.Entry<K, V> post = new LRUMapEntry<K, V>(pre.getKey(), pre.getValue().getPayload()); unWrapped.add(post); } return unWrapped; } finally { lock.unlock(); } }