view Main/jungle-main/transaction/DefaultTreeNodeChildren.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;

namespace JungleDB {
	public class DefaultTreeNodeChildren : TreeNodeChildren {

		public List<TreeNode> children;
		public TreeMap<string, byte[]> attrs;

		public DefaultTreeNodeChildren(List<TreeNode> _children, TreeMap<string, byte[]> _attrs){
			children = _children;
			attrs = _attrs;
		}

		private bool boundaryCheck(int _pos) {
			int size = children.length ();
			if (size < _pos) {
				return false;
			}
			return true;
		}

		public List<TreeNode> getChildrenAsRawList() {
			return children;
		}

		public Either<Error, TreeNode> addNewChildAt(int _pos) {
			if (!boundaryCheck(_pos) || _pos < 0) {
				return DefaultEither<Error, TreeNode>.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS);
			}

			List<TreeNode> newChildren = children.add(_pos, new DefaultTreeNode());
			TreeNode newNode = new DefaultTreeNode(newChildren, attrs);
			return DefaultEither<Error, TreeNode>.newB(newNode);
		}


		public Either<Error, TreeNode> deleteChildAt(int _pos) {
			if (!boundaryCheck(_pos) || _pos < 0 || size() == 0) {
				return DefaultEither<Error, TreeNode>.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS);
			}

			List<TreeNode> newChildren = children.delete(_pos);
			TreeNode newNode = new DefaultTreeNode(newChildren, attrs);

			return DefaultEither<Error, TreeNode>.newB(newNode);
		}


		public int size() {
			return children.length();
		}


	//	public Iterator<TreeNode> iterator() {
	//		return children.iterator();
	//	}


		public Either<Error, TreeNode> replaceNode(int _pos, TreeNode _replacement) {
			int size = children.length();
			if (!(0 <= _pos && _pos < size)) {
				return DefaultEither<Error, TreeNode>.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS);
			}
			TreeNode replacement = _replacement;

			List<TreeNode> newChildren = children.replace(_pos, replacement);
			TreeNode node = new DefaultTreeNode(newChildren, attrs);
			return DefaultEither<Error, TreeNode>.newB(node);
		}


		public Either<Error, TreeNode> at(int _pos) {
			if (children.length() < _pos + 1) {
				return DefaultEither<Error, TreeNode>.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS);
			}

			TreeNode Node = children.index(_pos);

			return DefaultEither<Error, TreeNode>.newB(Node);
		}


		public Either<Error, TreeNode> addNewChildAt(int _pos, TreeNode _newChild) {
			if (!boundaryCheck(_pos) || _pos < 0) {
				return DefaultEither<Error, TreeNode>.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS);
			}
			List<TreeNode> newChildren = children.add(_pos, _newChild);
			TreeNode newNode = new DefaultTreeNode(newChildren, attrs);

			return DefaultEither<Error, TreeNode>.newB(newNode);
		}

	}
}