Mercurial > hg > Members > tatsuki > bench > jungle-core
annotate src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChildren.java @ 21:848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
author | Shoshi TAMAKI |
---|---|
date | Sun, 06 Jan 2013 22:32:39 +0900 |
parents | 703f0be5368a |
children | 9fd57b7c6adb |
rev | line source |
---|---|
10 | 1 package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl; |
2 | |
3 import java.util.Iterator; | |
4 | |
5 import fj.P2; | |
21
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
6 import fj.data.Either; |
10 | 7 import fj.data.List; |
8 | |
9 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children; | |
10 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; | |
13 | 11 import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableWrapper; |
10 | 12 |
12 | 13 public class DefaultChildren implements Children |
10 | 14 { |
13 | 15 private final List<DefaultNode> children; |
16 private static final List<DefaultNode> EMPTY = List.nil(); | |
10 | 17 |
21
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
18 public enum Error{ |
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
19 INVALID_POSITION; |
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
20 } |
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
21 |
12 | 22 public DefaultChildren() |
10 | 23 { |
24 this(EMPTY); | |
25 } | |
26 | |
13 | 27 public DefaultChildren(List<DefaultNode> _children) |
10 | 28 { |
29 children = _children; | |
30 } | |
31 | |
19 | 32 @Override |
33 public int size() | |
34 { | |
35 return children.length(); | |
36 } | |
37 | |
13 | 38 public DefaultChildren add(DefaultNode _newNode) |
10 | 39 { |
12 | 40 return new DefaultChildren(children.snoc(_newNode)); |
10 | 41 } |
42 | |
21
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
43 public Either<Error,DefaultChildren> insert(DefaultNode _newNode,int _pos) |
10 | 44 { |
21
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
45 if(!check(_pos)){ |
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
46 return Either.left(Error.INVALID_POSITION); |
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
47 } |
10 | 48 |
13 | 49 P2<List<DefaultNode>,List<DefaultNode>> split = children.splitAt(_pos); |
50 List<DefaultNode> newChildren = split._1().snoc(_newNode).append(split._2()); | |
10 | 51 |
21
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
52 return Either.right(new DefaultChildren(newChildren)); |
10 | 53 } |
54 | |
21
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
55 public Either<Error,DefaultChildren> delete(int _pos) |
10 | 56 { |
21
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
57 if(!check(_pos)){ |
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
58 return Either.left(Error.INVALID_POSITION); |
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
59 } |
10 | 60 |
13 | 61 P2<List<DefaultNode>,List<DefaultNode>> split = children.splitAt(_pos); |
62 List<DefaultNode> newChildren = split._1().init().append(split._2()); | |
10 | 63 |
21
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
64 return Either.right(new DefaultChildren(newChildren)); |
10 | 65 } |
66 | |
21
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
67 public boolean check(int _pos) |
10 | 68 { |
69 if(children.length() < _pos){ | |
21
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
70 return false; |
10 | 71 } |
21
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
72 |
848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
Shoshi TAMAKI
parents:
19
diff
changeset
|
73 return true; |
10 | 74 } |
75 | |
76 @Override | |
77 public Iterator<Node> iterator() | |
78 { | |
13 | 79 return (new IterableWrapper<Node>(children)).iterator(); |
10 | 80 } |
81 } |