/** * 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; }
/** * 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); } } }
/** * Sets the maximum number of elements in this map. * * If the current size is greater than the new max size, the map will be * trimmed to fit the new max size constraint. * * @see #removeLRU() * * @param pMaxSize new size limit */ public void setMaxSize(int pMaxSize) { if (pMaxSize < 0) { throw new IllegalArgumentException("max size must be positive"); } maxSize = pMaxSize; while(size() > maxSize) { removeLRU(); } }
/** * 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))); }
/** * 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); }
/** * 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; }
/** * 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() >= mMaxSize) { removeLRU(); } return false; }
/** * 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; }
/** * 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); } } }
/** * 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); } } }
/** * 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() * mTrimFactor), 1); while ((removeCount--) > 0) { removeEntry(mHead.mNext); } } }
/** * Sets the maximum number of elements in this map. * * If the current size is greater than the new max size, the map will be * trimmed to fit the new max size constraint. * * @see #removeLRU() * * @param pMaxSize new size limit */ public void setMaxSize(int pMaxSize) { if (pMaxSize < 0) { throw new IllegalArgumentException("max size must be positive"); } mMaxSize = pMaxSize; while(size() > mMaxSize) { removeLRU(); } }
/** * Sets the maximum number of elements in this map. * * If the current size is greater than the new max size, the map will be * trimmed to fit the new max size constraint. * * @see #removeLRU() * * @param pMaxSize new size limit */ public void setMaxSize(int pMaxSize) { if (pMaxSize < 0) { throw new IllegalArgumentException("max size must be positive"); } maxSize = pMaxSize; while(size() > maxSize) { removeLRU(); } }
/** * Sets the maximum number of elements in this map. * * If the current size is greater than the new max size, the map will be * trimmed to fit the new max size constraint. * * @see #removeLRU() * * @param pMaxSize new size limit */ public void setMaxSize(int pMaxSize) { if (pMaxSize < 0) { throw new IllegalArgumentException("max size must be positive"); } maxSize = pMaxSize; while(size() > maxSize) { removeLRU(); } }
/** * Confirm that putAll(Map) does not cause the LRUMap * to exceed its maxiumum size. */ public void testPutAll() { LRUMap map2 = new LRUMap(3); map2.put(new Integer(1),"foo"); map2.put(new Integer(2),"foo"); map2.put(new Integer(3),"foo"); HashMap hashMap = new HashMap(); hashMap.put(new Integer(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))); }
/** * 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))); }
/** * 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))); }
/** * Test that the size of the map is reduced immediately * when setMaximumSize(int) is called */ public void testSetMaximumSize() { LRUMap map = new LRUMap(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); }
/** * Test that the size of the map is reduced immediately * when setMaximumSize(int) is called */ 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); }
/** * 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); }