All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.objectfabric.InstanceCache Maven / Gradle / Ivy

There is a newer version: 0.9.1
Show newest version
/**
 * This file is part of ObjectFabric (http://objectfabric.org).
 *
 * ObjectFabric is licensed under the Apache License, Version 2.0, the terms
 * of which may be found at http://www.apache.org/licenses/LICENSE-2.0.html.
 * 
 * Copyright ObjectFabric Inc.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

package org.objectfabric;

final class InstanceCache {

    private static final int MAX_SIZE = 10; // TODO tune

    private InstanceCache() {
    }

    static  List getOrCreateList(List threadLocal, PlatformConcurrentQueue> shared) {
        if (threadLocal == null) {
            threadLocal = shared.poll();

            if (threadLocal == null)
                threadLocal = new List();
        } else if (threadLocal.size() == 0) {
            List list = shared.poll();

            if (list != null) {
                if (Debug.ENABLED)
                    Debug.assertion(list.size() > 0);

                threadLocal = list;
            }
        }

        return threadLocal;
    }

    static  List recycle(List threadLocal, PlatformConcurrentQueue> shared, T instance) {
        if (Debug.ENABLED)
            checkNotCached(threadLocal, shared, instance);

        if (threadLocal == null)
            threadLocal = new List();

        threadLocal.add(instance);

        if (threadLocal.size() >= MAX_SIZE) {
            shared.add(threadLocal);
            threadLocal = null;
        }

        return threadLocal;
    }

    static  void checkNotCached(List threadLocal, PlatformConcurrentQueue> shared, T instance) {
        if (!Debug.ENABLED)
            throw new IllegalStateException();

        List> temp = new List>();

        for (;;) {
            List list = shared.poll();

            if (list == null)
                break;

            temp.add(list);
        }

        for (int i = 0; i < temp.size(); i++) {
            for (int t = 0; t < temp.get(i).size(); t++)
                Debug.assertion(instance != temp.get(i).get(t));

            shared.add(temp.get(i));
        }

        if (threadLocal != null)
            for (int i = 0; i < threadLocal.size(); i++)
                Debug.assertion(instance != threadLocal.get(i));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy