view src/main/java/jungle/impl/SimpleTreeGroup.java @ 7:c3c65308a11b

removed some package and added Graph API
author shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
date Fri, 29 Jun 2012 00:03:12 +0900
parents 1a5eaf5ce085
children
line wrap: on
line source

package jungle.impl;

import jungle.core.Tree;
import jungle.core.TreeGroup;
import jungle.core.graph.Graph;
import jungle.core.graph.Vertex;

public class SimpleTreeGroup implements TreeGroup
{
	private final String groupID;
	private final Graph graph;
	private final Vertex vertex;
	
	public SimpleTreeGroup(String _groupID,Vertex _vertex,Graph _graph)
	{
		groupID = _groupID;
		graph = _graph;
		vertex = _vertex;
		
		vertex.setPropertyIfAbsent(Simples.TREEGROUP_ID_COUNTER,Simples.TREEGROUP_FIRST_REVISION_ID);
		
		String treeID = String.format(Simples.TREEGROUP_TREEID_FORMAT,groupID,Simples.TREEGROUP_FIRST_REVISION_ID);
		// if not exist , previousID will be null. so then , create new Vertex for new Tree
		if(vertex.setPropertyIfAbsent(Simples.TREEGROUP_CURRENT_TREE_KEY,treeID) == null){
			graph.createVertex(treeID);
		}
	}

	@Override
	public String getID()
	{
		return groupID;
	}

	@Override
	public Tree latestTree()
	{
		String currentID = vertex.getProperty(Simples.TREEGROUP_CURRENT_TREE_KEY);
		Vertex vertex = graph.getVertex(currentID); 
		SimpleTree currentTree = new SimpleTree(this,vertex,graph);
		return currentTree;
	}
	
	public String newTreeID()
	{
		String currentID;
		String nextID;
		
		do{
			currentID = vertex.getProperty(Simples.TREEGROUP_ID_COUNTER);
			nextID = Long.toString(Long.parseLong(currentID) + 1);
		}while(vertex.compareAndSwapProprety(Simples.TREEGROUP_ID_COUNTER,currentID,nextID));
		
		return nextID;
	}
	
	@Override
	public int hashCode()
	{
		return vertex.hashCode();
	}
	
	@Override
	public boolean equals(Object _obj)
	{
		if(_obj instanceof SimpleTreeGroup){
			SimpleTreeGroup target = (SimpleTreeGroup)_obj;
			return target.vertex == vertex;
		}
		
		return false;
	}
}