view Main/jungle-network/operations/NetworkNodeOperation.cs @ 31:1466993c104c

byte[] to object Rewrite.
author Kazuma Takeda
date Fri, 20 Jan 2017 07:08:03 +0900
parents 1f99e150f336
children f2ea780b3e80
line wrap: on
line source

using UnityEngine;
using System.Collections;

namespace JungleDB {
	public class NetworkNodeOperation : NodeOperation {

		public int Position;
		public string Key;
		public object Value;
		public int commandType;

		// when switch use static readonly so, use const. 
		// when A code compalie, case const use is fast.
		// case readonly use is bit slow.
		public const int NUM_PUT_ATTRIBUTE = 1;
		public const int NUM_APPEND_CHILD = 2;
		public const int NUM_DELETE_CHILD = 3;
		public const int NUM_DELETE_ATTRIBUTE = 4;

		public NetworkNodeOperation () {
			this.Position = -2;
			this.Key = null;
			this.Value = null;
			this.commandType = 0;
		}

		public NetworkNodeOperation(NodeOperation op) {
			this.Position = op.getPosition();
			this.Key = op.getKey();
			this.Value = op.getValue();
			this.commandType = getCommandType(op.getCommand());
		}

		public static int getCommandType (Command c) {
			switch(c) {
			case Command.PUT_ATTRIBUTE:
				return NUM_PUT_ATTRIBUTE;
			case Command.APPEND_CHILD:
				return NUM_APPEND_CHILD;
			case Command.DELETE_CHILD:
				return NUM_DELETE_CHILD;
			case Command.DELETE_ATTRIBUTE:
				return NUM_DELETE_ATTRIBUTE;
			default:
				break;
			}
			return 0;
		}

		public static Command getCommand (int num) {
			switch(num) {
			case NUM_PUT_ATTRIBUTE:
				return Command.PUT_ATTRIBUTE;
			case NUM_APPEND_CHILD:
				return Command.APPEND_CHILD;
			case NUM_DELETE_CHILD:
				return Command.DELETE_CHILD;
			case NUM_DELETE_ATTRIBUTE:
				return Command.DELETE_ATTRIBUTE;
			default:
				break;
			}
			return Command.DEFAULT;
		}

		public Command getCommand() {
			return getCommand(commandType);
		}

		public int getPosition () {
			return this.Position;
		}

		public string getKey () {
			return this.Key;
		}

		public object getValue() {
			return this.Value;
		}

		public Either<Error, TreeNode> invoke (TreeNode target) {
			switch(getCommand(commandType)) {
			case Command.PUT_ATTRIBUTE:
				return target.getAttributes().put(this.Key, this.Value);
			case Command.APPEND_CHILD:
				return target.getChildren().addNewChildAt(this.Position);
			case Command.DELETE_CHILD:
				return target.getChildren().deleteChildAt(this.Position);
			case Command.DELETE_ATTRIBUTE:
				return target.getAttributes().delete(this.Key);
			}
			return null;
		}
	}
}