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

net.java.ao.cache.ConcurrentRelationsCache Maven / Gradle / Ivy

Go to download

This is the full Active Objects library, if you don't know which one to use, you probably want this one.

There is a newer version: 6.1.1
Show newest version
package net.java.ao.cache;

import net.java.ao.RawEntity;

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

import static com.google.common.base.Preconditions.*;

final class ConcurrentRelationsCache implements RelationsCache
{
    private final RelationsCache cache;
    private final ReadWriteLock lock;

    ConcurrentRelationsCache(RelationsCache cache)
    {
        this.cache = checkNotNull(cache);
        this.lock = new ReentrantReadWriteLock();
    }

    @Override
    public , K> T[] get(final RawEntity from, final Class toType, final Class> throughType,
                                               final String[] fields, final String where)
    {
        return withLock(lock.readLock(), new Callable()
        {
            @Override
            public T[] call()
            {
                return cache.get(from, toType, throughType, fields, where);
            }
        });
    }

    @Override
    public void put(final RawEntity from, final RawEntity[] through, final Class> throughType,
                    final RawEntity[] to, final Class> toType, final String[] fields, final String where)
    {
        withLock(lock.writeLock(), new Callable()
        {
            @Override
            public Void call()
            {
                cache.put(from, through, throughType, to, toType, fields, where);
                return null;
            }
        });
    }

    @Override
    public void remove(final Class>... types)
    {
        withLock(lock.writeLock(), new Callable()
        {
            @Override
            public Void call()
            {
                cache.remove(types);
                return null;
            }
        });
    }

    @Override
    public void remove(final RawEntity entity, final String[] fields)
    {
        withLock(lock.writeLock(), new Callable()
        {
            @Override
            public Void call()
            {
                cache.remove(entity, fields);
                return null;
            }
        });
    }

    @Override
    public void flush()
    {
        withLock(lock.writeLock(), new Callable()
        {
            @Override
            public Void call()
            {
                cache.flush();
                return null;
            }
        });
    }

    private static  T withLock(Lock l, Callable callable)
    {
        l.lock();
        try
        {
            return callable.call();
        }
        finally
        {
            l.unlock();
        }
    }

    private static interface Callable
    {
        T call();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy