/** * always returns {@code false}, and instead invokes {@code removeLRU()} * if {@code size >= maxSize}. */ protected boolean removeEldestEntry(Entry pEldest) { // NOTE: As removeLRU() may remove more than one entry, this is better // than simply removing the eldest entry. if (size() >= maxSize) { removeLRU(); } return false; }
/** * Test that the size of the map is reduced immediately * when setMaximumSize(int) is called */ @Test public void testSetMaximumSize() { LRUMap<String, String> map = new LRUMap<String, String>(6); map.put("1","1"); map.put("2","2"); map.put("3","3"); map.put("4","4"); map.put("5","5"); map.put("6","6"); map.setMaxSize(3); assertTrue("map should have size = 3, but actually = " + map.size(), map.size() == 3); }
/** * Confirm that putAll(Map) does not cause the LRUMap * to exceed its maxiumum size. */ @Test public void testPutAll() { LRUMap<Integer, String> map2 = new LRUMap<Integer, String>(3); map2.put(1,"foo"); map2.put(2,"foo"); map2.put(3,"foo"); HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); hashMap.put(4,"foo"); map2.putAll(hashMap); assertTrue("max size is 3, but actual size is " + map2.size(), map2.size() == 3); assertTrue("map should contain the Integer(4) object", map2.containsKey(new Integer(4))); }
/** * Removes the least recently used mapping(s) from this map. * <p> * How many mappings are removed from the map, is controlled by the * trim factor. * In any case, at least one mapping will be removed. * * @see #getTrimFactor() */ public void removeLRU() { int removeCount = (int) Math.max((size() * trimFactor), 1); while ((removeCount--) > 0) { removeEntry(head.next); } } }
@Test public void testMultiplePuts() { LRUMap<Integer, String> map2 = new LRUMap<Integer, String>(2); map2.put(1,"foo"); map2.put(2,"bar"); map2.put(3,"foo"); map2.put(4,"bar"); assertTrue("last value should exist",map2.get(new Integer(4)).equals("bar")); assertTrue("LRU should not exist", map2.get(new Integer(1)) == null); }
@Test public void testRemoveLRU() { LRUMap<Integer, String> map2 = new LRUMap<Integer, String>(3); map2.put(1,"foo"); map2.put(2,"foo"); map2.put(3,"foo"); map2.put(4,"foo"); // removes 1 since max size exceeded map2.removeLRU(); // should be Integer(2) assertTrue("Second to last value should exist",map2.get(new Integer(3)).equals("foo")); assertTrue("First value inserted should not exist", map2.get(new Integer(1)) == null); }
@Test public void testGetPromotion() { LRUMap<String, String> map = new LRUMap<String, String>(3); map.put("1","1"); map.put("2","2"); map.put("3","3"); // LRU is now 1 (then 2 then 3) // promote 1 to top // eviction order is now 2,3,1 map.get("1"); // add another value, forcing a remove // 2 should be evicted (then 3,1,4) map.put("4","4"); Iterator<String> keyIterator = map.keySet().iterator(); Object[] keys = new Object[3]; for (int i = 0; keyIterator.hasNext() ; ++i) { keys[i] = keyIterator.next(); } assertTrue("first evicted should be 3, was " + keys[0], keys[0].equals("3")); assertTrue("second evicted should be 1, was " + keys[1], keys[1].equals("1")); assertTrue("third evicted should be 4, was " + keys[2], keys[2].equals("4")); }
/** * Creates an LRUMap with the given max size. * * @param pMaxSize size limit */ public LRUMap(int pMaxSize) { super(null, true); setMaxSize(pMaxSize); }
/** * Method testWrapMaxSize */ @Test public void testWrapMaxSize() { LRUMap lru = new LRUMap(); lru.setMaxSize(2); TimeoutMap map = new TimeoutMap(lru, null, 1000L); assertNull(map.put("key", "value-1")); assertEquals("value-1", map.get("key")); assertNull(map.put("another", "value-2")); assertEquals("value-2", map.get("another")); assertNull(map.put("third", "value-3")); assertEquals("value-3", map.get("third")); // This value should have expired assertNull(map.get("key")); // These should be left assertEquals("value-2", map.get("another")); assertEquals("value-3", map.get("third")); }
public Map makeEmptyMap() { return new LRUMap(); }
protected Entry<K, V> removeEntry(Entry<K, V> pEntry) { Entry<K, V> entry = super.removeEntry(pEntry); processRemoved(pEntry); return entry; }
public void testRemoveLRU() { LRUMap<Integer, String> map2 = new LRUMap<Integer, String>(3); map2.put(1,"foo"); map2.put(2,"foo"); map2.put(3,"foo"); map2.put(4,"foo"); // removes 1 since max size exceeded map2.removeLRU(); // should be Integer(2) assertTrue("Second to last value should exist",map2.get(new Integer(3)).equals("foo")); assertTrue("First value inserted should not exist", map2.get(new Integer(1)) == null); }
@Test public void testGetPromotion() { LRUMap<String, String> map = new LRUMap<String, String>(3); map.put("1","1"); map.put("2","2"); map.put("3","3"); // LRU is now 1 (then 2 then 3) // promote 1 to top // eviction order is now 2,3,1 map.get("1"); // add another value, forcing a remove // 2 should be evicted (then 3,1,4) map.put("4","4"); Iterator<String> keyIterator = map.keySet().iterator(); Object[] keys = new Object[3]; for (int i = 0; keyIterator.hasNext() ; ++i) { keys[i] = keyIterator.next(); } assertTrue("first evicted should be 3, was " + keys[0], keys[0].equals("3")); assertTrue("second evicted should be 1, was " + keys[1], keys[1].equals("1")); assertTrue("third evicted should be 4, was " + keys[2], keys[2].equals("4")); }
/** * Creates an LRUMap with initial mappings from the given map, * and the given max size. * * @param pContents the map whose mappings are to be placed in this map. * May be {@code null}. * @param pMaxSize size limit */ public LRUMap(Map<? extends K, ? extends V> pContents, int pMaxSize) { super(pContents, true); setMaxSize(pMaxSize); }
public void testMultiplePuts() { LRUMap map2 = new LRUMap(2); map2.put(new Integer(1),"foo"); map2.put(new Integer(2),"bar"); map2.put(new Integer(3),"foo"); map2.put(new Integer(4),"bar"); assertTrue("last value should exist",map2.get(new Integer(4)).equals("bar")); assertTrue("LRU should not exist", map2.get(new Integer(1)) == null); }
/** * Removes the least recently used mapping(s) from this map. * <p> * How many mappings are removed from the map, is controlled by the * trim factor. * In any case, at least one mapping will be removed. * * @see #getTrimFactor() */ public void removeLRU() { int removeCount = (int) Math.max((size() * trimFactor), 1); while ((removeCount--) > 0) { removeEntry(head.next); } } }
/** * Method testWrapMaxSize */ public void testWrapMaxSize() { LRUMap lru = new LRUMap(); lru.setMaxSize(2); TimeoutMap map = new TimeoutMap(lru, null, 1000L); assertNull(map.put("key", "value-1")); assertEquals("value-1", map.get("key")); assertNull(map.put("another", "value-2")); assertEquals("value-2", map.get("another")); assertNull(map.put("third", "value-3")); assertEquals("value-3", map.get("third")); // This value should have expired assertNull(map.get("key")); // These should be left assertEquals("value-2", map.get("another")); assertEquals("value-3", map.get("third")); }
/** * Method testTimeoutWrapReturnNull */ @Test public void testTimeoutWrapReturnNull() { Map map = new TimeoutMap(new LRUMap(), null, 100L); assertNull(map.put("key", "value-1")); assertEquals("value-1", map.get("key")); assertNull(map.put("another", "value-2")); assertEquals("value-2", map.get("another")); synchronized (this) { try { Thread.sleep(110L); } catch (InterruptedException e) { // Continue, but might break the timeout thing below... } } // Values should now time out assertNull(map.get("key")); assertNull(map.get("another")); }
protected Entry<K, V> removeEntry(Entry<K, V> pEntry) { Entry<K, V> entry = super.removeEntry(pEntry); processRemoved(pEntry); return entry; }
/** * Confirm that putAll(Map) does not cause the LRUMap * to exceed its maxiumum size. */ public void testPutAll() { LRUMap<Integer, String> map2 = new LRUMap<Integer, String>(3); map2.put(1,"foo"); map2.put(2,"foo"); map2.put(3,"foo"); HashMap<Integer, String> hashMap = new HashMap<Integer, String>(); hashMap.put(4,"foo"); map2.putAll(hashMap); assertTrue("max size is 3, but actual size is " + map2.size(), map2.size() == 3); assertTrue("map should contain the Integer(4) object", map2.containsKey(new Integer(4))); }