Bug 4640: Change semantic-version to openconfig-version
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / TopologicalSort.java
index bb41743ef978a8a79b2886ed0e5cc95c133dcbbf..42db14607eed2e7b4d20dda9bd9156b99050a1e1 100644 (file)
@@ -7,21 +7,28 @@
  */
 package org.opendaylight.yangtools.yang.parser.util;
 
-import java.util.List;
-import java.util.Set;
-
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
 
 /**
  * Utility class that provides topological sort
  */
 public final class TopologicalSort {
 
+    /**
+     * It isn't desirable to create instance of this class
+     */
+    private TopologicalSort() {
+    }
+
     /**
      * Topological sort of dependent nodes in acyclic graphs.
      *
+     * @param nodes graph nodes
      * @return Sorted {@link List} of {@link Node}s. Order: Nodes with no
      *         dependencies starting.
      * @throws IllegalStateException
@@ -54,13 +61,13 @@ public final class TopologicalSort {
     }
 
     private static Set<Node> getDependentNodes(Set<Node> nodes) {
-        Set<Node> S = Sets.newHashSet();
+        Set<Node> dependentNodes = Sets.newHashSet();
         for (Node n : nodes) {
-            if (n.getOutEdges().size() == 0) {
-                S.add(n);
+            if (n.getOutEdges().isEmpty()) {
+                dependentNodes.add(n);
             }
         }
-        return S;
+        return dependentNodes;
     }
 
     private static void detectCycles(Set<Node> nodes) {
@@ -75,14 +82,13 @@ public final class TopologicalSort {
                 break;
             }
         }
-        Preconditions.checkState(cycle == false,
-                "Cycle detected in graph around node: " + cycledNode);
+        Preconditions.checkState(!cycle, "Cycle detected in graph around node: " + cycledNode);
     }
 
     /**
      * Interface for nodes in graph that can be sorted topologically
      */
-    public static interface Node {
+    public interface Node {
         Set<Edge> getInEdges();
 
         Set<Edge> getOutEdges();
@@ -91,7 +97,7 @@ public final class TopologicalSort {
     /**
      * Interface for edges in graph that can be sorted topologically
      */
-    public static interface Edge {
+    public interface Edge {
         Node getFrom();
 
         Node getTo();
@@ -104,6 +110,11 @@ public final class TopologicalSort {
         private final Set<Edge> inEdges;
         private final Set<Edge> outEdges;
 
+        public NodeImpl() {
+            inEdges = Sets.newHashSet();
+            outEdges = Sets.newHashSet();
+        }
+
         @Override
         public Set<Edge> getInEdges() {
             return inEdges;
@@ -119,11 +130,6 @@ public final class TopologicalSort {
             outEdges.add(e);
             to.getInEdges().add(e);
         }
-
-        public NodeImpl() {
-            inEdges = Sets.newHashSet();
-            outEdges = Sets.newHashSet();
-        }
     }
 
     /**
@@ -133,6 +139,11 @@ public final class TopologicalSort {
         private final Node from;
         private final Node to;
 
+        public EdgeImpl(Node from, Node to) {
+            this.from = from;
+            this.to = to;
+        }
+
         @Override
         public Node getFrom() {
             return from;
@@ -143,40 +154,41 @@ public final class TopologicalSort {
             return to;
         }
 
-        public EdgeImpl(Node from, Node to) {
-            this.from = from;
-            this.to = to;
-
-        }
-
         @Override
         public int hashCode() {
             final int prime = 31;
             int result = 1;
-            result = prime * result + ((from == null) ? 0 : from.hashCode());
-            result = prime * result + ((to == null) ? 0 : to.hashCode());
+            result = prime * result + Objects.hashCode(from);
+            result = prime * result + Objects.hashCode(to);
             return result;
         }
 
         @Override
         public boolean equals(Object obj) {
-            if (this == obj)
+            if (this == obj) {
                 return true;
-            if (obj == null)
+            }
+            if (obj == null) {
                 return false;
-            if (getClass() != obj.getClass())
+            }
+            if (getClass() != obj.getClass()) {
                 return false;
+            }
             EdgeImpl other = (EdgeImpl) obj;
             if (from == null) {
-                if (other.from != null)
+                if (other.from != null) {
                     return false;
-            } else if (!from.equals(other.from))
+                }
+            } else if (!from.equals(other.from)) {
                 return false;
+            }
             if (to == null) {
-                if (other.to != null)
+                if (other.to != null) {
                     return false;
-            } else if (!to.equals(other.to))
+                }
+            } else if (!to.equals(other.to)) {
                 return false;
+            }
             return true;
         }
     }