Mercurial > hg > Members > shoshi > TreeCMS > TreeCMSPrototype1
changeset 31:ff4d4704e5d7
modified Node API
delete setLinkedNode,getLinkedNode
added Link API
setNode,getNode
added SimpleLink and deleted SimpleLinkedNode
Test code is SimpleLinkTest2
added document/figure/linkednode1.graffle
author | shoshi |
---|---|
date | Wed, 17 Nov 2010 18:28:21 +0900 |
parents | 8d733b98c5de |
children | 02ef906d0341 |
files | CHANGELOG documents/figure/linkednode1.graffle src/treecms/proto/api/Link.java src/treecms/proto/api/Node.java src/treecms/proto/simple/SimpleLink.java src/treecms/proto/simple/SimpleNode.java src/treecms/proto/test/SimpleLinkTest2.java src/treecms/proto/test/SimpleNodeTest1.java |
diffstat | 8 files changed, 344 insertions(+), 85 deletions(-) [+] |
line wrap: on
line diff
--- a/CHANGELOG Wed Nov 10 00:36:51 2010 +0900 +++ b/CHANGELOG Wed Nov 17 18:28:21 2010 +0900 @@ -1,9 +1,33 @@ CHANGELOG +2010/11/17 +modified Node API + delete setLinkedNode,getLinkedNode + +added Link API + setNode,getNode + +added SimpleLink and deleted SimpleLinkedNode + Test code is SimpleLinkTest2 + +added document/figure/linkednode1.graffle + 2010/11/10 added Node API setLinkedNode,getLinkedNode for create link to other node. but not implemented yet. + +added SimpleLinkedNode + proxy based link implementaion. (not done yet) + +added PreOrderLinkTreeWalker + tree walker for linked node. + +added PreOrderLinkTreeWalkerTest + tests for PreOrderLinkTreeWalker + +modified SimpleNode + thread safe support (not done yet) 2010/11/09 added CHANGELOG
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/api/Link.java Wed Nov 17 18:28:21 2010 +0900 @@ -0,0 +1,7 @@ +package treecms.proto.api; + +public interface Link extends Node +{ + public void setNode(Node _link); + public Node getNode(); +} \ No newline at end of file
--- a/src/treecms/proto/api/Node.java Wed Nov 10 00:36:51 2010 +0900 +++ b/src/treecms/proto/api/Node.java Wed Nov 17 18:28:21 2010 +0900 @@ -7,14 +7,10 @@ public interface Node extends Iterable<Node> { public Iterator<Node> iterator(); + public void addChildren(List<Node> _child); public List<Node> getChildren(); - public boolean isChild(Node _child); - public Node addChild(Node _child); - public boolean removeChild(Node _child); - - public void up(Node _child); - public void down(Node _child); + public boolean isChild(Node _child); public String getTitle(); public void setTitle(String _title); @@ -22,9 +18,13 @@ public String getClassName(); public void setClassName(String _class); + public Node addChild(Node _child); + public boolean removeChild(Node _child); + + public void up(Node _child); + public void down(Node _child); + public void replace(Node _target,Node _newChild); + public NodeID getID(); public Node cloneNode(); - - public Node getLinkedNode(); - public void setLinkedNode(Node _node); } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/simple/SimpleLink.java Wed Nov 17 18:28:21 2010 +0900 @@ -0,0 +1,127 @@ +package treecms.proto.simple; + +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +import treecms.proto.api.Link; +import treecms.proto.api.Node; +import treecms.proto.api.NodeID; +import treecms.proto.id.IncrementalNodeID; + +public class SimpleLink implements Link +{ + private NodeID m_id; + private Node m_target; + private LinkedList<Node> m_children; + + public SimpleLink(Node _target) + { + m_id = new IncrementalNodeID(); + m_target = _target; + m_children = new LinkedList<Node>(); + } + + @Override + public Node getNode() + { + return m_target; + } + + @Override + public void setNode(Node link) + { + m_target = link; + } + + @Override + public Node addChild(Node child) + { + return child; + } + + @Override + public Node cloneNode() + { + return new SimpleLink(m_target); + } + + @Override + public List<Node> getChildren() + { + return Collections.unmodifiableList(m_children); + } + + @Override + public String getClassName() + { + return m_target.getClassName(); + } + + @Override + public NodeID getID() + { + return m_id; + } + + @Override + public String getTitle() + { + return m_target.getTitle(); + } + + @Override + public boolean isChild(Node child) + { + return false; + } + + @Override + public Iterator<Node> iterator() + { + return Collections.unmodifiableList(m_children).iterator(); + } + + @Override + public boolean removeChild(Node child) + { + return false; + } + + @Override + public void setClassName(String _class) + { + m_target.setClassName(_class); + } + + @Override + public void setTitle(String _title) + { + m_target.setTitle(_title); + } + + @Override + public void addChildren(List<Node> child) + { + return; + } + + @Override + public void down(Node child) + { + return; + } + + @Override + public void replace(Node target, Node newChild) + { + return; + } + + @Override + public void up(Node child) + { + return; + } +}
--- a/src/treecms/proto/simple/SimpleNode.java Wed Nov 10 00:36:51 2010 +0900 +++ b/src/treecms/proto/simple/SimpleNode.java Wed Nov 17 18:28:21 2010 +0900 @@ -1,33 +1,29 @@ package treecms.proto.simple; import java.util.Iterator; - import java.util.Collections; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.LinkedList; -import java.util.List; import treecms.proto.api.Node; import treecms.proto.api.NodeID; import treecms.proto.id.IncrementalNodeID; public class SimpleNode implements Node { - private LinkedList<Node> m_children; + private CopyOnWriteArrayList<Node> m_children; private NodeID m_id; private String m_title,m_class; public SimpleNode() { - m_children = new LinkedList<Node>(); - - m_id = new IncrementalNodeID(); - m_title = ""; - m_class = ""; + this(new IncrementalNodeID()); } private SimpleNode(NodeID _id) { - m_children = new LinkedList<Node>(); + m_children = new CopyOnWriteArrayList<Node>(); m_id = _id; m_title = ""; @@ -37,7 +33,6 @@ @Override public List<Node> getChildren() { - // TODO Auto-generated method stub // return unmodifiable list<node> return Collections.unmodifiableList(m_children); } @@ -45,54 +40,29 @@ @Override public boolean isChild(Node _child) { - // TODO Auto-generated method stub return m_children.contains(_child); } @Override public Node addChild(Node _child) { - // TODO Auto-generated method stub - if(m_children.contains(_child)){ - throw new IllegalArgumentException("the node already contains "+_child.getTitle()+":"+_child.getID()); + if(m_children.addIfAbsent(_child)){ + return _child; } - m_children.add(_child); - return _child; + return null; } @Override public boolean removeChild(Node _child) { - // TODO Auto-generated method stub return m_children.remove(_child); } @Override - public void up(Node _child) - { - // TODO Auto-generated method stub - int curPos = m_children.indexOf(_child); - if(curPos - 1 > 0){ - m_children.add(curPos - 1,m_children.remove(curPos)); - } - } - - @Override - public void down(Node _child) - { - // TODO Auto-generated method stub - int curPos = m_children.indexOf(_child); - if(curPos + 1 < m_children.size()){ - m_children.add(curPos + 1,m_children.remove(curPos)); - } - } - - @Override public Iterator<Node> iterator() { - // TODO Auto-generated method stub // return unmodifiable iterator - return Collections.unmodifiableList(m_children).iterator(); + return m_children.iterator(); } @Override @@ -101,17 +71,16 @@ return this.m_id; } + @Override public void addChildren(List<Node> _children) { - // TODO Auto-generated method stub - this.m_children.addAll(_children); + this.m_children.addAllAbsent(_children); } @Override public Node cloneNode() { - // TODO Auto-generated method stub SimpleNode clone = new SimpleNode(m_id.update()); clone.setTitle(getTitle()); clone.setClassName(getClassName()); @@ -122,40 +91,78 @@ @Override public String getTitle() { - // TODO Auto-generated method stub return m_title; } @Override public void setTitle(String _title) { - // TODO Auto-generated method stub m_title = _title; } @Override public String getClassName() { - // TODO Auto-generated method stub return m_class; } @Override public void setClassName(String _class) { - // TODO Auto-generated method stub m_class = _class; } @Override - public Node getLinkedNode() { - // TODO Auto-generated method stub - return null; + public void down(Node _child) + { + LinkedList<Node> children = new LinkedList<Node>(); + + //copy and build new list + Iterator<Node> itr = iterator(); + while(itr.hasNext()){ + Node next = itr.next(); + if(next == _child && itr.hasNext()){ + Node target = itr.next(); + children.add(target); + } + children.add(next); + } + + m_children = new CopyOnWriteArrayList<Node>(children); } @Override - public void setLinkedNode(Node node) { - // TODO Auto-generated method stub + public void replace(Node _target, Node _newChild) + { + LinkedList<Node> children = new LinkedList<Node>(); + for(Node child : m_children){ + if(child == _target){ + children.add(_newChild); + continue; + } + children.add(child); + } + } + + @Override + public void up(Node _child) + { + LinkedList<Node> children = new LinkedList<Node>(); + + //copy and build new list + Iterator<Node> itr = iterator(); + while(itr.hasNext()){ + Node next = itr.next(); + if(next == _child && !children.isEmpty()){ + Node target = children.removeLast(); + children.add(next); + children.add(target); + continue; + } + children.add(next); + } + + m_children = new CopyOnWriteArrayList<Node>(children); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/test/SimpleLinkTest2.java Wed Nov 17 18:28:21 2010 +0900 @@ -0,0 +1,70 @@ +package treecms.proto.test; + +import static org.hamcrest.core.IsNot.not; + +import org.junit.Test; +import org.junit.Assert; +import org.junit.runner.JUnitCore; +import treecms.proto.api.*; +import treecms.proto.simple.*; + +public class SimpleLinkTest2 +{ + public static void main(String _arg[]) + { + JUnitCore.main(SimpleLinkTest2.class.getName()); + } + + private Node m_root1; + + private Node m_root2; + + public SimpleLinkTest2() + { + m_root1 = new SimpleNode(); + m_root1.addChild(new SimpleNode()); + m_root1.addChild(new SimpleNode()); + m_root1.addChild(new SimpleNode()); + + m_root2 = new SimpleNode(); + m_root2.addChild(new SimpleNode()); + m_root2.addChild(new SimpleNode()); + } + + @Test + public void testCreateLink() + { + Link link = (Link)m_root1.addChild(new SimpleLink(m_root2)); + m_root2.setTitle("title"); + m_root2.setClassName("class"); + + Assert.assertEquals(link.getTitle(),m_root2.getTitle()); + Assert.assertEquals(link.getClassName(),m_root2.getClassName()); + Assert.assertThat(link.getID(),not(m_root2.getID())); + } + + @Test + public void testEditFromLink() + { + Link link = new SimpleLink(m_root2); + link.setClassName("class"); + link.setTitle("title"); + + Assert.assertEquals(link.getTitle(),m_root2.getTitle()); + Assert.assertEquals(link.getClassName(),m_root2.getClassName()); + Assert.assertThat(link.getID(),not(m_root2.getID())); + } + + @Test + public void testClone() + { + Link link = (Link)m_root1.addChild(new SimpleLink(m_root2)); + m_root2.setTitle("title"); + m_root2.setClassName("class"); + + Link clone = (Link)link.cloneNode(); + Assert.assertThat(clone.getID(),not(link.getID())); + Assert.assertEquals(m_root2.getClassName(),clone.getClassName()); + Assert.assertEquals(m_root2.getTitle(),clone.getTitle()); + } +}
--- a/src/treecms/proto/test/SimpleNodeTest1.java Wed Nov 10 00:36:51 2010 +0900 +++ b/src/treecms/proto/test/SimpleNodeTest1.java Wed Nov 17 18:28:21 2010 +0900 @@ -6,10 +6,8 @@ import org.junit.Test; import org.junit.Assert; import org.junit.runner.JUnitCore; - import treecms.proto.api.*; import treecms.proto.simple.*; - import java.util.List; public class SimpleNodeTest1 @@ -19,6 +17,17 @@ JUnitCore.main(SimpleNodeTest1.class.getName()); } + private Node m_target; + private Node m_child1,m_child2,m_child3; + + public SimpleNodeTest1() + { + m_target = new SimpleNode(); + m_child1 = m_target.addChild(new SimpleNode()); + m_child2 = m_target.addChild(new SimpleNode()); + m_child3 = m_target.addChild(new SimpleNode()); + } + @Test public void testCreateNode() { @@ -54,32 +63,47 @@ @Test public void testChildOperation() { - Node node = new SimpleNode(); - Node child1 = node.addChild(new SimpleNode()); - Node child2 = node.addChild(new SimpleNode()); - Node child3 = node.addChild(new SimpleNode()); + //check that correctly added. + List<Node> children = m_target.getChildren(); + Assert.assertEquals(children.size(),3); + Assert.assertEquals(children.get(0),m_child1); + Assert.assertEquals(children.get(1),m_child2); + Assert.assertEquals(children.get(2),m_child3); - //check that correctly added. - List<Node> children = node.getChildren(); - Assert.assertEquals(children.size(),3); - Assert.assertEquals(children.get(0),child1); - Assert.assertEquals(children.get(1),child2); - Assert.assertEquals(children.get(2),child3); + } + + @Test + public void testRemoveChild() + { + //remove + m_target.removeChild(m_child3); - //1,2,3 -> 2,3,1 - node.down(child1); - node.up(child3); + List<Node> children = m_target.getChildren(); + Assert.assertEquals(children.get(0),m_child1); + Assert.assertEquals(children.get(1),m_child2); + } + + @Test + public void testUpOperation() + { + //up + m_target.up(m_child2); - children = node.getChildren(); - Assert.assertEquals(children.get(0),child2); - Assert.assertEquals(children.get(1),child3); - Assert.assertEquals(children.get(2),child1); + List<Node> children = m_target.getChildren(); + Assert.assertEquals(children.get(0),m_child2); + Assert.assertEquals(children.get(1),m_child1); + Assert.assertEquals(children.get(2),m_child3); + } + + @Test + public void testDownOperation() + { + //up + m_target.down(m_child2); - //remove - node.removeChild(child3); - - children = node.getChildren(); - Assert.assertEquals(children.get(0),child2); - Assert.assertEquals(children.get(1),child1); + List<Node> children = m_target.getChildren(); + Assert.assertEquals(children.get(0),m_child1); + Assert.assertEquals(children.get(1),m_child3); + Assert.assertEquals(children.get(2),m_child2); } }