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

org.xblackcat.sjpu.skel.InstanceClassCachedFactory Maven / Gradle / Ivy

Go to download

Service for generating DB access logic in simple way via interfaces and annotations

There is a newer version: 2.0
Show newest version
package org.xblackcat.sjpu.skel;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 04.04.2014 15:47
 *
 * @author xBlackCat
 */
public class InstanceClassCachedFactory extends ACachedFactory> {
    protected final ReadWriteLock lock = new ReentrantReadWriteLock();

    public InstanceClassCachedFactory(IBuilder builder, Class... argClasses) {
        super(builder, argClasses);
    }

    public  T get(Class clazz, Object... args) throws GeneratorException {
        Key key = new Key(clazz, args);

        Class accessHelperClass;

        lock.readLock().lock();
        try {
            accessHelperClass = helpers.get(key);

        } finally {
            lock.readLock().unlock();
        }

        if (accessHelperClass == null) {
            lock.writeLock().lock();
            try {
                final Class builtClass = builder.build(clazz);
                Class oldAccessHelper = helpers.get(key);

                if (oldAccessHelper != null) {
                    accessHelperClass = oldAccessHelper;
                } else {
                    accessHelperClass = builtClass;
                }
            } finally {
                lock.writeLock().unlock();
            }
        }

        @SuppressWarnings("unchecked")
        final Class helperClass = (Class) accessHelperClass;
        return instantiate(helperClass, args);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy