Home | History | Annotate | Download | only in model
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.systemui.recents.model;
     18 
     19 import android.util.LruCache;
     20 
     21 import java.io.PrintWriter;
     22 
     23 /**
     24  * A mapping of {@link Task.TaskKey} to value, with additional LRU functionality where the least
     25  * recently referenced key/values will be evicted as more values than the given cache size are
     26  * inserted.
     27  *
     28  * In addition, this also allows the caller to invalidate cached values for keys that have since
     29  * changed.
     30  */
     31 public class TaskKeyLruCache<V> extends TaskKeyCache<V> {
     32 
     33     public interface EvictionCallback {
     34         public void onEntryEvicted(Task.TaskKey key);
     35     }
     36 
     37     private final LruCache<Integer, V> mCache;
     38     private final EvictionCallback mEvictionCallback;
     39 
     40     public TaskKeyLruCache(int cacheSize) {
     41         this(cacheSize, null);
     42     }
     43 
     44     public TaskKeyLruCache(int cacheSize, EvictionCallback evictionCallback) {
     45         mEvictionCallback = evictionCallback;
     46         mCache = new LruCache<Integer, V>(cacheSize) {
     47 
     48             @Override
     49             protected void entryRemoved(boolean evicted, Integer taskId, V oldV, V newV) {
     50                 if (mEvictionCallback != null) {
     51                     mEvictionCallback.onEntryEvicted(mKeys.get(taskId));
     52                 }
     53                 mKeys.remove(taskId);
     54             }
     55         };
     56     }
     57 
     58     /** Trims the cache to a specific size */
     59     final void trimToSize(int cacheSize) {
     60         mCache.trimToSize(cacheSize);
     61     }
     62 
     63     public void dump(String prefix, PrintWriter writer) {
     64         String innerPrefix = prefix + "  ";
     65 
     66         writer.print(prefix); writer.print(TAG);
     67         writer.print(" numEntries="); writer.print(mKeys.size());
     68         writer.println();
     69         int keyCount = mKeys.size();
     70         for (int i = 0; i < keyCount; i++) {
     71             writer.print(innerPrefix); writer.println(mKeys.get(mKeys.keyAt(i)));
     72         }
     73     }
     74 
     75     @Override
     76     protected V getCacheEntry(int id) {
     77         return mCache.get(id);
     78     }
     79 
     80     @Override
     81     protected void putCacheEntry(int id, V value) {
     82         mCache.put(id, value);
     83     }
     84 
     85     @Override
     86     protected void removeCacheEntry(int id) {
     87         mCache.remove(id);
     88     }
     89 
     90     @Override
     91     protected void evictAllCache() {
     92         mCache.evictAll();
     93     }
     94 }
     95