Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 21:848f73545c4d
add LoggingNode , LoggingAttributes , LoggingChildren , Logger
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChildren.java Thu Dec 20 18:40:55 2012 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChildren.java Sun Jan 06 22:32:39 2013 +0900 @@ -3,6 +3,7 @@ import java.util.Iterator; import fj.P2; +import fj.data.Either; import fj.data.List; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children; @@ -14,6 +15,10 @@ private final List<DefaultNode> children; private static final List<DefaultNode> EMPTY = List.nil(); + public enum Error{ + INVALID_POSITION; + } + public DefaultChildren() { this(EMPTY); @@ -35,31 +40,37 @@ return new DefaultChildren(children.snoc(_newNode)); } - public DefaultChildren insert(DefaultNode _newNode,int _pos) + public Either<Error,DefaultChildren> insert(DefaultNode _newNode,int _pos) { - check(_pos); + if(!check(_pos)){ + return Either.left(Error.INVALID_POSITION); + } P2<List<DefaultNode>,List<DefaultNode>> split = children.splitAt(_pos); List<DefaultNode> newChildren = split._1().snoc(_newNode).append(split._2()); - return new DefaultChildren(newChildren); + return Either.right(new DefaultChildren(newChildren)); } - public DefaultChildren delete(int _pos) + public Either<Error,DefaultChildren> delete(int _pos) { - check(_pos); + if(!check(_pos)){ + return Either.left(Error.INVALID_POSITION); + } P2<List<DefaultNode>,List<DefaultNode>> split = children.splitAt(_pos); List<DefaultNode> newChildren = split._1().init().append(split._2()); - return new DefaultChildren(newChildren); + return Either.right(new DefaultChildren(newChildren)); } - public void check(int _pos) + public boolean check(int _pos) { if(children.length() < _pos){ - throw new IllegalArgumentException("_pos > length"); + return false; } + + return true; } @Override
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultModifiedTreeEditor.java Thu Dec 20 18:40:55 2012 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultModifiedTreeEditor.java Sun Jan 06 22:32:39 2013 +0900 @@ -29,8 +29,7 @@ } @Override - public TreeEditResult putAttribute(NodePath _path, String _key, - ByteBuffer _value) { + public TreeEditResult putAttribute(NodePath _path, String _key,ByteBuffer _value) { // TODO Auto-generated method stub return null; }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultNode.java Thu Dec 20 18:40:55 2012 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultNode.java Sun Jan 06 22:32:39 2013 +0900 @@ -6,8 +6,8 @@ public class DefaultNode implements Node { - public DefaultAttributes attrs; - public DefaultChildren children; + public final DefaultAttributes attrs; + public final DefaultChildren children; public DefaultNode() { @@ -21,13 +21,13 @@ } @Override - public Children getChildren() + public DefaultChildren getChildren() { return children; } @Override - public Attributes getAttributes() + public DefaultAttributes getAttributes() { return attrs; }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTreeEditor.java Thu Dec 20 18:40:55 2012 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultTreeEditor.java Sun Jan 06 22:32:39 2013 +0900 @@ -1,5 +1,6 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl; +import java.util.LinkedList; import java.nio.ByteBuffer; import java.util.Iterator; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; @@ -87,14 +88,12 @@ public void cloneAndApplyToNodeEditorFromRoot(TraverseResult _r,NodeEditor _editor) { - Iterator<Node> nodes = _r.iterator(); + LinkedList<DefaultNode> path = new LinkedList<DefaultNode>(); - //root - if(!nodes.hasNext()){ - return; + for(Node n : _r){ + DefaultNode node = (DefaultNode)n; + path.add(node); } - - DefaultNode root = (DefaultNode)nodes.next(); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/DefaultLogger.java Sun Jan 06 22:32:39 2013 +0900 @@ -0,0 +1,39 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger; + +import java.util.Iterator; + +import fj.data.List; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.Operation; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode; + +public class DefaultLogger implements Logger +{ + private final EditableNode wrap; + private final List<Operation> log; + + private static final List<Operation> EMPTY = List.nil(); + + public DefaultLogger(EditableNode _wrap) + { + this(_wrap,EMPTY); + } + + private DefaultLogger(EditableNode _wrap,List<Operation> _log) + { + wrap = _wrap; + log = _log; + } + + @Override + public Iterator<Operation> iterator() + { + return log.iterator(); + } + + @Override + public Logger add(Operation _op) + { + return new DefaultLogger(wrap,log.cons(_op)); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/Logger.java Sun Jan 06 22:32:39 2013 +0900 @@ -0,0 +1,8 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.Operation; + +public interface Logger extends Iterable<Operation> +{ + public Logger add(Operation _op); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingAttributes.java Sun Jan 06 22:32:39 2013 +0900 @@ -0,0 +1,64 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger; + +import java.nio.ByteBuffer; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DeleteAttributeOperation; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.Operation; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.SetAttributeOperation; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableAttributes; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode; + +public class LoggingAttributes implements EditableAttributes +{ + private final EditableAttributes wrap; + private final Logger log; + + public LoggingAttributes(EditableAttributes _wrap,Logger _log) + { + wrap = _wrap; + log = _log; + } + + @Override + public ByteBuffer get(String _key) + { + return wrap.get(_key); + } + + @Override + public EditableNode delete(final String _key) + { + Operation opDelete = new DeleteAttributeOperation(){ + @Override + public String getKey(){ + return _key; + } + }; + + Logger newLog = log.add(opDelete); + EditableNode newWrap = wrap.delete(_key); + + return new LoggingNode(newWrap,newLog); + } + + @Override + public EditableNode put(final String _key,final ByteBuffer _value) + { + Operation opPut = new SetAttributeOperation(){ + @Override + public String getKey(){ + return _key; + } + + @Override + public ByteBuffer getValue(){ + return _value; + } + }; + + Logger newLog = log.add(opPut); + EditableNode newWrap = wrap.put(_key,_value); + + return new LoggingNode(newWrap,newLog); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingChildren.java Sun Jan 06 22:32:39 2013 +0900 @@ -0,0 +1,68 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger; + +import java.util.Iterator; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.AppendChildAtOperation; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.DeleteChildAtOperation; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.Operation; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableChildren; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableWrapper; + +public class LoggingChildren implements EditableChildren +{ + private final EditableChildren wrap; + private final Logger log; + + public LoggingChildren(EditableChildren _wrap,Logger _log) + { + wrap = _wrap; + log = _log; + } + + @Override + public int size() + { + return wrap.size(); + } + + @Override + public Iterator<Node> iterator() + { + IterableWrapper<Node> wrapper = new IterableWrapper<Node>(this); + return wrapper.iterator(); + } + + @Override + public EditableNode addNewChildAt(final int _pos) + { + Operation opAddChild = new AppendChildAtOperation(){ + @Override + public int position(){ + return _pos; + } + }; + + Logger newLog = log.add(opAddChild); + EditableNode newWrap = wrap.addNewChildAt(_pos); + + return new LoggingNode(newWrap,newLog); + } + + @Override + public EditableNode deleteChildAt(final int _pos) + { + Operation opDeleteChild = new DeleteChildAtOperation(){ + @Override + public int position(){ + return _pos; + } + }; + + Logger newLog = log.add(opDeleteChild); + EditableNode newWrap = wrap.deleteChildAt(_pos); + + return new LoggingNode(newWrap,newLog); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingNode.java Sun Jan 06 22:32:39 2013 +0900 @@ -0,0 +1,34 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableAttributes; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableChildren; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode; + +public class LoggingNode implements EditableNode +{ + public LoggingNode(EditableNode _wrap) + { + this(_wrap,new DefaultLogger(_wrap)); + } + + public LoggingNode(EditableNode _wrap,Logger _log) + { + wrap = _wrap; + log = _log; + } + + private final EditableNode wrap; + private final Logger log; + + @Override + public EditableAttributes getAttributes() + { + return new LoggingAttributes(wrap.getAttributes(),log); + } + + @Override + public EditableChildren getChildren() + { + return new LoggingChildren(wrap.getChildren(),log); + } +}