Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | package org.mozilla.gecko.util; |
michael@0 | 7 | |
michael@0 | 8 | import android.support.v4.util.LruCache; |
michael@0 | 9 | |
michael@0 | 10 | import java.util.concurrent.ConcurrentHashMap; |
michael@0 | 11 | |
michael@0 | 12 | /** |
michael@0 | 13 | * An LruCache that also supports a set of items that will never be evicted. |
michael@0 | 14 | * |
michael@0 | 15 | * Alas, LruCache is final, so we compose rather than inherit. |
michael@0 | 16 | */ |
michael@0 | 17 | public class NonEvictingLruCache<K, V> { |
michael@0 | 18 | private final ConcurrentHashMap<K, V> permanent = new ConcurrentHashMap<K, V>(); |
michael@0 | 19 | private final LruCache<K, V> evictable; |
michael@0 | 20 | |
michael@0 | 21 | public NonEvictingLruCache(final int evictableSize) { |
michael@0 | 22 | evictable = new LruCache<K, V>(evictableSize); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | public V get(K key) { |
michael@0 | 26 | V val = permanent.get(key); |
michael@0 | 27 | if (val == null) { |
michael@0 | 28 | return evictable.get(key); |
michael@0 | 29 | } |
michael@0 | 30 | return val; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | public void putWithoutEviction(K key, V value) { |
michael@0 | 34 | permanent.put(key, value); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | public void put(K key, V value) { |
michael@0 | 38 | evictable.put(key, value); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | public void evictAll() { |
michael@0 | 42 | evictable.evictAll(); |
michael@0 | 43 | } |
michael@0 | 44 | } |