Mercurial > hg > Members > shoshi > TreeCMSv2
annotate src/treecms/memory/OnMemoryTree.java @ 3:5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
author | shoshi |
---|---|
date | Fri, 18 Feb 2011 02:14:10 +0900 |
parents | 4a5ee88f02cf |
children | f5ed85be5640 |
rev | line source |
---|---|
2 | 1 package treecms.memory; |
2 | |
3 import java.util.LinkedList; | |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
4 import java.util.concurrent.ConcurrentHashMap; |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
5 |
2 | 6 import treecms.api.Forest; |
7 import treecms.api.Node; | |
8 import treecms.api.NodeData; | |
9 import treecms.api.NodeID; | |
10 import treecms.api.Tree; | |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
11 import treecms.tree.util.PreorderTreewalker; |
2 | 12 |
13 public class OnMemoryTree implements Tree | |
14 { | |
15 OnMemoryNode m_root; | |
16 OnMemoryForest m_forest; | |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
17 ConcurrentHashMap<String,OnMemoryNode> m_table; |
2 | 18 |
19 public OnMemoryTree(OnMemoryNode _newRoot,OnMemoryForest _forest) | |
20 { | |
21 m_root = _newRoot; | |
22 m_forest = _forest; | |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
23 |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
24 m_table = new ConcurrentHashMap<String,OnMemoryNode>(); |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
25 for(Node elem : new PreorderTreewalker(m_root)){ |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
26 m_table.put(elem.getID().getUUID(),(OnMemoryNode)elem); |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
27 } |
2 | 28 } |
29 | |
30 @Override | |
31 public Forest getForest() | |
32 { | |
33 return m_forest; | |
34 } | |
35 | |
36 @Override | |
37 public NodeID getID() | |
38 { | |
39 return m_root.getID(); | |
40 } | |
41 | |
42 @Override | |
43 public NodeData getData() | |
44 { | |
45 return m_root.getData(); | |
46 } | |
47 | |
48 @Override | |
49 public NodeData newData() | |
50 { | |
51 return m_root.newData(); | |
52 } | |
53 | |
54 @Override | |
55 public Node getNodeByUUID(String _uuid) | |
56 { | |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
57 return m_table.get(_uuid); |
2 | 58 } |
59 | |
60 @Override | |
61 public synchronized Node updateTree(Node _target,NodeData _newData) | |
62 { | |
63 LinkedList<OnMemoryNode> path = findAndClone(m_root,(OnMemoryNode)_target,_newData); | |
64 | |
65 if(path == null) | |
66 { | |
67 //not found. | |
68 return null; | |
69 } | |
70 | |
71 m_root = path.peekFirst(); | |
72 return path.peekLast(); | |
73 } | |
74 | |
75 OnMemoryNode cloneNode(OnMemoryNode _target,NodeData _newData) | |
76 { | |
77 OnMemoryNode clone = m_forest.createNode(_target.getID().update()); | |
78 | |
79 if(_newData != null){ | |
80 clone.m_data.add(_newData.list()); | |
81 clone.m_data.set(_newData.get()); | |
82 }else{ | |
83 clone.m_data.add(_target.m_data.list()); | |
84 clone.m_data.set(_target.m_data.get()); | |
85 } | |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
86 |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
87 m_table.put(clone.getID().getUUID(),clone); |
2 | 88 return clone; |
89 } | |
90 | |
91 LinkedList<OnMemoryNode> findAndClone(OnMemoryNode _parent,OnMemoryNode _target,NodeData _newData) | |
92 { | |
93 if(_parent.getID().isFamily(_target.getID())){ | |
94 //find. | |
95 LinkedList<OnMemoryNode> path = new LinkedList<OnMemoryNode>(); | |
96 OnMemoryNode clone = cloneNode((OnMemoryNode)_parent,_newData); | |
97 path.addFirst(clone); | |
98 return path; | |
99 } | |
100 | |
101 for(Node child : _parent.getData().list()){ | |
102 LinkedList<OnMemoryNode> path = findAndClone((OnMemoryNode)child,_target,_newData); | |
103 if(path != null){ | |
104 OnMemoryNode clone = cloneNode((OnMemoryNode)_parent,null); | |
105 clone.getData().list().remove(child); | |
106 clone.getData().list().add(path.peekFirst()); | |
107 path.addFirst(clone); | |
108 return path; | |
109 } | |
110 } | |
111 | |
112 return null; //not found. | |
113 } | |
114 | |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
115 @Override |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
116 public Node getRoot() |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
117 { |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
118 return m_root; |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
119 } |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
120 |
2 | 121 } |