|
|
@ -9,7 +9,7 @@ import androidx.annotation.NonNull; |
|
|
|
import androidx.annotation.Nullable; |
|
|
|
import androidx.annotation.Nullable; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Base class for pools of recycleable objects. |
|
|
|
* Base class for thread-safe pools of recycleable objects. |
|
|
|
* @param <T> the object type |
|
|
|
* @param <T> the object type |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class Pool<T> { |
|
|
|
public class Pool<T> { |
|
|
@ -19,8 +19,9 @@ public class Pool<T> { |
|
|
|
|
|
|
|
|
|
|
|
private int maxPoolSize; |
|
|
|
private int maxPoolSize; |
|
|
|
private int activeCount; |
|
|
|
private int activeCount; |
|
|
|
private LinkedBlockingQueue<T> mQueue; |
|
|
|
private LinkedBlockingQueue<T> queue; |
|
|
|
private Factory<T> factory; |
|
|
|
private Factory<T> factory; |
|
|
|
|
|
|
|
private final Object lock = new Object(); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Used to create new instances of objects when needed. |
|
|
|
* Used to create new instances of objects when needed. |
|
|
@ -37,7 +38,7 @@ public class Pool<T> { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public Pool(int maxPoolSize, @NonNull Factory<T> factory) { |
|
|
|
public Pool(int maxPoolSize, @NonNull Factory<T> factory) { |
|
|
|
this.maxPoolSize = maxPoolSize; |
|
|
|
this.maxPoolSize = maxPoolSize; |
|
|
|
this.mQueue = new LinkedBlockingQueue<>(maxPoolSize); |
|
|
|
this.queue = new LinkedBlockingQueue<>(maxPoolSize); |
|
|
|
this.factory = factory; |
|
|
|
this.factory = factory; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -48,8 +49,10 @@ public class Pool<T> { |
|
|
|
* @return whether the pool is empty |
|
|
|
* @return whether the pool is empty |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public boolean isEmpty() { |
|
|
|
public boolean isEmpty() { |
|
|
|
|
|
|
|
synchronized (lock) { |
|
|
|
return count() >= maxPoolSize; |
|
|
|
return count() >= maxPoolSize; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns a new item, from the recycled pool if possible (if there are recycled items), |
|
|
|
* Returns a new item, from the recycled pool if possible (if there are recycled items), |
|
|
@ -60,7 +63,8 @@ public class Pool<T> { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Nullable |
|
|
|
@Nullable |
|
|
|
public T get() { |
|
|
|
public T get() { |
|
|
|
T item = mQueue.poll(); |
|
|
|
synchronized (lock) { |
|
|
|
|
|
|
|
T item = queue.poll(); |
|
|
|
if (item != null) { |
|
|
|
if (item != null) { |
|
|
|
activeCount++; // poll decreases, this fixes
|
|
|
|
activeCount++; // poll decreases, this fixes
|
|
|
|
LOG.v("GET - Reusing recycled item.", this); |
|
|
|
LOG.v("GET - Reusing recycled item.", this); |
|
|
@ -76,6 +80,7 @@ public class Pool<T> { |
|
|
|
LOG.v("GET - Creating a new item.", this); |
|
|
|
LOG.v("GET - Creating a new item.", this); |
|
|
|
return factory.create(); |
|
|
|
return factory.create(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Recycles an item after it has been used. The item should come from a previous |
|
|
|
* Recycles an item after it has been used. The item should come from a previous |
|
|
@ -84,25 +89,29 @@ public class Pool<T> { |
|
|
|
* @param item used item |
|
|
|
* @param item used item |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void recycle(@NonNull T item) { |
|
|
|
public void recycle(@NonNull T item) { |
|
|
|
|
|
|
|
synchronized (lock) { |
|
|
|
LOG.v("RECYCLE - Recycling item.", this); |
|
|
|
LOG.v("RECYCLE - Recycling item.", this); |
|
|
|
if (--activeCount < 0) { |
|
|
|
if (--activeCount < 0) { |
|
|
|
throw new IllegalStateException("Trying to recycle an item which makes activeCount < 0." + |
|
|
|
throw new IllegalStateException("Trying to recycle an item which makes activeCount < 0." + |
|
|
|
"This means that this or some previous items being recycled were not coming from " + |
|
|
|
"This means that this or some previous items being recycled were not coming from " + |
|
|
|
"this pool, or some item was recycled more than once. " + this); |
|
|
|
"this pool, or some item was recycled more than once. " + this); |
|
|
|
} |
|
|
|
} |
|
|
|
if (!mQueue.offer(item)) { |
|
|
|
if (!queue.offer(item)) { |
|
|
|
throw new IllegalStateException("Trying to recycle an item while the queue is full. " + |
|
|
|
throw new IllegalStateException("Trying to recycle an item while the queue is full. " + |
|
|
|
"This means that this or some previous items being recycled were not coming from " + |
|
|
|
"This means that this or some previous items being recycled were not coming from " + |
|
|
|
"this pool, or some item was recycled more than once. " + this); |
|
|
|
"this pool, or some item was recycled more than once. " + this); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Clears the pool of recycled items. |
|
|
|
* Clears the pool of recycled items. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@CallSuper |
|
|
|
@CallSuper |
|
|
|
public void clear() { |
|
|
|
public void clear() { |
|
|
|
mQueue.clear(); |
|
|
|
synchronized (lock) { |
|
|
|
|
|
|
|
queue.clear(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -114,8 +123,10 @@ public class Pool<T> { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings("WeakerAccess") |
|
|
|
@SuppressWarnings("WeakerAccess") |
|
|
|
public final int count() { |
|
|
|
public final int count() { |
|
|
|
|
|
|
|
synchronized (lock) { |
|
|
|
return activeCount() + recycledCount(); |
|
|
|
return activeCount() + recycledCount(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns the active items managed by this pools, which means, items |
|
|
|
* Returns the active items managed by this pools, which means, items |
|
|
@ -125,8 +136,10 @@ public class Pool<T> { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings("WeakerAccess") |
|
|
|
@SuppressWarnings("WeakerAccess") |
|
|
|
public final int activeCount() { |
|
|
|
public final int activeCount() { |
|
|
|
|
|
|
|
synchronized (lock) { |
|
|
|
return activeCount; |
|
|
|
return activeCount; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns the recycled items managed by this pool, which means, items |
|
|
|
* Returns the recycled items managed by this pool, which means, items |
|
|
@ -137,7 +150,9 @@ public class Pool<T> { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@SuppressWarnings("WeakerAccess") |
|
|
|
@SuppressWarnings("WeakerAccess") |
|
|
|
public final int recycledCount() { |
|
|
|
public final int recycledCount() { |
|
|
|
return mQueue.size(); |
|
|
|
synchronized (lock) { |
|
|
|
|
|
|
|
return queue.size(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@NonNull |
|
|
|
@NonNull |
|
|
|