Mercurial > hg > Members > shoshi > TreeCMS > TreeCMSPrototype1
changeset 27:45881237e777
commit
line wrap: on
line diff
--- a/src/treecms/proto/api/Node.java Tue Nov 02 18:44:21 2010 +0900 +++ b/src/treecms/proto/api/Node.java Sun Nov 07 14:07:03 2010 +0900 @@ -1,27 +1,27 @@ package treecms.proto.api; -import java.util.Iterator; - import java.util.List; -public interface Node +import java.util.Iterator; + +public interface Node extends Iterable<Node> { public Iterator<Node> iterator(); - public List<Node> getChildList(); + public List<Node> getChildren(); public boolean isChild(Node _child); - public void addChild(Node _child); - public void addChildren(List<Node> _children); + public Node addChild(NodeParameters _child); public void removeChild(Node _child); public void up(Node _child); public void down(Node _child); - public void setClassName(String _class); - public String getClassName(); + public NodeParameters getParameters(); + public void setParameters(NodeParameters _params); - public void setTitle(String _title); - public String getTitle(); + public String getParameter(NodeParameters.Types _type); + public void setParameter(NodeParameters.Types _type,String _value); public NodeID getID(); + public Node cloneNode(); } \ No newline at end of file
--- a/src/treecms/proto/api/NodeID.java Tue Nov 02 18:44:21 2010 +0900 +++ b/src/treecms/proto/api/NodeID.java Sun Nov 07 14:07:03 2010 +0900 @@ -2,6 +2,7 @@ public interface NodeID { + public NodeID update(); public String toString(); public int compare(NodeID _target); }
--- a/src/treecms/proto/api/NodeIDFactory.java Tue Nov 02 18:44:21 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,7 +0,0 @@ -package treecms.proto.api; - -public interface NodeIDFactory -{ - public NodeID createNewID(); - public NodeID updateExistID(NodeID _); -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/api/NodeParameters.java Sun Nov 07 14:07:03 2010 +0900 @@ -0,0 +1,47 @@ +package treecms.proto.api; + +import java.util.Collections; +import java.util.Hashtable; +import java.util.Map; + +public class NodeParameters +{ + public enum Types{ + TITLE, + CLASS + } + + private Map<NodeParameters.Types,String> m_params; + + public NodeParameters() + { + m_params = new Hashtable<NodeParameters.Types,String>(); + } + + public NodeParameters(NodeParameters _params) + { + //copy constructor. + m_params = null; + m_params = new Hashtable<NodeParameters.Types,String>(_params.getParameters()); + } + + public Map<NodeParameters.Types,String> getParameters() + { + return Collections.unmodifiableMap(m_params); + } + + public String getParameter(NodeParameters.Types _type) + { + return m_params.get(_type); + } + + public void setParameter(NodeParameters.Types _type,String _value) + { + m_params.put(_type,_value); + } + + public Object clone() + { + return new NodeParameters(this); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/edit/EditableNode.java Sun Nov 07 14:07:03 2010 +0900 @@ -0,0 +1,74 @@ +package treecms.proto.edit; + +import java.util.LinkedList; + + +import java.util.List; +import treecms.proto.api.Node; + +public class EditableNode extends SimpleNode +{ + private Node m_newRoot; + private Node m_target; //node that wanted to edit. + + public EditableTreeBuilder(Node _root,Node _target) + { + //search path + LinkedList<Node> path = findPath(_root,_target); + + //clone root node. + m_newRoot = cloneNode(path.poll()); + + cloneTree(path,m_newRoot); + } + + public Node getTargetNode() + { + return m_target; + } + + private void cloneTree(LinkedList<Node> _path,Node _parent) + { + List<Node> children = _parent.getChildren(); + Node target = _path.poll(); + for(int i = 0;i < children.size();i ++){ + Node _child = children.get(i); + if(_child == target){ + //clone node + Node newNode = cloneNode(target); + m_target = newNode; // look out!! + + //remove old node from clonedTree + _parent.addChild(newNode); + _parent.removeChild(target); + + cloneTree(_path,newNode); + + break; + } + } + } + + private LinkedList<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.getChildren()){ + if(findPath(child,_child,_list)){ + _list.addFirst(child); + return true; + } + } + return false; + } +}
--- a/src/treecms/proto/edit/EditableTreeBuilder.java Tue Nov 02 18:44:21 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,85 +0,0 @@ -package treecms.proto.edit; - -import java.util.LinkedList; -import java.util.List; -import treecms.proto.api.Node; -import treecms.proto.api.TreeBuilder; -import treecms.proto.simple.SimpleTreeBuilder; - -public class EditableTreeBuilder extends SimpleTreeBuilder -{ - private Node m_newRoot; - private Node m_target; //node that wanted to edit. - - public EditableTreeBuilder(Node _target,TreeBuilder _builder) - { - //search path - LinkedList<Node> path = findPath(_builder.getContents(),_target); - - //clone root node. - m_newRoot = cloneNode(path.poll()); - - cloneTree(path,m_newRoot.getChildList()); - } - - public Node getTargetNode() - { - return m_target; - } - - private void cloneTree(LinkedList<Node> _path,List<Node> _children) - { - Node target = _path.poll(); - for(int i = 0;i < _children.size();i ++){ - Node _child = _children.get(i); - if(_child == target){ - //clone node - Node newNode = cloneNode(target); - m_target = newNode; // look out!! - - //remove old node from clonedTree - _children.add(i,newNode); - _children.remove(target); - - cloneTree(_path,newNode.getChildList()); - - break; - } - } - } - - public List<Node> findPathTest(Node _root,Node _child) - { - return findPath(_root,_child); - } - - private LinkedList<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; - } -}
--- a/src/treecms/proto/id/IncrementalNodeID.java Tue Nov 02 18:44:21 2010 +0900 +++ b/src/treecms/proto/id/IncrementalNodeID.java Sun Nov 07 14:07:03 2010 +0900 @@ -1,6 +1,7 @@ package treecms.proto.id; import treecms.proto.api.NodeID; +import java.util.UUID; import java.util.concurrent.atomic.AtomicLong; public class IncrementalNodeID implements NodeID @@ -10,26 +11,23 @@ private AtomicLong m_tip; - public IncrementalNodeID(String _inheritedID,AtomicLong _version) + public IncrementalNodeID() + { + m_inheritedID = UUID.randomUUID().toString(); + m_tip = new AtomicLong(); + m_version = m_tip.getAndIncrement(); + } + + private IncrementalNodeID(String _inheritedID,AtomicLong _version) { m_inheritedID = _inheritedID; m_version = _version.getAndIncrement(); m_tip = _version; } - String getInheritedID() - { - return m_inheritedID; - } - - long getVersion() + public NodeID update() { - return m_version; - } - - AtomicLong getTip() - { - return m_tip; + return new IncrementalNodeID(m_inheritedID,m_tip); } @Override
--- a/src/treecms/proto/id/IncrementalNodeIDFactory.java Tue Nov 02 18:44:21 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -package treecms.proto.id; - -import java.util.UUID; -import java.util.concurrent.atomic.AtomicLong; - -import treecms.proto.api.NodeID; -import treecms.proto.api.NodeIDFactory; - -public class IncrementalNodeIDFactory implements NodeIDFactory -{ - @Override - public NodeID createNewID() - { - // TODO Auto-generated method stub - return new IncrementalNodeID(UUID.randomUUID().toString(),new AtomicLong()); - } - - @Override - public NodeID updateExistID(NodeID _id) - { - // TODO Auto-generated method stub - IncrementalNodeID id = (IncrementalNodeID)_id; - return new IncrementalNodeID(id.getInheritedID(),id.getTip()); - } -}
--- a/src/treecms/proto/simple/SimpleNode.java Tue Nov 02 18:44:21 2010 +0900 +++ b/src/treecms/proto/simple/SimpleNode.java Sun Nov 07 14:07:03 2010 +0900 @@ -1,33 +1,44 @@ package treecms.proto.simple; import java.util.Iterator; +import java.util.Collections; import java.util.LinkedList; import java.util.List; import treecms.proto.api.Node; import treecms.proto.api.NodeID; +import treecms.proto.api.NodeParameters; +import treecms.proto.api.NodeParameters.Types; +import treecms.proto.id.IncrementalNodeID; public class SimpleNode implements Node { private LinkedList<Node> m_children; - private String m_class; - private String m_title; private NodeID m_id; + private NodeParameters m_params; - public SimpleNode(NodeID _id) + public SimpleNode() { m_children = new LinkedList<Node>(); - m_class = ""; - m_title = ""; + + m_id = new IncrementalNodeID(); + m_params = new NodeParameters(); + } + + private SimpleNode(NodeID _id) + { + m_children = new LinkedList<Node>(); m_id = _id; + m_params = new NodeParameters(); } @Override - public List<Node> getChildList() + public List<Node> getChildren() { // TODO Auto-generated method stub - return m_children; + // return unmodifiable list<node> + return Collections.unmodifiableList(m_children); } @Override @@ -37,9 +48,12 @@ } @Override - public void addChild(Node _child) { + public Node addChild(NodeParameters _params) { // TODO Auto-generated method stub - m_children.add(_child); + SimpleNode child = new SimpleNode(); + child.setParameters(_params); + m_children.add(child); + return child; } @Override @@ -67,33 +81,10 @@ } @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_children.iterator(); + // return unmodifiable iterator + return Collections.unmodifiableList(m_children).iterator(); } @Override @@ -102,9 +93,46 @@ return this.m_id; } - @Override public void addChildren(List<Node> _children) { // TODO Auto-generated method stub this.m_children.addAll(_children); } + + @Override + public NodeParameters getParameters() + { + // TODO Auto-generated method stub + return new NodeParameters(m_params); + } + + @Override + public void setParameters(NodeParameters _params) + { + // TODO Auto-generated method stub + m_params = new NodeParameters(_params); + } + + @Override + public Node cloneNode() + { + // TODO Auto-generated method stub + SimpleNode clone = new SimpleNode(m_id.update()); + clone.setParameters(m_params); + clone.addChildren(getChildren()); + return clone; + } + + @Override + public String getParameter(Types _type) + { + // TODO Auto-generated method stub + return m_params.getParameter(_type); + } + + @Override + public void setParameter(Types _type, String _value) + { + // TODO Auto-generated method stub + m_params.setParameter(_type,_value); + } }
--- a/src/treecms/proto/simple/SimpleTreeBuilder.java Tue Nov 02 18:44:21 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,43 +0,0 @@ -package treecms.proto.simple; - -import treecms.proto.api.Node; -import treecms.proto.api.TreeBuilder; -import treecms.proto.id.IncrementalNodeIDFactory; - -public class SimpleTreeBuilder implements TreeBuilder -{ - private Node m_root; - private IncrementalNodeIDFactory m_idFactory; - - public SimpleTreeBuilder() - { - m_idFactory = new IncrementalNodeIDFactory(); - m_root = createNode(); - } - - public Node getContents() - { - return m_root; - } - - public Node createNode() - { - return new SimpleNode(m_idFactory.createNewID()); - } - - @Override - public Node cloneNode(Node _node) - { - // TODO Auto-generated method stub - if(!(_node instanceof SimpleNode)){ - throw new IllegalArgumentException("cant clone , _node is not instance of SimpleNode"); - } - - SimpleNode clone = new SimpleNode(m_idFactory.updateExistID(_node.getID())); - clone.setClassName(_node.getClassName()); - clone.setTitle(_node.getTitle()); - clone.addChildren(_node.getChildList()); - - return clone; - } -}
--- a/src/treecms/proto/test/EditableTreeBuilderTest1.java Tue Nov 02 18:44:21 2010 +0900 +++ b/src/treecms/proto/test/EditableTreeBuilderTest1.java Sun Nov 07 14:07:03 2010 +0900 @@ -1,5 +1,10 @@ package treecms.proto.test; + + +import org.junit.Test; +import org.junit.runner.JUnitCore; + import treecms.proto.api.*; import treecms.proto.edit.EditableTreeBuilder; import treecms.proto.simple.*; @@ -8,9 +13,13 @@ { public static void main(String _args[]) { - new EditableTreeBuilderTest1(); + JUnitCore.main(EditableTreeBuilderTest1.class.getName()); } + private Node m_root; + private Node m_target1; + private Node m_target2; + public EditableTreeBuilderTest1() { @@ -19,57 +28,68 @@ root.setTitle("root"); Node child1 = builder.createNode(); - child1.setTitle("+-child1"); + child1.setTitle("child1"); Node child2 = builder.createNode(); - child2.setTitle("+-child2"); + child2.setTitle("child2"); root.addChild(child1); root.addChild(child2); Node child11 = builder.createNode(); - child11.setTitle(" +-child11"); + child11.setTitle("child11"); Node child12 = builder.createNode(); - child12.setTitle(" +-child12"); + child12.setTitle("child12"); Node child13 = builder.createNode(); - child13.setTitle(" +-child13"); + child13.setTitle("child13"); child1.addChild(child11); child1.addChild(child12); child1.addChild(child13); Node child21 = builder.createNode(); - child21.setTitle(" +-child21"); + child21.setTitle("child21"); child2.addChild(child21); Node child211 = builder.createNode(); - child211.setTitle(" +-child211"); + child211.setTitle("child211"); Node child212 = builder.createNode(); - child212.setTitle(" +-child212"); + child212.setTitle("child212"); child21.addChild(child211); child21.addChild(child212); - PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(builder.getContents()); - System.out.println("original........................................"); - print(walker); - - EditableTreeBuilder builder2 = new EditableTreeBuilder(child212,builder); - builder2.getTargetNode().setTitle("cloned child212"); - walker = new PreOrderTreeWalkerRecursive(builder2.getContents()); - System.out.println("cloned.........................................."); - print(walker); - - builder2 = new EditableTreeBuilder(builder2.getTargetNode(),builder); - walker = new PreOrderTreeWalkerRecursive(builder2.getContents()); - System.out.println("cloned.........................................."); - print(walker); + m_root = root; + m_target1 = child212; + m_target2 = child211; } - private void print(PreOrderTreeWalkerRecursive _walker) + @Test + public void testClone() { - for(Node node : _walker){ - System.out.println(node.getTitle() + "\t\tID:" + node.getID()); + Node target; + EditableTreeBuilder builder; + builder = new EditableTreeBuilder(m_root,m_target1); + target = builder.getTargetNode(); + target.setTitle("*"+m_target1.getTitle()); + System.out.println("-----------------------------------------------"); + print(builder.getContents(),0); + + builder = new EditableTreeBuilder(builder.getContents(),m_target2); + target = builder.getTargetNode(); + target.setTitle("*"+m_target2.getTitle()); + System.out.println("-----------------------------------------------"); + print(builder.getContents(),0); + } + + private void print(Node _root,int _indent) + { + for(int i = 0;i < _indent;i ++){ + System.out.print("\t"); + } + System.out.println(_root.getTitle()+"["+_root.getID()+"]"); + for(Node child : _root.getChildren()){ + print(child,_indent+1); } } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/test/IncrementalNodeIDTest1.java Sun Nov 07 14:07:03 2010 +0900 @@ -0,0 +1,95 @@ +package treecms.proto.test; + +import java.util.concurrent.ExecutorService; + +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import org.junit.Test; +import org.junit.Assert; +import org.junit.runner.JUnitCore; + +import treecms.proto.api.NodeID; +import treecms.proto.id.IncrementalNodeID; + +public class IncrementalNodeIDTest1 +{ + public static void main(String _args[]) + { + JUnitCore.main(IncrementalNodeIDTest1.class.getName()); + } + + public IncrementalNodeIDTest1() + { + } + + @Test + public void testIDstartsWithZero() + { + NodeID id = new IncrementalNodeID(); + long ver = Long.parseLong(id.toString().split("@")[1]); + Assert.assertEquals(ver,0); + } + + @Test + public void testUpdateIDFromLatestID() + { + int count = 100; + NodeID id = new IncrementalNodeID(); + + for(int i = 1;i <= count;i ++){ + id = id.update(); + long ver = Long.parseLong(id.toString().split("@")[1]); + Assert.assertEquals(ver,i); + } + } + + @Test + public void testUpdateIDFromOldID() + { + int count = 100; + NodeID id0 = new IncrementalNodeID(); + + for(int i = 1;i <= count;i ++){ + NodeID id = id0.update(); + long ver = Long.parseLong(id.toString().split("@")[1]); + Assert.assertEquals(ver,i); + } + } + + @Test + public void testMultiThreadedUpdateID() + { + final int threads = 10; + final int count = 100; + ExecutorService service = Executors.newFixedThreadPool(threads); + + final NodeID id = new IncrementalNodeID(); + for(int i = 0;i < threads;i ++){ + service.execute(new Runnable(){ + @Override + public void run() + { + // TODO Auto-generated method stub + for(int j = 0;j < count;j ++){ + id.update(); + } + } + }); + } + + service.shutdown(); + + try { + service.awaitTermination(1,TimeUnit.HOURS); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + //check id + NodeID tip = id.update(); + long ver = Long.parseLong(tip.toString().split("@")[1]); + Assert.assertEquals(ver,threads*count + 1); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/test/LinkedListTest1.java Sun Nov 07 14:07:03 2010 +0900 @@ -0,0 +1,22 @@ +package treecms.proto.test; + +import java.util.LinkedList; + +public class LinkedListTest1 +{ + public static void main(String _args[]) + { + LinkedList<Integer> list1 = new LinkedList<Integer>(); + list1.add(1); + list1.add(2); + list1.add(3); + + @SuppressWarnings("unchecked") + LinkedList<Integer> list2 = (LinkedList<Integer>) list1.clone(); + list2.remove(1); + + for(Integer num : list1){ + System.out.println(num); + } + } +}
--- a/src/treecms/proto/test/PreOrderTreeWalkerRecurisiveTest1.java Tue Nov 02 18:44:21 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,58 +0,0 @@ -package treecms.proto.test; - - -import treecms.proto.api.*; -import treecms.proto.simple.*; -import treecms.proto.edit.EditableTreeBuilder; - -public class PreOrderTreeWalkerRecurisiveTest1 -{ - public static void main(String _arg[]) - { - //create test tree. - 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); - - - //create editable tree. - EditableTreeBuilder builder1 = new EditableTreeBuilder(child212,builder); - PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(builder1.getContents()); - - for(Node node : walker){ - System.out.println(node.getTitle() + "\t\tID:" + node.getID()); - } - } -}
--- a/src/treecms/proto/test/PreOrderTreeWalkerRecursive.java Tue Nov 02 18:44:21 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,89 +0,0 @@ -package treecms.proto.test; -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; - } - */ -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/test/PreOrderTreeWalkerTest1.java Sun Nov 07 14:07:03 2010 +0900 @@ -0,0 +1,43 @@ +package treecms.proto.test; + + +import treecms.proto.api.*; +import treecms.proto.api.NodeParameters.Types; +import treecms.proto.simple.*; +import treecms.proto.util.PreOrderTreeWalker; + +public class PreOrderTreeWalkerTest1 +{ + public static void main(String _arg[]) + { + //create test tree. + Node root = new SimpleNode(); + root.setParameter(Types.TITLE,"root"); + + Node child1 = root.addChild(new NodeParameters()); + child1.setParameter(Types.TITLE,"child1"); + Node child2 = root.addChild(new NodeParameters()); + child2.setParameter(Types.TITLE,"child2"); + + Node child11 = child1.addChild(new NodeParameters()); + child11.setParameter(Types.TITLE,"child11"); + Node child12 = child1.addChild(new NodeParameters()); + child12.setParameter(Types.TITLE,"child12"); + Node child13 = child1.addChild(new NodeParameters()); + child13.setParameter(Types.TITLE,"child13"); + + Node child21 = child2.addChild(new NodeParameters()); + child21.setParameter(Types.TITLE,"child21"); + + Node child211 = child21.addChild(new NodeParameters()); + child211.setParameter(Types.TITLE,"child211"); + Node child212 = child21.addChild(new NodeParameters()); + child212.setParameter(Types.TITLE,"child212"); + + PreOrderTreeWalker walker = new PreOrderTreeWalker(root); + + for(Node node : walker){ + System.out.println(node.getParameter(Types.TITLE) + "\t\tID:" + node.getID()); + } + } +}
--- a/src/treecms/proto/test/RandomUUIDTest.java Tue Nov 02 18:44:21 2010 +0900 +++ b/src/treecms/proto/test/RandomUUIDTest.java Sun Nov 07 14:07:03 2010 +0900 @@ -1,46 +1,46 @@ package treecms.proto.test; -import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import org.junit.Test; +import org.junit.runner.JUnitCore; +import java.util.UUID; public class RandomUUIDTest { public static void main(String _args[]) { - //test1(); - //test2(); - test3(10); - } - - public static void test1() - { - while(true){ - System.out.println(UUID.randomUUID().toString()); - } + JUnitCore.main(RandomUUIDTest.class.getName()); } - public static void test2() + @Test + public void testUUIDGenerateMultiThread() { - while(true){ - System.out.println(UUID.fromString(""+System.currentTimeMillis()+"")); - } - } - - public static void test3(int _threadCount) - { - ExecutorService service = Executors.newFixedThreadPool(_threadCount); + final int threads = 10; + final int count = 100000; + ExecutorService service = Executors.newFixedThreadPool(threads); - for(int i = 0;i < _threadCount;i ++){ + for(int i = 0;i < threads;i ++){ service.execute(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub - while(true){ - UUID.randomUUID().toString(); + for(int i = 0;i < count;i ++){ + UUID.randomUUID(); } } }); } + + service.shutdown(); + + try { + service.awaitTermination(Long.MAX_VALUE,TimeUnit.DAYS); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } }
--- a/src/treecms/proto/test/SearchPathTest1.java Tue Nov 02 18:44:21 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,72 +0,0 @@ -package treecms.proto.test; - -import java.util.Iterator; - -import java.util.LinkedList; - -import treecms.proto.api.*; -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()); - } - } -}
--- a/src/treecms/proto/test/SimpleNodeTest1.java Tue Nov 02 18:44:21 2010 +0900 +++ b/src/treecms/proto/test/SimpleNodeTest1.java Sun Nov 07 14:07:03 2010 +0900 @@ -1,56 +1,83 @@ package treecms.proto.test; -import java.util.Iterator; +import static org.hamcrest.core.IsNull.notNullValue; +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.*; +import java.util.List; + public class SimpleNodeTest1 { public static void main(String _arg[]) { - TreeBuilder builder = new SimpleTreeBuilder(); - Node root = builder.getContents(); - root.setTitle("root"); + JUnitCore.main(SimpleNodeTest1.class.getName()); + } + + @Test + public void testCreateNode() + { + Node node = new SimpleNode(); + Assert.assertThat(node,notNullValue()); - Node child1 = builder.createNode(); - child1.setTitle("+-child1"); - Node child2 = builder.createNode(); - child2.setTitle("+-child2"); + String clsName = "test1"; + node.setParameter(NodeParameters.Types.CLASS,clsName); + Assert.assertEquals(node.getParameter(NodeParameters.Types.CLASS),clsName); - root.addChild(child1); - root.addChild(child2); + String title = "test2"; + node.setParameter(NodeParameters.Types.TITLE,title); + Assert.assertEquals(node.getParameter(NodeParameters.Types.TITLE),title); + } - Node child11 = builder.createNode(); - child11.setTitle(" +-child11"); - Node child12 = builder.createNode(); - child12.setTitle(" +-child12"); - Node child13 = builder.createNode(); - child13.setTitle(" +-child13"); + @Test + public void testClone() + { + Node node = new SimpleNode(); + + String clsName = "test1"; + node.setParameter(NodeParameters.Types.CLASS,clsName); + + String title = "test2"; + node.setParameter(NodeParameters.Types.TITLE,title); - child1.addChild(child11); - child1.addChild(child12); - child1.addChild(child13); - - Node child21 = builder.createNode(); - child21.setTitle(" +-child21"); - - child2.addChild(child21); + Node clone = node.cloneNode(); + Assert.assertThat(clone.getID(),not(node.getID())); + Assert.assertEquals(clone.getParameter(NodeParameters.Types.CLASS),node.getParameter(NodeParameters.Types.CLASS)); + Assert.assertEquals(clone.getParameter(NodeParameters.Types.TITLE),node.getParameter(NodeParameters.Types.TITLE)); + } + + @Test + public void testChildOperation() + { + Node node = new SimpleNode(); + Node child1 = node.addChild(new NodeParameters()); + Node child2 = node.addChild(new NodeParameters()); + Node child3 = node.addChild(new NodeParameters()); - Node child211 = builder.createNode(); - child211.setTitle(" +-child211"); - Node child212 = builder.createNode(); - child212.setTitle(" +-child212"); - - child21.addChild(child211); - child21.addChild(child212); + //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); + //1,2,3 -> 2,3,1 + node.down(child1); + node.up(child3); - PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(root); - Iterator<Node> itr = walker.iterator(); - while(itr.hasNext()){ - Node node = itr.next(); - System.out.println(node.getTitle()+"\t id:"+node.getID().toString()); - } + Assert.assertEquals(children.get(0),child2); + Assert.assertEquals(children.get(1),child3); + Assert.assertEquals(children.get(2),child1); + + //remove + node.removeChild(child3); + + Assert.assertEquals(children.get(0),child2); + Assert.assertEquals(children.get(1),child1); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treecms/proto/util/PreOrderTreeWalker.java Sun Nov 07 14:07:03 2010 +0900 @@ -0,0 +1,76 @@ +package treecms.proto.util; +import java.util.Iterator; + +import java.util.LinkedList; + +import treecms.proto.api.Node; + +public class PreOrderTreeWalker implements Iterable<Node> +{ + private Node m_root; + + public PreOrderTreeWalker(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.getChildren()){ + 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.getChildren()){ + if (findPath(child,node,list)) { + list.addFirst(child); + return true; + } + } + return false; // backtrack + } +}