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

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

package jungle.core.graph.simple;

import java.util.Iterator;
import java.util.concurrent.atomic.AtomicReference;
import fj.Ord;
import fj.P2;
import fj.data.Option;
import fj.data.TreeMap;
import jungle.core.graph.Graph;
import jungle.core.graph.Vertex;
import jungle.core.graph.Vertexes;
import jungle.util.Pair;

public class SimpleVertex implements Vertex
{
	private String id;
	private final SimpleGraph parent;
	private final AtomicReference<TreeMap<String,String>> propertiesHolder;
	private final AtomicReference<TreeMap<String,SimpleVertexes>> relationsHolder;
	
	private static final TreeMap<String,String> EMPTY_PROPERTIES_MAP = TreeMap.empty(Ord.stringOrd);
	private static final TreeMap<String,SimpleVertexes> EMPTY_RELATIONS_MAP = TreeMap.empty(Ord.stringOrd);
	
	public SimpleVertex(SimpleGraph _parent,String _id)
	{
		id = _id;
		parent = _parent;
		propertiesHolder = new AtomicReference<TreeMap<String,String>>(EMPTY_PROPERTIES_MAP);
		relationsHolder = new AtomicReference<TreeMap<String,SimpleVertexes>>(EMPTY_RELATIONS_MAP);
	}
	
	public SimpleVertex(SimpleGraph _parent,String _id,SimpleVertex _template)
	{
		id = _id;
		parent = _parent;
		
		//get snapshot
		TreeMap<String,String> snapshotProperties = _template.
		
		propertiesHolder = new AtomicReference<TreeMap<String,String>>();
		relationsHolder = new AtomicReference<TreeMap<String,SimpleVertexes>>();
	}
	
	private Pair<TreeMap<String,String>,TreeMap<String,SimpleVertexes>> createVertexSnapshot()
	{
		TreeMap<String,String> snapshotProperties = propertiesHolder.get();
		TreeMap<String,SimpleVertexes> snapshotRelations = relationsHolder.get();
		
		return new Pair<TreeMap<Stirng,String>>
	}
	
	void setID(String _id)
	{
		id = _id;
	}
	
	@Override
	public String setPropertyIfAbsent(String _key,String _value)
	{
		TreeMap<String,String> current,update;
		do{
			current = propertiesHolder.get();
			Option<String> value = current.get(_key);
			if(value.isSome()){
				return value.some();
			}
			
			update = current.set(_key,_value);
		}while(!propertiesHolder.compareAndSet(current,update));
		
		return null;
	}
	
	@Override
	public String toString()
	{
		return id;
	}
	
	@Override
	public String getID()
	{
		return id;
	}

	@Override
	public String getProperty(String _key)
	{
		TreeMap<String,String> current = propertiesHolder.get();
		Option<String> nullOrValue = current.get(_key);
		if(nullOrValue.isNone()){
			return null;
		}
		
		return nullOrValue.some();
	}

	@Override
	public void setProperty(String _key, String _value)
	{
		TreeMap<String,String> current , update;
		do{
			current = propertiesHolder.get();
			update = current.set(_key,_value);
		}while(!propertiesHolder.compareAndSet(current,update));
	}

	@Override
	public String removeProperty(String _key)
	{
		if(_key == null){
			throw new NullPointerException("_key is null");
		}
		
		String remove;
		TreeMap<String,String> current , update;
		do{
			current = propertiesHolder.get();
			Option<String> nullOrValue = current.get(_key);
			if(nullOrValue.isNone()){
				return null;
			}
			
			remove = nullOrValue.some();
			update = current.delete(_key);
		}while(!propertiesHolder.compareAndSet(current,update));
		
		return remove;
	}

	@Override
	public Vertexes getVertexes(String _key)
	{
		if(_key == null){
			throw new NullPointerException("_key is null");
		}
		
		TreeMap<String,SimpleVertexes> current = relationsHolder.get();
		Option<SimpleVertexes> nullOrValue = current.get(_key);
		if(nullOrValue.isNone()){
			return null;
		}
		
		return nullOrValue.some();
	}
	
	@Override
	public Vertexes removeVertexes(String _type)
	{
		if(_type == null){
			throw new NullPointerException("_type is null");
		}
		
		SimpleVertexes remove;
		TreeMap<String,SimpleVertexes> current , update;
		do{
			current = relationsHolder.get();
			Option<SimpleVertexes> nullOrValue = current.get(_type);
			if(nullOrValue.isNone()){
				return null;
			}
			
			remove = nullOrValue.some();
			update = current.delete(_type);
		}while(!relationsHolder.compareAndSet(current,update));
		
		return remove;
	}

	@Override
	public boolean compareAndSwapProprety(String _key,final String _except,final String _value)
	{
		if(_key == null || _except == null || _value == null){
			throw new NullPointerException("_key or _except or _value is null");
		}
		
		TreeMap<String,String> current , update;
		do{
			current = propertiesHolder.get();
			Option<String> nullOrValue = current.get(_key);
			if(nullOrValue.isNone()){
				return false;
			}
			
			String value = nullOrValue.some();
			if(!value.equals(_except)){
				return false;
			}
			
			update = current.set(_key,_value);
		}while(!propertiesHolder.compareAndSet(current,update));
		
		return true;
	}
	
	@Override
	public Graph getGraph()
	{
		return parent;
	}

	@Override
	public Vertexes createVertexes(String _type)
	{
		TreeMap<String,SimpleVertexes> current , update;
		
		SimpleVertexes newVertexes;
		do{
			current = relationsHolder.get();
			Option<SimpleVertexes> nullOrValue = current.get(_type);
			if(nullOrValue.isSome()){
				return null;
			}
			
			newVertexes = new SimpleVertexes(_type,parent);
			update = current.set(_type,newVertexes);
		}while(!relationsHolder.compareAndSet(current,update));
		
		return newVertexes;
	}

	@Override
	public Iterable<Pair<String,String>> getProperties()
	{
		final TreeMap<String,String> snapshot = propertiesHolder.get();
		Iterable<Pair<String,String>> snapshotIterableWrapper = new Iterable<Pair<String,String>>(){
			@Override
			public Iterator<Pair<String, String>> iterator(){
				return new Iterator<Pair<String,String>>(){
					Iterator<P2<String,String>> snapshotKeyValueIterator = snapshot.iterator();
					@Override
					public boolean hasNext(){
						return snapshotKeyValueIterator.hasNext();
					}
					@Override
					public Pair<String, String> next(){
						P2<String,String> keyValue = snapshotKeyValueIterator.next();
						return new Pair<String,String>(keyValue._1(),keyValue._2());
					}
					@Override
					public void remove() {
						throw new UnsupportedOperationException("removing is not supported.");
					}
				};
			}
		};
		return snapshotIterableWrapper;
	}
}