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

net.java.ao.cache.MemoryRelationsCache 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
/*
 * Copyright 2007 Daniel Spiewak
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at
 * 
 *	    http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.ao.cache;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import net.java.ao.RawEntity;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

final class MemoryRelationsCache implements RelationsCache
{
    private final Map[]> cache;
    private final Multimap>, MemoryRelationsCacheKey> typeMap;
    private final Multimap fieldMap;

    MemoryRelationsCache()
    {
        cache = new HashMap[]>();
        typeMap = HashMultimap.create();
        fieldMap = HashMultimap.create();
    }

    @Override
    public , K> T[] get(RawEntity from, Class toType, Class> throughType, String[] fields, String where)
    {
        return (T[]) cache.get(new MemoryRelationsCacheKey(from, toType, throughType, fields, where));
    }

    @Override
    public void put(RawEntity from, RawEntity[] through, Class> throughType, RawEntity[] to, Class> toType, String[] fields, String where)
    {
        final MemoryRelationsCacheKey key = new MemoryRelationsCacheKey(from, toType, throughType, fields, where);

        cache.put(key, to);
        typeMap.put(key.getThroughType(), key);

        for (String field : fields)
        {
            for (RawEntity entity : through)
            {
                fieldMap.put(new MemoryRelationsCacheMetaCacheKey(entity, field), key);
            }
        }
    }

    @Override
    public void remove(Class>... types)
    {
        for (Class> type : types)
        {
            Collection keys = typeMap.get(type);
            if (keys != null)
            {
                for (MemoryRelationsCacheKey key : keys)
                {
                    cache.remove(key);
                }
                typeMap.removeAll(type);
            }
        }
    }

    @Override
    public void remove(RawEntity entity, String[] fields)
    {
        for (String field : fields)
        {
            final Collection keys = fieldMap.get(new MemoryRelationsCacheMetaCacheKey(entity, field));
            if (keys != null)
            {
                for (MemoryRelationsCacheKey key : keys)
                {
                    cache.remove(key);
                }
            }
        }
    }

    @Override
    public void flush()
    {
        cache.clear();
        typeMap.clear();
        fieldMap.clear();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy