1/*
2 * Copyright (C) 2013 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
18package androidx.core.util;
19
20
21import androidx.annotation.NonNull;
22import androidx.annotation.Nullable;
23
24/**
25 * Helper class for creating pools of objects. An example use looks like this:
26 * <pre>
27 * public class MyPooledClass {
28 *
29 *     private static final SynchronizedPool<MyPooledClass> sPool =
30 *             new SynchronizedPool<MyPooledClass>(10);
31 *
32 *     public static MyPooledClass obtain() {
33 *         MyPooledClass instance = sPool.acquire();
34 *         return (instance != null) ? instance : new MyPooledClass();
35 *     }
36 *
37 *     public void recycle() {
38 *          // Clear state if needed.
39 *          sPool.release(this);
40 *     }
41 *
42 *     . . .
43 * }
44 * </pre>
45 *
46 */
47public final class Pools {
48
49    /**
50     * Interface for managing a pool of objects.
51     *
52     * @param <T> The pooled type.
53     */
54    public interface Pool<T> {
55
56        /**
57         * @return An instance from the pool if such, null otherwise.
58         */
59        @Nullable
60        T acquire();
61
62        /**
63         * Release an instance to the pool.
64         *
65         * @param instance The instance to release.
66         * @return Whether the instance was put in the pool.
67         *
68         * @throws IllegalStateException If the instance is already in the pool.
69         */
70        boolean release(@NonNull T instance);
71    }
72
73    private Pools() {
74        /* do nothing - hiding constructor */
75    }
76
77    /**
78     * Simple (non-synchronized) pool of objects.
79     *
80     * @param <T> The pooled type.
81     */
82    public static class SimplePool<T> implements Pool<T> {
83        private final Object[] mPool;
84
85        private int mPoolSize;
86
87        /**
88         * Creates a new instance.
89         *
90         * @param maxPoolSize The max pool size.
91         *
92         * @throws IllegalArgumentException If the max pool size is less than zero.
93         */
94        public SimplePool(int maxPoolSize) {
95            if (maxPoolSize <= 0) {
96                throw new IllegalArgumentException("The max pool size must be > 0");
97            }
98            mPool = new Object[maxPoolSize];
99        }
100
101        @Override
102        @SuppressWarnings("unchecked")
103        public T acquire() {
104            if (mPoolSize > 0) {
105                final int lastPooledIndex = mPoolSize - 1;
106                T instance = (T) mPool[lastPooledIndex];
107                mPool[lastPooledIndex] = null;
108                mPoolSize--;
109                return instance;
110            }
111            return null;
112        }
113
114        @Override
115        public boolean release(@NonNull T instance) {
116            if (isInPool(instance)) {
117                throw new IllegalStateException("Already in the pool!");
118            }
119            if (mPoolSize < mPool.length) {
120                mPool[mPoolSize] = instance;
121                mPoolSize++;
122                return true;
123            }
124            return false;
125        }
126
127        private boolean isInPool(@NonNull T instance) {
128            for (int i = 0; i < mPoolSize; i++) {
129                if (mPool[i] == instance) {
130                    return true;
131                }
132            }
133            return false;
134        }
135    }
136
137    /**
138     * Synchronized) pool of objects.
139     *
140     * @param <T> The pooled type.
141     */
142    public static class SynchronizedPool<T> extends SimplePool<T> {
143        private final Object mLock = new Object();
144
145        /**
146         * Creates a new instance.
147         *
148         * @param maxPoolSize The max pool size.
149         *
150         * @throws IllegalArgumentException If the max pool size is less than zero.
151         */
152        public SynchronizedPool(int maxPoolSize) {
153            super(maxPoolSize);
154        }
155
156        @Override
157        public T acquire() {
158            synchronized (mLock) {
159                return super.acquire();
160            }
161        }
162
163        @Override
164        public boolean release(@NonNull T element) {
165            synchronized (mLock) {
166                return super.release(element);
167            }
168        }
169    }
170}
171