view Main/jungle-main/store/impl/DefaultTreeEditor.cs @ 20:1f99e150f336

fix folder and add Object Mapper.
author Kazuma Takeda
date Thu, 15 Dec 2016 22:52:48 +0900
parents
children 9588ad364fdd
line wrap: on
line source

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace JungleDB {
	public class DefaultTreeEditor : TreeEditor {
		private Traverser traverser;
		public DefaultTreeEditor(Traverser traverser){
			this.traverser = traverser;
		}

		public Either<Error,LoggingNode> edit(TreeNode root,NodePath path, NodeEditor editor){
			DefaultEvaluator e = new DefaultEvaluator (path);
			Either<Error, Traversal> either = traverser.traverse (root, e);

			if (either.isA ()) {
				return DefaultEither<Error, LoggingNode>.newA (either.a ());
			}
			Traversal t = either.b ();
			return clone (t, editor);
		}

		private Either<Error, LoggingNode> clone(Traversal t, NodeEditor editor){
			List<Direction<TreeNode>> path = new List<Direction<TreeNode>> ();

			foreach (Direction<TreeNode> direction in t) {
				path = path.addLast (direction);
			}

			Direction<TreeNode> targetDirection = path.headList ();
			TreeNode target = targetDirection.getTarget ();
			Either<Error, LoggingNode> either = editor.edit (target);
			if (either.isA ()) {
				return DefaultEither<Error, LoggingNode>.newA (either.a ());
			}

			LoggingNode newWrap = either.b ();

			int pos = targetDirection.getPosition ();
			TreeNode child = newWrap.getWrap ();

			foreach (Direction<TreeNode> parentDirection in path.deleteHead()) {
				TreeNodeChildren chs = parentDirection.getTarget ().getChildren ();

				Either<Error, TreeNode> ret = chs.replaceNode (pos, child);
				if (ret.isA ()) {
					return DefaultEither<Error, LoggingNode>.newA (ret.a ());
				}

				child = ret.b ();
				pos = parentDirection.getPosition ();
			}

			TreeNode newRoot = child;
			LoggingNode logNode = editor.wrap (newRoot, newWrap.getOperationLog ());
			return DefaultEither<Error, LoggingNode>.newB (logNode);

		}
	}
}