94 lines
2.5 KiB
Java
94 lines
2.5 KiB
Java
|
package androidx.core.util;
|
||
|
|
||
|
import androidx.annotation.NonNull;
|
||
|
import androidx.annotation.Nullable;
|
||
|
public final class Pools {
|
||
|
|
||
|
public interface Pool<T> {
|
||
|
@Nullable
|
||
|
T acquire();
|
||
|
|
||
|
boolean release(@NonNull T t);
|
||
|
}
|
||
|
|
||
|
public static class SimplePool<T> implements Pool<T> {
|
||
|
private final Object[] mPool;
|
||
|
private int mPoolSize;
|
||
|
|
||
|
public SimplePool(int i) {
|
||
|
if (i > 0) {
|
||
|
this.mPool = new Object[i];
|
||
|
return;
|
||
|
}
|
||
|
throw new IllegalArgumentException("The max pool size must be > 0");
|
||
|
}
|
||
|
|
||
|
private boolean isInPool(@NonNull T t) {
|
||
|
for (int i = 0; i < this.mPoolSize; i++) {
|
||
|
if (this.mPool[i] == t) {
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
@Override // androidx.core.util.Pools.Pool
|
||
|
public T acquire() {
|
||
|
int i = this.mPoolSize;
|
||
|
if (i <= 0) {
|
||
|
return null;
|
||
|
}
|
||
|
int i2 = i - 1;
|
||
|
Object[] objArr = this.mPool;
|
||
|
T t = (T) objArr[i2];
|
||
|
objArr[i2] = null;
|
||
|
this.mPoolSize = i - 1;
|
||
|
return t;
|
||
|
}
|
||
|
|
||
|
@Override // androidx.core.util.Pools.Pool
|
||
|
public boolean release(@NonNull T t) {
|
||
|
if (!isInPool(t)) {
|
||
|
int i = this.mPoolSize;
|
||
|
Object[] objArr = this.mPool;
|
||
|
if (i >= objArr.length) {
|
||
|
return false;
|
||
|
}
|
||
|
objArr[i] = t;
|
||
|
this.mPoolSize = i + 1;
|
||
|
return true;
|
||
|
}
|
||
|
throw new IllegalStateException("Already in the pool!");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static class SynchronizedPool<T> extends SimplePool<T> {
|
||
|
private final Object mLock = new Object();
|
||
|
|
||
|
public SynchronizedPool(int i) {
|
||
|
super(i);
|
||
|
}
|
||
|
|
||
|
@Override // androidx.core.util.Pools.SimplePool, androidx.core.util.Pools.Pool
|
||
|
public T acquire() {
|
||
|
T t;
|
||
|
synchronized (this.mLock) {
|
||
|
t = (T) super.acquire();
|
||
|
}
|
||
|
return t;
|
||
|
}
|
||
|
|
||
|
@Override // androidx.core.util.Pools.SimplePool, androidx.core.util.Pools.Pool
|
||
|
public boolean release(@NonNull T t) {
|
||
|
boolean release;
|
||
|
synchronized (this.mLock) {
|
||
|
release = super.release(t);
|
||
|
}
|
||
|
return release;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private Pools() {
|
||
|
}
|
||
|
}
|