mobile/android/base/util/NonEvictingLruCache.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/util/NonEvictingLruCache.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,44 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko.util;
    1.10 +
    1.11 +import android.support.v4.util.LruCache;
    1.12 +
    1.13 +import java.util.concurrent.ConcurrentHashMap;
    1.14 +
    1.15 +/**
    1.16 + * An LruCache that also supports a set of items that will never be evicted.
    1.17 + *
    1.18 + * Alas, LruCache is final, so we compose rather than inherit.
    1.19 + */
    1.20 +public class NonEvictingLruCache<K, V> {
    1.21 +    private final ConcurrentHashMap<K, V> permanent = new ConcurrentHashMap<K, V>();
    1.22 +    private final LruCache<K, V> evictable;
    1.23 +
    1.24 +    public NonEvictingLruCache(final int evictableSize) {
    1.25 +        evictable = new LruCache<K, V>(evictableSize);
    1.26 +    }
    1.27 +
    1.28 +    public V get(K key) {
    1.29 +        V val = permanent.get(key);
    1.30 +        if (val == null) {
    1.31 +            return evictable.get(key);
    1.32 +        }
    1.33 +        return val;
    1.34 +    }
    1.35 +
    1.36 +    public void putWithoutEviction(K key, V value) {
    1.37 +        permanent.put(key, value);
    1.38 +    }
    1.39 +
    1.40 +    public void put(K key, V value) {
    1.41 +        evictable.put(key, value);
    1.42 +    }
    1.43 +
    1.44 +    public void evictAll() {
    1.45 +        evictable.evictAll();
    1.46 +    }
    1.47 +}

mercurial