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

com.alachisoft.ncache.client.CacheCollection Maven / Gradle / Ivy

package com.alachisoft.ncache.client;

import com.alachisoft.ncache.client.Cache;
import com.alachisoft.ncache.client.internal.caching.CacheImpl;
import com.alachisoft.ncache.client.internal.caching.DisconnectedClientCache;
import com.alachisoft.ncache.client.internal.util.ConfigReader;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import static tangible.DotNetToJavaStringHelper.isNullOrEmpty;

/**
 * Represents a collection of the caches initialized within the same application domain.
 */
public class CacheCollection implements Iterable {
    private HashMap caches = null;

    /**
     * Creates a CacheCollection instance.
     */
    public CacheCollection() {
        caches = new HashMap(8);
    }

    /**
     * @hidden
     * @param cacheId
     * @param c
     */
    public void addCache(String cacheId, CacheImpl c) {
        caches.put(cacheId.toLowerCase(), c);
    }

    /**
     * @hidden
     * @param cacheId
     * @return
     */
    public boolean contains(String cacheId) {
        return caches.containsKey(cacheId.toLowerCase());
    }

    /**
     * @hidden
     * @param cacheId
     * @return
     */
    public CacheImpl removeCache(String cacheId) {
        return (CacheImpl) caches.remove(cacheId.toLowerCase());
    }

    /**
     * The count of the caches contained in the collection.
     * @return The number of caches in collection.
     */
    public int size() {
        return caches.size();
    }

    /**
     * Gets all caches in the form of collection.
     * @return Collection of caches.
     */
    public Collection values() {
        return caches.values();
    }

    //~--- get methods --------------------------------------------------------


    /**
     * @hidden
     * @param cacheId
     * @return
     */
    public CacheImpl getCache(String cacheId) {
        return (CacheImpl) caches.get(cacheId.toLowerCase());

    }

    /**
     * @hidden
     * @param cacheId
     * @param useClientCache
     * @return
     * @throws Exception
     */
    public CacheImpl getCache(String cacheId, boolean useClientCache) throws Exception {
        String clientCache = "";
        boolean isPessimistic = false;

        tangible.RefObject tempRef_clientCache = new tangible.RefObject(clientCache);
        tangible.RefObject tempRef_isPessimistic = new tangible.RefObject(isPessimistic);
        boolean tempVar = useClientCache && hasClientCache(cacheId, tempRef_clientCache, tempRef_isPessimistic);
        clientCache = tempRef_clientCache.argvalue;
        isPessimistic = tempRef_isPessimistic.argvalue;
        if (tempVar) {
            if (!(isNullOrEmpty(clientCache))) {
                Object tempClientCache = caches.get(clientCache.toLowerCase());
                Object tempCacheId = caches.get(cacheId.toLowerCase());
                CacheImpl l1 = (tempClientCache instanceof CacheImpl) ? (CacheImpl) tempClientCache : null;
                CacheImpl l2 = (tempCacheId instanceof CacheImpl) ? (CacheImpl) tempCacheId : null;
                return new DisconnectedClientCache(l2, l1, l2.getSecurityParameters().getUserID(), l2.getSecurityParameters().getPassword(), isPessimistic);
            }
        }
        return (caches.get(cacheId.toLowerCase()) instanceof Cache) ? (CacheImpl) caches.get(cacheId.toLowerCase()) : null;
    }

    /**
     * Returns an {@link Iterator} which iterates over existent caches.
     * @return The iterator that iterates through caches.
     */
    public Iterator iterator() {
        return new CacheIterator();
    }

    /**
     * @hidden
     * @param cacheId
     * @param clientCacheID
     * @param isPessimistic
     * @return
     * @throws Exception
     */
    public boolean hasClientCache(String cacheId, tangible.RefObject clientCacheID, tangible.RefObject isPessimistic) throws Exception {
        if (cacheId == null) {
            throw new IllegalArgumentException("Value cannot be null."+System.lineSeparator()+"Parameter name: cacheId");
        }
        try {

            clientCacheID.argvalue = ConfigReader.ClientCacheID("client.ncconf", cacheId, isPessimistic).trim().toString();

            if (clientCacheID.argvalue.length() > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            throw e;
        }

    }

    private class CacheIterator implements Iterator {

        private HashMap _caches = (HashMap) caches.clone();
        Iterator keys = _caches.keySet().iterator();//int i=0;

        public boolean hasNext() {
            return keys.hasNext();
        }

        public CacheImpl next() {
            return (CacheImpl) _caches.get((String) keys.next());
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy