/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2016 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2016 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * OpenNMS(R) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <license@opennms.org>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.features.topology.plugins.topo.graphml.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class GraphMLGraph extends GraphMLElement {

    private List<GraphMLEdge> edges = new ArrayList<>();

    private List<GraphMLNode> nodes = new ArrayList<>();

    public void addEdge(GraphMLEdge edge) {
        edges.add(edge);
    }

    public void addNode(GraphMLNode node) {
        nodes.add(node);
    }

    public List<GraphMLEdge> getEdges() {
        return edges;
    }

    public List<GraphMLNode> getNodes() {
        return nodes;
    }

    public GraphMLNode getNodeById(String id) {
        return getNodes().stream().filter(node -> node.getId().equals(id)).findFirst().orElse(null);
    }

    public GraphMLEdge getEdgeById(String id) {
        return getEdges().stream().filter(edge -> edge.getId().equals(id)).findFirst().orElse(null);
    }

    @Override
    public <T> T accept(GraphMLElementVisitor<T> visitor) {
        return visitor.visit(this);
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), edges, nodes);
    }

    @Override
    public boolean equals(Object obj) {
        boolean equals = super.equals(obj);
        if (equals) {
            if (obj instanceof GraphMLGraph) {
                GraphMLGraph other = (GraphMLGraph) obj;
                equals = Objects.equals(edges, other.edges) && Objects.equals(nodes, other.nodes);
                return equals;
            }
        }
        return false;
    }
}

@@ -1279,25 +1279,26 @@
 va.util.
-ArrayList
+Collection
 ;%0Aimport
@@ -1310,18 +1310,27 @@
 .util.Li
-st
+nkedHashMap
 ;%0Aimport
@@ -1410,29 +1410,52 @@
     private 
-List%3C
+final LinkedHashMap%3CString, 
 GraphMLEdge%3E
@@ -1460,39 +1460,46 @@
 e%3E edges
+ById
  = new 
-ArrayList
+LinkedHashMap
 %3C%3E();%0A
-%0A
     priv
@@ -1502,21 +1502,44 @@
 private 
-List%3C
+final LinkedHashMap%3CString, 
 GraphMLN
@@ -1551,25 +1551,32 @@
 node
-s
+ById
  = new 
-ArrayList
+LinkedHashMap
 %3C%3E()
@@ -1635,21 +1635,39 @@
    edges
-.add(
+ById.put(edge.getId(), 
 edge);%0A 
@@ -1732,14 +1732,31 @@
 node
-s.add(
+ById.put(node.getId(), 
 node
@@ -1768,36 +1768,42 @@
   %7D%0A%0A    public 
-List
+Collection
 %3CGraphMLEdge%3E ge
@@ -1833,16 +1833,29 @@
 rn edges
+ById.values()
 ;%0A    %7D%0A
@@ -1870,12 +1870,18 @@
 lic 
-List
+Collection
 %3CGra
@@ -1922,17 +1922,29 @@
 urn node
-s
+ById.values()
 ;%0A    %7D%0A
@@ -2011,247 +2011,112 @@
 urn 
-getNodes().stream().filter(node -%3E node.getId().equals(id)).findFirst().orElse(null);%0A    %7D%0A%0A    public GraphMLEdge getEdgeById(String id) %7B%0A        return getEdges().stream().filter(edge -%3E edge.getId().equals(id)).findFirst().orElse(null
+nodeById.get(id);%0A    %7D%0A%0A    public GraphMLEdge getEdgeById(String id) %7B%0A        return edgesById.get(id
 );%0A 
@@ -2335,15 +2335,22 @@
 dges
+ById
 , node
-s
+ById
 );%0A 
@@ -2624,16 +2624,20 @@
 ls(edges
+ById
 , other.
@@ -2641,16 +2641,20 @@
 er.edges
+ById
 ) && Obj
@@ -2669,17 +2669,20 @@
 als(node
-s
+ById
 , other.
@@ -2685,17 +2685,20 @@
 her.node
-s
+ById
 );%0A     

Index the nodes and edges by id in order to improve the graph loading time.