From 4f9110f7b1725df1bf0045293f29c0cc65eecd26 Mon Sep 17 00:00:00 2001
From: Alexey Andreev <konsoletyper@gmail.com>
Date: Sun, 4 Feb 2018 22:30:07 +0300
Subject: [PATCH] Make graph.toString return dot representation of graph

---
 .../java/org/teavm/common/GraphBuilder.java   | 26 +++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/core/src/main/java/org/teavm/common/GraphBuilder.java b/core/src/main/java/org/teavm/common/GraphBuilder.java
index f5d7055c6..bda4123cd 100644
--- a/core/src/main/java/org/teavm/common/GraphBuilder.java
+++ b/core/src/main/java/org/teavm/common/GraphBuilder.java
@@ -99,11 +99,11 @@ public class GraphBuilder {
         return builtGraph;
     }
 
-    private static class GraphImpl implements Graph {
+    private static final class GraphImpl implements Graph {
         private final int[][] incomingEdgeList;
         private final int[][] outgoingEdgeList;
 
-        public GraphImpl(int[][] incomingEdgeList, int[][] outgoingEdgeList) {
+        GraphImpl(int[][] incomingEdgeList, int[][] outgoingEdgeList) {
             this.incomingEdgeList = incomingEdgeList;
             this.outgoingEdgeList = outgoingEdgeList;
         }
@@ -148,5 +148,27 @@ public class GraphBuilder {
         public int outgoingEdgesCount(int node) {
             return outgoingEdgeList[node].length;
         }
+
+        @Override
+        public String toString() {
+            StringBuilder sb = new StringBuilder();
+            sb.append("digraph {\n");
+
+            for (int i = 0; i < size(); ++i) {
+                if (outgoingEdgesCount(i) > 0) {
+                    sb.append("  ").append(i).append(" -> { ");
+                    int[] outgoingEdges = outgoingEdges(i);
+                    sb.append(outgoingEdges[0]);
+                    for (int j = 1; j < outgoingEdges.length; ++j) {
+                        sb.append(", ").append(outgoingEdges[j]);
+                    }
+                    sb.append(" }\n");
+                }
+            }
+
+            sb.append("}");
+
+            return sb.toString();
+        }
     }
 }