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;
14 import java.util.Objects;
18 * Utility class that provides topological sort.
20 * @deprecated Use {@link org.opendaylight.yangtools.util.TopologicalSort} instead.
23 public final class TopologicalSort {
26 * It isn't desirable to create instance of this class
28 private TopologicalSort() {
32 * Topological sort of dependent nodes in acyclic graphs.
34 * @param nodes graph nodes
35 * @return Sorted {@link List} of {@link Node}s. Order: Nodes with no
36 * dependencies starting.
37 * @throws IllegalStateException
38 * when cycle is present in the graph
40 public static List<Node> sort(final Set<Node> nodes) {
41 List<Node> sortedNodes = Lists.newArrayList();
43 Set<Node> dependentNodes = getDependentNodes(nodes);
45 while (!dependentNodes.isEmpty()) {
46 Node n = dependentNodes.iterator().next();
47 dependentNodes.remove(n);
51 for (Edge e : n.getInEdges()) {
53 m.getOutEdges().remove(e);
55 if (m.getOutEdges().isEmpty()) {
56 dependentNodes.add(m);
66 private static Set<Node> getDependentNodes(final Set<Node> nodes) {
67 Set<Node> dependentNodes = Sets.newHashSet();
68 for (Node n : nodes) {
69 if (n.getOutEdges().isEmpty()) {
70 dependentNodes.add(n);
73 return dependentNodes;
76 private static void detectCycles(final Set<Node> nodes) {
78 boolean cycle = false;
79 Node cycledNode = null;
81 for (Node n : nodes) {
82 if (!n.getOutEdges().isEmpty()) {
88 Preconditions.checkState(!cycle, "Cycle detected in graph around node: " + cycledNode);
92 * Interface for nodes in graph that can be sorted topologically
94 public interface Node {
95 Set<Edge> getInEdges();
97 Set<Edge> getOutEdges();
101 * Interface for edges in graph that can be sorted topologically
103 public interface Edge {
110 * Basic Node implementation.
112 public static class NodeImpl implements Node {
113 private final Set<Edge> inEdges;
114 private final Set<Edge> outEdges;
117 inEdges = Sets.newHashSet();
118 outEdges = Sets.newHashSet();
122 public Set<Edge> getInEdges() {
127 public Set<Edge> getOutEdges() {
131 public void addEdge(final Node to) {
132 Edge e = new EdgeImpl(this, to);
134 to.getInEdges().add(e);
139 * Basic Edge implementation
141 public static class EdgeImpl implements Edge {
142 private final Node from;
143 private final Node to;
145 public EdgeImpl(final Node from, final Node to) {
151 public Node getFrom() {
156 public Node getTo() {
161 public int hashCode() {
162 final int prime = 31;
164 result = prime * result + Objects.hashCode(from);
165 result = prime * result + Objects.hashCode(to);
170 public boolean equals(final Object obj) {
177 if (getClass() != obj.getClass()) {
180 EdgeImpl other = (EdgeImpl) obj;
182 if (other.from != null) {
185 } else if (!from.equals(other.from)) {
189 if (other.to != null) {
192 } else if (!to.equals(other.to)) {