/** * 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))); }
public void writeExternal( ObjectOutput out ) throws IOException { out.writeInt( maximumSize ); out.writeInt( size() ); for( Iterator iterator = keySet().iterator(); iterator.hasNext(); ) { Object key = iterator.next(); out.writeObject( key ); // be sure to call super.get(key), or you're likely to // get infinite promotion recursion Object value = super.get( key ); out.writeObject( value ); } }
/** * <p>Removes the key and its Object from the Map.</p> * * <p>(Note: this may result in the "Least Recently Used" * object being removed from the Map. In that case, * the removeLRU() method is called. See javadoc for * removeLRU() for more details.)</p> * * @param key Key of the Object to add. * @param value Object to add * @return Former value of the key */ public Object put( Object key, Object value ) { int mapSize = size(); Object retval = null; if ( mapSize >= maximumSize ) { // don't retire LRU if you are just // updating an existing key if (!containsKey(key)) { // lets retire the least recently used item in the cache removeLRU(); } } retval = super.put(key,value); return retval; }
/** * This method is used internally by the class for * finding and removing the LRU Object. */ protected void removeLRU() { Object key = getFirstKey(); // be sure to call super.get(key), or you're likely to // get infinite promotion recursion Object value = super.get(key); remove(key); processRemovedLRU(key,value); }
/** * <p>Get the value for a key from the Map. The key * will be promoted to the Most Recently Used position. * Note that get(Object) operations will modify * the underlying Collection. Calling get(Object) * inside of an iteration over keys, values, etc. is * currently unsupported.</p> * * @param key Key to retrieve * @return Returns the value. Returns null if the key has a * null value <i>or</i> if the key has no value. */ public Object get(Object key) { if(!containsKey(key)) return null; Object value = remove(key); super.put(key,value); return value; }
/** * 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.setMaximumSize(3); assertTrue("map should have size = 3, but actually = " + map.size(), map.size() == 3); }
public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException { maximumSize = in.readInt(); int size = in.readInt(); for( int i = 0; i < size; i++ ) { Object key = in.readObject(); Object value = in.readObject(); put(key,value); } }
public void testRemoveLRU() { LRUMap map2 = new LRUMap(3); map2.put(new Integer(1),"foo"); map2.put(new Integer(2),"foo"); map2.put(new Integer(3),"foo"); map2.put(new Integer(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); }
public void testGetPromotion() { LRUMap map = new LRUMap(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 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")); }
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); }
public synchronized int size() { return cache.size(); }
/** * This method is used internally by the class for * finding and removing the LRU Object. */ protected void removeLRU() { Object key = getFirstKey(); // be sure to call super.get(key), or you're likely to // get infinite promotion recursion Object value = super.get(key); remove(key); processRemovedLRU(key,value); }
/** * <p>Get the value for a key from the Map. The key * will be promoted to the Most Recently Used position. * Note that get(Object) operations will modify * the underlying Collection. Calling get(Object) * inside of an iteration over keys, values, etc. is * currently unsupported.</p> * * @param key Key to retrieve * @return Returns the value. Returns null if the key has a * null value <i>or</i> if the key has no value. */ public Object get(Object key) { if(!containsKey(key)) return null; Object value = remove(key); super.put(key,value); return value; }
public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException { maximumSize = in.readInt(); int size = in.readInt(); for( int i = 0; i < size; i++ ) { Object key = in.readObject(); Object value = in.readObject(); put(key,value); } }
public synchronized int size() { return strongReferenceCache.size(); }
/** * <p>Removes the key and its Object from the Map.</p> * * <p>(Note: this may result in the "Least Recently Used" * object being removed from the Map. In that case, * the removeLRU() method is called. See javadoc for * removeLRU() for more details.)</p> * * @param key Key of the Object to add. * @param value Object to add * @return Former value of the key */ public Object put( Object key, Object value ) { int mapSize = size(); Object retval = null; if ( mapSize >= maximumSize ) { // don't retire LRU if you are just // updating an existing key if (!containsKey(key)) { // lets retire the least recently used item in the cache removeLRU(); } } retval = super.put(key,value); return retval; }
/** * This method is used internally by the class for * finding and removing the LRU Object. */ protected void removeLRU() { Object key = getFirstKey(); // be sure to call super.get(key), or you're likely to // get infinite promotion recursion Object value = super.get(key); remove(key); processRemovedLRU(key,value); }