blob: e242fe5cfca3081844fb440b04d0964594f68864 [file] [log] [blame]
Romain Guyd1b3dd02009-03-31 17:53:57 -07001/*
2 * Copyright (C) 2009 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
17package android.util;
18
Mathew Inwood4eb56ab2018-08-14 17:24:32 +010019import android.annotation.UnsupportedAppUsage;
20
Romain Guyd1b3dd02009-03-31 17:53:57 -070021/**
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080022 * Helper class for crating pools of objects. An example use looks like this:
23 * <pre>
24 * public class MyPooledClass {
25 *
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -080026 * private static final SynchronizedPool<MyPooledClass> sPool =
27 * new SynchronizedPool<MyPooledClass>(10);
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080028 *
29 * public static MyPooledClass obtain() {
30 * MyPooledClass instance = sPool.acquire();
31 * return (instance != null) ? instance : new MyPooledClass();
32 * }
33 *
34 * public void recycle() {
35 * // Clear state if needed.
36 * sPool.release(this);
37 * }
38 *
39 * . . .
40 * }
41 * </pre>
42 *
Romain Guyd1b3dd02009-03-31 17:53:57 -070043 * @hide
44 */
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080045public final class Pools {
46
47 /**
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080048 * Interface for managing a pool of objects.
49 *
50 * @param <T> The pooled type.
51 */
52 public static interface Pool<T> {
53
54 /**
55 * @return An instance from the pool if such, null otherwise.
56 */
Mathew Inwood4eb56ab2018-08-14 17:24:32 +010057 @UnsupportedAppUsage
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080058 public T acquire();
59
60 /**
61 * Release an instance to the pool.
62 *
63 * @param instance The instance to release.
64 * @return Whether the instance was put in the pool.
65 *
66 * @throws IllegalStateException If the instance is already in the pool.
67 */
Mathew Inwood4eb56ab2018-08-14 17:24:32 +010068 @UnsupportedAppUsage
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080069 public boolean release(T instance);
70 }
71
Romain Guy10b40432009-04-02 09:03:39 -070072 private Pools() {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080073 /* do nothing - hiding constructor */
Romain Guyd1b3dd02009-03-31 17:53:57 -070074 }
75
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080076 /**
77 * Simple (non-synchronized) pool of objects.
78 *
79 * @param <T> The pooled type.
80 */
81 public static class SimplePool<T> implements Pool<T> {
Mathew Inwood4eb56ab2018-08-14 17:24:32 +010082 @UnsupportedAppUsage
Svetoslav Ganov60fba772012-12-13 16:01:00 -080083 private final Object[] mPool;
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080084
85 private int mPoolSize;
86
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080087 /**
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.
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080093 */
Mathew Inwood4eb56ab2018-08-14 17:24:32 +010094 @UnsupportedAppUsage
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080095 public SimplePool(int maxPoolSize) {
Svetoslav Ganovbe922dc2012-11-30 16:46:26 -080096 if (maxPoolSize <= 0) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080097 throw new IllegalArgumentException("The max pool size must be > 0");
98 }
Svetoslav Ganov60fba772012-12-13 16:01:00 -080099 mPool = new Object[maxPoolSize];
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800100 }
101
102 @Override
Svetoslav Ganov60fba772012-12-13 16:01:00 -0800103 @SuppressWarnings("unchecked")
Mathew Inwood4eb56ab2018-08-14 17:24:32 +0100104 @UnsupportedAppUsage
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800105 public T acquire() {
Svetoslav Ganov60fba772012-12-13 16:01:00 -0800106 if (mPoolSize > 0) {
107 final int lastPooledIndex = mPoolSize - 1;
108 T instance = (T) mPool[lastPooledIndex];
109 mPool[lastPooledIndex] = null;
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800110 mPoolSize--;
Svetoslav Ganov60fba772012-12-13 16:01:00 -0800111 return instance;
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800112 }
113 return null;
114 }
115
116 @Override
Mathew Inwood4eb56ab2018-08-14 17:24:32 +0100117 @UnsupportedAppUsage
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800118 public boolean release(T instance) {
119 if (isInPool(instance)) {
120 throw new IllegalStateException("Already in the pool!");
121 }
Svetoslav Ganov60fba772012-12-13 16:01:00 -0800122 if (mPoolSize < mPool.length) {
123 mPool[mPoolSize] = instance;
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800124 mPoolSize++;
125 return true;
126 }
127 return false;
128 }
129
130 private boolean isInPool(T instance) {
Svetoslav Ganov60fba772012-12-13 16:01:00 -0800131 for (int i = 0; i < mPoolSize; i++) {
132 if (mPool[i] == instance) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800133 return true;
134 }
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800135 }
136 return false;
137 }
Romain Guyd1b3dd02009-03-31 17:53:57 -0700138 }
139
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800140 /**
Eugene Susla2f5ee712017-06-23 17:25:24 -0700141 * Synchronized pool of objects.
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800142 *
143 * @param <T> The pooled type.
144 */
145 public static class SynchronizedPool<T> extends SimplePool<T> {
Eugene Susla2f5ee712017-06-23 17:25:24 -0700146 private final Object mLock;
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800147
148 /**
149 * Creates a new instance.
150 *
151 * @param maxPoolSize The max pool size.
Eugene Susla2f5ee712017-06-23 17:25:24 -0700152 * @param lock an optional custom object to synchronize on
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800153 *
154 * @throws IllegalArgumentException If the max pool size is less than zero.
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800155 */
Eugene Susla2f5ee712017-06-23 17:25:24 -0700156 public SynchronizedPool(int maxPoolSize, Object lock) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800157 super(maxPoolSize);
Eugene Susla2f5ee712017-06-23 17:25:24 -0700158 mLock = lock;
159 }
160
161 /** @see #SynchronizedPool(int, Object) */
Mathew Inwood4eb56ab2018-08-14 17:24:32 +0100162 @UnsupportedAppUsage
Eugene Susla2f5ee712017-06-23 17:25:24 -0700163 public SynchronizedPool(int maxPoolSize) {
164 this(maxPoolSize, new Object());
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800165 }
166
167 @Override
Mathew Inwood4eb56ab2018-08-14 17:24:32 +0100168 @UnsupportedAppUsage
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800169 public T acquire() {
170 synchronized (mLock) {
171 return super.acquire();
172 }
173 }
174
175 @Override
Mathew Inwood4eb56ab2018-08-14 17:24:32 +0100176 @UnsupportedAppUsage
Svetoslav Ganovabae2a12012-11-27 16:59:37 -0800177 public boolean release(T element) {
178 synchronized (mLock) {
179 return super.release(element);
180 }
181 }
Romain Guyd1b3dd02009-03-31 17:53:57 -0700182 }
183}