Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 51:bc119c52d5cc
modified LoggingNode package
author | Shoshi TAMAKI |
---|---|
date | Fri, 08 Feb 2013 02:21:00 +0900 |
parents | 0a3bff2d9c89 |
children | 8c6ff361b68a |
files | src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/DefaultOperationLog.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingAttributes.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingChildren.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingNode.java src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/OperationLog.java |
diffstat | 5 files changed, 74 insertions(+), 135 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/DefaultOperationLog.java Fri Feb 08 01:51:04 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/DefaultOperationLog.java Fri Feb 08 02:21:00 2013 +0900 @@ -1,33 +1,35 @@ 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.store.operations.NodeOperation; import fj.data.List; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.Operation; public class DefaultOperationLog implements OperationLog { - private final List<Operation> log; + private final List<NodeOperation> log; - private static final List<Operation> EMPTY = List.nil(); + private static final List<NodeOperation> EMPTY = List.nil(); public DefaultOperationLog() { this(EMPTY); } - private DefaultOperationLog(List<Operation> _log) + private DefaultOperationLog(List<NodeOperation> _log) { log = _log; } @Override - public Iterator<Operation> iterator() + public Iterator<NodeOperation> iterator() { return log.iterator(); } + @Override - public DefaultOperationLog add(Operation _op) + public DefaultOperationLog add(NodeOperation _op) { return new DefaultOperationLog(log.snoc(_op)); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingAttributes.java Fri Feb 08 01:51:04 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingAttributes.java Fri Feb 08 02:21:00 2013 +0900 @@ -1,25 +1,21 @@ 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.impl.ClonableAttributes; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.ClonableNode; 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.operations.NodeOperation; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.PutAttributeOperation; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableAttributes; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer.EditableNode; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultEither; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; -public class LoggingAttributes<T extends ClonableNode<T>> - implements ClonableAttributes<LoggingNode<T>> +public class LoggingAttributes<T extends EditableNode<T>> implements EditableAttributes<LoggingNode<T>> { - private final EditableAttributes<T> wrap; - private final Logger log; + private final T wrap; + private final OperationLog log; - public LoggingAttributes(EditableAttributes<T> _wrap,Logger _log) + public LoggingAttributes(T _wrap,OperationLog _log) { wrap = _wrap; log = _log; @@ -28,55 +24,36 @@ @Override public ByteBuffer get(String _key) { - return wrap.get(_key); + EditableAttributes<T> attributes = wrap.getAttributes(); + return attributes.get(_key); + } + + private Either<Error,LoggingNode<T>> edit(NodeOperation _op) + { + Either<Error,T> either = _op.invoke(wrap); + if(either.isA()){ + return DefaultEither.newA(either.a()); + } + + T newWrap = either.b(); + OperationLog newLog = log.add(_op); + LoggingNode<T> newLoggingNode = new LoggingNode<T>(newWrap,newLog); + + return DefaultEither.newB(newLoggingNode); } @Override public Either<Error,LoggingNode<T>> delete(final String _key) { - Either<Error,T> either = wrap.delete(_key); - if(either.isA()){ - return DefaultEither.newA(either.a()); - } - - Operation opDelete = new DeleteAttributeOperation(){ - @Override - public String getKey(){ - return _key; - } - }; - - Logger newLog = log.add(opDelete); - T newWrap = either.b(); - LoggingNode<T> newLoggingNode = new LoggingNode<T>(newWrap,newLog); - - return DefaultEither.newB(newLoggingNode); + DeleteAttributeOperation deleteAttribute = new DeleteAttributeOperation(_key); + return edit(deleteAttribute); } @Override public Either<Error,LoggingNode<T>> put(final String _key,final ByteBuffer _value) { - Either<Error,T> either = wrap.put(_key,_value); - if(either.isA()){ - return DefaultEither.newA(either.a()); - } - - Operation opPut = new SetAttributeOperation(){ - @Override - public String getKey(){ - return _key; - } - - @Override - public ByteBuffer getValue(){ - return _value; - } - }; - - Logger newLog = log.add(opPut); - LoggingNode<T> newLoggingNode = new LoggingNode<T>(either.b(),newLog); - - return DefaultEither.newB(newLoggingNode); + PutAttributeOperation putAttribute = new PutAttributeOperation(_key,_value); + return edit(putAttribute); } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingChildren.java Fri Feb 08 01:51:04 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingChildren.java Fri Feb 08 02:21:00 2013 +0900 @@ -1,22 +1,22 @@ 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.store.impl.ClonableChildren; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.ClonableNode; 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.operations.NodeOperation; +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.DefaultEither; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.IterableConverter; -public class LoggingChildren<T extends ClonableNode<T>> implements ClonableChildren<LoggingNode<T>> +public class LoggingChildren<T extends EditableNode<T>> implements EditableChildren<LoggingNode<T>> { - private final ClonableChildren<T> wrap; - private final DefaultOperationLog log; + private final T wrap; + private final OperationLog log; - public LoggingChildren(ClonableChildren<T> _wrap,DefaultOperationLog _log) + public LoggingChildren(T _wrap,OperationLog _log) { wrap = _wrap; log = _log; @@ -25,12 +25,15 @@ @Override public int size() { - return wrap.size(); + EditableChildren<T> children = wrap.getChildren(); + return children.size(); } @Override public Iterator<LoggingNode<T>> iterator() { + EditableChildren<T> children = wrap.getChildren(); + IterableConverter.Converter<LoggingNode<T>,T> converter = new IterableConverter.Converter<LoggingNode<T>,T>(){ @Override public LoggingNode<T> conv(T _b) { @@ -38,70 +41,42 @@ } }; - IterableConverter<LoggingNode<T>,T> iterable = new IterableConverter<LoggingNode<T>,T>(wrap,converter); + IterableConverter<LoggingNode<T>,T> iterable = new IterableConverter<LoggingNode<T>,T>(children,converter); return iterable.iterator(); } + public Either<Error,LoggingNode<T>> edit(NodeOperation _op) + { + Either<Error,T> either = _op.invoke(wrap); + if(either.isA()){ + return DefaultEither.newA(either.a()); + } + + T newWrap = either.b(); + OperationLog newLog = log.add(_op); + LoggingNode<T> newLoggingNode = new LoggingNode<T>(newWrap,newLog); + return DefaultEither.newB(newLoggingNode); + } + @Override public Either<Error,LoggingNode<T>> addNewChildAt(final int _pos) { - Either<Error,T> either = wrap.addNewChildAt(_pos); - if(either.isA()){ - return DefaultEither.newA(either.a()); - } - - Operation opAddChild = new AppendChildAtOperation(){ - @Override - public int position(){ - return _pos; - } - }; - - Logger newLog = log.add(opAddChild); - T newWrap = either.b(); - LoggingNode<T> newLoggingNode = new LoggingNode<T>(newWrap,newLog); - - return DefaultEither.newB(newLoggingNode); + NodeOperation addNewChildAt = new AppendChildAtOperation(_pos); + return edit(addNewChildAt); } @Override public Either<Error,LoggingNode<T>> deleteChildAt(final int _pos) { - Either<Error,T> either = wrap.deleteChildAt(_pos); - if(either.isA()){ - return DefaultEither.newA(either.a()); - } - - Operation opDeleteChild = new DeleteChildAtOperation(){ - @Override - public int position(){ - return _pos; - } - }; - - Logger newLog = log.add(opDeleteChild); - LoggingNode<T> newLoggingNode = new LoggingNode<T>(either.b(),newLog); - - return DefaultEither.newB(newLoggingNode); - } - - @Override - public Either<Error,LoggingNode<T>> replaceNode(int _pos,LoggingNode<T> _replacement) - { - T target = _replacement.getWrapper(); - Either<Error,T> e = wrap.replaceNode(_pos,target); - if(e.isA()){ - return DefaultEither.newA(e.a()); - } - - T newWrap = e.b(); - return DefaultEither.newB(new LoggingNode<T>(newWrap,log)); + NodeOperation deleteChildAt = new DeleteChildAtOperation(_pos); + return edit(deleteChildAt); } @Override public Either<Error,LoggingNode<T>> at(int _pos) { - Either<Error,T> either = wrap.at(_pos); + EditableChildren<T> children = wrap.getChildren(); + Either<Error,T> either = children.at(_pos); if(either.isA()){ return DefaultEither.newA(either.a()); } @@ -109,17 +84,4 @@ T newWrap = either.b(); return DefaultEither.newB(new LoggingNode<T>(newWrap,log)); } - - @Override - public Either<Error, LoggingNode<T>> addNewChildAt(int _pos,LoggingNode<T> _newChild) - { - Either<Error,T> either = wrap.addNewChildAt(_pos,_newChild.getWrapper()); - if(either.isA()){ - return DefaultEither.newA(either.a()); - } - - T newWrap = either.b(); - LoggingNode<T> newLoggingNode = new LoggingNode<T>(newWrap,log); - return DefaultEither.newB(newLoggingNode); - } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingNode.java Fri Feb 08 01:51:04 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/LoggingNode.java Fri Feb 08 02:21:00 2013 +0900 @@ -1,8 +1,5 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.ClonableAttributes; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.ClonableChildren; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.ClonableNode; 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; @@ -14,28 +11,28 @@ this(_wrap,new DefaultOperationLog()); } - public LoggingNode(T _wrap,DefaultOperationLog _log) + public LoggingNode(T _wrap,OperationLog _log) { wrap = _wrap; log = _log; } private final T wrap; - private final DefaultOperationLog log; + private final OperationLog log; @Override public EditableAttributes<LoggingNode<T>> getAttributes() { - return new LoggingAttributes<T>(wrap.getAttributes(),log); + return new LoggingAttributes<T>(wrap,log); } @Override public EditableChildren<LoggingNode<T>> getChildren() { - return new LoggingChildren<T>(wrap.getChildren(),log); + return new LoggingChildren<T>(wrap,log); } - public DefaultOperationLog getLogger() + public OperationLog getLogger() { return log; }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/OperationLog.java Fri Feb 08 01:51:04 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/logger/OperationLog.java Fri Feb 08 02:21:00 2013 +0900 @@ -1,8 +1,9 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl.logger; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.Operation; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.operations.NodeOperation; + -public interface OperationLog extends Iterable<Operation> +public interface OperationLog extends Iterable<NodeOperation> { - public OperationLog add(Operation _op); + public OperationLog add(NodeOperation _op); }