diff --git a/runtime/src/main/java/dev/ionfusion/fusion/cli/cover/CoverageReportWriter.java b/runtime/src/main/java/dev/ionfusion/fusion/cli/cover/CoverageReportWriter.java index f5b4c510..a8d7fe13 100644 --- a/runtime/src/main/java/dev/ionfusion/fusion/cli/cover/CoverageReportWriter.java +++ b/runtime/src/main/java/dev/ionfusion/fusion/cli/cover/CoverageReportWriter.java @@ -4,7 +4,7 @@ package dev.ionfusion.fusion.cli.cover; import static dev.ionfusion.fusion._Private_Trampoline.discoverModulesInRepository; -import static dev.ionfusion.runtime._private.cover.CoverageDatabase.SRCLOC_COMPARE; +import static dev.ionfusion.runtime.base.SourceLocation.compareByLineColumn; import static dev.ionfusion.runtime.base.SourceName.FUSION_SOURCE_EXTENSION; import static java.nio.file.Files.walkFileTree; @@ -421,9 +421,9 @@ private void renderSource(HtmlWriter sourceHtml, SourceLocation coverageLoc = locations[locationIndex]; // We shouldn't skip past a known location. - assert SRCLOC_COMPARE.compare(currentLoc, coverageLoc) <= 0; + assert compareByLineColumn(currentLoc, coverageLoc) <= 0; - if (SRCLOC_COMPARE.compare(currentLoc, coverageLoc) == 0) + if (compareByLineColumn(currentLoc, coverageLoc) == 0) { boolean covered = myDatabase.locationCovered(coverageLoc); diff --git a/runtime/src/main/java/dev/ionfusion/runtime/_private/cover/CoverageDatabase.java b/runtime/src/main/java/dev/ionfusion/runtime/_private/cover/CoverageDatabase.java index 5691cd45..34d86f43 100644 --- a/runtime/src/main/java/dev/ionfusion/runtime/_private/cover/CoverageDatabase.java +++ b/runtime/src/main/java/dev/ionfusion/runtime/_private/cover/CoverageDatabase.java @@ -29,7 +29,6 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; -import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -41,45 +40,6 @@ */ public class CoverageDatabase { - private static final class SourceNameComparator - implements Comparator - { - @Override - public int compare(SourceName a, SourceName b) - { - return a.display().compareTo(b.display()); - } - } - - static final Comparator SRCNAME_COMPARE = - new SourceNameComparator(); - - - /** - * Compares locations by line/column, ignoring the offset. - */ - private static final class SourceLocationComparator - implements Comparator - { - @Override - public int compare(SourceLocation a, SourceLocation b) - { - if (a.getLine() < b.getLine()) return -1; - if (a.getLine() > b.getLine()) return 1; - - if (a.getColumn() < b.getColumn()) return -1; - if (a.getColumn() > b.getColumn()) return 1; - - return 0; - } - } - - public static final Comparator SRCLOC_COMPARE = - new SourceLocationComparator(); - - - //========================================================================= - private final Set myRepositories = new HashSet<>(); private final Map myLocations = new HashMap<>(); @@ -206,7 +166,7 @@ SourceName[] sortedNames() SourceName[] sourceArray = sourceSet.toArray(new SourceName[0]); - Arrays.sort(sourceArray, SRCNAME_COMPARE); + Arrays.sort(sourceArray, SourceName::compareByDisplay); return sourceArray; } @@ -235,7 +195,7 @@ public synchronized SourceLocation[] sortedLocations(SourceName name) SourceLocation[] locsArray = locsList.toArray(new SourceLocation[0]); - Arrays.sort(locsArray, SRCLOC_COMPARE); + Arrays.sort(locsArray, SourceLocation::compareByLineColumn); return locsArray; } diff --git a/runtime/src/main/java/dev/ionfusion/runtime/base/SourceLocation.java b/runtime/src/main/java/dev/ionfusion/runtime/base/SourceLocation.java index 05866b7f..eee64c38 100644 --- a/runtime/src/main/java/dev/ionfusion/runtime/base/SourceLocation.java +++ b/runtime/src/main/java/dev/ionfusion/runtime/base/SourceLocation.java @@ -80,6 +80,9 @@ public long getStartOffset() } + //================================================================================== + // Concrete implementations + private static final class Shorts extends SourceLocation { @@ -186,6 +189,9 @@ public long getStartOffset() } + //================================================================================== + + /** * Returns an instance that represents an unknown location in the given * source. @@ -442,4 +448,24 @@ public int hashCode() result ^= (result << 29) ^ (result >> 3); return result; } + + + /** + * Compares locations by line then column, ignoring the source-name and offset. + * + * @param o1 the first object to be compared. + * @param o2 the second object to be compared. + * + * @return a negative integer, zero, or a positive integer as the first argument is + * less than, equal to, or greater than the second. + */ + public static int compareByLineColumn(SourceLocation o1, SourceLocation o2) + { + int result = Long.compare(o1.getLine(), o2.getLine()); + if (result == 0) + { + result = Long.compare(o1.getColumn(), o2.getColumn()); + } + return result; + } } diff --git a/runtime/src/main/java/dev/ionfusion/runtime/base/SourceName.java b/runtime/src/main/java/dev/ionfusion/runtime/base/SourceName.java index 8b276c4f..2364a537 100644 --- a/runtime/src/main/java/dev/ionfusion/runtime/base/SourceName.java +++ b/runtime/src/main/java/dev/ionfusion/runtime/base/SourceName.java @@ -164,6 +164,21 @@ public int hashCode() } + /** + * Compares sources by their {@linkplain #display() display form}. + * + * @param o1 the first object to be compared. + * @param o2 the second object to be compared. + * + * @return a negative integer, zero, or a positive integer as the first argument is + * less than, equal to, or greater than the second. + */ + public static int compareByDisplay(SourceName o1, SourceName o2) + { + return o1.display().compareTo(o2.display()); + } + + //=========================================================================