DuplicationComparator duplicationComparator = new DuplicationComparator(component.uuid(), component.projectUuid());
@Override public int compare(@Nullable Duplication d1, @Nullable Duplication d2) { if (d1 == null || d2 == null) { return -1; } ComponentDto file1 = d1.componentDto(); ComponentDto file2 = d2.componentDto(); if (file1 != null && file1.equals(file2)) { // if duplication on same file => order by starting line return d1.from().compareTo(d2.from()); } if (sameFile(file1) && !sameFile(file2)) { // the current resource must be displayed first return -1; } if (sameFile(file2) && !sameFile(file1)) { // the current resource must be displayed first return 1; } if (sameProject(file1) && !sameProject(file2)) { // if resource is in the same project, this it must be displayed first return -1; } if (sameProject(file2) && !sameProject(file1)) { // if resource is in the same project, this it must be displayed first return 1; } return d1.from().compareTo(d2.from()); }
public List<Block> parse(DbSession session, ComponentDto component, @Nullable String branch, @Nullable String pullRequest, @Nullable String duplicationsData) { Map<String, ComponentDto> componentsByKey = newHashMap(); List<Block> blocks = newArrayList(); if (duplicationsData != null) { try { SMInputFactory inputFactory = initStax(); SMHierarchicCursor root = inputFactory.rootElementCursor(new StringReader(duplicationsData)); root.advance(); // <duplications> SMInputCursor cursor = root.childElementCursor("g"); while (cursor.getNext() != null) { List<Duplication> duplications = newArrayList(); SMInputCursor bCursor = cursor.childElementCursor("b"); while (bCursor.getNext() != null) { String from = bCursor.getAttrValue("s"); String size = bCursor.getAttrValue("l"); String componentKey = bCursor.getAttrValue("r"); if (from != null && size != null && componentKey != null) { duplications.add(createDuplication(componentsByKey, branch, pullRequest, from, size, componentKey, session)); } } Collections.sort(duplications, new DuplicationComparator(component.uuid(), component.projectUuid())); blocks.add(new Block(duplications)); } Collections.sort(blocks, new BlockComparator()); } catch (XMLStreamException e) { throw new IllegalStateException("XML is not valid", e); } } return blocks; }
ComponentDto fileOnDifferentProject = db.components().insertComponent(newFileDto(project2, null)); DuplicationsParser.DuplicationComparator comparator = new DuplicationsParser.DuplicationComparator(currentFile.uuid(), currentFile.projectUuid()); assertThat(comparator.compare(Duplication.newComponent(currentFile, 2, 2), Duplication.newComponent(currentFile, 5, 2))).isEqualTo(-1); assertThat(comparator.compare(Duplication.newComponent(currentFile, 2, 2), Duplication.newComponent(fileOnSameProject, 5, 2))).isEqualTo(-1); assertThat(comparator.compare(Duplication.newComponent(fileOnSameProject, 2, 2), Duplication.newComponent(currentFile, 5, 2))).isEqualTo(1); assertThat(comparator.compare(Duplication.newComponent(fileOnSameProject, 5, 2), Duplication.newComponent(fileOnDifferentProject, 2, 2))).isEqualTo(-1); assertThat(comparator.compare(Duplication.newComponent(fileOnDifferentProject, 5, 2), Duplication.newComponent(fileOnSameProject, 2, 2))).isEqualTo(1); assertThat(comparator.compare(Duplication.newComponent(fileOnDifferentProject, 5, 2), Duplication.newComponent(project3, 2, 2))).isEqualTo(1); assertThat(comparator.compare(null, Duplication.newComponent(fileOnSameProject, 2, 2))).isEqualTo(-1); assertThat(comparator.compare(Duplication.newComponent(fileOnSameProject, 2, 2), null)).isEqualTo(-1); assertThat(comparator.compare(null, null)).isEqualTo(-1); assertThat(comparator.compare(Duplication.newComponent(currentFile, 2, 2), Duplication.newRemovedComponent("key1", 5, 2))).isEqualTo(-1); assertThat(comparator.compare(Duplication.newRemovedComponent("key2", 2, 2), Duplication.newComponent(currentFile, 5, 2))).isEqualTo(1);