Source code
package android.support.v4.util;
public final class Pools {
public interface Pool<T> {
T acquire();
boolean release(T t);
}
public static class SimplePool<T> implements Pool<T> {
private final Object[] mPool;
private int mPoolSize;
public SimplePool(int maxPoolSize) {
if (maxPoolSize <= 0) {
throw new IllegalArgumentException("The max pool size must be > 0");
}
this.mPool = new Object[maxPoolSize];
}
public T acquire() {
if (this.mPoolSize <= 0) {
return null;
}
int lastPooledIndex = this.mPoolSize - 1;
T instance = this.mPool[lastPooledIndex];
this.mPool[lastPooledIndex] = null;
this.mPoolSize--;
return instance;
}
public boolean release(T instance) {
if (isInPool(instance)) {
throw new IllegalStateException("Already in the pool!");
} else if (this.mPoolSize >= this.mPool.length) {
return false;
} else {
this.mPool[this.mPoolSize] = instance;
this.mPoolSize++;
return true;
}
}
private boolean isInPool(T instance) {
for (int i = 0; i < this.mPoolSize; i++) {
if (this.mPool[i] == instance) {
return true;
}
}
return false;
}
}
public static class SynchronizedPool<T> extends SimplePool<T> {
private final Object mLock = new Object();
public SynchronizedPool(int maxPoolSize) {
super(maxPoolSize);
}
public T acquire() {
T acquire;
synchronized (this.mLock) {
acquire = super.acquire();
}
return acquire;
}
public boolean release(T element) {
boolean release;
synchronized (this.mLock) {
release = super.release(element);
}
return release;
}
}
private Pools() {
}
}