Skip to content

Commit

Permalink
Restrict drawTree children to List, to better enforce ordering guaran…
Browse files Browse the repository at this point in the history
…tees
  • Loading branch information
FeldrinH committed Oct 22, 2023
1 parent 518a0da commit 35ebc17
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/main/java/ee/ut/dendroloj/Dendrologist.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import org.graphstream.graph.Graph;

import java.awt.*;
import java.awt.Color;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;

public class Dendrologist {
Expand Down Expand Up @@ -111,7 +111,7 @@ public static <T> void drawBinaryTree(T root, Function<T, String> label, Functio
* (children may include null values to indicate empty/missing branches)
* @param <T> type of nodes in the tree
*/
public static <T> void drawTree(T root, Function<T, String> label, Function<T, Collection<T>> children) {
public static <T> void drawTree(T root, Function<T, String> label, Function<T, List<T>> children) {
if (isHeadless()) {
System.err.println("Dendrologist: Running in headless environment. Ignoring call to drawTree.");
return;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/ee/ut/dendroloj/GenericTreeLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.MultiGraph;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;

class GenericTreeLayout {

private static final AtomicLong edgeIdCounter = new AtomicLong(0);

public static <T> Graph assembleGraph(T root, Function<T, String> getLabel, Function<T, Collection<T>> getChildren) {
public static <T> Graph assembleGraph(T root, Function<T, String> getLabel, Function<T, List<T>> getChildren) {
Graph graph = new MultiGraph("dendroloj");
addToGraph(graph, root, null, 0.0, 0.0, getLabel, getChildren);
return graph;
}

private static <T> LayoutResult addToGraph(Graph graph, T node, Node parent, double x, double y,
Function<T, String> getLabel, Function<T, Collection<T>> getChildren) {
Function<T, String> getLabel, Function<T, List<T>> getChildren) {
if (node == null) {
return new LayoutResult(1.0, 0.0);
}
Expand All @@ -46,7 +46,7 @@ private static <T> LayoutResult addToGraph(Graph graph, T node, Node parent, dou
}

double width = 0.0, firstChildOffset = 0.0, lastChildOffset = 0.0;
final Collection<T> children = getChildren.apply(node);
final List<T> children = getChildren.apply(node);
if (isEmpty(children)) {
width = 1.0;
} else {
Expand All @@ -73,7 +73,7 @@ private static <T> LayoutResult addToGraph(Graph graph, T node, Node parent, dou
return new LayoutResult(width, offset);
}

private static boolean isEmpty(Collection<?> array) {
private static boolean isEmpty(List<?> array) {
for (Object element : array) {
if (element != null) {
return false;
Expand Down

0 comments on commit 35ebc17

Please sign in to comment.