mobile/android/thirdparty/com/squareup/picasso/LruCache.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * Copyright (C) 2011 The Android Open Source Project
michael@0 3 *
michael@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 5 * you may not use this file except in compliance with the License.
michael@0 6 * You may obtain a copy of the License at
michael@0 7 *
michael@0 8 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 9 *
michael@0 10 * Unless required by applicable law or agreed to in writing, software
michael@0 11 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 13 * See the License for the specific language governing permissions and
michael@0 14 * limitations under the License.
michael@0 15 */
michael@0 16 package com.squareup.picasso;
michael@0 17
michael@0 18 import android.content.Context;
michael@0 19 import android.graphics.Bitmap;
michael@0 20 import java.util.LinkedHashMap;
michael@0 21 import java.util.Map;
michael@0 22
michael@0 23 /** A memory cache which uses a least-recently used eviction policy. */
michael@0 24 public class LruCache implements Cache {
michael@0 25 final LinkedHashMap<String, Bitmap> map;
michael@0 26 private final int maxSize;
michael@0 27
michael@0 28 private int size;
michael@0 29 private int putCount;
michael@0 30 private int evictionCount;
michael@0 31 private int hitCount;
michael@0 32 private int missCount;
michael@0 33
michael@0 34 /** Create a cache using an appropriate portion of the available RAM as the maximum size. */
michael@0 35 public LruCache(Context context) {
michael@0 36 this(Utils.calculateMemoryCacheSize(context));
michael@0 37 }
michael@0 38
michael@0 39 /** Create a cache with a given maximum size in bytes. */
michael@0 40 public LruCache(int maxSize) {
michael@0 41 if (maxSize <= 0) {
michael@0 42 throw new IllegalArgumentException("Max size must be positive.");
michael@0 43 }
michael@0 44 this.maxSize = maxSize;
michael@0 45 this.map = new LinkedHashMap<String, Bitmap>(0, 0.75f, true);
michael@0 46 }
michael@0 47
michael@0 48 @Override public Bitmap get(String key) {
michael@0 49 if (key == null) {
michael@0 50 throw new NullPointerException("key == null");
michael@0 51 }
michael@0 52
michael@0 53 Bitmap mapValue;
michael@0 54 synchronized (this) {
michael@0 55 mapValue = map.get(key);
michael@0 56 if (mapValue != null) {
michael@0 57 hitCount++;
michael@0 58 return mapValue;
michael@0 59 }
michael@0 60 missCount++;
michael@0 61 }
michael@0 62
michael@0 63 return null;
michael@0 64 }
michael@0 65
michael@0 66 @Override public void set(String key, Bitmap bitmap) {
michael@0 67 if (key == null || bitmap == null) {
michael@0 68 throw new NullPointerException("key == null || bitmap == null");
michael@0 69 }
michael@0 70
michael@0 71 Bitmap previous;
michael@0 72 synchronized (this) {
michael@0 73 putCount++;
michael@0 74 size += Utils.getBitmapBytes(bitmap);
michael@0 75 previous = map.put(key, bitmap);
michael@0 76 if (previous != null) {
michael@0 77 size -= Utils.getBitmapBytes(previous);
michael@0 78 }
michael@0 79 }
michael@0 80
michael@0 81 trimToSize(maxSize);
michael@0 82 }
michael@0 83
michael@0 84 private void trimToSize(int maxSize) {
michael@0 85 while (true) {
michael@0 86 String key;
michael@0 87 Bitmap value;
michael@0 88 synchronized (this) {
michael@0 89 if (size < 0 || (map.isEmpty() && size != 0)) {
michael@0 90 throw new IllegalStateException(
michael@0 91 getClass().getName() + ".sizeOf() is reporting inconsistent results!");
michael@0 92 }
michael@0 93
michael@0 94 if (size <= maxSize || map.isEmpty()) {
michael@0 95 break;
michael@0 96 }
michael@0 97
michael@0 98 Map.Entry<String, Bitmap> toEvict = map.entrySet().iterator().next();
michael@0 99 key = toEvict.getKey();
michael@0 100 value = toEvict.getValue();
michael@0 101 map.remove(key);
michael@0 102 size -= Utils.getBitmapBytes(value);
michael@0 103 evictionCount++;
michael@0 104 }
michael@0 105 }
michael@0 106 }
michael@0 107
michael@0 108 /** Clear the cache. */
michael@0 109 public final void evictAll() {
michael@0 110 trimToSize(-1); // -1 will evict 0-sized elements
michael@0 111 }
michael@0 112
michael@0 113 /** Returns the sum of the sizes of the entries in this cache. */
michael@0 114 public final synchronized int size() {
michael@0 115 return size;
michael@0 116 }
michael@0 117
michael@0 118 /** Returns the maximum sum of the sizes of the entries in this cache. */
michael@0 119 public final synchronized int maxSize() {
michael@0 120 return maxSize;
michael@0 121 }
michael@0 122
michael@0 123 public final synchronized void clear() {
michael@0 124 evictAll();
michael@0 125 }
michael@0 126
michael@0 127 /** Returns the number of times {@link #get} returned a value. */
michael@0 128 public final synchronized int hitCount() {
michael@0 129 return hitCount;
michael@0 130 }
michael@0 131
michael@0 132 /** Returns the number of times {@link #get} returned {@code null}. */
michael@0 133 public final synchronized int missCount() {
michael@0 134 return missCount;
michael@0 135 }
michael@0 136
michael@0 137 /** Returns the number of times {@link #set(String, Bitmap)} was called. */
michael@0 138 public final synchronized int putCount() {
michael@0 139 return putCount;
michael@0 140 }
michael@0 141
michael@0 142 /** Returns the number of values that have been evicted. */
michael@0 143 public final synchronized int evictionCount() {
michael@0 144 return evictionCount;
michael@0 145 }
michael@0 146 }

mercurial