This commit is contained in:
Bane Stojkovic
2026-10-27 16:44:17 +01:00
4 changed files with 75 additions and 7 deletions
@@ -86,8 +86,10 @@ public class Netlist implements Comparable<Netlist> {
}
public String getSwitchSetupString() {
return getSwitchSetup().isEmpty() ? "[ ALL ]" : "[ " +
getSwitchSetup().stream().map(x -> x.toString()).collect(Collectors.joining(",")) + " ]";
return switchSetup.isEmpty() ? "[ ALL ]" : "[ " +
getSortedSwitchSetup().stream()
.map(SwitchSetup::toString)
.collect(Collectors.joining(", ")) + " ]";
}
@Override
@@ -103,6 +105,21 @@ public class Netlist implements Comparable<Netlist> {
@Override
public int compareTo(Netlist o) {
return switchSetup.toString().compareToIgnoreCase(o.switchSetup.toString());
List<SwitchSetup> left = getSortedSwitchSetup();
List<SwitchSetup> right = o.getSortedSwitchSetup();
SwitchSetup unrestricted =
new SwitchSetup(new ArrayList<Position>());
SwitchSetup firstLeft = left.isEmpty() ? unrestricted : left.get(0);
SwitchSetup firstRight = right.isEmpty() ? unrestricted : right.get(0);
return firstLeft.compareTo(firstRight);
}
private List<SwitchSetup> getSortedSwitchSetup() {
List<SwitchSetup> result = new ArrayList<>(switchSetup);
Collections.sort(result);
return result;
}
}
@@ -37,7 +37,7 @@ public class NetlistSummaryHtmlReport {
for (Summary summary : summaries) {
if (summaries.size() > 1) {
sb.append("<h2>Switch configuration: ").append(summary.getNetlist().getSwitchSetup())
sb.append("<h2>Switch configuration: ").append(summary.getNetlist().getSwitchSetupString())
.append("</h2>");
}
@@ -83,8 +83,17 @@ public class Position implements Comparable<Position> {
@Override
public int compareTo(Position o) {
if (theSwitch instanceof IDIYComponent<?> && o.theSwitch instanceof IDIYComponent<?>)
return ((IDIYComponent<?>) theSwitch).getName().compareToIgnoreCase(((IDIYComponent<?>) o.theSwitch).getName());
return 0;
int comparison = getSwitchName().compareToIgnoreCase(o.getSwitchName());
return comparison != 0
? comparison
: Integer.compare(position, o.position);
}
String getSwitchName() {
String name = theSwitch instanceof IDIYComponent<?>
? ((IDIYComponent<?>) theSwitch).getName()
: theSwitch.getClass().getName();
return name == null ? "" : name;
}
}
@@ -21,6 +21,7 @@
*/
package org.diylc.netlist;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -82,6 +83,47 @@ public class SwitchSetup implements Comparable<SwitchSetup> {
@Override
public int compareTo(SwitchSetup o) {
List<Position> left = new ArrayList<>(positions);
List<Position> right = new ArrayList<>(o.positions);
Collections.sort(left);
Collections.sort(right);
int i = 0;
int j = 0;
while (i < left.size() || j < right.size()) {
int comparison;
if (i == left.size()) {
// This setup omits the next switch: use its lowest position.
comparison = Integer.compare(0, right.get(j++).getPosition());
} else if (j == right.size()) {
comparison = Integer.compare(left.get(i++).getPosition(), 0);
} else {
Position a = left.get(i);
Position b = right.get(j);
int nameComparison =
a.getSwitchName().compareToIgnoreCase(b.getSwitchName());
if (nameComparison < 0) {
comparison = Integer.compare(a.getPosition(), 0);
i++;
} else if (nameComparison > 0) {
comparison = Integer.compare(0, b.getPosition());
j++;
} else {
comparison = Integer.compare(a.getPosition(), b.getPosition());
i++;
j++;
}
}
if (comparison != 0) {
return comparison;
}
}
// Same earliest full configuration: use the description as a tie-breaker.
return toString().compareToIgnoreCase(o.toString());
}
}