(no commit message)
[utils] / system / general / src / main / java / org / wamblee / system / graph / Graph.java
index ddfd2cf783a9003bb5a3db98d20ff02eb0631455..9078b92014438ab69ded3e1d67c7f8147f1450de 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2008 the original author or authors.
+ * Copyright 2005-2010 the original author or authors.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,8 +25,8 @@ import java.util.List;
  * @author Erik Brakkee
  */
 public class Graph {
-
     private List<Node> nodes;
+
     private List<Edge> edges;
 
     /**
@@ -42,15 +42,17 @@ public class Graph {
      * 
      * @param aNode
      *            Node to add.
+     * 
      * @throws IllegalArgumentException
      *             In case the node already exists. Node equality is checked
      *             using <code>equals</code>.
      */
     public void addNode(Node aNode) {
         if (nodes.contains(aNode)) {
-            throw new IllegalArgumentException("Node '" + aNode.getName()
-                    + "' already exists");
+            throw new IllegalArgumentException("Node '" + aNode.getName() +
+                "' already exists");
         }
+
         nodes.add(aNode);
     }
 
@@ -59,6 +61,7 @@ public class Graph {
      * 
      * @param aName
      *            Node name.
+     * 
      * @return Node or null if not found.
      */
     public Node findNode(String aName) {
@@ -67,6 +70,7 @@ public class Graph {
                 return node;
             }
         }
+
         return null;
     }
 
@@ -75,16 +79,19 @@ public class Graph {
      * 
      * @param aNode
      *            Node to remove.
+     * 
      * @return True iff the node was removed.
+     * 
      * @throws IllegalArgumentException
      *             In case there are edges of which the node is a part.
      */
     public boolean removeNode(Node aNode) {
         if (!findOutgoing(aNode).isEmpty() || !findIncoming(aNode).isEmpty()) {
-            throw new IllegalArgumentException("Cannot remove node '"
-                    + aNode.getName()
-                    + "' because it is connected to one or more edges");
+            throw new IllegalArgumentException("Cannot remove node '" +
+                aNode.getName() +
+                "' because it is connected to one or more edges");
         }
+
         return nodes.remove(aNode);
     }
 
@@ -107,6 +114,7 @@ public class Graph {
      * 
      * @param aEdge
      *            Edge to add.
+     * 
      * @throws IllegalArgumentException
      *             In case one of the nodes of the edges is not part of the
      *             graph or if the same edge (as determined by
@@ -114,32 +122,40 @@ public class Graph {
      */
     public void addEdge(Edge aEdge) {
         if (edges.contains(aEdge)) {
-            throw new IllegalArgumentException("Edge '" + aEdge
-                    + "' already exists");
+            throw new IllegalArgumentException("Edge '" + aEdge +
+                "' already exists");
         }
+
         if (!nodes.contains(aEdge.getFrom())) {
-            throw new IllegalArgumentException("From node '" + aEdge.getFrom()
-                    + "' from edge '" + aEdge + "' is not part of the graph");
+            throw new IllegalArgumentException("From node '" + aEdge.getFrom() +
+                "' from edge '" + aEdge + "' is not part of the graph");
         }
+
         if (!nodes.contains(aEdge.getTo())) {
-            throw new IllegalArgumentException("To node '" + aEdge.getTo()
-                    + "' from edge '" + aEdge + "' is not part of the graph");
+            throw new IllegalArgumentException("To node '" + aEdge.getTo() +
+                "' from edge '" + aEdge + "' is not part of the graph");
         }
+
         edges.add(aEdge);
     }
 
     /**
-     * Removes an edge. 
-     * @param aEdge Edge to remove. 
-     * @return True if the edge was removed. 
+     * Removes an edge.
+     * 
+     * @param aEdge
+     *            Edge to remove.
+     * 
+     * @return True if the edge was removed.
      */
     public boolean removeEdge(Edge aEdge) {
         return edges.remove(aEdge);
     }
 
     /**
-     * Adds a number of edges. 
-     * @param aEdges Edges to add. 
+     * Adds a number of edges.
+     * 
+     * @param aEdges
+     *            Edges to add.
      */
     public void addEdges(List<Edge> aEdges) {
         for (Edge edge : aEdges) {
@@ -148,25 +164,29 @@ public class Graph {
     }
 
     /**
-     * Gets the nodes. 
-     * @return Copy of the list of nodes of the graph. 
+     * Gets the nodes.
+     * 
+     * @return Copy of the list of nodes of the graph.
      */
     public List<Node> getNodes() {
         return new ArrayList<Node>(nodes);
     }
 
     /**
-     * Gets the edges. 
-     * @return Copy of the list of edges of the graph. 
+     * Gets the edges.
+     * 
+     * @return Copy of the list of edges of the graph.
      */
     public List<Edge> getEdges() {
         return new ArrayList<Edge>(edges);
     }
 
     /**
-     * Extends the graph with edges using an edge factory. All combinations of 
-     * nodes are passed to the factory which creates additional edges. 
-     * @param aFactory Edge factory. 
+     * Extends the graph with edges using an edge factory. All combinations of
+     * nodes are passed to the factory which creates additional edges.
+     * 
+     * @param aFactory
+     *            Edge factory.
      */
     public void extend(EdgeFactory aFactory) {
         for (Node from : nodes) {
@@ -175,68 +195,84 @@ public class Graph {
             }
         }
     }
-    
+
     /**
-     * Applies a filter to the graph for removing elements. 
-     * @param aFilter Filter to apply. 
+     * Applies a filter to the graph for removing elements.
+     * 
+     * @param aFilter
+     *            Filter to apply.
      */
     public void applyFilter(EdgeFilter aFilter) {
-        for (Iterator<Edge> edge = edges.iterator(); edge.hasNext(); ) { 
-            if (aFilter.isViolated(edge.next())) { 
+        for (Iterator<Edge> edge = edges.iterator(); edge.hasNext();) {
+            if (aFilter.isViolated(edge.next())) {
                 edge.remove();
             }
         }
     }
 
     /**
-     * Finds all outgoing edges of a node. More specifically, finds 
-     * all edges <code>e</code> for which <code>e.getFrom().getName() = aNode.getName()</code>.
-     * @param aNode Node for which to find outgoing edges. 
-     * @return List of outgoing edges. 
+     * Finds all outgoing edges of a node. More specifically, finds all edges
+     * <code>e</code> for which <code>e.getFrom().getName() =
+     * aNode.getName()</code>.
+     * 
+     * @param aNode
+     *            Node for which to find outgoing edges.
+     * 
+     * @return List of outgoing edges.
      */
     public List<Edge> findOutgoing(Node aNode) {
         List<Edge> result = new ArrayList<Edge>();
+
         for (Edge edge : edges) {
             if (edge.getFrom().getName().equals(aNode.getName())) {
                 result.add(edge);
             }
         }
+
         return result;
     }
 
     /**
-     * Finds all incoming edges of a node. 
-     * More specifically, finds 
-     * all edges <code>e</code> for which <code>e.getTo().getName() = aNode.getName()</code>.
-     * @param aNode Node for which to find incoming edges. 
-     * @return List of incoming edges. 
+     * Finds all incoming edges of a node. More specifically, finds all edges
+     * <code>e</code> for which <code>e.getTo().getName() =
+     * aNode.getName()</code>.
+     * 
+     * @param aNode
+     *            Node for which to find incoming edges.
+     * 
+     * @return List of incoming edges.
      */
     public List<Edge> findIncoming(Node aNode) {
         List<Edge> result = new ArrayList<Edge>();
+
         for (Edge edge : edges) {
             if (edge.getTo().getName().equals(aNode.getName())) {
                 result.add(edge);
             }
         }
+
         return result;
     }
 
     /**
-     * Implements a visitor design pattern.
-     * This loops over all nodes and all edges and invokes the appropriate visit
-     * methods on the visitor.  
-     * @param aVisitor Visitor. 
+     * Implements a visitor design pattern. This loops over all nodes and all
+     * edges and invokes the appropriate visit methods on the visitor.
+     * 
+     * @param aVisitor
+     *            Visitor.
      */
     public void accept(Visitor aVisitor) {
-        List<Node> nodes = getNodes(); // copy to make sure the visitor can
+        List<Node> allNodes = getNodes(); // copy to make sure the visitor can
         // modify the
         // list of nodes as part of the loop.
-        List<Edge> edges = getEdges(); // copy ..... (see above).
 
-        for (Node node : nodes) {
+        List<Edge> allEdges = getEdges(); // copy ..... (see above).
+
+        for (Node node : allNodes) {
             aVisitor.visitNode(node);
         }
-        for (Edge edge : edges) {
+
+        for (Edge edge : allEdges) {
             aVisitor.visitEdge(edge);
         }
     }