view src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/DefaultEditableAttributes.java @ 19:703f0be5368a

added attribute cache
author Shoshi TAMAKI
date Thu, 20 Dec 2012 18:09:17 +0900
parents 35c661de095d
children
line wrap: on
line source

package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer;

import java.nio.ByteBuffer;

import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Attributes;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DeleteAttributeOperation;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.Operation;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.SetAttributeOperation;

public class DefaultEditableAttributes implements EditableAttributes
{
	private Node target;
	private Attributes attrs;
	private EditCache cache;
	
	public DefaultEditableAttributes(Node _target,EditCache _cache)
	{
		target = _target;
		attrs = _target.getAttributes();
		cache = _cache;
	}

	@Override
	public ByteBuffer get(String _key)
	{
		Operation op = cache.getAttribute(_key);
		
		if(op == null){
			return attrs.get(_key);
		}
		
		Command c = op.getCommand();
		
		if(c == Command.SET_ATTRIBUTE){
			SetAttributeOperation saop = (SetAttributeOperation)op;
			String key = saop.getKey();
			if(key.equals(_key)){
				return saop.getValue();
			}
			throw new IllegalStateException("fail key.equals(_key)");
		}else if(c == Command.DELETE_ATTRIBUTE){
			DeleteAttributeOperation daop = (DeleteAttributeOperation)op;
			String key = daop.getKey();
			if(key.equals(_key)){
				return null;
			}
			throw new IllegalStateException("fail key.equals(_key)");
		}
		
		throw new IllegalStateException("fail unknown command" + c);
	}

	@Override
	public EditableNode delete(String _key)
	{
		EditCache newCache = cache.deleteAttribute(_key);
		return new DefaultEditableNode(target,newCache);
	}

	@Override
	public EditableNode put(String _key, ByteBuffer _value)
	{
		EditCache newCache = cache.setAttribute(_key,_value);
		return new DefaultEditableNode(target,newCache);
	}
}