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

org.xblackcat.sjpu.skel.InstanceCachedFactory 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;

/**
 * 04.04.2014 15:47
 *
 * @author xBlackCat
 */
public class InstanceCachedFactory extends ACachedFactory {
    public InstanceCachedFactory(IBuilder builder, Class... argClasses) {
        super(builder, argClasses);
    }

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

        lock.readLock().lock();
        try {
            @SuppressWarnings({"unchecked"})
            T accessHelper = (T) helpers.get(key);

            if (accessHelper != null) {
                return accessHelper;
            }
        } finally {
            lock.readLock().unlock();
        }

        lock.writeLock().lock();
        try {
            final Class builtClass = builder.build(clazz);
            T accessHelper = instantiate(builtClass, args);
            @SuppressWarnings({"unchecked"})
            T oldAccessHelper = (T) helpers.get(key);

            if (oldAccessHelper != null) {
                return oldAccessHelper;
            }

            helpers.put(key, accessHelper);

            return accessHelper;
        } finally {
            lock.writeLock().unlock();
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy