← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15555: use map internally in AbstractNode, speeds up lookup based on name, also enforces name uniqueness...

 

------------------------------------------------------------
revno: 15555
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-06-04 20:33:34 +0200
message:
  use map internally in AbstractNode, speeds up lookup based on name, also enforces name uniqueness (might be name+namespace in the future)
added:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/exception/DuplicateNodeException.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/AbstractNode.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/Node.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/exception/InvalidTypeException.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/types/SimpleNode.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/AbstractNode.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/AbstractNode.java	2014-06-04 10:49:04 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/AbstractNode.java	2014-06-04 18:33:34 +0000
@@ -29,9 +29,12 @@
  */
 
 import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import org.hisp.dhis.node.exception.DuplicateNodeException;
 import org.hisp.dhis.node.exception.InvalidTypeException;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -46,7 +49,7 @@
 
     private String comment;
 
-    private final List<Node> nodes = Lists.newArrayList();
+    private Map<String, Node> children = Maps.newHashMap();
 
     protected AbstractNode( String name, NodeType nodeType )
     {
@@ -96,27 +99,74 @@
     @Override
     public <T extends Node> T addChild( T child ) throws InvalidTypeException
     {
-        if ( child == null )
+        if ( child == null || child.getName() == null )
         {
             return null;
         }
 
-        nodes.add( child );
+        if ( children.containsKey( child.getName() ) )
+        {
+            throw new DuplicateNodeException();
+        }
+
+        children.put( child.getName(), child );
         return child;
     }
 
     @Override
     public <T extends Node> void addChildren( Iterable<T> children )
     {
-        for ( Node node : children )
-        {
-            addChild( node );
-        }
+        for ( Node child : children )
+        {
+            addChild( child );
+        }
+    }
+
+    @Override
+    public Node getChild( String name )
+    {
+        if ( children.containsKey( name ) )
+        {
+            return children.get( name );
+        }
+
+        return null;
     }
 
     @Override
     public List<Node> getChildren()
     {
-        return nodes;
+        return Lists.newArrayList( children.values() );
+    }
+
+    @Override
+    public boolean equals( Object o )
+    {
+        if ( this == o ) return true;
+        if ( o == null || getClass() != o.getClass() ) return false;
+
+        AbstractNode that = (AbstractNode) o;
+
+        if ( name != null ? !name.equals( that.name ) : that.name != null ) return false;
+
+        return true;
+    }
+
+    @Override
+    public int hashCode()
+    {
+        return name != null ? name.hashCode() : 0;
+    }
+
+    @Override
+    public String toString()
+    {
+        return "Node{" +
+            "name='" + name + '\'' +
+            ", nodeType=" + nodeType +
+            ", namespace='" + namespace + '\'' +
+            ", comment='" + comment + '\'' +
+            ", children=" + children +
+            '}';
     }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/Node.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/Node.java	2014-06-04 10:49:04 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/Node.java	2014-06-04 18:33:34 +0000
@@ -83,5 +83,19 @@
      */
     <T extends Node> void addChildren( Iterable<T> children );
 
+    /**
+     * Returns child with name, or null if it doesn't exist.
+     *
+     * @param name Name to search for
+     * @return A node with that name if it exists, or null
+     */
+    Node getChild( String name );
+
+    /**
+     * Get all child notes associated with this node. Please note that the returned list is a copy
+     * of the internal list, and changes to the list will not be reflected in the node.
+     *
+     * @return List of child nodes associated with this node
+     */
     List<Node> getChildren();
 }

=== added file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/exception/DuplicateNodeException.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/exception/DuplicateNodeException.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/exception/DuplicateNodeException.java	2014-06-04 18:33:34 +0000
@@ -0,0 +1,40 @@
+package org.hisp.dhis.node.exception;
+
+/*
+ * Copyright (c) 2004-2014, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
+ */
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class DuplicateNodeException extends RuntimeException
+{
+    public DuplicateNodeException()
+    {
+        super( "A node with that name already exists in the child list." );
+    }
+}

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/exception/InvalidTypeException.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/exception/InvalidTypeException.java	2014-06-02 22:47:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/exception/InvalidTypeException.java	2014-06-04 18:33:34 +0000
@@ -33,8 +33,8 @@
  */
 public class InvalidTypeException extends RuntimeException
 {
-    public InvalidTypeException( String message )
+    public InvalidTypeException()
     {
-        super( message );
+        super( "Adding children to a node of type simple is not allowed." );
     }
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/types/SimpleNode.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/types/SimpleNode.java	2014-06-04 11:18:05 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/node/types/SimpleNode.java	2014-06-04 18:33:34 +0000
@@ -67,12 +67,12 @@
     @Override
     public <T extends Node> T addChild( T child ) throws InvalidTypeException
     {
-        throw new InvalidTypeException( "Adding children to a node of type simple is not allowed." );
+        throw new InvalidTypeException();
     }
 
     @Override
     public <T extends Node> void addChildren( Iterable<T> children )
     {
-        throw new InvalidTypeException( "Adding children to a node of type simple is not allowed." );
+        throw new InvalidTypeException();
     }
 }