view src/alice/jungle/operations/NetworkNodePath.java @ 83:b3ccefdf2b43

Added PersistentExample
author one
date Fri, 25 Oct 2013 18:44:29 +0900
parents 5b9708d9febc
children
line wrap: on
line source

package alice.jungle.operations;

import java.util.Iterator;
import java.util.LinkedList;

import org.msgpack.annotation.Message;

import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Pair;

@Message
public class NetworkNodePath implements NodePath
{
	LinkedList<Integer> path;
	
	public NetworkNodePath() {
		path = new LinkedList<Integer>();
		path.add(-1);
	}
	
	public NetworkNodePath(NodePath _p) {
		path = new LinkedList<Integer>();
		for(Integer pos: _p) {
			path.add(pos);
		}
	}
	
	private NetworkNodePath(LinkedList<Integer> _path) {
		path = _path;
	}
	
	@Override
	public Iterator<Integer> iterator() {
		return path.iterator();
	}

	@Override
	public NetworkNodePath add(int _pos) {
		LinkedList<Integer> newPath = copyPath();
		newPath.add(_pos);
		return new NetworkNodePath(newPath);
	}

	@Override
	public Pair<Integer, NodePath> pop() {
		LinkedList<Integer> cPath = copyPath();
		int e = cPath.getFirst();
		cPath.remove();
		return new Pair<Integer, NodePath>(e, new NetworkNodePath(cPath));
	}

	@Override
	public int size() {
		return path.size();
	}

	private LinkedList<Integer> copyPath() {
		LinkedList<Integer> newPath = new LinkedList<Integer>();
		for (Integer i : path) {
			newPath.add(i);
		}
		return newPath;
	}
	
	@Override
	public String toString() {
		return path.toString();
	}
	
}