view src/alice/datasegment/Receiver.java @ 100:b01fb5090e28 working

add default code to each case statements
author kazz <kazz@cr.ie.u-ryukyu.ac.jp>
date Thu, 28 Jun 2012 01:50:29 +0900
parents 33a19ca88e43
children bddb8be357f6
line wrap: on
line source

package alice.datasegment;

import java.io.IOException;

import org.msgpack.MessagePack;
import org.msgpack.type.Value;
import org.msgpack.type.ValueType;

import alice.codesegment.InputDataSegment;

/**
 * MessagePack implementation and DataSegment Receiver
 * @author kazz
 *
 */
public class Receiver {
	public InputDataSegment ids;
	public int index;
	public Value val;
	public String from;
	public CommandType type;
	
	public String managerKey; // for debugging
	public String key; 		  // for debugging
	
	public Receiver(InputDataSegment ids, CommandType type) {
		this.ids = ids;
		this.type = type;
		ids.regist();
	}
	
	public void setKey(String managerKey, String key) {
		this.managerKey = managerKey;
		this.key = key;
		setKey(managerKey, key, 0);
	}

	public void setKey(String managerKey, String key, int index) {
		switch (type) {
		case PEEK:
			ids.peek(this, managerKey, key, index);
			break;
		case TAKE:
			ids.take(this, managerKey, key, index);
			break;
		default:
			break;
		}
		ids.setKey();
	}
	
	public void setKey(String key) {
		this.key = key;
		setKey(key, 0);
	}

	public void setKey(String key, int index) {
		switch (type) {
		case PEEK:
			ids.peek(this, key, index);
			break;
		case TAKE:
			ids.take(this, key, index);
			break;
		default:
			break;
		}
		ids.setKey();
	}
	
	public String asString() {
		if (val.getType() == ValueType.RAW) {
			return val.asRawValue().getString();
		}
		return null;
	}

	public int asInteger() {
		if (val.getType() == ValueType.INTEGER) {
			return val.asIntegerValue().getInt();
		}
		return 0;
	}
	
	public Float asFloat() {
		if (val.getType() == ValueType.FLOAT) {
			return val.asFloatValue().getFloat();
		}
		return 0.0f;
	}
	
	public <T> T asClass(Class<T> clazz) {
		MessagePack msgpack = new MessagePack();
		try {
			return msgpack.convert(val, clazz);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}