/*
 * 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.serialize;

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

public class CacheModify<S, V, R> extends SerializedCacheUpdate<S, V, R> {
	private V var;
	
	private CacheResult<R> newValue;
	
	public CacheModify(S session, V var, CacheResult<R> newValue) {
	    super(session);
        this.var = var;
        this.newValue = newValue;
    }
    public V getVar() {
		return var;
	}
	public CacheResult<R> getNewValue() {
		return newValue;
	}
	
	@Override
	public void performUpdate(Cache<S, V, R> cache) {
	    if (newValue.isValid()) {
	        cache.modifyCache(getSession(), var, newValue.getResult());
	    }
	    else {
	        cache.modifyCache(getSession(), var);
	    }
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer("<CacheOp: Session ");
		sb.append(getSession());
		sb.append(" modifies var ");
		sb.append(getVar());
		sb.append(" to ");
		sb.append(getNewValue());
		sb.append(">");
		return sb.toString();
	}
}
