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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/com/squareup/picasso/LruCache.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,146 @@
     1.4 +/*
     1.5 + * Copyright (C) 2011 The Android Open Source Project
     1.6 + *
     1.7 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.8 + * you may not use this file except in compliance with the License.
     1.9 + * You may obtain a copy of the License at
    1.10 + *
    1.11 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.12 + *
    1.13 + * Unless required by applicable law or agreed to in writing, software
    1.14 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.16 + * See the License for the specific language governing permissions and
    1.17 + * limitations under the License.
    1.18 + */
    1.19 +package com.squareup.picasso;
    1.20 +
    1.21 +import android.content.Context;
    1.22 +import android.graphics.Bitmap;
    1.23 +import java.util.LinkedHashMap;
    1.24 +import java.util.Map;
    1.25 +
    1.26 +/** A memory cache which uses a least-recently used eviction policy. */
    1.27 +public class LruCache implements Cache {
    1.28 +  final LinkedHashMap<String, Bitmap> map;
    1.29 +  private final int maxSize;
    1.30 +
    1.31 +  private int size;
    1.32 +  private int putCount;
    1.33 +  private int evictionCount;
    1.34 +  private int hitCount;
    1.35 +  private int missCount;
    1.36 +
    1.37 +  /** Create a cache using an appropriate portion of the available RAM as the maximum size. */
    1.38 +  public LruCache(Context context) {
    1.39 +    this(Utils.calculateMemoryCacheSize(context));
    1.40 +  }
    1.41 +
    1.42 +  /** Create a cache with a given maximum size in bytes. */
    1.43 +  public LruCache(int maxSize) {
    1.44 +    if (maxSize <= 0) {
    1.45 +      throw new IllegalArgumentException("Max size must be positive.");
    1.46 +    }
    1.47 +    this.maxSize = maxSize;
    1.48 +    this.map = new LinkedHashMap<String, Bitmap>(0, 0.75f, true);
    1.49 +  }
    1.50 +
    1.51 +  @Override public Bitmap get(String key) {
    1.52 +    if (key == null) {
    1.53 +      throw new NullPointerException("key == null");
    1.54 +    }
    1.55 +
    1.56 +    Bitmap mapValue;
    1.57 +    synchronized (this) {
    1.58 +      mapValue = map.get(key);
    1.59 +      if (mapValue != null) {
    1.60 +        hitCount++;
    1.61 +        return mapValue;
    1.62 +      }
    1.63 +      missCount++;
    1.64 +    }
    1.65 +
    1.66 +    return null;
    1.67 +  }
    1.68 +
    1.69 +  @Override public void set(String key, Bitmap bitmap) {
    1.70 +    if (key == null || bitmap == null) {
    1.71 +      throw new NullPointerException("key == null || bitmap == null");
    1.72 +    }
    1.73 +
    1.74 +    Bitmap previous;
    1.75 +    synchronized (this) {
    1.76 +      putCount++;
    1.77 +      size += Utils.getBitmapBytes(bitmap);
    1.78 +      previous = map.put(key, bitmap);
    1.79 +      if (previous != null) {
    1.80 +        size -= Utils.getBitmapBytes(previous);
    1.81 +      }
    1.82 +    }
    1.83 +
    1.84 +    trimToSize(maxSize);
    1.85 +  }
    1.86 +
    1.87 +  private void trimToSize(int maxSize) {
    1.88 +    while (true) {
    1.89 +      String key;
    1.90 +      Bitmap value;
    1.91 +      synchronized (this) {
    1.92 +        if (size < 0 || (map.isEmpty() && size != 0)) {
    1.93 +          throw new IllegalStateException(
    1.94 +              getClass().getName() + ".sizeOf() is reporting inconsistent results!");
    1.95 +        }
    1.96 +
    1.97 +        if (size <= maxSize || map.isEmpty()) {
    1.98 +          break;
    1.99 +        }
   1.100 +
   1.101 +        Map.Entry<String, Bitmap> toEvict = map.entrySet().iterator().next();
   1.102 +        key = toEvict.getKey();
   1.103 +        value = toEvict.getValue();
   1.104 +        map.remove(key);
   1.105 +        size -= Utils.getBitmapBytes(value);
   1.106 +        evictionCount++;
   1.107 +      }
   1.108 +    }
   1.109 +  }
   1.110 +
   1.111 +  /** Clear the cache. */
   1.112 +  public final void evictAll() {
   1.113 +    trimToSize(-1); // -1 will evict 0-sized elements
   1.114 +  }
   1.115 +
   1.116 +  /** Returns the sum of the sizes of the entries in this cache. */
   1.117 +  public final synchronized int size() {
   1.118 +    return size;
   1.119 +  }
   1.120 +
   1.121 +  /** Returns the maximum sum of the sizes of the entries in this cache. */
   1.122 +  public final synchronized int maxSize() {
   1.123 +    return maxSize;
   1.124 +  }
   1.125 +
   1.126 +  public final synchronized void clear() {
   1.127 +    evictAll();
   1.128 +  }
   1.129 +
   1.130 +  /** Returns the number of times {@link #get} returned a value. */
   1.131 +  public final synchronized int hitCount() {
   1.132 +    return hitCount;
   1.133 +  }
   1.134 +
   1.135 +  /** Returns the number of times {@link #get} returned {@code null}. */
   1.136 +  public final synchronized int missCount() {
   1.137 +    return missCount;
   1.138 +  }
   1.139 +
   1.140 +  /** Returns the number of times {@link #set(String, Bitmap)} was called. */
   1.141 +  public final synchronized int putCount() {
   1.142 +    return putCount;
   1.143 +  }
   1.144 +
   1.145 +  /** Returns the number of values that have been evicted. */
   1.146 +  public final synchronized int evictionCount() {
   1.147 +    return evictionCount;
   1.148 +  }
   1.149 +}

mercurial