view src/main/java/jungle/core/graph/simple/SimpleGraph.java @ 8:abed5bd92fcb

commit
author shoshi <shoshi@cr.ie.u-ryukyu.ac.jp>
date Tue, 03 Jul 2012 18:59:28 +0900
parents 761d04aecfcb
children
line wrap: on
line source

package jungle.core.graph.simple;

import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

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

public class SimpleGraph implements Graph
{
	private final AtomicLong ids;
	private final ConcurrentHashMap<String,SimpleVertex> vertexes;
	
	public SimpleGraph()
	{
		ids = new AtomicLong(0);
		vertexes = new ConcurrentHashMap<String,SimpleVertex>();
	}

	@Override
	public Vertex createVertex()
	{
		SimpleVertex vertex,newVertex;
		do{
			String id = Long.toString(ids.getAndIncrement());
			newVertex = new SimpleVertex(this,id);
			vertex = vertexes.putIfAbsent(id,newVertex);
		}while(vertex != null);
		
		return newVertex;
	}
	
	@Override
	public boolean isSameGraph(Graph _g)
	{
		if(_g instanceof SimpleGraph){
			return _g == this;
		}
		
		return false;
	}
	
	@Override
	public Vertex createVertex(String _id)
	{
		SimpleVertex vertex = new SimpleVertex(this,_id);
		SimpleVertex value = vertexes.putIfAbsent(_id,vertex);
		return value != null ? value : vertex;
	}
	
	@Override
	public Vertex getVertex(String _id)
	{
		return vertexes.get(_id);
	}

	@Override
	public Iterator<Vertex> vertexes()
	{
		final Iterator<SimpleVertex> itr = vertexes.values().iterator();
		Iterator<Vertex> wrapper = new Iterator<Vertex>(){
			@Override
			public boolean hasNext()
			{
				return itr.hasNext();
			}

			@Override
			public Vertex next()
			{
				return itr.next();
			}

			@Override
			public void remove()
			{
				throw new UnsupportedOperationException("remove is not supported");
			}
		};
		return wrapper;
	}
	
	@Override
	public Vertex createVertexFromTemplate(Vertex _template)
	{
		String newID = Long.toString(ids.incrementAndGet());
		
	}

	@Override
	public Vertex createVertexFromTemplate(String _id,Vertex _template)
	{
		if(_template instanceof SimpleVertex){
			SimpleVertex template = (SimpleVertex)_template;
			if(template.getGraph().equals(this)){
				SimpleVertex newVertex = new SimpleVertex(this,_id,_template);
				
			}
			
			throw new IllegalArgumentException("_template is a vertex from other graph.");
		}
		
		throw new IllegalArgumentException("_template is not a instance of SimpleVertex");
	}
}