view src/main/csharp/jp.ac.u-ryukyu.ie.cr/jungle-network/operations/NetworkNodeOperation.cs @ 12:b71d9ea6bd8e

Add Network Operation Class. this codes can not test yet.
author Kazuma
date Sun, 23 Oct 2016 12:25:57 +0900
parents
children
line wrap: on
line source

using UnityEngine;
using System.Collections;

public class NetworkNodeOperation : NodeOperation {

	public int Position;
	public string Key;
	public byte[] 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 byte[] 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;
	}

}