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

com.robrua.orianna.store.Cache Maven / Gradle / Ivy

There is a newer version: 2.4.5
Show newest version
package com.robrua.orianna.store;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.robrua.orianna.type.core.OriannaObject;

/**
 * An in-memory cache for data
 *
 * @author Rob Rua (FatalElement - NA) ([email protected])
 */
public class Cache extends DataStore {
    private final Map>, Map>> cache = new HashMap<>();
    private final Map>, Boolean> haveAll = new HashMap<>();

    @Override
    protected boolean allowsNullStoreKeys() {
        return false;
    }

    @Override
    protected > boolean checkHasAll(final Class type) {
        final Boolean val = haveAll.get(type);

        if(val == null) {
            return false;
        }

        return val.booleanValue();
    }

    @Override
    protected > void doDelete(final Class type, final List keys) {
        final Map> stored = cache.get(type);

        if(stored != null) {
            for(final Object key : keys) {
                stored.remove(key);
            }
        }
    }

    @Override
    protected > void doDelete(final Class type, final Object key) {
        final Map> stored = cache.get(type);

        if(stored != null) {
            stored.remove(key);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    protected > List doGet(final Class type, final List keys) {
        final Map> stored = cache.get(type);

        final List result = new ArrayList<>(keys.size());
        if(stored == null) {
            for(int i = 0; i < keys.size(); i++) {
                result.add(null);
            }
        }
        else {
            for(final Object key : keys) {
                result.add((T)stored.get(key));
            }
        }

        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected > T doGet(final Class type, final Object key) {
        final Map> stored = cache.get(type);

        if(stored == null) {
            return null;
        }

        return (T)stored.get(key);
    }

    @SuppressWarnings("unchecked")
    @Override
    protected > List doGetAll(final Class type) {
        final Map> stored = cache.get(type);

        if(stored == null) {
            return Collections.emptyList();
        }

        final List result = new ArrayList<>();
        for(final Object val : stored.values()) {
            result.add((T)val);
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    public > CloseableIterator doGetIterator(final Class type) {
        final Map> stored = cache.get(type);

        if(stored == null) {
            return CloseableIterator.emptyIterator();
        }

        return CloseableIterator.fromIterator((Iterator)stored.values().iterator());
    }

    @SuppressWarnings("unchecked")
    @Override
    protected > void doStore(final List objs, final List keys, final boolean isFullSet) {
        final Class type = (Class)objs.get(0).getClass();
        if(isFullSet) {
            haveAll.put(type, Boolean.TRUE);
        }

        Map> stored = cache.get(type);
        if(stored == null) {
            stored = new HashMap<>();
            cache.put(type, stored);
        }

        for(int i = 0; i < objs.size(); i++) {
            stored.put(keys.get(i), objs.get(i));
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    protected > void doStore(final T obj, final Object key) {
        final Class type = (Class)obj.getClass();
        Map> stored = cache.get(type);
        if(stored == null) {
            stored = new HashMap<>();
            cache.put(type, stored);
        }

        stored.put(key, obj);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy