view src/alice/datasegment/Receiver.java @ 205:28469b1671e7 working

repair flip API
author sugi
date Tue, 26 Mar 2013 01:45:05 +0900
parents c6a99216707a
children 96110f25adcc
line wrap: on
line source

package alice.datasegment;

import java.io.IOException;

import org.msgpack.type.ArrayValue;
import org.msgpack.type.Value;
import org.msgpack.type.ValueFactory;
import org.msgpack.type.ValueType;

import alice.codesegment.InputDataSegment;
import alice.codesegment.SingletonMessage;

/**
 * MessagePack implementation and DataSegment Receiver
 * @author kazz
 *
 */
public class Receiver {
	public InputDataSegment ids;
	public int index;
	public Value val;
	public Object obj;
	public String from;
	public CommandType type;
	
	public String managerKey; // for debugging
	public String key; 		  // for debugging
	private Command cmd;      // for flip
	
	public Receiver(InputDataSegment ids, CommandType type) {
		this.ids = ids;
		this.type = type;
		ids.regist();
	}
	
	public void setCommand(Command cmd) {
		this.cmd = cmd;
		
	}
	
	public void flip(CommandType type, String key, int val){
		flip(type, key, ValueFactory.createIntegerValue(val));
	}
	
	public void flip(CommandType type, String key, String val){
		flip(type, key, ValueFactory.createRawValue(val));
	}
	
	public void flip(CommandType type, String key, byte[] val){
		flip(type, key, ValueFactory.createRawValue(val, true));
	}
	
	public void flip(CommandType type, String key, Object obj, Boolean covertFlag){
		cmd.setElement(type, key, null, obj);
		ids.flip(cmd);
	}
	
	
	public void flip(CommandType type, String key, Value val){
		cmd.setElement(type, key, val, null);
		ids.flip(cmd);
	}
	
	public void setKey(String managerKey, String key) {
		this.managerKey = managerKey;
		setKey(managerKey, key, 0);
	}

	public void setKey(String managerKey, String key, int index) {
		this.key = key;
		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) {
		setKey(key, 0);
	}

	public void setKey(String key, int index) {
		this.key = key;
		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 ArrayValue asArray(){
		if (val.getType() == ValueType.ARRAY){
			return val.asArrayValue();
		}
		return null;
	}
	
	public <T> T asClass(Class<T> clazz) {
		try {
			return SingletonMessage.getInstance().convert(val, clazz);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}