*/
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
}
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) {
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();
/**
* Interface for edges in graph that can be sorted topologically
*/
- public static interface Edge {
+ public interface Edge {
Node getFrom();
Node getTo();
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;
outEdges.add(e);
to.getInEdges().add(e);
}
-
- public NodeImpl() {
- inEdges = Sets.newHashSet();
- outEdges = Sets.newHashSet();
- }
}
/**
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;
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;
}
}