1/**
2 * Copyright (c) 2012, Google Inc.
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
18package com.android.mail.utils;
19
20import com.google.common.collect.Lists;
21
22import java.util.Deque;
23
24/**
25 * This class maintains a cache of soft references to objects.  This allows callers to use a pool
26 * of object instances
27 * @param <T>
28 */
29public class ObjectCache<T> {
30    public interface Callback<T> {
31        T newInstance();
32        void onObjectReleased(T object);
33    }
34
35    private final Deque<T> mDataStore = Lists.newLinkedList();
36
37    private final Callback<T> mCallback;
38    private final int mMaxSize;
39
40    /**
41     * Creates a new ObjectCache instance
42     * @param callbacks Callback object that that will return a new instance of the object, and
43     *        perform any cleanup when the object is released back to the cache
44     */
45    public ObjectCache(Callback<T> callbacks, int maxSize) {
46        mCallback = callbacks;
47        mMaxSize = maxSize;
48    }
49
50    /**
51     * Returns an instance of the specified object type, creating a new instance if needed.
52     */
53    public T get() {
54        T result;
55        synchronized (mDataStore) {
56            result = mDataStore.poll();
57        }
58        if (result == null) {
59            result = mCallback.newInstance();
60        }
61        return result;
62    }
63
64    /**
65     * Releases the specified object back to the cache.  Once an object is released, it can be
66     * returned by subsequent calls to get()
67     */
68    public void release(T objectToCache) {
69        synchronized (mDataStore) {
70            if (mDataStore.size() < mMaxSize) {
71                mCallback.onObjectReleased(objectToCache);
72                mDataStore.add(objectToCache);
73            }
74        }
75    }
76}
77