Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 24:ec033569ab8f
commit
line wrap: on
line diff
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultAttributes.java Thu Jan 10 23:22:42 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultAttributes.java Fri Jan 11 22:33:00 2013 +0900 @@ -2,37 +2,34 @@ import java.nio.ByteBuffer; -import fj.Ord; +import fj.data.List; import fj.data.Option; import fj.data.TreeMap; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Attributes; 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 DefaultAttributes implements Attributes +public class DefaultAttributes implements EditableAttributes { private final TreeMap<String,ByteBuffer> attrs; - private static final TreeMap<String,ByteBuffer> EMPTY = TreeMap.empty(Ord.stringOrd); + private final List<DefaultNode> children; - public DefaultAttributes() + public DefaultAttributes(List<DefaultNode> _children,TreeMap<String,ByteBuffer> _attrs) { - this(EMPTY); - } - - private DefaultAttributes(TreeMap<String,ByteBuffer> _attrs) - { + children = _children; attrs = _attrs; } - public DefaultAttributes remove(String _key) + @Override + public Either<Error,EditableNode> put(String _key,ByteBuffer _value) { - return new DefaultAttributes(attrs.delete(_key)); - } - - public DefaultAttributes put(String _key,ByteBuffer _value) - { - return new DefaultAttributes(attrs.set(_key,_value)); + TreeMap<String,ByteBuffer> newAttrs = attrs.set(_key,_value); + EditableNode newNode = new DefaultNode(children,newAttrs); + + return DefaultEither.newB(newNode); } @Override @@ -42,4 +39,10 @@ return result.isSome() ? result.some() : null; } + @Override + public Either<Error,EditableNode> delete(String _key) + { + return null; + } + }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChildren.java Thu Jan 10 23:22:42 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/impl/DefaultChildren.java Fri Jan 11 22:33:00 2013 +0900 @@ -1,30 +1,27 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.impl; +import java.nio.ByteBuffer; 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; +import fj.data.TreeMap; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; 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.IterableWrapper; public class DefaultChildren implements EditableChildren { private final List<DefaultNode> children; - private static final List<DefaultNode> EMPTY = List.nil(); + private final TreeMap<String,ByteBuffer> attrs; - public DefaultChildren() - { - this(EMPTY); - } - - public DefaultChildren(List<DefaultNode> _children) + public DefaultChildren(List<DefaultNode> _children,TreeMap<String,ByteBuffer> _attrs) { children = _children; + attrs = _attrs; } @Override @@ -33,33 +30,33 @@ return children.length(); } - public DefaultChildren add(DefaultNode _newNode) - { - return new DefaultChildren(children.snoc(_newNode)); - } - - public Either<Error,DefaultChildren> insert(DefaultNode _newNode,int _pos) + @Override + public Either<Error,EditableNode> addNewChildAt(int _pos) { if(!check(_pos)){ - return Either.left(Error.INVALID_POSITION); + return DefaultEither.newA(OUT_OF_RANGE); } P2<List<DefaultNode>,List<DefaultNode>> split = children.splitAt(_pos); - List<DefaultNode> newChildren = split._1().snoc(_newNode).append(split._2()); + List<DefaultNode> newChildren = split._1().snoc(new DefaultNode()).append(split._2()); + EditableNode newNode = new DefaultNode(newChildren,attrs); - return Either.right(new DefaultChildren(newChildren)); + return DefaultEither.newB(newNode); } - public Either<Error,DefaultChildren> delete(int _pos) + @Override + public Either<Error,EditableNode> deleteChildAt(int _pos) { if(!check(_pos)){ - return Either.left(Error.INVALID_POSITION); + return DefaultEither.newA(OUT_OF_RANGE); } P2<List<DefaultNode>,List<DefaultNode>> split = children.splitAt(_pos); List<DefaultNode> newChildren = split._1().init().append(split._2()); - return Either.right(new DefaultChildren(newChildren)); + EditableNode newNode = new DefaultNode(newChildren,attrs); + + return DefaultEither.newB(newNode); } public boolean check(int _pos) @@ -76,17 +73,4 @@ { return (new IterableWrapper<Node>(children)).iterator(); } - - @Override - public Either<Error,EditableNode> addNewChildAt(int _pos) - { - - return null; - } - - @Override - public EditableNode deleteChildAt(int _pos) - { - return null; - } }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/AppendChildAt.java Thu Jan 10 23:22:42 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/AppendChildAt.java Fri Jan 11 22:33:00 2013 +0900 @@ -2,6 +2,7 @@ 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 AppendChildAt implements NodeEditor { @@ -18,7 +19,7 @@ Either<Error,EditableNode> either = _e.getChildren().addNewChildAt(pos); if(either.isA()){ // error - + return either; } return DefaultEither.newB(either.b());
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/DefaultEditableAttributes.java Thu Jan 10 23:22:42 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer; - -import java.nio.ByteBuffer; - -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Attributes; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command; -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; - -public class DefaultEditableAttributes implements EditableAttributes -{ - private Node target; - private Attributes attrs; - private EditCache cache; - - public DefaultEditableAttributes(Node _target,EditCache _cache) - { - target = _target; - attrs = _target.getAttributes(); - cache = _cache; - } - - @Override - public ByteBuffer get(String _key) - { - Operation op = cache.getAttribute(_key); - - if(op == null){ - return attrs.get(_key); - } - - Command c = op.getCommand(); - - if(c == Command.SET_ATTRIBUTE){ - SetAttributeOperation saop = (SetAttributeOperation)op; - String key = saop.getKey(); - if(key.equals(_key)){ - return saop.getValue(); - } - throw new IllegalStateException("fail key.equals(_key)"); - }else if(c == Command.DELETE_ATTRIBUTE){ - DeleteAttributeOperation daop = (DeleteAttributeOperation)op; - String key = daop.getKey(); - if(key.equals(_key)){ - return null; - } - throw new IllegalStateException("fail key.equals(_key)"); - } - - throw new IllegalStateException("fail unknown command" + c); - } - - @Override - public EditableNode delete(String _key) - { - EditCache newCache = cache.deleteAttribute(_key); - return new DefaultEditableNode(target,newCache); - } - - @Override - public EditableNode put(String _key, ByteBuffer _value) - { - EditCache newCache = cache.setAttribute(_key,_value); - return new DefaultEditableNode(target,newCache); - } -}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/DefaultEditableChildren.java Thu Jan 10 23:22:42 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer; - -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.Operation; -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; - -public class DefaultEditableChildren implements EditableChildren -{ - private final Node target; - private final EditCache cache; - - public DefaultEditableChildren(Node _target,EditCache _cache) - { - target = _target; - cache = _cache; - } - - @Override - public int size() - { - return 0; - } - - @Override - public Iterator<Node> iterator() - { - return null; - } - - @Override - public Either<Error,EditableNode> addNewChildAt(final int _pos) - { - AppendChildAtOperation op = new AppendChildAtOperation(){ - @Override - public int position(){ - return _pos; - } - }; - - return null; - } - - @Override - public Either<Error,EditableNode> deleteChildAt(int _pos) - { - return null; - } -} \ No newline at end of file
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/DefaultEditableNode.java Thu Jan 10 23:22:42 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer; - -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Node; - -public class DefaultEditableNode implements EditableNode -{ - private final Node target; - private final EditCache cache; - - public DefaultEditableNode(Node _target) - { - this(_target,new EditCache()); - } - - public DefaultEditableNode(Node _target,EditCache _cache) - { - target = _target; - cache = _cache; - } - - @Override - public EditableAttributes getAttributes() - { - return new DefaultEditableAttributes(target,cache); - } - - @Override - public EditableChildren getChildren() - { - return new DefaultEditableChildren(target,cache); - } -}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/DefaultNodeEditResult.java Thu Jan 10 23:22:42 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer; - -public class DefaultNodeEditResult implements NodeEditResult -{ - private final NodeEditResultStatus status; - private final EditableNode node; - - public DefaultNodeEditResult(NodeEditResultStatus _status,EditableNode _node) - { - status = _status; - node = _node; - } - - @Override - public EditableNode getEditableNode() - { - return node; - } - - @Override - public NodeEditResultStatus getStatus() - { - return status; - } -}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/DeleteAttribute.java Thu Jan 10 23:22:42 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/DeleteAttribute.java Fri Jan 11 22:33:00 2013 +0900 @@ -1,6 +1,7 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; public class DeleteAttribute implements NodeEditor {
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/DeleteChildAt.java Thu Jan 10 23:22:42 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/DeleteChildAt.java Fri Jan 11 22:33:00 2013 +0900 @@ -1,6 +1,7 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; public class DeleteChildAt implements NodeEditor {
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/EditableAttributes.java Thu Jan 10 23:22:42 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/EditableAttributes.java Fri Jan 11 22:33:00 2013 +0900 @@ -1,12 +1,15 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer; import java.nio.ByteBuffer; - import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Attributes; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultError; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; public interface EditableAttributes extends Attributes { public Either<Error,EditableNode> delete(String _key); public Either<Error,EditableNode> put(String _key,ByteBuffer _value); + + public static final Error KEY_NOT_FOUND = new DefaultError(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/EditableChildren.java Thu Jan 10 23:22:42 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/EditableChildren.java Fri Jan 11 22:33:00 2013 +0900 @@ -1,10 +1,14 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.core.Children; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.DefaultError; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; public interface EditableChildren extends Children { public Either<Error,EditableNode> addNewChildAt(int _pos); public Either<Error,EditableNode> deleteChildAt(int _pos); + + public final Error OUT_OF_RANGE = new DefaultError(); }
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/NodeEditResult.java Thu Jan 10 23:22:42 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer; - -public interface NodeEditResult -{ - EditableNode getEditableNode(); - NodeEditResultStatus getStatus(); -}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/NodeEditor.java Thu Jan 10 23:22:42 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/NodeEditor.java Fri Jan 11 22:33:00 2013 +0900 @@ -1,6 +1,7 @@ package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer; import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; public interface NodeEditor {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/PutAttribute.java Fri Jan 11 22:33:00 2013 +0900 @@ -0,0 +1,25 @@ +package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer; + +import java.nio.ByteBuffer; + +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Error; + +public class PutAttribute implements NodeEditor +{ + private final String key; + private final ByteBuffer value; + + public PutAttribute(String _key,ByteBuffer _value) + { + key = _key; + value = _value; + } + + @Override + public Either<Error,EditableNode> edit(EditableNode _e) + { + EditableAttributes attrs = _e.getAttributes(); + return attrs.put(key,value); + } +}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/store/trasnformer/SetAttribute.java Thu Jan 10 23:22:42 2013 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -package jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.trasnformer; - -import java.nio.ByteBuffer; - -import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.util.Either; - -public class SetAttribute implements NodeEditor -{ - private final String key; - private final ByteBuffer value; - - public SetAttribute(String _key,ByteBuffer _value) - { - key = _key; - value = _value; - } - - @Override - public Either<Error,EditableNode> edit(EditableNode _e) - { - EditableAttributes attrs = _e.getAttributes(); - return attrs.put(key,value); - } -}
--- a/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraversal.java Thu Jan 10 23:22:42 2013 +0900 +++ b/src/main/java/jp/ac/u_ryukyu/ie/cr/shoshi/jungle/traverser/DefaultTraversal.java Fri Jan 11 22:33:00 2013 +0900 @@ -6,7 +6,6 @@ public class DefaultTraversal implements Traversal { - @Override public Iterator<Node> iterator() { @@ -18,5 +17,4 @@ { return null; } - }