annotate src/treecms/proto/simple/SimpleTreeBuilder.java @ 23:a3f8f5da4947

modify SimpleNode compatible for multithread
author ShoshiTAMAKI
date Sun, 24 Oct 2010 13:49:29 +0900
parents e950264f82d3
children f0c35d444982
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
1 package treecms.proto.simple;
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
2
23
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
3 import java.util.UUID;
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
4 import java.util.concurrent.ConcurrentHashMap;
20
ShoshiTAMAKI
parents: 13
diff changeset
5 import treecms.proto.api.Node;
ShoshiTAMAKI
parents: 13
diff changeset
6 import treecms.proto.api.TreeBuilder;
ShoshiTAMAKI
parents: 13
diff changeset
7
ShoshiTAMAKI
parents: 13
diff changeset
8 public class SimpleTreeBuilder implements TreeBuilder
0
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
9 {
20
ShoshiTAMAKI
parents: 13
diff changeset
10 private Node m_root;
23
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
11
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
12 private ConcurrentHashMap<Thread,NodeUUID> m_idMap;
8
8fbe7ba1d820 added EditableNodeAPI
shoshi
parents: 7
diff changeset
13
20
ShoshiTAMAKI
parents: 13
diff changeset
14 public SimpleTreeBuilder()
0
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
15 {
23
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
16 m_idMap = new ConcurrentHashMap<Thread,NodeUUID>();
20
ShoshiTAMAKI
parents: 13
diff changeset
17 m_root = new SimpleNode(generateUUID());
0
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
18 }
7
shoshi
parents: 0
diff changeset
19
20
ShoshiTAMAKI
parents: 13
diff changeset
20 public Node getContents()
0
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
21 {
8
8fbe7ba1d820 added EditableNodeAPI
shoshi
parents: 7
diff changeset
22 return m_root;
7
shoshi
parents: 0
diff changeset
23 }
shoshi
parents: 0
diff changeset
24
20
ShoshiTAMAKI
parents: 13
diff changeset
25 public Node createNode()
7
shoshi
parents: 0
diff changeset
26 {
20
ShoshiTAMAKI
parents: 13
diff changeset
27 return new SimpleNode(generateUUID());
0
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
28 }
7
shoshi
parents: 0
diff changeset
29
23
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
30 public String generateUUID()
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
31 {
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
32 NodeUUID uuid;
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
33 uuid = m_idMap.get(Thread.currentThread());
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
34 if(uuid == null){
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
35 uuid = new NodeUUID();
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
36 m_idMap.put(Thread.currentThread(),uuid);
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
37 }
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
38
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
39 return uuid.getAndIncrement();
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
40 }
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
41
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
42 private class NodeUUID
7
shoshi
parents: 0
diff changeset
43 {
23
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
44 private UUID m_uuid;
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
45 private long m_counter;
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
46
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
47 public NodeUUID()
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
48 {
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
49 m_uuid = UUID.randomUUID();
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
50 m_counter = 0;
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
51 }
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
52
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
53 public String getAndIncrement()
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
54 {
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
55 String ret = m_uuid.toString() + "-" + m_counter;
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
56 m_counter++;
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
57 return ret;
a3f8f5da4947 modify SimpleNode compatible for multithread
ShoshiTAMAKI
parents: 20
diff changeset
58 }
7
shoshi
parents: 0
diff changeset
59 }
0
f815c7c1fb38 hg init
shoshi
parents:
diff changeset
60 }