Mercurial > hg > Members > tatsuki > bench > jungle-core
annotate src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/transaction/DefaultTreeNodeChildren.java @ 146:371b6ddb78f2
repair putAttributeIndex and deleteAttributeIndex
author | one |
---|---|
date | Fri, 21 Nov 2014 12:46:06 +0900 |
parents | ef183969bf31 |
children | a2c374a2686b |
rev | line source |
---|---|
37 | 1 package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.transaction; |
2 | |
3 import java.nio.ByteBuffer; | |
4 import java.util.Iterator; | |
71 | 5 |
37 | 6 import fj.P2; |
7 import fj.data.List; | |
8 import fj.data.TreeMap; | |
81 | 9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNode; |
52 | 10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.TreeNodeChildren; |
37 | 11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.NodeEditorError; |
12 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; | |
13 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; | |
14 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; | |
15 | |
86 | 16 public class DefaultTreeNodeChildren implements TreeNodeChildren |
37 | 17 { |
39 | 18 public static void main(String _args[]) |
19 { | |
20 List<Integer> list = List.range(0,5); | |
41 | 21 P2<List<Integer>, List<Integer>> split = list.splitAt(0); |
39 | 22 System.out.println(split._1().length()); |
23 System.out.println(split._2().length()); | |
24 | |
25 } | |
26 | |
81 | 27 public List<TreeNode> children; |
71 | 28 public TreeMap<String,ByteBuffer> attrs; |
87 | 29 |
81 | 30 public DefaultTreeNodeChildren(List<TreeNode> _children,TreeMap<String,ByteBuffer> _attrs) |
37 | 31 { |
71 | 32 children = _children; |
33 attrs = _attrs; | |
37 | 34 } |
35 | |
36 private boolean boundaryCheck(int _pos) | |
37 { | |
71 | 38 int size = children.length(); |
39 | 39 if(size < _pos){ |
37 | 40 return false; |
41 } | |
42 | |
43 return true; | |
44 } | |
45 | |
46 @Override | |
81 | 47 public List<TreeNode> getChildrenAsRawList(){ |
75
26dfa90016d1
error delete but 2 test program commentout and not action BulletinBoard
one
parents:
74
diff
changeset
|
48 return children; |
26dfa90016d1
error delete but 2 test program commentout and not action BulletinBoard
one
parents:
74
diff
changeset
|
49 } |
26dfa90016d1
error delete but 2 test program commentout and not action BulletinBoard
one
parents:
74
diff
changeset
|
50 @Override |
81 | 51 public Either<Error,TreeNode> addNewChildAt(int _pos) |
37 | 52 { |
39 | 53 if(!boundaryCheck(_pos) || _pos < 0){ |
37 | 54 return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); |
55 } | |
56 | |
81 | 57 P2<List<TreeNode>,List<TreeNode>> split = children.splitAt(_pos); |
58 List<TreeNode> newChildren = split._1().snoc(new DefaultTreeNode()).append(split._2()); | |
97 | 59 TreeNode newNode = new DefaultTreeNode(newChildren,attrs); |
71 | 60 return DefaultEither.newB(newNode); |
37 | 61 } |
62 | |
63 @Override | |
81 | 64 public Either<Error,TreeNode> deleteChildAt(int _pos) |
37 | 65 { |
39 | 66 if(!boundaryCheck(_pos) || _pos < 0 || size() == 0){ |
37 | 67 return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); |
68 } | |
71 | 69 |
81 | 70 P2<List<TreeNode>,List<TreeNode>> split = children.splitAt(_pos); |
71 List<TreeNode> newChildren = split._1().append(split._2().tail()); | |
97 | 72 TreeNode newNode = new DefaultTreeNode(newChildren,attrs); |
37 | 73 |
71 | 74 return DefaultEither.newB(newNode); |
37 | 75 } |
76 | |
77 @Override | |
78 public int size() | |
79 { | |
71 | 80 return children.length(); |
37 | 81 } |
82 | |
83 @Override | |
81 | 84 public Iterator<TreeNode> iterator() |
37 | 85 { |
71 | 86 return children.iterator(); |
37 | 87 } |
88 | |
89 @Override | |
81 | 90 public Either<Error,TreeNode> replaceNode(int _pos,TreeNode _replacement) |
37 | 91 { |
71 | 92 int size = children.length(); |
41 | 93 if(!(0 <= _pos && _pos < size)){ |
37 | 94 return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); |
95 } | |
81 | 96 TreeNode replacement = _replacement; |
37 | 97 |
81 | 98 P2<List<TreeNode>,List<TreeNode>> split = children.splitAt(_pos + 1); |
99 List<TreeNode> init = split._1().reverse().tail().reverse(); | |
100 List<TreeNode> newInit = init.snoc(replacement); | |
101 List<TreeNode> newList = newInit.append(split._2()); | |
102 TreeNode node = new DefaultTreeNode(newList,attrs); | |
103 return DefaultEither.newB(node); | |
37 | 104 } |
41 | 105 |
106 @Override | |
81 | 107 public Either<Error,TreeNode> at(int _pos) |
41 | 108 { |
142 | 109 System.out.println(children.length()); |
110 if(children.length() < _pos + 1){ | |
111 return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); | |
112 } | |
113 | |
114 TreeNode Node = children.index(_pos); | |
41 | 115 |
71 | 116 return DefaultEither.newB(Node); |
41 | 117 } |
118 | |
119 @Override | |
81 | 120 public Either<Error,TreeNode> addNewChildAt(int _pos,TreeNode _newChild) |
41 | 121 { |
122 if(!boundaryCheck(_pos) || _pos < 0){ | |
123 return DefaultEither.newA(NodeEditorError.INDEX_OUT_OF_BOUNDS); | |
124 } | |
125 | |
126 | |
81 | 127 P2<List<TreeNode>,List<TreeNode>> split = children.splitAt(_pos); |
128 List<TreeNode> newChildren = split._1().snoc(_newChild).append(split._2()); | |
129 TreeNode newNode = new DefaultTreeNode(newChildren,attrs); | |
41 | 130 |
71 | 131 return DefaultEither.newB(newNode); |
41 | 132 } |
37 | 133 } |