annotate src/jungle/test/bbs/JungleManager.java @ 45:bf3dc481cc9b

modified JungleManager to singleton
author one
date Fri, 12 Jul 2013 09:47:22 +0900
parents 12d87bb4b84d
children ccfe9b5e8f11
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
39
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
1 package jungle.test.bbs;
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
2
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
3 import java.nio.ByteBuffer;
15
bf4a7f899c4e add some files
one
parents:
diff changeset
4
31
190f6a3bdab2 rename some packages
one
parents: 27
diff changeset
5 import alice.jungle.core.NetworkDefaultJungle;
20
506388c02b4b fix using edit method
one
parents: 18
diff changeset
6 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.DefaultJungle;
15
bf4a7f899c4e add some files
one
parents:
diff changeset
7 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.Jungle;
bf4a7f899c4e add some files
one
parents:
diff changeset
8 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTree;
39
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.JungleTreeEditor;
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command;
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.NodePath;
15
bf4a7f899c4e add some files
one
parents:
diff changeset
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.DefaultTreeEditor;
39
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger.TreeOperationLog;
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation;
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
15 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.TreeOperation;
15
bf4a7f899c4e add some files
one
parents:
diff changeset
16 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.traverser.DefaultTraverser;
39
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
17 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either;
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
18 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error;
15
bf4a7f899c4e add some files
one
parents:
diff changeset
19
bf4a7f899c4e add some files
one
parents:
diff changeset
20 public class JungleManager {
45
bf3dc481cc9b modified JungleManager to singleton
one
parents: 43
diff changeset
21 private static JungleManager jm = new JungleManager();
43
12d87bb4b84d modified LogUpdateCodeSegment
one
parents: 42
diff changeset
22 private Jungle jungle;
12d87bb4b84d modified LogUpdateCodeSegment
one
parents: 42
diff changeset
23
45
bf3dc481cc9b modified JungleManager to singleton
one
parents: 43
diff changeset
24 private JungleManager() {
15
bf4a7f899c4e add some files
one
parents:
diff changeset
25
bf4a7f899c4e add some files
one
parents:
diff changeset
26 }
bf4a7f899c4e add some files
one
parents:
diff changeset
27
45
bf3dc481cc9b modified JungleManager to singleton
one
parents: 43
diff changeset
28 public static void setJungle(Jungle _j) {
bf3dc481cc9b modified JungleManager to singleton
one
parents: 43
diff changeset
29 jm.jungle = _j;
15
bf4a7f899c4e add some files
one
parents:
diff changeset
30 }
bf4a7f899c4e add some files
one
parents:
diff changeset
31
43
12d87bb4b84d modified LogUpdateCodeSegment
one
parents: 42
diff changeset
32 public static Jungle getJungle() {
12d87bb4b84d modified LogUpdateCodeSegment
one
parents: 42
diff changeset
33 return jm.jungle;
12d87bb4b84d modified LogUpdateCodeSegment
one
parents: 42
diff changeset
34 }
12d87bb4b84d modified LogUpdateCodeSegment
one
parents: 42
diff changeset
35
12d87bb4b84d modified LogUpdateCodeSegment
one
parents: 42
diff changeset
36 public static JungleTree createNewTree(String name) {
12d87bb4b84d modified LogUpdateCodeSegment
one
parents: 42
diff changeset
37 return jm.jungle.createNewTree(name);
15
bf4a7f899c4e add some files
one
parents:
diff changeset
38 }
bf4a7f899c4e add some files
one
parents:
diff changeset
39
bf4a7f899c4e add some files
one
parents:
diff changeset
40
43
12d87bb4b84d modified LogUpdateCodeSegment
one
parents: 42
diff changeset
41 public static Either<Error, JungleTreeEditor> edit(JungleTreeEditor _editor ,TreeOperationLog _log) {
39
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
42 JungleTreeEditor editor = _editor;
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
43 Either<Error, JungleTreeEditor> either = null;
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
44 for (TreeOperation op : _log) {
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
45 either = _edit(editor, op);
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
46 if(either.isA()) {
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
47 return either;
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
48 }
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
49 editor = either.b();
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
50 }
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
51 return either;
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
52 }
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
53
43
12d87bb4b84d modified LogUpdateCodeSegment
one
parents: 42
diff changeset
54 private static Either<Error, JungleTreeEditor> _edit(JungleTreeEditor editor,
39
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
55 TreeOperation op) {
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
56 NodePath path = op.getNodePath();
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
57 NodeOperation nodeOp = op.getNodeOperation();
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
58 Command c = nodeOp.getCommand();
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
59 String key = "";
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
60 switch (c) {
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
61 case PUT_ATTRIBUTE:
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
62 key = nodeOp.getKey();
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
63 ByteBuffer value = nodeOp.getValue();
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
64 return editor.putAttribute(path, key, value);
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
65 case DELETE_ATTRIBUTE:
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
66 key = nodeOp.getKey();
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
67 return editor.deleteAttribute(path, key);
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
68 case APPEND_CHILD:
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
69 return editor.addNewChildAt(path, 0);
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
70 case DELETE_CHILD:
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
71 return editor.deleteChildAt(path, 0);
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
72 }
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
73 return null;
10359a815068 add alice.jungle.codesegment.remote
one
parents: 31
diff changeset
74 }
15
bf4a7f899c4e add some files
one
parents:
diff changeset
75 }