Search in sources :

Example 1 with LocatedTree

use of org.sonar.maven.model.LocatedTree in project sonar-java by SonarSource.

the class PomElementOrderCheck method checkPositions.

private static List<Location> checkPositions(LocatedTree... trees) {
    List<LocatedTree> expectedOrder = Arrays.stream(trees).filter(Objects::nonNull).collect(Collectors.toList());
    List<LocatedTree> observedOrder = expectedOrder.stream().sorted(LINE_COMPARATOR).collect(Collectors.toList());
    int lastWrongPosition = -1;
    int firstWrongPosition = -1;
    for (int index = 0; index < expectedOrder.size(); index++) {
        if (observedOrder.indexOf(expectedOrder.get(index)) != index) {
            lastWrongPosition = index;
            if (firstWrongPosition == -1) {
                firstWrongPosition = index;
            }
        }
    }
    if (lastWrongPosition == -1) {
        return Collections.emptyList();
    }
    List<Location> issues = new ArrayList<>();
    // only reports between first and last wrong position
    for (int index = firstWrongPosition; index <= lastWrongPosition; index++) {
        issues.add(new Location("Expected position: " + (index + 1), expectedOrder.get(index).startLocation().line()));
    }
    return issues;
}
Also used : ArrayList(java.util.ArrayList) LocatedTree(org.sonar.maven.model.LocatedTree) Location(org.sonar.java.xml.maven.PomCheckContext.Location)

Aggregations

ArrayList (java.util.ArrayList)1 Location (org.sonar.java.xml.maven.PomCheckContext.Location)1 LocatedTree (org.sonar.maven.model.LocatedTree)1