view src/main/csharp/jp.ac.u-ryukyu.ie.cr/jungle-main/transaction/DefaultTreeNodeChildren.cs @ 17:01a08cf4b2d9

Liq Files
author Kazuma
date Mon, 07 Nov 2016 01:05:24 +0900
parents abe0c247f5a5
children
line wrap: on
line source

using UnityEngine;
using System.Collections;

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);
	}

}