view src/alice/jungle/datasegment/store/container/DefaultNodePathContainer.java @ 75:87ec5dd0dc27

Rename from alice.jungle.datasegment.store.operation to alice.jungle.datasegment.store.container
author one
date Tue, 15 Oct 2013 14:43:29 +0900
parents src/alice/jungle/datasegment/store/operations/DefaultNodePathContainer.java@89e39301ccaa
children
line wrap: on
line source

package alice.jungle.datasegment.store.container;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultNodePath;

import org.msgpack.MessagePack;
import org.msgpack.annotation.Message;
import org.msgpack.template.IntegerTemplate;
import org.msgpack.template.ListTemplate;
import org.msgpack.type.Value;

@Message
public class DefaultNodePathContainer {

	public Value pathValue;
	
	public static void main(String[] args) throws IOException {
		DefaultNodePath p = new DefaultNodePath();
		p = p.add(1).add(2).add(3);
		DefaultNodePathContainer pathContainer = new DefaultNodePathContainer();
		pathContainer.unconvert(p);
		NodePath convertedPath = pathContainer.convert();
		for (int i : convertedPath) {
			System.out.println(i);
		}
	}
	
	public DefaultNodePathContainer() {
		
	}
	
	public void unconvert(NodePath path) throws IOException {
		MessagePack msgpack = new MessagePack();
		List<Integer> list = new LinkedList<Integer>();
		for(Integer i : path) {
			list.add(i);
		}
		/* Remove first Element(-1). */
		list.remove(0);
		Value v = msgpack.unconvert(list);
		pathValue = v;
	}
	
	public DefaultNodePath convert() throws IOException {
		MessagePack msgpack = new MessagePack();
		msgpack.register(List.class, new ListTemplate(IntegerTemplate.getInstance()));
		List<Integer> convertedList = (List<Integer>)msgpack.convert(pathValue, List.class);
		DefaultNodePath path = new DefaultNodePath();
		for (int i: convertedList) {
			path = path.add(i);
		}
		return path;
	}
}