Mercurial > hg > Members > shoshi > TreeCMS > TreeCMSPrototype1
changeset 20:e950264f82d3
commit
author | ShoshiTAMAKI |
---|---|
date | Sun, 17 Oct 2010 00:29:56 +0900 |
parents | a281fb7eaf39 |
children | cce963b8a4fd |
files | src/treecms/proto/api/BrowseAPI.java src/treecms/proto/api/Browser.java src/treecms/proto/api/EditAPI.java src/treecms/proto/api/Editor.java src/treecms/proto/api/Node.java src/treecms/proto/api/NodeAPI.java src/treecms/proto/api/NodeAPITreeBuilder.java src/treecms/proto/api/TreeBuilder.java src/treecms/proto/edit/EditableNodeAPITreeBuilder.java src/treecms/proto/edit/EditableTreeBuilder.java src/treecms/proto/edit/PreOrderTreeWalkerRecursive.java src/treecms/proto/edit/type1/EditableNodeAPI.java src/treecms/proto/edit/type1/EditableNodeAPITreeBuilder.java src/treecms/proto/simple/SimpleNode.java src/treecms/proto/simple/SimpleNodeAPI.java src/treecms/proto/simple/SimpleNodeAPITreeBuilder.java src/treecms/proto/simple/SimpleTreeBuilder.java src/treecms/proto/test/PreOrderTreeWalker.java src/treecms/proto/test/PreOrderTreeWalkerRecursive.java src/treecms/proto/test/PreOrderTreeWalkerRecursive1.java src/treecms/proto/test/SearchPathRootToChildTest.java src/treecms/proto/test/SearchPathTest1.java src/treecms/proto/test/SimpleNodeTest1.java src/treecms/proto/test/Test1.java src/treecms/proto/test/Test2.java src/treecms/proto/test/Test3.java src/treecms/proto/test/TestMultiThread1.java src/treecms/proto/test/TestMultiThread2.java src/treecms/proto/test/UUIDTest.java |
diffstat | 29 files changed, 517 insertions(+), 1119 deletions(-) [+] |
line wrap: on
line diff
--- a/src/treecms/proto/api/BrowseAPI.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -package treecms.proto.api; - -public interface BrowseAPI -{ - NodeAPI useContents(); -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/api/Browser.java Sun Oct 17 00:29:56 2010 +0900 @@ -0,0 +1,6 @@ +package treecms.proto.api; + +public interface Browser +{ + Node useContents(); +}
--- a/src/treecms/proto/api/EditAPI.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -package treecms.proto.api; - -public interface EditAPI extends BrowseAPI -{ - void login(String user,String pass); - void logout(); -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/api/Editor.java Sun Oct 17 00:29:56 2010 +0900 @@ -0,0 +1,7 @@ +package treecms.proto.api; + +public interface Editor extends Browser +{ + void login(String user,String pass); + void logout(); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/api/Node.java Sun Oct 17 00:29:56 2010 +0900 @@ -0,0 +1,23 @@ +package treecms.proto.api; + +import java.util.Iterator; + +import java.util.List; + +public interface Node +{ + Iterator<Node> iterator(); + List<Node> getChildList(); + boolean isChild(Node _child); + + void addChild(Node _child); + void removeChild(Node _child); + + void up(Node _child); + void down(Node _child); + + void setClassName(String _class); + void setTitle(String _title); + String getClassName(); + String getTitle(); +} \ No newline at end of file
--- a/src/treecms/proto/api/NodeAPI.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,23 +0,0 @@ -package treecms.proto.api; - -import java.util.Iterator; - -import java.util.List; - -public interface NodeAPI -{ - Iterator<NodeAPI> iterator(); - List<NodeAPI> getChildList(); - boolean isChild(NodeAPI _child); - - void addChild(NodeAPI _child); - void removeChild(NodeAPI _child); - - void up(NodeAPI _child); - void down(NodeAPI _child); - - void setClassName(String _class); - void setTitle(String _title); - String getClassName(); - String getTitle(); -} \ No newline at end of file
--- a/src/treecms/proto/api/NodeAPITreeBuilder.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -package treecms.proto.api; - -public interface NodeAPITreeBuilder -{ - public NodeAPI getContents(); - public NodeAPI createNode(); -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/api/TreeBuilder.java Sun Oct 17 00:29:56 2010 +0900 @@ -0,0 +1,9 @@ +package treecms.proto.api; + +import treecms.proto.edit.PreOrderTreeWalkerRecursive; + +public interface TreeBuilder +{ + public Node getContents(); + public Node createNode(); +}
--- a/src/treecms/proto/edit/EditableNodeAPITreeBuilder.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,72 +0,0 @@ -package treecms.proto.edit; - -import java.util.LinkedList; -import java.util.List; -import java.util.UUID; - -import treecms.proto.api.NodeAPI; -import treecms.proto.api.NodeAPITreeBuilder; -import treecms.proto.test.PreOrderTreeWalkerRecursive; - -public class EditableNodeAPITreeBuilder implements NodeAPITreeBuilder -{ - private NodeAPITreeBuilder m_builder; - private NodeAPI m_newRoot; - private NodeAPI m_oldRoot; - - public EditableNodeAPITreeBuilder(NodeAPI _target,NodeAPITreeBuilder _builder) - { - m_builder = _builder; - - //search path - m_oldRoot = _builder.getContents(); - Iterable<NodeAPI> itr = new PreOrderTreeWalkerRecursive(m_oldRoot); - - LinkedList<NodeAPI> reverse = new LinkedList<NodeAPI>(); - for(NodeAPI child : itr){ - reverse.addFirst(child); - } - - LinkedList<NodeAPI> path = new LinkedList<NodeAPI>(); - NodeAPI target = _target; - for(NodeAPI node : reverse){ - if(node.isChild(target)){ - path.addFirst(target); - target = node; - } - } - path.addFirst(m_oldRoot); - - LinkedList<NodeAPI> copyList = new LinkedList<NodeAPI>(); - for(NodeAPI node : path){ - System.out.println(node.getTitle()); - - NodeAPI copy = m_builder.createNode(); - copy.setTitle(node.getTitle() + " copied"); - for(NodeAPI child : node.getChildList()){ - copy.addChild(child); - } - if(!copyList.isEmpty()){ - List<NodeAPI> childList = copyList.getLast().getChildList(); - childList.add(childList.indexOf(node),copy); - childList.remove(node); - } - copyList.add(copy); - } - m_newRoot = copyList.getFirst(); - } - - @Override - public NodeAPI getContents() - { - // TODO Auto-generated method stub - return m_newRoot; - } - - @Override - public NodeAPI createNode() - { - // TODO Auto-generated method stub - return m_builder.createNode(); - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/edit/EditableTreeBuilder.java Sun Oct 17 00:29:56 2010 +0900 @@ -0,0 +1,82 @@ +package treecms.proto.edit; + +import java.util.LinkedList; +import java.util.List; + +import treecms.proto.api.Node; +import treecms.proto.api.TreeBuilder; + +public class EditableTreeBuilder implements TreeBuilder +{ + private TreeBuilder m_builder; + private Node m_newRoot; + private Node m_oldRoot; + + public EditableTreeBuilder(Node _target,TreeBuilder _builder) + { + m_builder = _builder; + + //search path + m_oldRoot = _builder.getContents(); + List<Node> path = findPath(m_oldRoot,_target); + + //clone tree + List<Node> clonedTree = cloneTree(path); + m_newRoot = clonedTree.get(0); + } + + private List<Node> cloneTree(List<Node> _path) + { + List<Node> clonedTree = new LinkedList<Node>(); + + Node origRoot = _path.get(0); + Node clonedRoot = m_builder.createNode(); + clonedRoot.setTitle(origRoot.getTitle()); + clonedRoot.setClassName(origRoot.getClassName()); + + + return clonedTree; + } + + public List<Node> findPathTest(Node _root,Node _child) + { + return findPath(_root,_child); + } + + private List<Node> findPath(Node _root,Node _child) + { + LinkedList<Node> list = new LinkedList<Node>(); + findPath(_root,_child,list); + list.addFirst(_root); + return list; + } + + private boolean findPath(Node _root,Node _child,LinkedList<Node> _list) + { + if(_root == _child){ + return true; + } + + for(Node child : _root.getChildList()){ + if(findPath(child,_child,_list)){ + _list.addFirst(child); + return true; + } + } + return false; + } + + @Override + public Node getContents() + { + // TODO Auto-generated method stub + return m_newRoot; + } + + @Override + public Node createNode() + { + // TODO Auto-generated method stub + return m_builder.createNode(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/edit/PreOrderTreeWalkerRecursive.java Sun Oct 17 00:29:56 2010 +0900 @@ -0,0 +1,89 @@ +package treecms.proto.edit; +import java.util.Iterator; + +import java.util.LinkedList; + +import treecms.proto.api.Node; + +public class PreOrderTreeWalkerRecursive implements Iterable<Node> +{ + private Node m_root; + + public PreOrderTreeWalkerRecursive(Node _root) + { + m_root = _root; + } + + @Override + public Iterator<Node> iterator() + { + return new PreOrderRecursiveIterator(m_root); + } + + class PreOrderRecursiveIterator implements Iterator<Node> + { + private LinkedList<Node> nextList; + + public PreOrderRecursiveIterator(Node _root) + { + nextList = new LinkedList<Node>(); + getChildren(_root, nextList); + } + + void getChildren(Node node, LinkedList<Node>list) { + list.add(node); + for(Node child : node.getChildList()){ + getChildren(child,list); + } + } + + @Override + public boolean hasNext() + { + return !nextList.isEmpty(); + } + + @Override + public Node next() + { + return nextList.poll(); + } + + @Override + public void remove() + { + throw new UnsupportedOperationException("cant remove from itrerator"); + } + } + + public LinkedList<Node> findPath(Node root, Node node) { + LinkedList<Node> list = new LinkedList<Node>(); + list.addFirst(root); + findPath(root,node,list); + return list; + } + + private boolean findPath(Node root, Node node, LinkedList<Node> list) { + if (root==node) return true; + for(Node child : node.getChildList()){ + if (findPath(child,node,list)) { + list.addFirst(child); + return true; + } + } + return false; // backtrack + } + + /* + public Node cloneTree(LinkedList<Node> path) { + Node old = path.poll(); + Node node = new SimpleNode(old.getTitle()); + node.setClassName(old.getClassName()); + for(Node child: old.getChildList()) { + if (child==old && !path.isEmpty()) child = cloneTree(path); + node.getChildList().add(child); + } + return node; + } + */ +}
--- a/src/treecms/proto/edit/type1/EditableNodeAPI.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,129 +0,0 @@ -package treecms.proto.edit.type1; - -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; - -import treecms.proto.api.NodeAPI; - -public class EditableNodeAPI implements NodeAPI -{ - private boolean m_flagChanged; - - private String m_uuid; - private String m_class; - private String m_title; - - private NodeAPI m_orig; - - private LinkedList<NodeAPI> m_children; - - public EditableNodeAPI(String _uuid) - { - m_uuid = _uuid; - m_flagChanged = false; - m_children = new LinkedList<NodeAPI>(); - } - - public boolean isChanged() - { - return m_flagChanged; - } - - public void setOriginal(NodeAPI _orig) - { - m_orig = _orig; - } - - public NodeAPI getOriginal() - { - return m_orig; - } - - public String getUUID() - { - return m_uuid; - } - - - @Override - public List<NodeAPI> getChildList() - { - // TODO Auto-generated method stub - return m_children; - } - - @Override - public boolean isChild(NodeAPI _child) - { - // TODO Auto-generated method stub - return m_children.contains(_child); - } - - @Override - public void addChild(NodeAPI _child) { - // TODO Auto-generated method stub - m_children.add(_child); - m_flagChanged = true; - } - - @Override - public void removeChild(NodeAPI _child) { - // TODO Auto-generated method stub - if(m_children.remove(_child)){ - m_flagChanged = true; - } - } - - @Override - public void up(NodeAPI _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)); - m_flagChanged = true; - } - } - - @Override - public void down(NodeAPI _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)); - m_flagChanged = true; - } - } - - @Override - public void setClassName(String _class) { - // TODO Auto-generated method stub - this.m_class = _class; - m_flagChanged = true; - } - - @Override - public void setTitle(String _title) { - // TODO Auto-generated method stub - this.m_title = _title; - m_flagChanged = true; - } - - @Override - public String getClassName() { - // TODO Auto-generated method stub - return this.m_class; - } - - @Override - public String getTitle() { - // TODO Auto-generated method stub - return this.m_title; - } - - @Override - public Iterator<NodeAPI> iterator() { - // TODO Auto-generated method stub - return m_children.iterator(); - } -}
--- a/src/treecms/proto/edit/type1/EditableNodeAPITreeBuilder.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,69 +0,0 @@ -package treecms.proto.edit.type1; - -import java.util.List; -import java.util.UUID; - -import treecms.proto.api.NodeAPI; -import treecms.proto.api.NodeAPITreeBuilder; - -public class EditableNodeAPITreeBuilder implements NodeAPITreeBuilder -{ - private NodeAPI m_root; - private NodeAPI m_clone; - - public EditableNodeAPITreeBuilder(NodeAPI _root) - { - m_root = _root; - m_clone = clone(_root); - } - - private NodeAPI clone(NodeAPI _orig) - { - EditableNodeAPI clone = new EditableNodeAPI(generateUUID()); - clone.setClassName(_orig.getClassName()); - clone.setTitle(_orig.getTitle()); - clone.setOriginal(_orig); - - List<NodeAPI> children = _orig.getChildList(); - for(NodeAPI child : children){ - //System.out.println(child.getTitle()); - clone.addChild(clone(child)); - } - - return clone; - } - - public void discard() - { - //discard changes. - m_clone = clone(m_root); - } - - public void commit() - { - //commit changes - } - - public void push() - { - //push changes - } - - public String generateUUID() - { - return Long.toString(System.currentTimeMillis()); - } - - @Override - public NodeAPI getContents() { - // TODO Auto-generated method stub - return this.m_clone; - } - - @Override - public NodeAPI createNode() { - // TODO Auto-generated method stub - return new EditableNodeAPI(generateUUID()); - } - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/simple/SimpleNode.java Sun Oct 17 00:29:56 2010 +0900 @@ -0,0 +1,104 @@ +package treecms.proto.simple; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +import treecms.proto.api.Node; + +public class SimpleNode implements Node +{ + private LinkedList<Node> m_childs; + + private String m_class; + private String m_title; + private String m_uuid; + + public SimpleNode(String _uuid) + { + m_childs = new LinkedList<Node>(); + m_class = ""; + m_title = ""; + + m_uuid = _uuid; + } + + public String getUUID() + { + return m_uuid; + } + + @Override + public List<Node> getChildList() + { + // TODO Auto-generated method stub + return m_childs; + } + + @Override + public boolean isChild(Node _child) { + // TODO Auto-generated method stub + return m_childs.contains(_child); + } + + @Override + public void addChild(Node _child) { + // TODO Auto-generated method stub + m_childs.add(_child); + } + + @Override + public void removeChild(Node _child) { + // TODO Auto-generated method stub + m_childs.remove(_child); + } + + @Override + public void up(Node _child) { + // TODO Auto-generated method stub + int curPos = m_childs.indexOf(_child); + if(curPos - 1 > 0){ + m_childs.add(curPos - 1,m_childs.remove(curPos)); + } + } + + @Override + public void down(Node _child) { + // TODO Auto-generated method stub + int curPos = m_childs.indexOf(_child); + if(curPos + 1 < m_childs.size()){ + m_childs.add(curPos + 1,m_childs.remove(curPos)); + } + } + + @Override + public void setClassName(String _class) { + // TODO Auto-generated method stub + this.m_class = _class; + } + + @Override + public void setTitle(String _title) { + // TODO Auto-generated method stub + this.m_title = _title; + } + + @Override + public String getClassName() { + // TODO Auto-generated method stub + return this.m_class; + } + + @Override + public String getTitle() { + // TODO Auto-generated method stub + return this.m_title; + } + + @Override + public Iterator<Node> iterator() { + // TODO Auto-generated method stub + return m_childs.iterator(); + } + +}
--- a/src/treecms/proto/simple/SimpleNodeAPI.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,104 +0,0 @@ -package treecms.proto.simple; - -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; - -import treecms.proto.api.NodeAPI; - -public class SimpleNodeAPI implements NodeAPI -{ - private LinkedList<NodeAPI> m_childs; - - private String m_class; - private String m_title; - private String m_uuid; - - public SimpleNodeAPI(String _uuid) - { - m_childs = new LinkedList<NodeAPI>(); - m_class = ""; - m_title = ""; - - m_uuid = _uuid; - } - - public String getUUID() - { - return m_uuid; - } - - @Override - public List<NodeAPI> getChildList() - { - // TODO Auto-generated method stub - return m_childs; - } - - @Override - public boolean isChild(NodeAPI _child) { - // TODO Auto-generated method stub - return m_childs.contains(_child); - } - - @Override - public void addChild(NodeAPI _child) { - // TODO Auto-generated method stub - m_childs.add(_child); - } - - @Override - public void removeChild(NodeAPI _child) { - // TODO Auto-generated method stub - m_childs.remove(_child); - } - - @Override - public void up(NodeAPI _child) { - // TODO Auto-generated method stub - int curPos = m_childs.indexOf(_child); - if(curPos - 1 > 0){ - m_childs.add(curPos - 1,m_childs.remove(curPos)); - } - } - - @Override - public void down(NodeAPI _child) { - // TODO Auto-generated method stub - int curPos = m_childs.indexOf(_child); - if(curPos + 1 < m_childs.size()){ - m_childs.add(curPos + 1,m_childs.remove(curPos)); - } - } - - @Override - public void setClassName(String _class) { - // TODO Auto-generated method stub - this.m_class = _class; - } - - @Override - public void setTitle(String _title) { - // TODO Auto-generated method stub - this.m_title = _title; - } - - @Override - public String getClassName() { - // TODO Auto-generated method stub - return this.m_class; - } - - @Override - public String getTitle() { - // TODO Auto-generated method stub - return this.m_title; - } - - @Override - public Iterator<NodeAPI> iterator() { - // TODO Auto-generated method stub - return m_childs.iterator(); - } - -}
--- a/src/treecms/proto/simple/SimpleNodeAPITreeBuilder.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -package treecms.proto.simple; - -import treecms.proto.api.*; - -public class SimpleNodeAPITreeBuilder implements NodeAPITreeBuilder -{ - private NodeAPI m_root; - - public SimpleNodeAPITreeBuilder() - { - m_root = new SimpleNodeAPI(generateUUID()); - return; - } - - public NodeAPI getContents() - { - return m_root; - } - - public NodeAPI createNode() - { - return new SimpleNodeAPI(generateUUID()); - } - - String generateUUID() - { - return Long.toString(System.currentTimeMillis()); - } - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/simple/SimpleTreeBuilder.java Sun Oct 17 00:29:56 2010 +0900 @@ -0,0 +1,35 @@ +package treecms.proto.simple; + +import java.util.Random; + +import treecms.proto.api.Node; +import treecms.proto.api.TreeBuilder; + +public class SimpleTreeBuilder implements TreeBuilder +{ + private Node m_root; + private Random m_rand; + + public SimpleTreeBuilder() + { + m_rand = new Random(); + m_root = new SimpleNode(generateUUID()); + return; + } + + public Node getContents() + { + return m_root; + } + + public Node createNode() + { + return new SimpleNode(generateUUID()); + } + + String generateUUID() + { + return Long.toString(m_rand.nextLong()); + } + +}
--- a/src/treecms/proto/test/PreOrderTreeWalker.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -package treecms.proto.test; - -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; - -import treecms.proto.api.NodeAPI; - -public class PreOrderTreeWalker implements Iterable<NodeAPI> -{ - private NodeAPI m_root; - - public PreOrderTreeWalker(NodeAPI _root) - { - m_root = _root; - } - - class NodeState { - NodeAPI node; - int position; - NodeState(NodeAPI n,int p) { node = n; position = p; } - } - class IteratorState implements Iterator<NodeAPI> { - LinkedList<NodeState>nodeStack; - int position; - NodeAPI current, next; - - IteratorState(NodeAPI root) { - current = root; - position = 0; - nodeStack = new LinkedList<NodeState>(); - } - public boolean hasNext() { - while (position>=current.getChildList().size()) { - if (nodeStack.isEmpty()) return false; - NodeState s = nodeStack.poll(); - current = s.node; - position = s.position; - } - next = current.getChildList().get(position++); - if (! next.getChildList().isEmpty()) { - nodeStack.addLast(new NodeState(current,position)); - current = next; - position = 0; - } - return true; - } - public NodeAPI next() { - return next; - } - public void remove() { - } - } - - @Override - public Iterator<NodeAPI> iterator() { - return new IteratorState(m_root); - } -}
--- a/src/treecms/proto/test/PreOrderTreeWalkerRecursive.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,90 +0,0 @@ -package treecms.proto.test; -import java.util.Iterator; -import java.util.LinkedList; - -import treecms.proto.api.NodeAPI; - -public class PreOrderTreeWalkerRecursive implements Iterable<NodeAPI> -{ - private NodeAPI m_root; - - public PreOrderTreeWalkerRecursive(NodeAPI _root) - { - m_root = _root; - } - - @Override - public Iterator<NodeAPI> iterator() - { - // TODO Auto-generated method stub - return new PreOrderRecursiveIterator(m_root); - } - - class PreOrderRecursiveIterator implements Iterator<NodeAPI> - { - private NodeAPI m_root; - private LinkedList<Iterator<NodeAPI>> m_itrList; - - private int m_pos; - - public PreOrderRecursiveIterator(NodeAPI _root) - { - m_root = _root; - m_itrList = new LinkedList<Iterator<NodeAPI>>(); - for(NodeAPI child : m_root.getChildList()){ - Iterator<NodeAPI> itr = new PreOrderRecursiveIterator(child); - m_itrList.add(itr); - } - - m_pos = -1; - } - - @Override - public boolean hasNext() - { - // TODO Auto-generated method stub - int pos = m_pos; - - if(pos < 0){ - return true; - } - - while(pos < m_itrList.size()){ - if(m_itrList.get(pos).hasNext()){ - return true; - } - pos++; - } - - return false; - } - - @Override - public NodeAPI next() - { - // TODO Auto-generated method stub - if(m_pos < 0){ - m_pos ++; - return m_root; - } - - while(m_pos < m_itrList.size()){ - NodeAPI next = m_itrList.get(m_pos).next(); - if(next != null){ - return next; - } - m_pos ++; - } - - return null; - } - - @Override - public void remove() - { - // TODO Auto-generated method stub - throw new UnsupportedOperationException("cant remove from itrerator"); - } - - } -}
--- a/src/treecms/proto/test/PreOrderTreeWalkerRecursive1.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,88 +0,0 @@ -package treecms.proto.test; -import java.util.Iterator; -import java.util.LinkedList; - -import treecms.proto.api.NodeAPI; -import treecms.proto.simple.SimpleNodeAPI; - -public class PreOrderTreeWalkerRecursive1 implements Iterable<NodeAPI> -{ - private NodeAPI m_root; - - public PreOrderTreeWalkerRecursive1(NodeAPI _root) - { - m_root = _root; - } - - @Override - public Iterator<NodeAPI> iterator() - { - return new PreOrderRecursiveIterator(m_root); - } - - class PreOrderRecursiveIterator implements Iterator<NodeAPI> - { - private LinkedList<NodeAPI> nextList; - - public PreOrderRecursiveIterator(NodeAPI _root) - { - nextList = new LinkedList<NodeAPI>(); - getChildren(_root, nextList); - } - - void getChildren(NodeAPI node, LinkedList<NodeAPI>list) { - list.add(node); - for(NodeAPI child : node.getChildList()){ - getChildren(child,list); - } - } - - @Override - public boolean hasNext() - { - return !nextList.isEmpty(); - } - - @Override - public NodeAPI next() - { - return nextList.poll(); - } - - @Override - public void remove() - { - throw new UnsupportedOperationException("cant remove from itrerator"); - } - } - - public LinkedList<NodeAPI> findPath(NodeAPI root, NodeAPI node) { - LinkedList<NodeAPI> list = new LinkedList<NodeAPI>(); - list.addFirst(root); - findPath(root,node,list); - return list; - } - - private boolean findPath(NodeAPI root, NodeAPI node, LinkedList<NodeAPI> list) { - if (root==node) return true; - for(NodeAPI child : node.getChildList()){ - if (findPath(child,node,list)) { - list.addFirst(child); - return true; - } - } - return false; // backtrack - } - - public NodeAPI cloneTree(LinkedList<NodeAPI> path) { - NodeAPI old = path.poll(); - NodeAPI node = new SimpleNodeAPI(old.getTitle()); - node.setClassName(old.getClassName()); - for(NodeAPI child: old.getChildList()) { - if (child==old && !path.isEmpty()) child = cloneTree(path); - node.getChildList().add(child); - } - return node; - } - -}
--- a/src/treecms/proto/test/SearchPathRootToChildTest.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,71 +0,0 @@ -package treecms.proto.test; - -import java.util.Iterator; -import java.util.LinkedList; - -import treecms.proto.api.*; -import treecms.proto.simple.*; - -public class SearchPathRootToChildTest -{ - public static void main(String _arg[]) - { - NodeAPITreeBuilder builder = new SimpleNodeAPITreeBuilder(); - NodeAPI root = builder.getContents(); - root.setTitle("root"); - - NodeAPI child1 = builder.createNode(); - child1.setTitle("+-child1"); - NodeAPI child2 = builder.createNode(); - child2.setTitle("+-child2"); - - root.addChild(child1); - root.addChild(child2); - - NodeAPI child11 = builder.createNode(); - child11.setTitle(" +-child11"); - NodeAPI child12 = builder.createNode(); - child12.setTitle(" +-child12"); - NodeAPI child13 = builder.createNode(); - child13.setTitle(" +-child13"); - - child1.addChild(child11); - child1.addChild(child12); - child1.addChild(child13); - - NodeAPI child21 = builder.createNode(); - child21.setTitle(" +-child21"); - - child2.addChild(child21); - - NodeAPI child211 = builder.createNode(); - child211.setTitle(" +-child211"); - NodeAPI child212 = builder.createNode(); - child212.setTitle(" +-child212"); - - child21.addChild(child211); - child21.addChild(child212); - - - PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(root); - Iterator<NodeAPI> itr = walker.iterator(); - - LinkedList<NodeAPI> reverse = new LinkedList<NodeAPI>(); - while(itr.hasNext()){ - reverse.addFirst(itr.next()); - } - - NodeAPI target = child212; - LinkedList<NodeAPI> path = new LinkedList<NodeAPI>(); - for(NodeAPI node : reverse){ - if(node.isChild(target)){ - path.addFirst(target); - target = node; - } - } - - for(NodeAPI node : path){ - System.out.println(node.getTitle()); - } - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/test/SearchPathTest1.java Sun Oct 17 00:29:56 2010 +0900 @@ -0,0 +1,73 @@ +package treecms.proto.test; + +import java.util.Iterator; + +import java.util.LinkedList; + +import treecms.proto.api.*; +import treecms.proto.edit.PreOrderTreeWalkerRecursive; +import treecms.proto.simple.*; + +public class SearchPathTest1 +{ + public static void main(String _arg[]) + { + TreeBuilder builder = new SimpleTreeBuilder(); + Node root = builder.getContents(); + root.setTitle("root"); + + Node child1 = builder.createNode(); + child1.setTitle("+-child1"); + Node child2 = builder.createNode(); + child2.setTitle("+-child2"); + + root.addChild(child1); + root.addChild(child2); + + Node child11 = builder.createNode(); + child11.setTitle(" +-child11"); + Node child12 = builder.createNode(); + child12.setTitle(" +-child12"); + Node child13 = builder.createNode(); + child13.setTitle(" +-child13"); + + child1.addChild(child11); + child1.addChild(child12); + child1.addChild(child13); + + Node child21 = builder.createNode(); + child21.setTitle(" +-child21"); + + child2.addChild(child21); + + Node child211 = builder.createNode(); + child211.setTitle(" +-child211"); + Node child212 = builder.createNode(); + child212.setTitle(" +-child212"); + + child21.addChild(child211); + child21.addChild(child212); + + + PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(root); + Iterator<Node> itr = walker.iterator(); + + LinkedList<Node> reverse = new LinkedList<Node>(); + while(itr.hasNext()){ + reverse.addFirst(itr.next()); + } + + Node target = child212; + LinkedList<Node> path = new LinkedList<Node>(); + for(Node node : reverse){ + if(node.isChild(target)){ + path.addFirst(target); + target = node; + } + } + + for(Node node : path){ + System.out.println(node.getTitle()); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/test/SimpleNodeTest1.java Sun Oct 17 00:29:56 2010 +0900 @@ -0,0 +1,57 @@ +package treecms.proto.test; + +import java.util.Iterator; + +import treecms.proto.api.*; +import treecms.proto.edit.PreOrderTreeWalkerRecursive; +import treecms.proto.simple.*; + +public class SimpleNodeTest1 +{ + public static void main(String _arg[]) + { + TreeBuilder builder = new SimpleTreeBuilder(); + Node root = builder.getContents(); + root.setTitle("root"); + + Node child1 = builder.createNode(); + child1.setTitle("+-child1"); + Node child2 = builder.createNode(); + child2.setTitle("+-child2"); + + root.addChild(child1); + root.addChild(child2); + + Node child11 = builder.createNode(); + child11.setTitle(" +-child11"); + Node child12 = builder.createNode(); + child12.setTitle(" +-child12"); + Node child13 = builder.createNode(); + child13.setTitle(" +-child13"); + + child1.addChild(child11); + child1.addChild(child12); + child1.addChild(child13); + + Node child21 = builder.createNode(); + child21.setTitle(" +-child21"); + + child2.addChild(child21); + + Node child211 = builder.createNode(); + child211.setTitle(" +-child211"); + Node child212 = builder.createNode(); + child212.setTitle(" +-child212"); + + child21.addChild(child211); + child21.addChild(child212); + + + PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(root); + Iterator<Node> itr = walker.iterator(); + while(itr.hasNext()){ + Node node = itr.next(); + System.out.println(node.getTitle()); + } + } +}
--- a/src/treecms/proto/test/Test1.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,56 +0,0 @@ -package treecms.proto.test; - -import java.util.Iterator; - -import treecms.proto.api.*; -import treecms.proto.simple.*; - -public class Test1 -{ - public static void main(String _arg[]) - { - NodeAPITreeBuilder builder = new SimpleNodeAPITreeBuilder(); - NodeAPI root = builder.getContents(); - root.setTitle("root"); - - NodeAPI child1 = builder.createNode(); - child1.setTitle("+-child1"); - NodeAPI child2 = builder.createNode(); - child2.setTitle("+-child2"); - - root.addChild(child1); - root.addChild(child2); - - NodeAPI child11 = builder.createNode(); - child11.setTitle(" +-child11"); - NodeAPI child12 = builder.createNode(); - child12.setTitle(" +-child12"); - NodeAPI child13 = builder.createNode(); - child13.setTitle(" +-child13"); - - child1.addChild(child11); - child1.addChild(child12); - child1.addChild(child13); - - NodeAPI child21 = builder.createNode(); - child21.setTitle(" +-child21"); - - child2.addChild(child21); - - NodeAPI child211 = builder.createNode(); - child211.setTitle(" +-child211"); - NodeAPI child212 = builder.createNode(); - child212.setTitle(" +-child212"); - - child21.addChild(child211); - child21.addChild(child212); - - - PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(root); - Iterator<NodeAPI> itr = walker.iterator(); - while(itr.hasNext()){ - NodeAPI node = itr.next(); - System.out.println(node.getTitle()); - } - } -}
--- a/src/treecms/proto/test/Test2.java Mon Aug 30 02:44:50 2010 +0900 +++ b/src/treecms/proto/test/Test2.java Sun Oct 17 00:29:56 2010 +0900 @@ -1,62 +1,61 @@ package treecms.proto.test; import java.util.Iterator; +import java.util.List; import treecms.proto.api.*; import treecms.proto.simple.*; -import treecms.proto.edit.type1.*; - -/* - * treecms.proto.edit.type2 Test - */ +import treecms.proto.edit.EditableTreeBuilder; +import treecms.proto.edit.PreOrderTreeWalkerRecursive; public class Test2 { public static void main(String _arg[]) { - NodeAPITreeBuilder builder = new SimpleNodeAPITreeBuilder(); - NodeAPI root = builder.getContents(); + TreeBuilder builder = new SimpleTreeBuilder(); + Node root = builder.getContents(); root.setTitle("root"); - NodeAPI child1 = builder.createNode(); + Node child1 = builder.createNode(); child1.setTitle("child1"); - NodeAPI child2 = builder.createNode(); + Node child2 = builder.createNode(); child2.setTitle("child2"); root.addChild(child1); root.addChild(child2); - NodeAPI child11 = builder.createNode(); + Node child11 = builder.createNode(); child11.setTitle("child11"); - NodeAPI child12 = builder.createNode(); + Node child12 = builder.createNode(); child12.setTitle("child12"); - NodeAPI child13 = builder.createNode(); + Node child13 = builder.createNode(); child13.setTitle("child13"); child1.addChild(child11); child1.addChild(child12); child1.addChild(child13); - NodeAPI child21 = builder.createNode(); + Node child21 = builder.createNode(); child21.setTitle("child21"); child2.addChild(child21); - NodeAPI child211 = builder.createNode(); + Node child211 = builder.createNode(); child211.setTitle("child211"); - NodeAPI child212 = builder.createNode(); + Node child212 = builder.createNode(); child212.setTitle("child212"); child21.addChild(child211); child21.addChild(child212); + EditableTreeBuilder builder1 = new EditableTreeBuilder(child212,builder); + PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(builder1.getContents()); - NodeAPITreeBuilder builder1 = new EditableNodeAPITreeBuilder(root); - PreOrderTreeWalker walker = new PreOrderTreeWalker(builder1.getContents()); - Iterator<NodeAPI> itr = walker.iterator(); + //Iterator<Node> itr = walker.iterator(); + List<Node> list = builder1.findPathTest(root,child212); + Iterator<Node> itr = list.iterator(); while(itr.hasNext()){ - NodeAPI node = itr.next(); - node.setTitle(node.getTitle()+":edit"); + Node node = itr.next(); System.out.println(node.getTitle()); } }
--- a/src/treecms/proto/test/Test3.java Mon Aug 30 02:44:50 2010 +0900 +++ b/src/treecms/proto/test/Test3.java Sun Oct 17 00:29:56 2010 +0900 @@ -10,37 +10,37 @@ { public static void main(String _arg[]) { - NodeAPITreeBuilder builder = new SimpleNodeAPITreeBuilder(); - NodeAPI root = builder.getContents(); + TreeBuilder builder = new SimpleTreeBuilder(); + Node root = builder.getContents(); root.setTitle("root"); - NodeAPI child1 = builder.createNode(); + Node child1 = builder.createNode(); child1.setTitle("+-child1"); - NodeAPI child2 = builder.createNode(); + Node child2 = builder.createNode(); child2.setTitle("+-child2"); root.addChild(child1); root.addChild(child2); - NodeAPI child11 = builder.createNode(); + Node child11 = builder.createNode(); child11.setTitle(" +-child11"); - NodeAPI child12 = builder.createNode(); + Node child12 = builder.createNode(); child12.setTitle(" +-child12"); - NodeAPI child13 = builder.createNode(); + Node child13 = builder.createNode(); child13.setTitle(" +-child13"); child1.addChild(child11); child1.addChild(child12); child1.addChild(child13); - NodeAPI child21 = builder.createNode(); + Node child21 = builder.createNode(); child21.setTitle(" +-child21"); child2.addChild(child21); - NodeAPI child211 = builder.createNode(); + Node child211 = builder.createNode(); child211.setTitle(" +-child211"); - NodeAPI child212 = builder.createNode(); + Node child212 = builder.createNode(); child212.setTitle(" +-child212"); child21.addChild(child211); @@ -48,15 +48,15 @@ //i want to edit child22 System.out.println("Path of Root to Target --------------"); - EditableNodeAPITreeBuilder builder2 = new EditableNodeAPITreeBuilder(child212,builder); + EditableTreeBuilder builder2 = new EditableTreeBuilder(child212,builder); System.out.println("-----------------------------"); //check that tree correctly copied. PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(builder2.getContents()); - Iterator<NodeAPI> itr = walker.iterator(); + Iterator<Node> itr = walker.iterator(); while(itr.hasNext()){ - NodeAPI node = itr.next(); + Node node = itr.next(); System.out.println(node.getTitle()); } }
--- a/src/treecms/proto/test/TestMultiThread1.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,117 +0,0 @@ -package treecms.proto.test; - -import java.util.Iterator; -import java.util.Random; - -import treecms.proto.api.NodeAPI; -import treecms.proto.api.NodeAPITreeBuilder; -import treecms.proto.edit.type1.*; -import treecms.proto.simple.SimpleNodeAPITreeBuilder; - -public class TestMultiThread1 -{ - public static void main(String _arg[]) throws Exception - { - new TestMultiThread1(); - } - - public TestMultiThread1() throws Exception - { - NodeAPITreeBuilder builder = new SimpleNodeAPITreeBuilder(); - NodeAPI root = builder.getContents(); - root.setTitle("t"); - - NodeAPI child1 = builder.createNode(); - child1.setTitle("1"); - NodeAPI child2 = builder.createNode(); - child2.setTitle("2"); - - root.addChild(child1); - root.addChild(child2); - - NodeAPI child11 = builder.createNode(); - child11.setTitle("11"); - NodeAPI child12 = builder.createNode(); - child12.setTitle("12"); - NodeAPI child13 = builder.createNode(); - child13.setTitle("13"); - - child1.addChild(child11); - child1.addChild(child12); - child1.addChild(child13); - - NodeAPI child21 = builder.createNode(); - child21.setTitle("21"); - - child2.addChild(child21); - - NodeAPI child211 = builder.createNode(); - child211.setTitle("211"); - NodeAPI child212 = builder.createNode(); - child212.setTitle("212"); - - child21.addChild(child211); - child21.addChild(child212); - - Thread th1 = new RenderingWorkerThread(root); - Thread th2 = new EditingWorkerThread(root); - Thread th3 = new EditingWorkerThread(root); - - th1.start(); - th2.start(); - th3.start(); - - th1.join(); - th2.join(); - th3.join(); - } - - class RenderingWorkerThread extends Thread - { - private NodeAPI m_root; - - public RenderingWorkerThread(NodeAPI _node) - { - m_root = _node; - } - - public void run() - { - PreOrderTreeWalker walker = new PreOrderTreeWalker(m_root); - for(int i = 0;i < 3;i ++){ - String result = ""; - Iterator<NodeAPI> itr = walker.iterator(); - while(itr.hasNext()){ - NodeAPI node = itr.next(); - result += (node.getTitle() + " "); - } - System.out.println(result); - } - } - } - - class EditingWorkerThread extends Thread - { - private NodeAPI m_root; - - public EditingWorkerThread(NodeAPI _node) - { - m_root = _node; - } - public void run() - { - NodeAPITreeBuilder builder = new EditableNodeAPITreeBuilder(m_root); - PreOrderTreeWalker walker = new PreOrderTreeWalker(builder.getContents()); - for(int i = 0;i < 3;i ++){ - Iterator<NodeAPI> itr = walker.iterator(); - while(itr.hasNext()){ - NodeAPI node = itr.next(); - if((new Random()).nextBoolean()){ - node.setTitle("edit"); - } - } - } - // - } - } -}
--- a/src/treecms/proto/test/TestMultiThread2.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,112 +0,0 @@ -package treecms.proto.test; - -import java.util.Iterator; -import java.util.Random; - -import treecms.proto.api.NodeAPI; -import treecms.proto.api.NodeAPITreeBuilder; -import treecms.proto.edit.type1.*; -import treecms.proto.simple.SimpleNodeAPITreeBuilder; - -public class TestMultiThread2 -{ - public static void main(String _arg[]) throws Exception - { - new TestMultiThread2(); - } - - public TestMultiThread2() throws Exception - { - NodeAPITreeBuilder builder = new SimpleNodeAPITreeBuilder(); - NodeAPI root = builder.getContents(); - root.setTitle("t"); - - NodeAPI child1 = builder.createNode(); - child1.setTitle("1"); - NodeAPI child2 = builder.createNode(); - child2.setTitle("2"); - - root.addChild(child1); - root.addChild(child2); - - NodeAPI child11 = builder.createNode(); - child11.setTitle("11"); - NodeAPI child12 = builder.createNode(); - child12.setTitle("12"); - - child1.addChild(child11); - child1.addChild(child12); - - NodeAPI child21 = builder.createNode(); - child21.setTitle("21"); - - child2.addChild(child21); - - Thread th2 = new EditingWorkerThread(root); - Thread th3 = new EditingWorkerThread(root); - - th2.start(); - th3.start(); - - th2.join(); - th3.join(); - - PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(root); - String result = ""; - Iterator<NodeAPI> itr = walker.iterator(); - while(itr.hasNext()){ - NodeAPI node = itr.next(); - result += (node.getTitle() + "\t"); - } - System.out.println(result); - } - - class RenderingWorkerThread extends Thread - { - private NodeAPI m_root; - - public RenderingWorkerThread(NodeAPI _node) - { - m_root = _node; - } - - public void run() - { - PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(m_root); - for(int i = 0;i < 3;i ++){ - String result = ""; - Iterator<NodeAPI> itr = walker.iterator(); - while(itr.hasNext()){ - NodeAPI node = itr.next(); - result += (node.getTitle() + " "); - } - System.out.println(result); - } - } - } - - class EditingWorkerThread extends Thread - { - private NodeAPI m_root; - - public EditingWorkerThread(NodeAPI _node) - { - m_root = _node; - } - public void run() - { - NodeAPITreeBuilder builder = new EditableNodeAPITreeBuilder(m_root); - PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(builder.getContents()); - for(int i = 0;i < 3;i ++){ - Iterator<NodeAPI> itr = walker.iterator(); - while(itr.hasNext()){ - NodeAPI node = itr.next(); - if((new Random()).nextBoolean()){ - node.setTitle("edit"); - } - } - } - // - } - } -}
--- a/src/treecms/proto/test/UUIDTest.java Mon Aug 30 02:44:50 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -package treecms.proto.test; - -import java.util.*; - -public class UUIDTest extends Thread -{ - public static void main(String _args[]) - { - Thread th1 = new UUIDTest(); - Thread th2 = new UUIDTest(); - Thread th3 = new UUIDTest(); - Thread th4 = new UUIDTest(); - - th1.start(); - th2.start(); - th3.start(); - th4.start(); - - try { - th1.join(); - th2.join(); - th3.join(); - th4.join(); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - public static synchronized UUID getUUID() - { - return UUID.randomUUID(); - } - - public void run() - { - for(int i = 0;i < 3;i ++){ - try{ - //System.out.println(UUID.randomUUID().toString()); - System.out.println(UUIDTest.getUUID()); - }catch(Exception _err){ - _err.printStackTrace(); - } - } - } -}