Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 56b6ad3e authored by Jesse Wilson's avatar Jesse Wilson
Browse files

Add a new method, LruCache.remove

Change-Id: Iae78a2ed4d719d4f14a4677ecb6fe5bc823bb660
http://b/3184897
parent b3103093
Loading
Loading
Loading
Loading
+14 −1
Original line number Original line Diff line number Diff line
@@ -206346,6 +206346,19 @@
 visibility="public"
 visibility="public"
>
>
</method>
</method>
<method name="remove"
 return="V"
 abstract="false"
 native="false"
 synchronized="true"
 static="false"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="key" type="K">
</parameter>
</method>
<method name="size"
<method name="size"
 return="int"
 return="int"
 abstract="false"
 abstract="false"
@@ -265592,7 +265605,7 @@
 deprecated="not deprecated"
 deprecated="not deprecated"
 visibility="public"
 visibility="public"
>
>
<parameter name="t" type="T">
<parameter name="arg0" type="T">
</parameter>
</parameter>
</method>
</method>
</interface>
</interface>
+25 −3
Original line number Original line Diff line number Diff line
@@ -51,6 +51,10 @@ import java.util.Map;
 *         cache.put(key, value);
 *         cache.put(key, value);
 *     }
 *     }
 *   }}</pre>
 *   }}</pre>
 *
 * <p>This class does not allow null to be used as a key or value. A return
 * value of null from {@link #get}, {@link #put} or {@link #remove} is
 * unambiguous: the key was not in the cache.
 */
 */
public class LruCache<K, V> {
public class LruCache<K, V> {
    private final LinkedHashMap<K, V> map;
    private final LinkedHashMap<K, V> map;
@@ -86,7 +90,7 @@ public class LruCache<K, V> {
     */
     */
    public synchronized final V get(K key) {
    public synchronized final V get(K key) {
        if (key == null) {
        if (key == null) {
            throw new NullPointerException();
            throw new NullPointerException("key == null");
        }
        }


        V result = map.get(key);
        V result = map.get(key);
@@ -118,7 +122,7 @@ public class LruCache<K, V> {
     */
     */
    public synchronized final V put(K key, V value) {
    public synchronized final V put(K key, V value) {
        if (key == null || value == null) {
        if (key == null || value == null) {
            throw new NullPointerException();
            throw new NullPointerException("key == null || value == null");
        }
        }


        putCount++;
        putCount++;
@@ -133,7 +137,7 @@ public class LruCache<K, V> {


    private void trimToSize(int maxSize) {
    private void trimToSize(int maxSize) {
        while (size > maxSize) {
        while (size > maxSize) {
            Map.Entry<K, V> toEvict = map.eldest();
            Map.Entry<K, V> toEvict = map.eldest(); // equal to map.entrySet().iterator().next();
            if (toEvict == null) {
            if (toEvict == null) {
                break; // map is empty; if size is not 0 then throw an error below
                break; // map is empty; if size is not 0 then throw an error below
            }
            }
@@ -154,6 +158,24 @@ public class LruCache<K, V> {
        }
        }
    }
    }


    /**
     * Removes the entry for {@code key} if it exists.
     *
     * @return the previous value mapped by {@code key}. Although that entry is
     *     no longer cached, it has not been passed to {@link #entryEvicted}.
     */
    public synchronized final V remove(K key) {
        if (key == null) {
            throw new NullPointerException("key == null");
        }

        V previous = map.remove(key);
        if (previous != null) {
            size -= safeSizeOf(key, previous);
        }
        return previous;
    }

    /**
    /**
     * Called for entries that have reached the tail of the least recently used
     * Called for entries that have reached the tail of the least recently used
     * queue and are be removed. The default implementation does nothing.
     * queue and are be removed. The default implementation does nothing.
+39 −0
Original line number Original line Diff line number Diff line
@@ -337,6 +337,45 @@ public final class LruCacheTest extends TestCase {
        assertSnapshot(cache);
        assertSnapshot(cache);
    }
    }


    public void testRemoveDoesNotCallEntryEvicted() {
        LruCache<String, String> cache = new LruCache<String, String>(10) {
            @Override protected void entryEvicted(String key, String value) {
                fail();
            }
        };
        cache.put("a", "A");
        assertEquals("A", cache.remove("a"));
    }

    public void testRemoveWithCustomSizes() {
        LruCache<String, String> cache = new LruCache<String, String>(10) {
            @Override protected int sizeOf(String key, String value) {
                return value.length();
            }
        };
        cache.put("a", "123456");
        cache.put("b", "1234");
        cache.remove("a");
        assertEquals(4, cache.size());
    }

    public void testRemoveAbsentElement() {
        LruCache<String, String> cache = new LruCache<String, String>(10);
        cache.put("a", "A");
        cache.put("b", "B");
        assertEquals(null, cache.remove("c"));
        assertEquals(2, cache.size());
    }

    public void testRemoveNullThrows() {
        LruCache<String, String> cache = new LruCache<String, String>(10);
        try {
            cache.remove(null);
            fail();
        } catch (NullPointerException expected) {
        }
    }

    private LruCache<String, String> newCreatingCache() {
    private LruCache<String, String> newCreatingCache() {
        return new LruCache<String, String>(3) {
        return new LruCache<String, String>(3) {
            @Override protected String create(String key) {
            @Override protected String create(String key) {