/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is Protege-2000.
 *
 * The Initial Developer of the Original Code is Stanford University. Portions
 * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
 *
 * Protege was developed by Stanford Medical Informatics
 * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
 * with support from the National Library of Medicine, the National Science
 * Foundation, and the Defense Advanced Research Projects Agency.  Current
 * information about Protege can be obtained at http://protege.stanford.edu.
 *
 */

package edu.stanford.smi.protege.util.transaction.cache.impl;

import java.util.HashSet;
import java.util.Set;

import edu.stanford.smi.protege.util.transaction.cache.Cache;
import edu.stanford.smi.protege.util.transaction.cache.CacheResult;

/**
 * This is a basic cache that takes the behavior of its delegate.  But it
 * ignores the impact of transactions on the cache and assumes that its 
 * delegate does the same.  
 *  
 * It will only return 
 * valid results in the case that the transaction isolation level is NONE or where
 * the caller covers the in transaction case.
 * 
 * @author tredmond
 *
 */
public class CompleteableCache<S, V, R> implements Cache<S, V, R> {
    enum CompletionStatus {
        NORMAL, GETTING_COMPLETE_CACHE, CACHE_COMPLETE;
    };
    
    private CompletionStatus status = CompletionStatus.NORMAL;
    private Set<V> invalidReads = new HashSet<V>();
    private Cache<S, V, R> delegate;
    
    public CompleteableCache(Cache<S, V, R> delegate) {
        this.delegate = delegate;
    }
    
    public CacheResult<R> readCache(S session, V var) {
        CacheResult<R> result = delegate.readCache(session, var);
        if (result.getResult() == null &&
                status == CompletionStatus.CACHE_COMPLETE && 
                !invalidReads.contains(var) && !result.isValid()) {
            return new CacheResult<R>(null, true);
        }
        return result;
    }

    public void updateCache(S session, V var) {
        if (status != CompletionStatus.NORMAL) {
            invalidReads.add(var);
        }
        delegate.updateCache(session, var);
    }

    public void updateCache(S session, V var, R value) {
        if (status != CompletionStatus.NORMAL) {
            invalidReads.remove(var);
        }
        delegate.updateCache(session, var, value);
    }

    public void modifyCache(S session, V var) {
        if (status != CompletionStatus.NORMAL) {
            invalidReads.add(var);
        }
        delegate.modifyCache(session, var);
    }

    public void modifyCache(S session, V var, R value) {
        if (status != CompletionStatus.NORMAL) {
            invalidReads.remove(var);
        }
        delegate.modifyCache(session, var, value);
    }
    
    public void delete(S session) {
        delegate.delete(session);
    }

    public boolean isDeleted() {
        return delegate.isDeleted();
    }
    
    public void startCompleteCache() {
        status = CompletionStatus.GETTING_COMPLETE_CACHE;
        invalidReads = new HashSet<V>();
        delegate.startCompleteCache();
    }

    public void finishCompleteCache() {
        status = CompletionStatus.CACHE_COMPLETE;
        delegate.finishCompleteCache();
    }
    
    public void abortCompleteCache() {
        status = CompletionStatus.NORMAL;
        invalidReads = null;
        delegate.abortCompleteCache();
    }
    
    public boolean isCacheComplete() {
        return status == CompletionStatus.CACHE_COMPLETE;
    }

    public void beginTransaction(S session) {
        delegate.beginTransaction(session);
    }

    public void commitTransaction(S session) {
        delegate.commitTransaction(session);
    }
    
    public void rollbackTransaction(S session) {
        delegate.rollbackTransaction(session);
    }
    
    public int getTransactionNesting(S session) {
        return delegate.getTransactionNesting(session);
    }

    public void flush() {
        localFlush();
        delegate.flush();
    }
    
    private void localFlush() {
        status = CompletionStatus.NORMAL;
        invalidReads.clear();
    }


}
