Commits

Dmytro Kovalchuk committed 014f03e Draft

Refactoring and cleanup of API

Comments (0)

Files changed (7)

registry/src/main/java/net/anatolich/registry/AbstractItemNode.java

-/*
- * Copyright 2012 Anatolich <anatolich@anatolich.net>.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package net.anatolich.registry;
-
-/**
- * Leaf node which cannot have child nodes.
- * @author Anatolich <anatolich@anatolich.net>
- * @version 1.0 
- */
-public abstract class AbstractItemNode<T extends Attributes> extends AbstractNode<T> {
-
-    public AbstractItemNode(String name, T attributes) {
-        super(name, attributes, 0);
-    }
-
-    public AbstractItemNode(String name, T attributes, int position) {
-        super(name, attributes, position);
-    }
-
-    /**
-     * Does nothing. Actually call of this method may signalize that something is 
-     * wrong with algorithm or configuration.
-     * @param node 
-     */
-    @Override
-    public final void addChild(AbstractNode node) {
-    }
-
-    /**
-     * Does nothing.
-     * @param node 
-     */
-    @Override
-    public final void removeChild(AbstractNode node) {
-    }
-
-}

registry/src/main/java/net/anatolich/registry/AbstractNode.java

 
 /**
  * Tree element that holds required data.
+ *
  * @author Anatolich <anatolich@anatolich.net>
  * @version 1.0
  */
     }
 
     /**
-     * Gets position number. Node is closer to start of it's parent's node 
-     * children list when position is lower. 
-     * @return 
+     * Gets position number. Node is closer to start of it's parent's node
+     * children list when position is lower.
+     *
+     * @return
      */
     public final int position() {
         return position;
 
     /**
      * Comparator capable to correctly sort list of child nodes.
-     * @return 
+     *
+     * @return
      */
     protected static Comparator<AbstractNode> positionComparator() {
         return new PositionalComparator();
     }
 
     /**
-     * Adds child node to current node. Parent reference for added node is set 
+     * Adds child node to current node. Parent reference for added node is set
      * to point to current node.
-     * @param node 
+     *
+     * @param node
      */
     public void addChild(AbstractNode node) {
         AbstractNode existingNode = getChild(node.getName());
-        if( existingNode != null ){
+        if (existingNode != null) {
             int nodePosition = children.indexOf(existingNode);
             children.set(nodePosition, node); // replacing old node with the same name
         } else {
             children.add(node);
         }
-        
+
         Collections.sort(children, AbstractNode.positionComparator());
         node.setParent(this);
         fireNodeAdded(node);
 
     /**
      * Gets child node by name.
+     *
      * @param name node name.
-     * @return node with provided name or null if node with specified name is 
+     * @return node with provided name or null if node with specified name is
      * not present in child nodes list.
      */
     public final AbstractNode getChild(String name) {
     }
 
     /**
-     * Returns child node by it's sequential position in child node list. 
-     * @param index child node index. It's not {@link #position() } but simple index in list.
-     * @return child node specified by index. Will throw runtime exception when node index is not in range of child list.
+     * Returns child node by it's sequential position in child node list.
+     *
+     * @param index child node index. It's not {@link #position() } but simple
+     * index in list.
+     * @return child node specified by index. Will throw runtime exception when
+     * node index is not in range of child list.
      */
     public final AbstractNode getChild(int index) {
         return children.get(index);
             nodeChangeListener.nodeRemoved(new NodeChangeEvent(removedNode, this));
         }
     }
+
+    /**
+     * Abstract node that isn't capable to hold child nodes.
+     * @param <T> 
+     */
+    public static class LeafNode<T extends Attributes> extends AbstractNode<T> {
+
+        public LeafNode(String name, T attributes) {
+            super(name, attributes, 0);
+        }
+
+        public LeafNode(String name, T attributes, int position) {
+            super(name, attributes, position);
+        }
+
+        /**
+         * Does nothing. Actually call of this method may signalize that
+         * something is wrong with algorithm or configuration.
+         *
+         * @param node
+         */
+        @Override
+        public final void addChild(AbstractNode node) {
+        }
+
+        /**
+         * Does nothing.
+         *
+         * @param node
+         */
+        @Override
+        public final void removeChild(AbstractNode node) {
+        }
+    }
 }

registry/src/main/java/net/anatolich/registry/NodeChangeListener.java

     
     void nodeRemoved(NodeChangeEvent event);
     
-    void nodePositionChanges(NodeChangeEvent event);
+    void nodePositionChanged(NodeChangeEvent event);
     
 }

registry/src/main/java/net/anatolich/registry/Registry.java

     private NodeEventSupport registryEventSupport;
 
     public Registry() {
-        rootNode = new RootNode();
-        this.registryEventSupport = new NodeEventSupport();
-        this.rootNode.addNodeChangeListener(this);
+        this(new NodeEventSupport());
     }
 
     Registry(NodeEventSupport registryEventSupport) {
     }
 
     @Override
-    public void nodePositionChanges(NodeChangeEvent event) {
+    public void nodePositionChanged(NodeChangeEvent event) {
         throw new UnsupportedOperationException("Not supported yet.");
     }
 

registry/src/test/java/net/anatolich/registry/AbstractItemNodeTest.java

-/*
- * Copyright 2012 Anatolich <anatolich@anatolich.net>.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package net.anatolich.registry;
-
-import net.anatolich.registry.AbstractItemNode;
-import net.anatolich.registry.AbstractNode;
-import net.anatolich.registry.Attributes;
-import org.easymock.EasyMockSupport;
-import static org.junit.Assert.*;
-import org.junit.Before;
-import org.junit.Test;
-
-/**
- *
- * @author Anatolich <anatolich@anatolich.net>
- */
-public class AbstractItemNodeTest extends EasyMockSupport{
-   
-    private AbstractItemNode node;
-   
-    @Before
-    public void setUp() {
-        node = new AbstractItemNode("Name", createNiceMock(Attributes.class)) {};
-    }
-
-    @Test
-    public void testAddNode() {
-        node.addChild(createMock(AbstractNode.class));
-        
-        assertEquals(0, node.childrenCount());
-    }
-    
-    @Test
-    public void testRemoveNode() {
-        node.removeChild(createMock(AbstractNode.class));
-        
-        assertEquals(0, node.childrenCount());
-    }
-    
-    @Test(expected=IndexOutOfBoundsException.class)
-    public void testGetChild(){
-        node.getChild(0);
-        
-    }
-    
-    @Test
-    public void ensureEmptyIteratorReturned(){
-        assertFalse(node.childIterator().hasNext());
-    }
-}

registry/src/test/java/net/anatolich/registry/LeafNodeTest.java

+/*
+ * Copyright 2012 Anatolich <anatolich@anatolich.net>.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.anatolich.registry;
+
+import org.easymock.EasyMockSupport;
+import static org.junit.Assert.*;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ *
+ * @author Anatolich <anatolich@anatolich.net>
+ */
+public class LeafNodeTest extends EasyMockSupport{
+   
+    private AbstractNode.LeafNode node;
+   
+    @Before
+    public void setUp() {
+        node = new AbstractNode.LeafNode("Name", createNiceMock(Attributes.class)) {};
+    }
+
+    @Test
+    public void testAddNode() {
+        node.addChild(createMock(AbstractNode.class));
+        
+        assertEquals(0, node.childrenCount());
+    }
+    
+    @Test
+    public void testRemoveNode() {
+        node.removeChild(createMock(AbstractNode.class));
+        
+        assertEquals(0, node.childrenCount());
+    }
+    
+    @Test(expected=IndexOutOfBoundsException.class)
+    public void testGetChild(){
+        node.getChild(0);
+        
+    }
+    
+    @Test
+    public void ensureEmptyIteratorReturned(){
+        assertFalse(node.childIterator().hasNext());
+    }
+}

registry/src/test/java/net/anatolich/registry/RegistryTest.java

         }
     }
 
-    static class MenuItemNode extends AbstractItemNode<Attributes> {
+    static class MenuItemNode extends AbstractNode.LeafNode<Attributes> {
 
         public MenuItemNode(String name, int position) {
             super(name, new Attributes() {