0
|
1 using UnityEngine;
|
|
2 using System.Collections.Generic;
|
|
3
|
|
4 public class DefaultJungleTreeEditor : JungleTreeEditor {
|
|
5
|
|
6 private TransactionManager txManager;
|
|
7 private TreeNode root;
|
|
8 private TreeEditor editor;
|
|
9 private TreeOperationLog log;
|
|
10
|
|
11 public DefaultJungleTreeEditor(TreeNode _root,TransactionManager _txManager,TreeEditor _editor)
|
|
12 : this(_root, _txManager, _editor, new DefaultTreeOperationLog())
|
|
13 {
|
|
14 }
|
|
15
|
|
16
|
|
17
|
7
|
18 public DefaultJungleTreeEditor(TreeNode newNode,TransactionManager txManager,TreeEditor editor,TreeOperationLog log)
|
0
|
19 {
|
|
20 this.root = newNode;
|
7
|
21 this.txManager = txManager;
|
|
22 this.editor = editor;
|
|
23 this.log = log;
|
0
|
24 }
|
|
25
|
|
26
|
|
27
|
|
28 private Either<Error,JungleTreeEditor> _edit(NodePath _path,NodeEditor _e)
|
|
29 {
|
|
30 Either<Error, LoggingNode> either = editor.edit (root, _path, _e);
|
|
31 if (either.isA ()) {
|
|
32 return DefaultEither<Error, JungleTreeEditor>.newA (either.a ());
|
|
33 }
|
|
34
|
|
35 LoggingNode newLogging = either.b ();
|
|
36 OperationLog newLog = newLogging.getOperationLog ();
|
|
37 TreeNode newNode = newLogging.getWrap ();
|
|
38
|
|
39 IterableConverter<TreeOperation,NodeOperation>.Converter<TreeOperation, NodeOperation> converter = new InnerConverter (_path);
|
|
40
|
|
41
|
|
42 IEnumerable<TreeOperation> iterable = new IterableConverter<TreeOperation, NodeOperation> (newLog, converter);
|
|
43 DefaultTreeOperationLog treeOperationLog = new DefaultTreeOperationLog (iterable, newLog.length ());
|
|
44 TreeOperationLog newTreeOpLog = log.append (treeOperationLog);
|
|
45
|
|
46 JungleTreeEditor newEditor = new DefaultJungleTreeEditor (newNode, txManager, editor, newTreeOpLog);
|
|
47 return DefaultEither<Error, JungleTreeEditor>.newB (newEditor);
|
|
48
|
|
49 }
|
|
50
|
|
51
|
|
52 public Either<Error, JungleTreeEditor> replaceNewRootNode() {
|
|
53 replaceRootNodeAt appendChildAt = new replaceRootNodeAt ();
|
|
54 return _edit (new DefaultNodePath(), appendChildAt);
|
|
55 }
|
|
56
|
|
57 public Either<Error, JungleTreeEditor> addNewChildAt(NodePath _path, int _pos) {
|
|
58 AppendChildAt appendChildAt = new AppendChildAt (_pos);
|
|
59 return _edit (_path, appendChildAt);
|
|
60 }
|
|
61
|
|
62 public Either<Error,JungleTreeEditor> deleteChildAt(NodePath _path, int _pos) {
|
|
63 DeleteChildAt deleteChildAt = new DeleteChildAt(_pos);
|
|
64 return _edit(_path,deleteChildAt);
|
|
65 }
|
|
66
|
7
|
67 public Either<Error, JungleTreeEditor> putAttribute(NodePath _path, string _key, byte[] _value) {
|
0
|
68 PutAttribute putAttribute = new PutAttribute (_key, _value);
|
|
69 return _edit (_path, putAttribute);
|
|
70 }
|
|
71
|
|
72 public Either<Error, JungleTreeEditor> deleteAttribute(NodePath _path, string _key) {
|
|
73 DeleteAttribute deleteAttribute = new DeleteAttribute (_key);
|
|
74 return _edit (_path, deleteAttribute);
|
|
75 }
|
|
76
|
|
77 public Either<Error,JungleTreeEditor> edit(NodePath _path,NodeEditor _editor) {
|
|
78 return _edit(_path,_editor);
|
|
79 }
|
|
80
|
7
|
81 /// <summary>
|
|
82 /// Treeを変更したあとSuccess(push)を行う
|
|
83 /// </summary>
|
|
84
|
0
|
85 public Either<Error,JungleTreeEditor> success() {
|
7
|
86 Either<Error,TransactionManager> either = this.txManager.commit(this.root, this.log);
|
|
87 // このlogをサーバにpushする?
|
0
|
88 if(either.isA()){
|
|
89 return DefaultEither<Error, JungleTreeEditor>.newA(either.a());
|
|
90 }
|
|
91
|
|
92 TransactionManager newTxManager = either.b();
|
7
|
93 JungleTreeEditor newTreeEditor = new DefaultJungleTreeEditor(this.root, newTxManager, this.editor);
|
0
|
94
|
|
95 return DefaultEither<Error, JungleTreeEditor>.newB(newTreeEditor);
|
|
96 }
|
|
97
|
|
98 public Either<Error, JungleTreeEditor> flushSuccess() {
|
|
99 return success();
|
|
100 }
|
|
101
|
|
102 public class InnerConverter : IterableConverter<TreeOperation,NodeOperation>.Converter<TreeOperation,NodeOperation>{
|
|
103
|
|
104 NodePath path;
|
|
105
|
|
106 public InnerConverter(NodePath _path) {
|
|
107 path = _path;
|
|
108 }
|
|
109
|
|
110
|
|
111 public TreeOperation conv(NodeOperation _b){
|
|
112 return new DefaultTreeOperation(path,_b);
|
|
113 }
|
|
114 }
|
|
115 }
|