0
|
1 package treecms.proto.simple;
|
|
2
|
23
|
3 import java.util.UUID;
|
|
4 import java.util.concurrent.ConcurrentHashMap;
|
20
|
5 import treecms.proto.api.Node;
|
|
6 import treecms.proto.api.TreeBuilder;
|
|
7
|
|
8 public class SimpleTreeBuilder implements TreeBuilder
|
0
|
9 {
|
20
|
10 private Node m_root;
|
23
|
11
|
|
12 private ConcurrentHashMap<Thread,NodeUUID> m_idMap;
|
8
|
13
|
20
|
14 public SimpleTreeBuilder()
|
0
|
15 {
|
23
|
16 m_idMap = new ConcurrentHashMap<Thread,NodeUUID>();
|
20
|
17 m_root = new SimpleNode(generateUUID());
|
0
|
18 }
|
7
|
19
|
20
|
20 public Node getContents()
|
0
|
21 {
|
8
|
22 return m_root;
|
7
|
23 }
|
|
24
|
20
|
25 public Node createNode()
|
7
|
26 {
|
20
|
27 return new SimpleNode(generateUUID());
|
0
|
28 }
|
7
|
29
|
23
|
30 public String generateUUID()
|
|
31 {
|
|
32 NodeUUID uuid;
|
|
33 uuid = m_idMap.get(Thread.currentThread());
|
|
34 if(uuid == null){
|
|
35 uuid = new NodeUUID();
|
|
36 m_idMap.put(Thread.currentThread(),uuid);
|
|
37 }
|
|
38
|
|
39 return uuid.getAndIncrement();
|
|
40 }
|
|
41
|
|
42 private class NodeUUID
|
7
|
43 {
|
23
|
44 private UUID m_uuid;
|
|
45 private long m_counter;
|
|
46
|
|
47 public NodeUUID()
|
|
48 {
|
|
49 m_uuid = UUID.randomUUID();
|
|
50 m_counter = 0;
|
|
51 }
|
|
52
|
|
53 public String getAndIncrement()
|
|
54 {
|
|
55 String ret = m_uuid.toString() + "-" + m_counter;
|
|
56 m_counter++;
|
|
57 return ret;
|
|
58 }
|
7
|
59 }
|
0
|
60 }
|