Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2fd569a0 authored by Svetoslav Ganov's avatar Svetoslav Ganov Committed by Android (Google) Code Review
Browse files

Merge "Simplification of the Pools implementation."

parents 0bf39c80 60fba770
Loading
Loading
Loading
Loading
+12 −32
Original line number Original line Diff line number Diff line
@@ -69,24 +69,16 @@ public final class Pools {
        /* do nothing - hiding constructor */
        /* do nothing - hiding constructor */
    }
    }


    private static class PoolableHolder<T> {
        T mPoolable;
        PoolableHolder<T> mNext;
    }

    /**
    /**
     * Simple (non-synchronized) pool of objects.
     * Simple (non-synchronized) pool of objects.
     *
     *
     * @param <T> The pooled type.
     * @param <T> The pooled type.
     */
     */
    public static class SimplePool<T> implements Pool<T> {
    public static class SimplePool<T> implements Pool<T> {
        private final int mMaxPoolSize;
        private final Object[] mPool;


        private int mPoolSize;
        private int mPoolSize;


        private PoolableHolder<T> mEmptyHolders;
        private PoolableHolder<T> mPool;

        /**
        /**
         * Creates a new instance.
         * Creates a new instance.
         *
         *
@@ -98,20 +90,18 @@ public final class Pools {
            if (maxPoolSize <= 0) {
            if (maxPoolSize <= 0) {
                throw new IllegalArgumentException("The max pool size must be > 0");
                throw new IllegalArgumentException("The max pool size must be > 0");
            }
            }
            mMaxPoolSize = maxPoolSize;
            mPool = new Object[maxPoolSize];
        }
        }


        @Override
        @Override
        @SuppressWarnings("unchecked")
        public T acquire() {
        public T acquire() {
            if (mPool != null) {
            if (mPoolSize > 0) {
                PoolableHolder<T> holder = mPool;
                final int lastPooledIndex = mPoolSize - 1;
                mPool = holder.mNext;
                T instance = (T) mPool[lastPooledIndex];
                T poolable = holder.mPoolable;
                mPool[lastPooledIndex] = null;
                holder.mPoolable = null;
                holder.mNext = mEmptyHolders;
                mEmptyHolders = holder;
                mPoolSize--;
                mPoolSize--;
                return poolable;
                return instance;
            }
            }
            return null;
            return null;
        }
        }
@@ -121,16 +111,8 @@ public final class Pools {
            if (isInPool(instance)) {
            if (isInPool(instance)) {
                throw new IllegalStateException("Already in the pool!");
                throw new IllegalStateException("Already in the pool!");
            }
            }
            if (mPoolSize < mMaxPoolSize) {
            if (mPoolSize < mPool.length) {
                PoolableHolder<T> holder = mEmptyHolders;
                mPool[mPoolSize] = instance;
                if (holder == null) {
                    holder = new PoolableHolder<T>();
                } else {
                    mEmptyHolders = holder.mNext;
                }
                holder.mPoolable = instance;
                holder.mNext = mPool;
                mPool = holder;
                mPoolSize++;
                mPoolSize++;
                return true;
                return true;
            }
            }
@@ -138,12 +120,10 @@ public final class Pools {
        }
        }


        private boolean isInPool(T instance) {
        private boolean isInPool(T instance) {
            PoolableHolder<T> current = mPool;
            for (int i = 0; i < mPoolSize; i++) {
            while (current != null) {
                if (mPool[i] == instance) {
                if (current.mPoolable == instance) {
                    return true;
                    return true;
                }
                }
                current = current.mNext;
            }
            }
            return false;
            return false;
        }
        }