view src/main/java/jp/ac/u_ryukyu/ie/cr/jungle/store/logger/LoggingChildren.java @ 265:b3a04bc21b23 Implementation_of_communication

add UnDefineNode
author tatsuki
date Tue, 13 Dec 2016 03:16:12 +0900
parents cac88cf813f1
children c62462c28807
line wrap: on
line source

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


import jp.ac.u_ryukyu.ie.cr.jungle.core.Children;
import jp.ac.u_ryukyu.ie.cr.jungle.transaction.node.TreeNode;
import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.AppendChildAtOperation;
import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.ChildMoveOperation;
import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.DeleteChildAtOperation;
import jp.ac.u_ryukyu.ie.cr.jungle.store.operations.NodeOperation;
import jp.ac.u_ryukyu.ie.cr.jungle.util.DefaultEither;
import jp.ac.u_ryukyu.ie.cr.jungle.util.Either;
import jp.ac.u_ryukyu.ie.cr.jungle.util.Error;

public class LoggingChildren 
{
	private final TreeNode wrap;
	private final OperationLog log;
	
	public LoggingChildren(TreeNode _wrap,OperationLog _log)
	{
		wrap = _wrap;
		log = _log;
	}

	public int size()
	{
		Children children = wrap.getChildren();
		return children.size();
	}
	
	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 Either<Error,LoggingNode> addNewChildAt(final int _pos)
	{
		NodeOperation addNewChildAt = new AppendChildAtOperation(_pos);
		return edit(addNewChildAt);
	}

	public Either<Error,LoggingNode> deleteChildAt(final int _pos)
	{
		NodeOperation deleteChildAt = new DeleteChildAtOperation(_pos);
		return edit(deleteChildAt);
	}


	public Either<Error,LoggingNode> moveChild(String move, int childNum) {
		NodeOperation moveChild = new ChildMoveOperation(move,childNum);
		return edit(moveChild);
	}

	public Either<Error,LoggingNode> at(int _pos)
	{
		Children children = wrap.getChildren();
		Either<Error,TreeNode> either = children.at(_pos);
		if(either.isA()){
			return DefaultEither.newA(either.a());
		}
		
		TreeNode node = either.b();
		LoggingNode logNode = new LoggingNode(node);
		return DefaultEither.newB(logNode);
	}
}