Mercurial > hg > Members > shoshi > TreeCMSv2
annotate src/treecms/memory/OnMemoryTreeEditor.java @ 6:12604eb6b615
added javadoc
author | shoshi |
---|---|
date | Mon, 14 Mar 2011 23:24:38 +0900 |
parents | 5fa718b63cd5 |
children | fc19e38b669b |
rev | line source |
---|---|
2 | 1 package treecms.memory; |
2 | |
6 | 3 import java.util.LinkedList; |
4 | |
5 import treecms.api.Node; | |
6 import treecms.api.NodeData; | |
2 | 7 import treecms.api.TreeEditor; |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
8 import treecms.merger.Merger; |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
9 import treecms.merger.ReplaceMerger; |
2 | 10 |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
11 public class OnMemoryTreeEditor extends OnMemoryTree implements TreeEditor |
2 | 12 { |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
13 OnMemoryTree m_tree; |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
14 OnMemoryNode m_oldRoot; |
2 | 15 |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
16 public OnMemoryTreeEditor(OnMemoryForest _forest,OnMemoryTree _tree) |
2 | 17 { |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
18 super(_tree.m_root,_forest); |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
19 m_oldRoot = m_root; |
2 | 20 } |
21 | |
22 @Override | |
23 public boolean commit(boolean _force) | |
24 { | |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
25 if(!check() || _force){ |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
26 m_tree.m_root = m_root; |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
27 } |
2 | 28 return false; |
29 } | |
30 | |
31 @Override | |
32 public boolean pull() | |
33 { | |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
34 m_root = m_tree.m_root; |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
35 return true; |
2 | 36 } |
37 | |
38 @Override | |
39 public boolean check() | |
40 { | |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
41 if(m_tree.m_root.getID().equals(m_oldRoot.getID())){ |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
42 return false; |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
43 } |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
44 return true; |
2 | 45 } |
46 | |
47 @Override | |
48 public void merge() | |
49 { | |
3
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
50 //call merger |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
51 Merger merger = new ReplaceMerger(); |
5fa718b63cd5
finished treecms.memory basic implementation ( not tested yet. )
shoshi
parents:
2
diff
changeset
|
52 m_root = (OnMemoryNode)merger.merge(m_tree.m_root,m_root); |
2 | 53 } |
6 | 54 |
55 @Override | |
56 public synchronized Node updateTree(Node _target,NodeData _newData) | |
57 { | |
58 LinkedList<OnMemoryNode> path = findAndClone(m_root,(OnMemoryNode)_target,_newData); | |
59 | |
60 if(path == null) | |
61 { | |
62 //not found. | |
63 return null; | |
64 } | |
65 | |
66 m_root = path.peekFirst(); | |
67 return path.peekLast(); | |
68 } | |
69 | |
70 OnMemoryNode cloneNode(OnMemoryNode _target,NodeData _newData) | |
71 { | |
72 OnMemoryNode clone = m_forest.createNode(_target.getID().update(),_newData); | |
73 m_table.put(clone.getID().getUUID(),clone); | |
74 return clone; | |
75 } | |
76 | |
77 LinkedList<OnMemoryNode> findAndClone(OnMemoryNode _parent,OnMemoryNode _target,NodeData _newData) | |
78 { | |
79 if(_parent.getID().isFamily(_target.getID())){ | |
80 //find. | |
81 LinkedList<OnMemoryNode> path = new LinkedList<OnMemoryNode>(); | |
82 OnMemoryNode clone = cloneNode((OnMemoryNode)_parent,_newData); | |
83 path.addFirst(clone); | |
84 return path; | |
85 } | |
86 | |
87 for(Node child : _parent.getData().list()){ | |
88 LinkedList<OnMemoryNode> path = findAndClone((OnMemoryNode)child,_target,_newData); | |
89 if(path != null){ | |
90 OnMemoryNode clone = cloneNode((OnMemoryNode)_parent,null); | |
91 clone.getData().list().remove(child); | |
92 clone.getData().list().add(path.peekFirst()); | |
93 path.addFirst(clone); | |
94 return path; | |
95 } | |
96 } | |
97 | |
98 return null; //not found. | |
99 } | |
100 | |
101 @Override | |
102 public Node getRoot() | |
103 { | |
104 return m_root; | |
105 } | |
2 | 106 } |