changeset 23:a3f8f5da4947

modify SimpleNode compatible for multithread
author ShoshiTAMAKI
date Sun, 24 Oct 2010 13:49:29 +0900
parents 92637ef791b7
children f0c35d444982
files src/treecms/proto/simple/SimpleNode.java src/treecms/proto/simple/SimpleTreeBuilder.java
diffstat 2 files changed, 35 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/treecms/proto/simple/SimpleNode.java	Sat Oct 23 01:14:45 2010 +0900
+++ b/src/treecms/proto/simple/SimpleNode.java	Sun Oct 24 13:49:29 2010 +0900
@@ -3,7 +3,6 @@
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
-
 import treecms.proto.api.Node;
 
 public class SimpleNode implements Node
@@ -21,11 +20,7 @@
 		m_title = "";
 		
 		m_uuid = _uuid;
-	}
-	
-	public String getUUID()
-	{
-		return m_uuid;
+		
 	}
 	
 	@Override
@@ -101,6 +96,7 @@
 		return m_childs.iterator();
 	}
 	
+	@Override
 	public String getID()
 	{
 		return this.m_uuid;
--- a/src/treecms/proto/simple/SimpleTreeBuilder.java	Sat Oct 23 01:14:45 2010 +0900
+++ b/src/treecms/proto/simple/SimpleTreeBuilder.java	Sun Oct 24 13:49:29 2010 +0900
@@ -1,20 +1,20 @@
 package treecms.proto.simple;
 
-import java.util.Random;
-
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
 import treecms.proto.api.Node;
 import treecms.proto.api.TreeBuilder;
 
 public class SimpleTreeBuilder implements TreeBuilder
 {
 	private Node m_root;
-	private Random m_rand;
+	
+	private ConcurrentHashMap<Thread,NodeUUID> m_idMap;
 	
 	public SimpleTreeBuilder()
 	{
-		m_rand = new Random();
+		m_idMap = new ConcurrentHashMap<Thread,NodeUUID>();
 		m_root = new SimpleNode(generateUUID());
-		return;
 	}
 	
 	public Node getContents()
@@ -27,9 +27,34 @@
 		return new SimpleNode(generateUUID());
 	}
 	
-	String generateUUID()
+	public String generateUUID()
+	{
+		NodeUUID uuid;
+		uuid = m_idMap.get(Thread.currentThread());
+		if(uuid == null){
+			uuid = new NodeUUID();
+			m_idMap.put(Thread.currentThread(),uuid);
+		}
+		
+		return uuid.getAndIncrement();
+	}
+
+	private class NodeUUID
 	{
-		return Long.toString(m_rand.nextLong());
+		private UUID m_uuid;
+		private long m_counter;
+		
+		public NodeUUID()
+		{
+			m_uuid = UUID.randomUUID(); 
+			m_counter = 0;
+		}
+		
+		public String getAndIncrement()
+		{
+			String ret = m_uuid.toString() + "-" + m_counter;
+			m_counter++;
+			return ret;
+		}
 	}
-	
 }