changeset 25:99346c04a3cf

broken
author ShoshiTAMAKI
date Sun, 31 Oct 2010 18:34:02 +0900
parents f0c35d444982
children 9b91329e8a04
files src/treecms/proto/api/Node.java src/treecms/proto/api/NodeID.java src/treecms/proto/api/NodeIDFactory.java src/treecms/proto/api/TreeBuilder.java src/treecms/proto/id/NodeIDFactoryImpl.java src/treecms/proto/id/NodeIDImpl.java src/treecms/proto/merge/MergedTreeBuilder.java src/treecms/proto/simple/SimpleNode.java src/treecms/proto/simple/SimpleTreeBuilder.java src/treecms/proto/test/EditableTreeBuilderTest1.java src/treecms/proto/test/RandomUUIDTest.java src/treecms/proto/util/NodeUUIDComparator.java
diffstat 12 files changed, 184 insertions(+), 90 deletions(-) [+]
line wrap: on
line diff
--- a/src/treecms/proto/api/Node.java	Sun Oct 24 15:45:13 2010 +0900
+++ b/src/treecms/proto/api/Node.java	Sun Oct 31 18:34:02 2010 +0900
@@ -6,19 +6,21 @@
 
 public interface Node
 {
-	Iterator<Node> iterator();
-	List<Node> getChildList();
-	boolean isChild(Node _child);
+	public Iterator<Node> iterator();
+	public List<Node> getChildList();
+	public boolean isChild(Node _child);
 	
-	void addChild(Node _child);
-	void removeChild(Node _child);
+	public void addChild(Node _child);
+	public void removeChild(Node _child);
 	
-	void up(Node _child);
-	void down(Node _child);
+	public void up(Node _child);
+	public void down(Node _child);
+	
+	public void setClassName(String _class);
+	public String getClassName();
 	
-	void setClassName(String _class);
-	void setTitle(String _title);
-	String getClassName();
-	String getTitle();
-	String getID();
+	public void setTitle(String _title);
+	public String getTitle();
+	
+	public NodeID getID();
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/treecms/proto/api/NodeID.java	Sun Oct 31 18:34:02 2010 +0900
@@ -0,0 +1,7 @@
+package treecms.proto.api;
+
+public interface NodeID
+{
+	public String toString();
+	public int compare(NodeID _target);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/treecms/proto/api/NodeIDFactory.java	Sun Oct 31 18:34:02 2010 +0900
@@ -0,0 +1,7 @@
+package treecms.proto.api;
+
+public interface NodeIDFactory
+{
+	public NodeID createNewID();
+	public NodeID updateExistID(NodeID _);
+}
--- a/src/treecms/proto/api/TreeBuilder.java	Sun Oct 24 15:45:13 2010 +0900
+++ b/src/treecms/proto/api/TreeBuilder.java	Sun Oct 31 18:34:02 2010 +0900
@@ -1,9 +1,8 @@
 package treecms.proto.api;
 
-import treecms.proto.edit.PreOrderTreeWalkerRecursive;
-
 public interface TreeBuilder
 {
 	public Node getContents();
 	public Node createNode();
+	public Node cloneNode(Node _node);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/treecms/proto/id/NodeIDFactoryImpl.java	Sun Oct 31 18:34:02 2010 +0900
@@ -0,0 +1,25 @@
+package treecms.proto.id;
+
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicLong;
+
+import treecms.proto.api.NodeID;
+import treecms.proto.api.NodeIDFactory;
+
+public class NodeIDFactoryImpl implements NodeIDFactory
+{
+	@Override
+	public NodeID createNewID()
+	{
+		// TODO Auto-generated method stub
+		return new NodeIDImpl(UUID.randomUUID().toString(),new AtomicLong());
+	}
+
+	@Override
+	public NodeID updateExistID(NodeID _id)
+	{
+		// TODO Auto-generated method stub
+		NodeIDImpl id = (NodeIDImpl)_id;
+		return new NodeIDImpl(id.getInheritedID(),id.getTip());
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/treecms/proto/id/NodeIDImpl.java	Sun Oct 31 18:34:02 2010 +0900
@@ -0,0 +1,57 @@
+package treecms.proto.id;
+
+import treecms.proto.api.NodeID;
+import java.util.concurrent.atomic.AtomicLong;
+
+public class NodeIDImpl implements NodeID
+{
+	private String m_inheritedID;
+	private final long m_version;
+	
+	private AtomicLong m_tip;
+	
+	public NodeIDImpl(String _inheritedID,AtomicLong _version)
+	{
+		m_inheritedID = _inheritedID;
+		m_version = _version.getAndIncrement();
+		m_tip = _version;
+	}
+	
+	String getInheritedID()
+	{
+		return m_inheritedID;
+	}
+	
+	long getVersion()
+	{
+		return m_version;
+	}
+	
+	AtomicLong getTip()
+	{
+		return m_tip;
+	}
+	
+	@Override
+	public int compare(NodeID _target)
+	{
+		// TODO Auto-generated method stub
+		if(m_inheritedID.equals(((NodeIDImpl)_target).m_inheritedID)){
+			long diff = m_version - ((NodeIDImpl)_target).m_version;
+			if(diff == 0){
+				return 0;
+			}else if(diff < 0){
+				return -1;
+			}else{
+				return 1;
+			}
+		}
+		return -2;
+	}
+	
+	@Override
+	public String toString()
+	{
+		return m_inheritedID + "@" + m_version;
+	}
+}
--- a/src/treecms/proto/merge/MergedTreeBuilder.java	Sun Oct 24 15:45:13 2010 +0900
+++ b/src/treecms/proto/merge/MergedTreeBuilder.java	Sun Oct 31 18:34:02 2010 +0900
@@ -1,26 +1,28 @@
 package treecms.proto.merge;
 
 import treecms.proto.api.Node;
+
+import java.util.List;
 import treecms.proto.api.TreeBuilder;
 
 public class MergedTreeBuilder implements TreeBuilder
 {
 	private Node m_root;
 	
-	private TreeBuilder m_target1;
-	private TreeBuilder m_target2;
+	private TreeBuilder m_target;
 	
 	public MergedTreeBuilder(TreeBuilder _target1,TreeBuilder _target2)
 	{
-		m_target1 = _target1;
-		m_target2 = _target2;
+		Node root1 = _target1.getContents();
+		Node root2 = _target2.getContents();
+		
+		merge(root2,root1);
 	}
 	
-	public Node merge(Node _root1,Node _root2)
+	public void merge(Node _target,Node _root)
 	{
-		Node root = null;
-		
-		return root;
+		List<Node> targetChld = _target.getChildList();
+		List<Node> rootChld = _root.getChildList();
 	}
 	
 	@Override
--- a/src/treecms/proto/simple/SimpleNode.java	Sun Oct 24 15:45:13 2010 +0900
+++ b/src/treecms/proto/simple/SimpleNode.java	Sun Oct 31 18:34:02 2010 +0900
@@ -4,6 +4,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import treecms.proto.api.Node;
+import treecms.proto.api.NodeID;
 
 public class SimpleNode implements Node
 {
@@ -11,16 +12,15 @@
 	
 	private String m_class;
 	private String m_title;
-	private String m_uuid;
+	private NodeID m_id;
 	
-	public SimpleNode(String _uuid)
+	public SimpleNode(NodeID _id)
 	{
 		m_childs = new LinkedList<Node>();
 		m_class = "";
 		m_title = "";
 		
-		m_uuid = _uuid;
-		
+		m_id = _id;
 	}
 	
 	@Override
@@ -97,9 +97,9 @@
 	}
 	
 	@Override
-	public String getID()
+	public NodeID getID()
 	{
-		return this.m_uuid;
+		return this.m_id;
 	}
 
 }
--- a/src/treecms/proto/simple/SimpleTreeBuilder.java	Sun Oct 24 15:45:13 2010 +0900
+++ b/src/treecms/proto/simple/SimpleTreeBuilder.java	Sun Oct 31 18:34:02 2010 +0900
@@ -1,25 +1,18 @@
 package treecms.proto.simple;
 
-import java.util.UUID;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicLong;
-
 import treecms.proto.api.Node;
 import treecms.proto.api.TreeBuilder;
+import treecms.proto.id.NodeIDFactoryImpl;
 
 public class SimpleTreeBuilder implements TreeBuilder
 {
 	private Node m_root;
-	
-	private AtomicLong m_ids;
-	private ConcurrentHashMap<Thread,NodeUUID> m_idMap;
+	private NodeIDFactoryImpl m_idFactory;
 	
 	public SimpleTreeBuilder()
 	{
-		m_idMap = new ConcurrentHashMap<Thread,NodeUUID>();
-		m_ids = new AtomicLong();
-		
-		m_root = new SimpleNode(generateUUID());
+		m_idFactory = new NodeIDFactoryImpl();
+		m_root = createNode();
 	}
 	
 	public Node getContents()
@@ -29,49 +22,16 @@
 	
 	public Node createNode()
 	{
-		return new SimpleNode(generateUUID());
-	}
-	
-	public String generateUUID()
-	{
-		return generateUUID_AtomicLong();
-		//return generateUUID_ThreadLocal();
-	}
-	
-	public String generateUUID_AtomicLong()
-	{
-		//blocking?
-		return Long.toString(m_ids.getAndIncrement());
-	}
-	
-	public String generateUUID_ThreadLocal()
-	{
-		NodeUUID uuid;
-		uuid = m_idMap.get(Thread.currentThread());
-		if(uuid == null){
-			uuid = new NodeUUID();
-			m_idMap.put(Thread.currentThread(),uuid);
-		}
-		
-		return uuid.getAndIncrement();
+		return new SimpleNode(m_idFactory.createNewID());
 	}
 
-	private class NodeUUID
+	@Override
+	public Node cloneNode(Node _node)
 	{
-		private UUID m_uuid;
-		private long m_counter;
-		
-		public NodeUUID()
-		{
-			m_uuid = UUID.randomUUID(); 
-			m_counter = 0;
+		// TODO Auto-generated method stub
+		if(!(_node instanceof SimpleNode)){
+			throw new IllegalArgumentException();
 		}
-		
-		public String getAndIncrement()
-		{
-			String ret = m_uuid.toString() + "-" + m_counter;
-			m_counter++;
-			return ret;
-		}
+		return new SimpleNode(m_idFactory.updateExistID(_node.getID()));
 	}
 }
--- a/src/treecms/proto/test/EditableTreeBuilderTest1.java	Sun Oct 24 15:45:13 2010 +0900
+++ b/src/treecms/proto/test/EditableTreeBuilderTest1.java	Sun Oct 31 18:34:02 2010 +0900
@@ -1,7 +1,5 @@
 package treecms.proto.test;
 
-import java.util.Iterator;
-
 import treecms.proto.edit.*;
 import treecms.proto.api.*;
 import treecms.proto.simple.*;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/treecms/proto/test/RandomUUIDTest.java	Sun Oct 31 18:34:02 2010 +0900
@@ -0,0 +1,46 @@
+package treecms.proto.test;
+
+import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+public class RandomUUIDTest
+{
+	public static void main(String _args[])
+	{
+		//test1();
+		//test2();
+		test3(10);
+	}
+	
+	public static void test1()
+	{
+		while(true){
+			System.out.println(UUID.randomUUID().toString());
+		}
+	}
+	
+	public static void test2()
+	{
+		while(true){
+			System.out.println(UUID.fromString(""+System.currentTimeMillis()+""));
+		}
+	}
+	
+	public static void test3(int _threadCount)
+	{
+		ExecutorService service = Executors.newFixedThreadPool(_threadCount);
+		
+		for(int i = 0;i < _threadCount;i ++){
+			service.execute(new Runnable(){
+				@Override
+				public void run() {
+					// TODO Auto-generated method stub
+					while(true){
+						UUID.randomUUID().toString();
+					}
+				}
+			});
+		}
+	}
+}
--- a/src/treecms/proto/util/NodeUUIDComparator.java	Sun Oct 24 15:45:13 2010 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-package treecms.proto.util;
-
-public class NodeUUIDComparator
-{
-	public static String compare(String _target1,String _target2)
-	{
-		return Long.toString(Math.min(Long.parseLong(_target1),Long.parseLong(_target2)));
-	}
-}