view src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/impl/logger/LoggingNode.java @ 0:44465893e8b8

first Commit
author Kazuma
date Wed, 30 Nov 2016 01:47:55 +0900
parents
children
line wrap: on
line source

package jp.ac.u_ryukyu.ie.cr.jungle.store.impl.logger;

import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.NodeOperation;
import jp.ac.u_ryukyu.ie.cr.jungle.util.Error;
import jp.ac.u_ryukyu.ie.cr.jungle.store.impl.TreeNode;
import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.ReplaceRootNodeOperation;
import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither;
import jp.ac.u_ryukyu.ie.cr.jungle.util.Either;


public class LoggingNode
{
  
  private final TreeNode wrap;
  private final OperationLog log;
  
	public LoggingNode(TreeNode _wrap)
	{
		this(_wrap,new DefaultOperationLog());
	}
	
	public LoggingNode(TreeNode _wrap,OperationLog _log)
	{
		wrap = _wrap;
		log = _log;
	}
	
	public LoggingAttributes getAttributes()
	{
		return new LoggingAttributes(wrap,log);
	}
	
	public LoggingChildren getChildren()
	{
		return new LoggingChildren(wrap,log);
	}
	

	public OperationLog getOperationLog()
	{
		return log;
	}
	

  public Either<Error, LoggingNode> replaceNewRootNode() {
   NodeOperation replaceRootNode = new ReplaceRootNodeOperation();
   return edit(replaceRootNode);
  }

  public Either<Error, LoggingNode> edit(NodeOperation op){
    Either<Error,TreeNode> either = op.invoke(wrap);
    if(either.isA()){
      return DefaultEither.newA(either.a());
    }
    
    TreeNode newWrap = either.b();
    OperationLog newLog = log.add(op);
    LoggingNode newLoggingNode = new LoggingNode(newWrap,newLog);
    return DefaultEither.newB(newLoggingNode);
  }
  
  public TreeNode getWrap()
  {
    return wrap;
  }
}