Romain Guy | d1b3dd0 | 2009-03-31 17:53:57 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.util; |
| 18 | |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
| 20 | |
Romain Guy | d1b3dd0 | 2009-03-31 17:53:57 -0700 | [diff] [blame] | 21 | /** |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 22 | * Helper class for crating pools of objects. An example use looks like this: |
| 23 | * <pre> |
| 24 | * public class MyPooledClass { |
| 25 | * |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 26 | * private static final SynchronizedPool<MyPooledClass> sPool = |
| 27 | * new SynchronizedPool<MyPooledClass>(10); |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 28 | * |
| 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 Guy | d1b3dd0 | 2009-03-31 17:53:57 -0700 | [diff] [blame] | 43 | * @hide |
| 44 | */ |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 45 | public final class Pools { |
| 46 | |
| 47 | /** |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 48 | * 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 Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 57 | @UnsupportedAppUsage |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 58 | 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 Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 68 | @UnsupportedAppUsage |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 69 | public boolean release(T instance); |
| 70 | } |
| 71 | |
Romain Guy | 10b4043 | 2009-04-02 09:03:39 -0700 | [diff] [blame] | 72 | private Pools() { |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 73 | /* do nothing - hiding constructor */ |
Romain Guy | d1b3dd0 | 2009-03-31 17:53:57 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 76 | /** |
| 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 Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 82 | @UnsupportedAppUsage |
Svetoslav Ganov | 60fba77 | 2012-12-13 16:01:00 -0800 | [diff] [blame] | 83 | private final Object[] mPool; |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 84 | |
| 85 | private int mPoolSize; |
| 86 | |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 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. |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 93 | */ |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 94 | @UnsupportedAppUsage |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 95 | public SimplePool(int maxPoolSize) { |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 96 | if (maxPoolSize <= 0) { |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 97 | throw new IllegalArgumentException("The max pool size must be > 0"); |
| 98 | } |
Svetoslav Ganov | 60fba77 | 2012-12-13 16:01:00 -0800 | [diff] [blame] | 99 | mPool = new Object[maxPoolSize]; |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
Svetoslav Ganov | 60fba77 | 2012-12-13 16:01:00 -0800 | [diff] [blame] | 103 | @SuppressWarnings("unchecked") |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 104 | @UnsupportedAppUsage |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 105 | public T acquire() { |
Svetoslav Ganov | 60fba77 | 2012-12-13 16:01:00 -0800 | [diff] [blame] | 106 | if (mPoolSize > 0) { |
| 107 | final int lastPooledIndex = mPoolSize - 1; |
| 108 | T instance = (T) mPool[lastPooledIndex]; |
| 109 | mPool[lastPooledIndex] = null; |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 110 | mPoolSize--; |
Svetoslav Ganov | 60fba77 | 2012-12-13 16:01:00 -0800 | [diff] [blame] | 111 | return instance; |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 112 | } |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | @Override |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 117 | @UnsupportedAppUsage |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 118 | public boolean release(T instance) { |
| 119 | if (isInPool(instance)) { |
| 120 | throw new IllegalStateException("Already in the pool!"); |
| 121 | } |
Svetoslav Ganov | 60fba77 | 2012-12-13 16:01:00 -0800 | [diff] [blame] | 122 | if (mPoolSize < mPool.length) { |
| 123 | mPool[mPoolSize] = instance; |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 124 | mPoolSize++; |
| 125 | return true; |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | private boolean isInPool(T instance) { |
Svetoslav Ganov | 60fba77 | 2012-12-13 16:01:00 -0800 | [diff] [blame] | 131 | for (int i = 0; i < mPoolSize; i++) { |
| 132 | if (mPool[i] == instance) { |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 133 | return true; |
| 134 | } |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 135 | } |
| 136 | return false; |
| 137 | } |
Romain Guy | d1b3dd0 | 2009-03-31 17:53:57 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 140 | /** |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 141 | * Synchronized pool of objects. |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 142 | * |
| 143 | * @param <T> The pooled type. |
| 144 | */ |
| 145 | public static class SynchronizedPool<T> extends SimplePool<T> { |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 146 | private final Object mLock; |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * Creates a new instance. |
| 150 | * |
| 151 | * @param maxPoolSize The max pool size. |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 152 | * @param lock an optional custom object to synchronize on |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 153 | * |
| 154 | * @throws IllegalArgumentException If the max pool size is less than zero. |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 155 | */ |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 156 | public SynchronizedPool(int maxPoolSize, Object lock) { |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 157 | super(maxPoolSize); |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 158 | mLock = lock; |
| 159 | } |
| 160 | |
| 161 | /** @see #SynchronizedPool(int, Object) */ |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 162 | @UnsupportedAppUsage |
Eugene Susla | 2f5ee71 | 2017-06-23 17:25:24 -0700 | [diff] [blame] | 163 | public SynchronizedPool(int maxPoolSize) { |
| 164 | this(maxPoolSize, new Object()); |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 168 | @UnsupportedAppUsage |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 169 | public T acquire() { |
| 170 | synchronized (mLock) { |
| 171 | return super.acquire(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | @Override |
Mathew Inwood | 4eb56ab | 2018-08-14 17:24:32 +0100 | [diff] [blame] | 176 | @UnsupportedAppUsage |
Svetoslav Ganov | abae2a1 | 2012-11-27 16:59:37 -0800 | [diff] [blame] | 177 | public boolean release(T element) { |
| 178 | synchronized (mLock) { |
| 179 | return super.release(element); |
| 180 | } |
| 181 | } |
Romain Guy | d1b3dd0 | 2009-03-31 17:53:57 -0700 | [diff] [blame] | 182 | } |
| 183 | } |