Mercurial > hg > Members > shoshi > jungle > jungle-core
changeset 7:c3c65308a11b
removed some package and added Graph API
author | shoshi <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 29 Jun 2012 00:03:12 +0900 |
parents | 1a5eaf5ce085 |
children | abed5bd92fcb |
files | memo.txt src/main/java/jungle/core/Children.java src/main/java/jungle/core/Jungle.java src/main/java/jungle/core/Links.java src/main/java/jungle/core/OrderedNodeSet.java src/main/java/jungle/core/TreeNode.java src/main/java/jungle/core/graph/Vertex.java src/main/java/jungle/core/graph/simple/SimpleVertex.java src/main/java/jungle/core/table/PropertySequence.java src/main/java/jungle/core/table/Record.java src/main/java/jungle/core/table/Table.java src/main/java/jungle/core/table/simple/SimplePropertySequence.java src/main/java/jungle/core/table/simple/SimpleRecord.java src/main/java/jungle/core/table/simple/SimpleTable.java src/main/java/jungle/impl/SimpleChildren.java src/main/java/jungle/impl/SimpleEditor.java src/main/java/jungle/impl/SimpleJungle.java src/main/java/jungle/impl/SimpleLink.java src/main/java/jungle/impl/SimpleLinks.java src/main/java/jungle/impl/SimpleOrderedNodeSet.java src/main/java/jungle/impl/SimpleTree.java src/main/java/jungle/impl/SimpleTreeGroup.java src/main/java/jungle/impl/SimpleTreeNode.java src/main/java/jungle/impl/Simples.java src/test/java/jungle/core/table/AbstractPropertySequenceTestTemplate.java src/test/java/jungle/core/table/AbstractRecordTestTemplate.java src/test/java/jungle/core/table/AbstractTableTestTemplate.java src/test/java/jungle/core/table/simple/SimplePropertySequenceTest.java src/test/java/jungle/core/table/simple/SimpleRecordTest.java src/test/java/jungle/core/table/simple/SimpleTableTest.java src/test/java/jungle/misc/fj/XMLNodeTest.java |
diffstat | 31 files changed, 357 insertions(+), 925 deletions(-) [+] |
line wrap: on
line diff
--- a/memo.txt Mon Jun 25 23:48:53 2012 +0900 +++ b/memo.txt Fri Jun 29 00:03:12 2012 +0900 @@ -50,4 +50,7 @@ 2012/06/25 ・Vertexes のテストコードが動いた. ・SimpleJungle などを GraphAPI を用いて記述する. - ・SimpleTreeGroup , SimpleTree , SimpleNode をどうするか \ No newline at end of file + ・SimpleTreeGroup , SimpleTree , SimpleNode をどうするか + +2012/06/27 + ・Graph を用いた SimpleJungle の設計をもっと考える \ No newline at end of file
--- a/src/main/java/jungle/core/Children.java Mon Jun 25 23:48:53 2012 +0900 +++ b/src/main/java/jungle/core/Children.java Fri Jun 29 00:03:12 2012 +0900 @@ -1,6 +1,8 @@ package jungle.core; -public interface Children extends OrderedNodeSet<TreeNode> +public interface Children extends Iterable<TreeNode> { public TreeNode getAt(int _pos); + public boolean contains(TreeNode n); + public int size(); }
--- a/src/main/java/jungle/core/Jungle.java Mon Jun 25 23:48:53 2012 +0900 +++ b/src/main/java/jungle/core/Jungle.java Fri Jun 29 00:03:12 2012 +0900 @@ -2,5 +2,7 @@ public interface Jungle { - TreeGroup createTreeGroup(); + public TreeGroup createTreeGroup(); + public TreeGroup createTreeGroup(String _id); + public TreeGroup getTreeGroupByID(String _id); }
--- a/src/main/java/jungle/core/Links.java Mon Jun 25 23:48:53 2012 +0900 +++ b/src/main/java/jungle/core/Links.java Fri Jun 29 00:03:12 2012 +0900 @@ -1,6 +1,8 @@ package jungle.core; -public interface Links extends OrderedNodeSet<Link> +public interface Links extends Iterable<Link> { public Link getAt(int _pos); + public boolean contains(Link n); + public int size(); }
--- a/src/main/java/jungle/core/OrderedNodeSet.java Mon Jun 25 23:48:53 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ -package jungle.core; - -public interface OrderedNodeSet<T extends Node> extends Iterable<T> -{ - public boolean contains(T n); - public T getAt(int pos); - public int size(); -}
--- a/src/main/java/jungle/core/TreeNode.java Mon Jun 25 23:48:53 2012 +0900 +++ b/src/main/java/jungle/core/TreeNode.java Fri Jun 29 00:03:12 2012 +0900 @@ -4,5 +4,4 @@ { Children children(); Links links(); - String cid(); }
--- a/src/main/java/jungle/core/graph/Vertex.java Mon Jun 25 23:48:53 2012 +0900 +++ b/src/main/java/jungle/core/graph/Vertex.java Fri Jun 29 00:03:12 2012 +0900 @@ -7,6 +7,7 @@ public String getProperty(String _key); public void setProperty(String _key,String _value); + public String setPropertyIfAbsent(String _key,String _value); public String removeProperty(String _key); public boolean compareAndSwapProprety(String _key,String _except,String _value);
--- a/src/main/java/jungle/core/graph/simple/SimpleVertex.java Mon Jun 25 23:48:53 2012 +0900 +++ b/src/main/java/jungle/core/graph/simple/SimpleVertex.java Fri Jun 29 00:03:12 2012 +0900 @@ -27,6 +27,13 @@ } @Override + public String setPropertyIfAbsent(String _key,String _value) + { + String previousValue = properties.putIfAbsent(_key,_value); + return previousValue; + } + + @Override public String toString() { return id;
--- a/src/main/java/jungle/core/table/PropertySequence.java Mon Jun 25 23:48:53 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -package jungle.core.table; - -public interface PropertySequence extends Iterable<String> -{ - public void add(String _value); - public String get(int _pos); - public String remove(int _pos); - public boolean isSameSequence(PropertySequence _seq); - public int size(); -}
--- a/src/main/java/jungle/core/table/Record.java Mon Jun 25 23:48:53 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -package jungle.core.table; - -import java.util.Iterator; - -import jungle.util.Pair; - -public interface Record -{ - public void setProperty(String _key,String _value); - public String getProperty(String _key); - public String removeProperty(String _key); - public PropertySequence createSequence(String _key); - public PropertySequence getSequence(String _key); - public PropertySequence removeSequence(String _key); - public void setSequence(String _key,PropertySequence _seq); - - public Iterator<Pair<String,String>> properties(); - public Iterator<Pair<String,PropertySequence>> sequences(); - - /* - * for concurrent purpose - */ - public boolean compareAndSwapProperty(String _key,String _expect,String _value); - public PropertySequence createSequenceIfAbsent(String _key); -}
--- a/src/main/java/jungle/core/table/Table.java Mon Jun 25 23:48:53 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,10 +0,0 @@ -package jungle.core.table; - -public interface Table -{ - public Record create(String _key); - public Record find(String _key); - public Record remove(String _key); - - public Record findAndCreateIfNotExist(String _key); -}
--- a/src/main/java/jungle/core/table/simple/SimplePropertySequence.java Mon Jun 25 23:48:53 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,97 +0,0 @@ -package jungle.core.table.simple; - -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; -import javax.annotation.concurrent.ThreadSafe; - -import jungle.core.table.PropertySequence; - -@ThreadSafe -public class SimplePropertySequence implements PropertySequence -{ - private final CopyOnWriteArrayList<String> sequence; - private final Collection<String> readonlyWrapper; - - public SimplePropertySequence() - { - sequence = new CopyOnWriteArrayList<String>(); - readonlyWrapper = Collections.unmodifiableCollection(sequence); - } - - private SimplePropertySequence(List<String> _copyTarget) - { - sequence = new CopyOnWriteArrayList<String>(_copyTarget); - readonlyWrapper = Collections.unmodifiableCollection(sequence); - } - - @Override - public Iterator<String> iterator() - { - return readonlyWrapper.iterator(); - } - - @Override - public void add(String _value) - { - if(_value == null){ - throw new NullPointerException("_value == null"); - } - - sequence.add(_value); - } - - @Override - public String get(int _pos) - { - return sequence.get(_pos); - } - - @Override - public String remove(int _pos) - { - return sequence.remove(_pos); - } - - @Override - public int size() - { - return sequence.size(); - } - - public boolean isSameSequence(PropertySequence _obj) - { - // suspicious - if(_obj instanceof PropertySequence){ - PropertySequence seq = (PropertySequence)_obj; - if(seq.size() != size()){ - return false; - } - - Iterator<String> itr1 = seq.iterator(); - Iterator<String> itr2 = this.iterator(); - - while(itr1.hasNext() && itr2.hasNext()){ - String s1 = itr1.next(); - String s2 = itr2.next(); - - if(!s1.equals(s2)){ - return false; - } - } - - if(itr1.hasNext() == itr2.hasNext()){ - return true; - } - } - - return false; - } - - SimplePropertySequence snapshot() - { - return new SimplePropertySequence(sequence); - } -} \ No newline at end of file
--- a/src/main/java/jungle/core/table/simple/SimpleRecord.java Mon Jun 25 23:48:53 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,141 +0,0 @@ -package jungle.core.table.simple; - -import java.util.Iterator; -import java.util.Map.Entry; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -import javax.annotation.concurrent.ThreadSafe; - -import jungle.core.table.PropertySequence; -import jungle.core.table.Record; -import jungle.util.Pair; - -@ThreadSafe -public class SimpleRecord implements Record -{ - private final ConcurrentHashMap<String,String> properties; - private final ConcurrentHashMap<String,SimplePropertySequence> sequences; - - public SimpleRecord() - { - properties = new ConcurrentHashMap<String,String>(); - sequences = new ConcurrentHashMap<String,SimplePropertySequence>(); - } - - @Override - public void setProperty(String _key, String _value) - { - properties.put(_key,_value); - } - - @Override - public String getProperty(String _key) - { - return properties.get(_key); - } - - @Override - public String removeProperty(String _key) - { - return properties.remove(_key); - } - - @Override - public PropertySequence createSequence(String _key) - { - SimplePropertySequence seq = new SimplePropertySequence(); - sequences.put(_key,seq); - return seq; - } - - @Override - public PropertySequence getSequence(String _key) - { - return sequences.get(_key); - } - - @Override - public PropertySequence removeSequence(String _key) - { - return sequences.remove(_key); - } - - @Override - public void setSequence(String _key,PropertySequence _seq) - { - if(_seq instanceof SimplePropertySequence){ - - } - } - - @Override - public Iterator<Pair<String, String>> properties() - { - final Set<Entry<String,String>> entries = properties.entrySet(); - final Iterator<Entry<String,String>> itr = entries.iterator(); - return new Iterator<Pair<String,String>>(){ - - @Override - public boolean hasNext() - { - return itr.hasNext(); - } - - @Override - public Pair<String, String> next() - { - Entry<String,String> ent = itr.next(); - return new Pair<String,String>(ent.getKey(),ent.getValue()); - } - - @Override - public void remove() - { - throw new UnsupportedOperationException("removing is not supported"); - } - }; - } - - @Override - public Iterator<Pair<String, PropertySequence>> sequences() - { - final Set<Entry<String,SimplePropertySequence>> entries = sequences.entrySet(); - final Iterator<Entry<String,SimplePropertySequence>> itr = entries.iterator(); - return new Iterator<Pair<String,PropertySequence>>(){ - - @Override - public boolean hasNext() - { - return itr.hasNext(); - } - - @Override - public Pair<String, PropertySequence> next() - { - Entry<String,SimplePropertySequence> ent = itr.next(); - return new Pair<String,PropertySequence> (ent.getKey(),ent.getValue()); - } - - @Override - public void remove() - { - throw new UnsupportedOperationException("removeing is not supported."); - } - }; - } - - @Override - public boolean compareAndSwapProperty(String _key, String _expect,String _value) - { - return properties.replace(_key,_expect,_value); - } - - @Override - public PropertySequence createSequenceIfAbsent(String _key) - { - SimplePropertySequence seq = new SimplePropertySequence(); - SimplePropertySequence old = sequences.putIfAbsent(_key,seq); - return old != null ? old : seq; - } -}
--- a/src/main/java/jungle/core/table/simple/SimpleTable.java Mon Jun 25 23:48:53 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -package jungle.core.table.simple; - -import java.util.concurrent.ConcurrentHashMap; - -import jungle.core.table.Record; -import jungle.core.table.Table; - -public class SimpleTable implements Table -{ - private final ConcurrentHashMap<String,SimpleRecord> table; - - public SimpleTable() - { - table = new ConcurrentHashMap<String,SimpleRecord>(); - } - - @Override - public Record create(String _key) - { - SimpleRecord r = new SimpleRecord(); - Record b = table.putIfAbsent(_key,r); - return (b == null) ? r : null; - } - - @Override - public Record find(String _key) - { - return table.get(_key); - } - - @Override - public Record remove(String _key) - { - return table.remove(_key); - } - - @Override - public Record findAndCreateIfNotExist(String _key) - { - SimpleRecord r = new SimpleRecord(); - SimpleRecord find = table.putIfAbsent(_key,r); - return find != null ? find : r; - } -}
--- a/src/main/java/jungle/impl/SimpleChildren.java Mon Jun 25 23:48:53 2012 +0900 +++ b/src/main/java/jungle/impl/SimpleChildren.java Fri Jun 29 00:03:12 2012 +0900 @@ -1,45 +1,106 @@ package jungle.impl; - import java.util.Iterator; - import jungle.core.Children; -import jungle.core.OrderedNodeSet; +import jungle.core.TreeGroup; import jungle.core.TreeNode; +import jungle.core.graph.Graph; +import jungle.core.graph.Vertex; +import jungle.core.graph.Vertexes; public class SimpleChildren implements Children { - private final OrderedNodeSet<TreeNode> sets; + private final SimpleTreeGroup group; + private final Graph graph; + private final Vertexes children; - public SimpleChildren(PropertySequence _seq) + public SimpleChildren(SimpleTreeGroup _group,Vertexes _children,Graph _graph) { - this(new SimpleOrderedNodeSet<TreeNode>()); + graph = _graph; + group = _group; + children = _children; + } + + public Iterator<TreeNode> iterator() + { + return new TreeNodeIteratorWrapper(children.iterator()); } - private SimpleChildren(OrderedNodeSet<TreeNode> _set) + private class TreeNodeIteratorWrapper implements Iterator<TreeNode> { - sets = _set; + private Iterator<Vertex> iterator; + + public TreeNodeIteratorWrapper(Iterator<Vertex> _iterator) + { + iterator = _iterator; + } + + @Override + public boolean hasNext() + { + return iterator.hasNext(); + } + + @Override + public TreeNode next() + { + Vertex nextVertex = iterator.next(); + if(nextVertex != null){ + return new SimpleTreeNode(group,nextVertex,graph); + } + + return null; + } + + @Override + public void remove() + { + throw new UnsupportedOperationException("removing is not supported"); + } } - public Iterator<TreeNode> iterator() + public boolean contains(TreeNode _treeNode) { - return sets.iterator(); + if(_treeNode instanceof SimpleTreeNode){ + SimpleTreeNode converted = (SimpleTreeNode)_treeNode; + Vertex vertex = converted.getVertex(); + if(converted.getGroup().equals(group)){ + return children.contains(vertex); + } + } + + return false; } - public boolean contains(TreeNode n) + public TreeNode getAt(int _position) { - return sets.contains(n); - } - - public TreeNode getAt(int pos) - { - return sets.getAt(pos); + if(children.size() < _position || _position < 0){ + throw new IndexOutOfBoundsException("invalid position value."); + } + + Vertex vertex = children.at(_position); + return new SimpleTreeNode(group,vertex,graph); } public int size() { - return sets.size(); + return children.size(); + } + + @Override + public int hashCode() + { + return children.hashCode(); } - + @Override + public boolean equals(Object _obj) + { + if(_obj instanceof SimpleChildren){ + SimpleChildren target = (SimpleChildren)_obj; + return target.equals(children); + } + + return false; + } }
--- a/src/main/java/jungle/impl/SimpleEditor.java Mon Jun 25 23:48:53 2012 +0900 +++ b/src/main/java/jungle/impl/SimpleEditor.java Fri Jun 29 00:03:12 2012 +0900 @@ -39,9 +39,6 @@ public Link createLinkAt(TreeNode _target, Tree _linkTarget) { - String cid = _target.cid(); - - return null; }
--- a/src/main/java/jungle/impl/SimpleJungle.java Mon Jun 25 23:48:53 2012 +0900 +++ b/src/main/java/jungle/impl/SimpleJungle.java Fri Jun 29 00:03:12 2012 +0900 @@ -4,8 +4,7 @@ import jungle.core.Jungle; import jungle.core.TreeGroup; import jungle.core.graph.Graph; -import jungle.core.table.Record; -import jungle.core.table.Table; +import jungle.core.graph.Vertex; public class SimpleJungle implements Jungle { @@ -15,12 +14,40 @@ { graph = _graph; } + + @Override + public TreeGroup createTreeGroup(String _id) + { + Vertex vertex = graph.createVertex(_id); + + if(vertex == null){ + return null; + } + + return new SimpleTreeGroup(_id,vertex,graph); + } + + @Override + public TreeGroup getTreeGroupByID(String _id) + { + Vertex vertex = graph.getVertex(_id); + if(vertex != null){ + return new SimpleTreeGroup(_id,vertex,graph); + } + + return null; + } + @Override public TreeGroup createTreeGroup() { - String groupID = UUID.randomUUID().toString(); - SimpleTreeGroup group = new SimpleTreeGroup(groupID,graph); + String groupID = null; + Vertex vertex = null; + do{ + groupID = UUID.randomUUID().toString(); + vertex = graph.createVertex(groupID); + }while(vertex == null); - return new SimpleTreeGroup(groupID,graph); + return new SimpleTreeGroup(groupID,vertex,graph); } -} +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/jungle/impl/SimpleLink.java Fri Jun 29 00:03:12 2012 +0900 @@ -0,0 +1,37 @@ +package jungle.impl; + +import jungle.core.Link; +import jungle.core.TreeGroup; +import jungle.core.graph.Graph; +import jungle.core.graph.Vertex; + +public class SimpleLink implements Link +{ + private final Vertex vertex; + private final Graph graph; + + public SimpleLink(Vertex _vertex,Graph _graph) + { + vertex = _vertex; + graph = _graph; + } + + @Override + public String get(String _key) + { + return vertex.getProperty(Simples.PROPERTY_KEY_PREFIX + _key); + } + + @Override + public TreeGroup destination() + { + String groupID = vertex.getProperty(Simples.LINK_DESTINATION_KEY); + Vertex vertex = graph.getVertex(groupID); + return new SimpleTreeGroup(groupID,vertex,graph); + } + + public Vertex getVertex() + { + return vertex; + } +}
--- a/src/main/java/jungle/impl/SimpleLinks.java Mon Jun 25 23:48:53 2012 +0900 +++ b/src/main/java/jungle/impl/SimpleLinks.java Fri Jun 29 00:03:12 2012 +0900 @@ -3,49 +3,84 @@ import java.util.Iterator; import jungle.core.Link; import jungle.core.Links; -import jungle.core.OrderedNodeSet; +import jungle.core.graph.Graph; +import jungle.core.graph.Vertex; +import jungle.core.graph.Vertexes; public class SimpleLinks implements Links { - private final OrderedNodeSet<Link> set; + @SuppressWarnings("unused") + private final SimpleTreeGroup group; + private final Vertexes links; + private final Graph graph; - public SimpleLinks() + public SimpleLinks(SimpleTreeGroup _group,Vertexes _links,Graph _graph) { - this(new SimpleOrderedNodeSet<Link>()); + group = _group; + links = _links; + graph = _graph; } - private SimpleLinks(SimpleOrderedNodeSet<Link> _set) + public boolean contains(Link _link) { - set = _set; - } - - public boolean contains(Link n) - { - return set.contains(n); + if(_link instanceof SimpleLink){ + SimpleLink converted = (SimpleLink)_link; + Vertex link = converted.getVertex(); + return links.contains(link); + } + + return false; } public int size() { - return set.size(); + return links.size(); } public Iterator<Link> iterator() { - return set.iterator(); + return new SimpleLinkIteratorWrapper(links.iterator()); } - - public Link getAt(int _pos) + + private class SimpleLinkIteratorWrapper implements Iterator<Link> { - if(_pos > size()){ - return null; + private final Iterator<Vertex> iterator; + + public SimpleLinkIteratorWrapper(Iterator<Vertex> _iterator) + { + iterator = _iterator; } - return set.getAt(_pos); + @Override + public boolean hasNext() + { + return iterator.hasNext(); + } + + @Override + public Link next() + { + Vertex vertex = iterator.next(); + if(vertex != null){ + return new SimpleLink(vertex,graph); + } + + return null; + } + + @Override + public void remove() + { + throw new UnsupportedOperationException("removing is not supported."); + } } - @Override - public String toString() + public Link getAt(int _position) { - return set.toString(); + if(links.size() < _position || _position < 0){ + throw new IndexOutOfBoundsException("invalid _position value"); + } + + return new SimpleLink(links.at(_position),graph); } }
--- a/src/main/java/jungle/impl/SimpleOrderedNodeSet.java Mon Jun 25 23:48:53 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,83 +0,0 @@ -package jungle.impl; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedList; - -import jungle.core.Node; -import jungle.core.OrderedNodeSet; - -public class SimpleOrderedNodeSet<T extends Node> implements OrderedNodeSet<T> -{ - private LinkedList<T> list; - private HashSet<T> set; - private Collection<T> listGuard; - - public SimpleOrderedNodeSet() - { - this(new LinkedList<T>(),new HashSet<T>()); - } - - private SimpleOrderedNodeSet(LinkedList<T> _list,HashSet<T> _set) - { - list = _list; - set = _set; - listGuard = Collections.unmodifiableList(list); - } - - public Iterator<T> iterator() - { - return listGuard.iterator(); - } - - public boolean contains(T _n) - { - return set.contains(_n); - } - - public T getAt(int _pos) - { - if(_pos > size()){ - return null; - } - - return list.get(_pos); - } - - public OrderedNodeSet<T> remove(int _pos) - { - if(_pos > size()){ - return null; - } - - LinkedList<T> copyOfList = new LinkedList<T>(list); - HashSet<T> copyOfSet = new HashSet<T>(set); - - T obj = copyOfList.remove(_pos); - copyOfSet.remove(obj); - - return new SimpleOrderedNodeSet<T>(copyOfList,copyOfSet); - } - - public OrderedNodeSet<T> add(T _n) - { - if(_n == null){ - throw new NullPointerException("the add target is null."); - } - - LinkedList<T> copyOfList = new LinkedList<T>(list); - HashSet<T> copyOfSet = new HashSet<T>(set); - - copyOfList.add(_n); - copyOfSet.add(_n); - - return new SimpleOrderedNodeSet<T>(copyOfList,copyOfSet); - } - - public int size() - { - return list.size(); - } -}
--- a/src/main/java/jungle/impl/SimpleTree.java Mon Jun 25 23:48:53 2012 +0900 +++ b/src/main/java/jungle/impl/SimpleTree.java Fri Jun 29 00:03:12 2012 +0900 @@ -1,6 +1,5 @@ package jungle.impl; -import jungle.core.Attributes; import jungle.core.Children; import jungle.core.Editor; import jungle.core.Links; @@ -8,23 +7,22 @@ import jungle.core.TreeGroup; import jungle.core.graph.Graph; import jungle.core.graph.Vertex; -import jungle.core.table.Record; public class SimpleTree implements Tree { - private final String treeID; + private final SimpleTreeGroup group; private final Graph graph; private final Vertex vertex; - private final SimpleChildren children; - private final SimpleLinks links; - public SimpleTree(String _treeID,Graph _graph) + public SimpleTree(SimpleTreeGroup _group,Vertex _vertex,Graph _graph) { - treeID = _treeID; + group = _group; graph = _graph; - vertex = graph.createVertex(_treeID); - children = new SimpleChildren(vertex.createVertexes(Simples.TREENODE_CHILDREN_KEY),graph); - links = new SimpleLinks(vertex.createVertexes(Simples.TREENODE_LINKS_KEY)); + vertex = _vertex; + + // initialize vertex + vertex.createVertexes(Simples.TREENODE_CHILDREN_KEY); + vertex.createVertexes(Simples.TREENODE_LINKS_KEY); } public TreeGroup getGroup() @@ -34,26 +32,26 @@ public String get(String _key) { - return record.getProperty(Simples.PROPERTY_KEY_PREFIX+_key); + return vertex.getProperty(_key); } public String treeID() { - return treeID; + return vertex.getID(); } public Children children() { - return children; + return new SimpleChildren(group,vertex.getVertexes(Simples.TREENODE_CHILDREN_KEY),graph); } public Links links() { - return links; + return new SimpleLinks(group,vertex.getVertexes(Simples.TREENODE_LINKS_KEY),graph); } - - public String cid() + + public Editor getEditor() { - return treeID; + return null; } }
--- a/src/main/java/jungle/impl/SimpleTreeGroup.java Mon Jun 25 23:48:53 2012 +0900 +++ b/src/main/java/jungle/impl/SimpleTreeGroup.java Fri Jun 29 00:03:12 2012 +0900 @@ -1,67 +1,73 @@ package jungle.impl; -import java.util.concurrent.atomic.AtomicLong; -import java.util.concurrent.atomic.AtomicReference; - -import jungle.core.Editor; import jungle.core.Tree; import jungle.core.TreeGroup; import jungle.core.graph.Graph; -import jungle.core.table.Record; -import jungle.core.table.Table; +import jungle.core.graph.Vertex; public class SimpleTreeGroup implements TreeGroup { private final String groupID; private final Graph graph; - private final AtomicLong revisionCounter; - private final AtomicReference<SimpleTree> tip; + private final Vertex vertex; - public SimpleTreeGroup(String _groupID,Graph _graph) + public SimpleTreeGroup(String _groupID,Vertex _vertex,Graph _graph) { groupID = _groupID; graph = _graph; - revisionCounter = new AtomicLong(); - tip = new AtomicReference<SimpleTree>(); + vertex = _vertex; + + vertex.setPropertyIfAbsent(Simples.TREEGROUP_ID_COUNTER,Simples.TREEGROUP_FIRST_REVISION_ID); - String treeID = String.format(Simples.TREEGROUP_TREEID_FORMAT,groupID,Long.toHexString(revisionCounter.getAndIncrement())); - SimpleTree first = new SimpleTree(treeID,graph); - tip.set(first); + String treeID = String.format(Simples.TREEGROUP_TREEID_FORMAT,groupID,Simples.TREEGROUP_FIRST_REVISION_ID); + // if not exist , previousID will be null. so then , create new Vertex for new Tree + if(vertex.setPropertyIfAbsent(Simples.TREEGROUP_CURRENT_TREE_KEY,treeID) == null){ + graph.createVertex(treeID); + } } + @Override public String getID() { return groupID; } + @Override public Tree latestTree() { - String treeID = record.getProperty(Simples.TREEGROUP_TIP_TREE_KEY); - String key = String.format(Simples.TREEGROUP_TREEID_FORMAT,groupID,treeID); - Record r = table.find(key); - SimpleTree tip = new SimpleTree(this,treeID,r); - return tip; + String currentID = vertex.getProperty(Simples.TREEGROUP_CURRENT_TREE_KEY); + Vertex vertex = graph.getVertex(currentID); + SimpleTree currentTree = new SimpleTree(this,vertex,graph); + return currentTree; } public String newTreeID() { - return Long.toHexString(revisionCounter.incrementAndGet()); + String currentID; + String nextID; + + do{ + currentID = vertex.getProperty(Simples.TREEGROUP_ID_COUNTER); + nextID = Long.toString(Long.parseLong(currentID) + 1); + }while(vertex.compareAndSwapProprety(Simples.TREEGROUP_ID_COUNTER,currentID,nextID)); + + return nextID; } - public Editor newEditor(Tree _t) + @Override + public int hashCode() { - TreeGroup tg = _t.getGroup(); - - if(tg != this){ - return null; + return vertex.hashCode(); + } + + @Override + public boolean equals(Object _obj) + { + if(_obj instanceof SimpleTreeGroup){ + SimpleTreeGroup target = (SimpleTreeGroup)_obj; + return target.vertex == vertex; } - SimpleEditor editor = new SimpleEditor(this,_t); - return editor; - } - - public AtomicReference<SimpleTree> getLatestTreeHolder() - { - return tip; + return false; } }
--- a/src/main/java/jungle/impl/SimpleTreeNode.java Mon Jun 25 23:48:53 2012 +0900 +++ b/src/main/java/jungle/impl/SimpleTreeNode.java Fri Jun 29 00:03:12 2012 +0900 @@ -3,45 +3,75 @@ import jungle.core.Attributes; import jungle.core.Children; import jungle.core.Links; +import jungle.core.TreeGroup; import jungle.core.TreeNode; -import jungle.core.table.Record; +import jungle.core.graph.Graph; +import jungle.core.graph.Vertex; +import jungle.core.graph.Vertexes; public class SimpleTreeNode implements TreeNode { - private final Record record; - private final String id; + private final Graph graph; + private final SimpleTreeGroup group; + private final Vertex vertex; - public SimpleTreeNode(String _id,Record _r) + public SimpleTreeNode(SimpleTreeGroup _group,Vertex _vertex,Graph _graph) { - if(_id == null || _r == null){ - throw new NullPointerException("_id == null || _r == null"); - } + group = _group; + vertex = _vertex; + graph = _graph; - id = _id; - record = _r; + // initialize vertex + vertex.createVertexes(Simples.TREENODE_CHILDREN_KEY); + vertex.createVertexes(Simples.TREENODE_LINKS_KEY); + } + + public TreeGroup getGroup() + { + return group; + } + + public Vertex getVertex() + { + return vertex; } @Override - public String get(String _key) { - // TODO Auto-generated method stub - return null; + public String get(String _key) + { + return vertex.getProperty(_key); } @Override - public Children children() { - // TODO Auto-generated method stub - return null; + public Children children() + { + Vertexes children = vertex.getVertexes(Simples.TREENODE_CHILDREN_KEY); + return new SimpleChildren(group,children,graph); } @Override - public Links links() { - // TODO Auto-generated method stub - return null; + public Links links() + { + Vertexes links = vertex.getVertexes(Simples.TREENODE_LINKS_KEY); + return new SimpleLinks(group,links,graph); } @Override - public String cid() { - // TODO Auto-generated method stub - return null; + public int hashCode() + { + return vertex.hashCode(); + } + + @Override + public boolean equals(Object _obj) + { + if(_obj instanceof SimpleTreeNode){ + SimpleTreeNode converted = (SimpleTreeNode)_obj; + if(converted.getGroup() == group){ + return converted.getVertex().equals(vertex); + } + } + + return false; } }
--- a/src/main/java/jungle/impl/Simples.java Mon Jun 25 23:48:53 2012 +0900 +++ b/src/main/java/jungle/impl/Simples.java Fri Jun 29 00:03:12 2012 +0900 @@ -1,9 +1,5 @@ package jungle.impl; -import jungle.core.Attributes; -import jungle.core.Children; -import jungle.core.Links; - public class Simples { public static final String PROPERTY_KEY_PREFIX = "@"; @@ -13,6 +9,8 @@ public static final String LINK_DESTINATION_KEY = "DESTINATION"; - public static final String TREEGROUP_TIP_TREE_KEY = "TIP"; + public static final String TREEGROUP_CURRENT_TREE_KEY = "CURRENT"; public static final String TREEGROUP_TREEID_FORMAT = "%s@%s"; + public static final String TREEGROUP_FIRST_REVISION_ID = "0"; + public static final String TREEGROUP_ID_COUNTER = "CURRENT_ID"; }
--- a/src/test/java/jungle/core/table/AbstractPropertySequenceTestTemplate.java Mon Jun 25 23:48:53 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,85 +0,0 @@ -package jungle.core.table; - -import junit.framework.Assert; -import junit.framework.TestCase; - -public abstract class AbstractPropertySequenceTestTemplate extends TestCase -{ - public abstract PropertySequence newInstance(); - - public void testInitialSizeIsZero() - { - PropertySequence seq = newInstance(); - Assert.assertEquals(0,seq.size()); - } - - public static final String ONE = "one"; - public static final String TWO = "two"; - public static final String THREE = "three"; - - public PropertySequence prepare() - { - PropertySequence seq = newInstance(); - seq.add(ONE); - seq.add(TWO); - seq.add(THREE); - - return seq; - } - - public void testAddIsNotAcceptNullValue() - { - PropertySequence seq = newInstance(); - try{ - seq.add(null); - Assert.fail("seq.add(null) does not throw NullPointerException"); - }catch(NullPointerException _e){ - Assert.assertTrue(true); - } - - } - - public void testAdd() - { - PropertySequence seq = prepare(); - - int size = seq.size(); - Assert.assertEquals(3,size); - - String one = seq.get(0); - String two = seq.get(1); - String three = seq.get(2); - - Assert.assertEquals(ONE,one); - Assert.assertEquals(TWO,two); - Assert.assertEquals(THREE,three); - } - - public void testGet() - { - PropertySequence seq = prepare(); - Assert.assertEquals(ONE,seq.get(0)); - } - - public void testSize() - { - PropertySequence seq = prepare(); - Assert.assertEquals(3,seq.size()); - } - - public void testRemove() - { - PropertySequence seq = prepare(); - - String removed = seq.remove(1); - Assert.assertEquals(removed,TWO); - - Assert.assertEquals(ONE,seq.get(0)); - Assert.assertEquals(THREE,seq.get(1)); - } - - public void testThreadSafety() - { - Assert.assertTrue(true); - } -}
--- a/src/test/java/jungle/core/table/AbstractRecordTestTemplate.java Mon Jun 25 23:48:53 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,161 +0,0 @@ -package jungle.core.table; - -import java.util.HashSet; -import java.util.Iterator; -import jungle.util.Pair; -import junit.framework.Assert; -import junit.framework.TestCase; - -public abstract class AbstractRecordTestTemplate extends TestCase -{ - public abstract Record newInstance(); - - public static final String ONE = "one"; - public static final String TWO = "two"; - public static final String THREE = "three"; - - public Record prepare() - { - Record r = newInstance(); - - r.setProperty(ONE,ONE); - r.setProperty(TWO,TWO); - r.setProperty(THREE,THREE); - - r.createSequence(ONE); - r.createSequence(TWO); - r.createSequence(THREE); - - return r; - } - - public void testCompareAndSwapProperty() - { - Record r = newInstance(); - - r.setProperty(ONE,ONE); - Assert.assertFalse(r.compareAndSwapProperty(ONE,THREE,THREE)); - Assert.assertTrue(r.compareAndSwapProperty(ONE,ONE,TWO)); - Assert.assertEquals(r.getProperty(ONE),TWO); - } - - public void testCreateSequenceIfAbsent() - { - Record r = newInstance(); - - PropertySequence one = r.createSequence(ONE); - Assert.assertEquals(one,r.createSequenceIfAbsent(ONE)); - Assert.assertNull(r.getProperty(TWO)); - Assert.assertNotNull(r.createSequenceIfAbsent(TWO)); - } - - public void testSetProperty() - { - Record r = prepare(); - r.setProperty("TEST","TEST"); - - Assert.assertEquals(r.getProperty("TEST"),"TEST"); - } - - public void testNullProperty() - { - Record r = newInstance(); - try{ - r.setProperty(null,"hoge"); - r.setProperty("hoge",null); - - Assert.fail("r.setProperty(null,v) or r.setProperty(k,null) does not throw NullPointerException"); - }catch(NullPointerException _e){ - Assert.assertTrue(true); - } - } - - public void testGetProperty() - { - Record r = prepare(); - Assert.assertEquals(r.getProperty(ONE),ONE); - Assert.assertEquals(r.getProperty(TWO),TWO); - Assert.assertEquals(r.getProperty(THREE),THREE); - } - - public void testRemoveProperty() - { - Record r = prepare(); - String removed = r.removeProperty(TWO); - Assert.assertEquals(TWO,removed); - Assert.assertNull(r.getProperty(TWO)); - } - - public void testCreateSequence() - { - Record r = prepare(); - PropertySequence seq = r.createSequence("TEST"); - - Assert.assertNotNull(seq); - Assert.assertEquals(seq,r.getSequence("TEST")); - } - - public void testGetSequence() - { - Record r = newInstance(); - PropertySequence one = r.createSequence(ONE); - PropertySequence two = r.createSequence(TWO); - PropertySequence three = r.createSequence(THREE); - - Assert.assertEquals(r.getSequence(ONE),one); - Assert.assertEquals(r.getSequence(TWO),two); - Assert.assertEquals(r.getSequence(THREE),three); - } - - public void testRemoveSequence() - { - Record r = prepare(); - PropertySequence removed = r.removeSequence(TWO); - - Assert.assertNotNull(removed); - Assert.assertNull(r.removeSequence(TWO)); - } - - public void testProperties() - { - Record r = prepare(); - - HashSet<String> props = new HashSet<String>(); - props.add(ONE); - props.add(TWO); - props.add(THREE); - - Iterator<Pair<String,String>> itr = r.properties(); - while(itr.hasNext()){ - Pair<String,String> p = itr.next(); - Assert.assertEquals(p.left(),p.right()); - Assert.assertTrue(props.contains(p.left())); - props.remove(p.left()); - } - - Assert.assertEquals(0,props.size()); - } - - public void testSequences() - { - Record r = newInstance(); - PropertySequence one = r.createSequence(ONE); - PropertySequence two = r.createSequence(TWO); - PropertySequence three = r.createSequence(THREE); - - HashSet<PropertySequence> props = new HashSet<PropertySequence>(); - props.add(one); - props.add(two); - props.add(three); - - Iterator<Pair<String,PropertySequence>> itr = r.sequences(); - while(itr.hasNext()){ - Pair<String,PropertySequence> p = itr.next(); - PropertySequence seq = p.right(); - Assert.assertTrue(props.contains(seq)); - props.remove(seq); - } - - Assert.assertEquals(0,props.size()); - } -}
--- a/src/test/java/jungle/core/table/AbstractTableTestTemplate.java Mon Jun 25 23:48:53 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,75 +0,0 @@ -package jungle.core.table; - -import junit.framework.Assert; -import junit.framework.TestCase; - -public abstract class AbstractTableTestTemplate extends TestCase -{ - public abstract Table newInstance(); - - public Table prepare() - { - Table t = newInstance(); - - t.create(ONE); - t.create(TWO); - t.create(THREE); - - return t; - } - - public static final String ONE = "one"; - public static final String TWO = "two"; - public static final String THREE = "three"; - - public void testFindRecordAndCreateIfNotExist() - { - Table t = newInstance(); - - Record one = t.create(ONE); - Assert.assertEquals(one,t.findAndCreateIfNotExist(ONE)); - - Record four = t.find("four"); - Assert.assertNull(four); - Assert.assertNotNull(t.findAndCreateIfNotExist("four")); - } - - public void testCreateRecord() - { - Table t = newInstance(); - - Record one = t.create(ONE); - Record two = t.create(TWO); - Record three = t.create(THREE); - - Assert.assertNotNull(one); - Assert.assertNotNull(two); - Assert.assertNotNull(three); - } - - public void testFindRecord() - { - Table t = newInstance(); - - Record one = t.create(ONE); - Assert.assertNotNull(one); - Assert.assertEquals(one,t.find(ONE)); - Assert.assertNull(t.find("NOENTRY")); - } - - public void testRemoveRecord() - { - Table t = newInstance(); - - Record one = t.create(ONE); - Record two = t.create(TWO); - - Assert.assertNotNull(one); - Assert.assertNotNull(two); - - Assert.assertEquals(one,t.remove(ONE)); - Assert.assertEquals(two,t.remove(TWO)); - Assert.assertNull(t.remove(ONE)); - Assert.assertNull(t.remove(TWO)); - } -}
--- a/src/test/java/jungle/core/table/simple/SimplePropertySequenceTest.java Mon Jun 25 23:48:53 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,27 +0,0 @@ -package jungle.core.table.simple; - -import jungle.core.table.AbstractPropertySequenceTestTemplate; -import junit.framework.Assert; - -public class SimplePropertySequenceTest extends AbstractPropertySequenceTestTemplate -{ - @Override - public SimplePropertySequence newInstance() - { - return new SimplePropertySequence(); - } - - public void testSnapshot() - { - SimplePropertySequence seq = newInstance(); - - seq.add("hoge"); - - SimplePropertySequence ss = seq.snapshot(); - Assert.assertNotNull(ss); - Assert.assertTrue(ss.isSameSequence(seq)); - - ss.add("hoge2"); - Assert.assertFalse(ss.isSameSequence(seq)); - } -}
--- a/src/test/java/jungle/core/table/simple/SimpleRecordTest.java Mon Jun 25 23:48:53 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -package jungle.core.table.simple; - -import jungle.core.table.AbstractRecordTestTemplate; -import jungle.core.table.Record; - -public class SimpleRecordTest extends AbstractRecordTestTemplate -{ - - @Override - public Record newInstance() - { - return new SimpleRecord(); - } - -}
--- a/src/test/java/jungle/core/table/simple/SimpleTableTest.java Mon Jun 25 23:48:53 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -package jungle.core.table.simple; - -import jungle.core.table.AbstractTableTestTemplate; -import jungle.core.table.Table; - -public class SimpleTableTest extends AbstractTableTestTemplate -{ - - @Override - public Table newInstance() - { - return new SimpleTable(); - } - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/test/java/jungle/misc/fj/XMLNodeTest.java Fri Jun 29 00:03:12 2012 +0900 @@ -0,0 +1,23 @@ +package jungle.misc.fj; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + +public class XMLNodeTest +{ + public static void main(String _args[]) throws Exception + { + DocumentBuilder b = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document d = b.newDocument(); + + Element e = d.createElement("hoge"); + NodeList l1 = e.getElementsByTagName("hoge"); + NodeList l2 = e.getElementsByTagName("hoge"); + System.out.println(l1.toString()); + System.out.println(l2.toString()); + } +}