2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.util;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Lists;
12 import com.google.common.collect.Sets;
13 import java.util.List;
17 * Utility class that provides topological sort
19 public final class TopologicalSort {
22 * It isn't desirable to create instance of this class
24 private TopologicalSort() {
28 * Topological sort of dependent nodes in acyclic graphs.
30 * @return Sorted {@link List} of {@link Node}s. Order: Nodes with no
31 * dependencies starting.
32 * @throws IllegalStateException
33 * when cycle is present in the graph
35 public static List<Node> sort(Set<Node> nodes) {
36 List<Node> sortedNodes = Lists.newArrayList();
38 Set<Node> dependentNodes = getDependentNodes(nodes);
40 while (!dependentNodes.isEmpty()) {
41 Node n = dependentNodes.iterator().next();
42 dependentNodes.remove(n);
46 for (Edge e : n.getInEdges()) {
48 m.getOutEdges().remove(e);
50 if (m.getOutEdges().isEmpty()) {
51 dependentNodes.add(m);
61 private static Set<Node> getDependentNodes(Set<Node> nodes) {
62 Set<Node> dependentNodes = Sets.newHashSet();
63 for (Node n : nodes) {
64 if (n.getOutEdges().isEmpty()) {
65 dependentNodes.add(n);
68 return dependentNodes;
71 private static void detectCycles(Set<Node> nodes) {
73 boolean cycle = false;
74 Node cycledNode = null;
76 for (Node n : nodes) {
77 if (!n.getOutEdges().isEmpty()) {
83 Preconditions.checkState(!cycle, "Cycle detected in graph around node: " + cycledNode);
87 * Interface for nodes in graph that can be sorted topologically
89 public interface Node {
90 Set<Edge> getInEdges();
92 Set<Edge> getOutEdges();
96 * Interface for edges in graph that can be sorted topologically
98 public interface Edge {
105 * Basic Node implementation.
107 public static class NodeImpl implements Node {
108 private final Set<Edge> inEdges;
109 private final Set<Edge> outEdges;
112 inEdges = Sets.newHashSet();
113 outEdges = Sets.newHashSet();
117 public Set<Edge> getInEdges() {
122 public Set<Edge> getOutEdges() {
126 public void addEdge(Node to) {
127 Edge e = new EdgeImpl(this, to);
129 to.getInEdges().add(e);
134 * Basic Edge implementation
136 public static class EdgeImpl implements Edge {
137 private final Node from;
138 private final Node to;
140 public EdgeImpl(Node from, Node to) {
146 public Node getFrom() {
151 public Node getTo() {
156 public int hashCode() {
157 final int prime = 31;
159 result = prime * result + ((from == null) ? 0 : from.hashCode());
160 result = prime * result + ((to == null) ? 0 : to.hashCode());
165 public boolean equals(Object obj) {
172 if (getClass() != obj.getClass()) {
175 EdgeImpl other = (EdgeImpl) obj;
177 if (other.from != null) {
180 } else if (!from.equals(other.from)) {
184 if (other.to != null) {
187 } else if (!to.equals(other.to)) {