Skip to content

Commit

Permalink
Use arrays insted of set for Duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmeneses authored and SonarTech committed Sep 4, 2019
1 parent 2ffcee0 commit 55a5f78
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class Duplication {
.thenComparing(DuplicateToFileKey.INSTANCE).thenComparing(DuplicateToTextBlock.INSTANCE);

private final TextBlock original;
private final SortedSet<Duplicate> duplicates;
private final Duplicate[] duplicates;

/**
* @throws NullPointerException if {@code original} is {@code null} or {@code duplicates} is {@code null} or {@code duplicates} contains {@code null}
Expand All @@ -48,8 +48,7 @@ public final class Duplication {
public Duplication(TextBlock original, List<Duplicate> duplicates) {
this.original = requireNonNull(original, "original TextBlock can not be null");
validateDuplicates(original, duplicates);
this.duplicates = new TreeSet<>(DUPLICATE_COMPARATOR);
this.duplicates.addAll(duplicates);
this.duplicates = duplicates.stream().sorted(DUPLICATE_COMPARATOR).distinct().toArray(Duplicate[]::new);
}

private static void validateDuplicates(TextBlock original, List<Duplicate> duplicates) {
Expand Down Expand Up @@ -80,7 +79,7 @@ public TextBlock getOriginal() {
* </ul
* <p>The returned set can not be empty and no inner duplicate can contain the original {@link TextBlock}.</p>
*/
public SortedSet<Duplicate> getDuplicates() {
public Duplicate[] getDuplicates() {
return this.duplicates;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonar.ce.task.projectanalysis.duplication;

import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -124,7 +125,7 @@ protected void initializeForFile(Component file) {
for (Duplication duplication : duplications) {
blocks++;
addLines(duplication.getOriginal(), duplicatedLineNumbers);
InnerDuplicate[] innerDuplicates = duplication.getDuplicates().stream()
InnerDuplicate[] innerDuplicates = Arrays.stream(duplication.getDuplicates())
.filter(x -> x instanceof InnerDuplicate)
.map(d -> (InnerDuplicate) d)
.toArray(InnerDuplicate[]::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -178,7 +179,7 @@ private List<DefaultIssue> loadOpenIssues(String componentUuid, DbSession dbSess
issue.setSelectedAt(System.currentTimeMillis());
result.add(issue);
});
return ImmutableList.copyOf(result);
return Collections.unmodifiableList(result);
}

private static void setChanges(Map<String, List<IssueChangeDto>> changeDtoByIssueKey, DefaultIssue i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface ScmInfo {
boolean hasChangesetForLine(int lineNumber);

/**
* Return all ChangeSets, in order, for all lines that have changesets.
* Return all ChangeSets, index by line number. Some values might be null.
*/
Changeset[] getAllChangesets();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Changeset getChangesetForLine(int lineNumber) {

@Override
public boolean hasChangesetForLine(int lineNumber) {
return lineNumber - 1 < lineChangesets.length && lineChangesets[lineNumber - 1] != null;
return lineNumber > 0 && lineNumber - 1 < lineChangesets.length && lineChangesets[lineNumber - 1] != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonar.ce.task.projectanalysis.source.linereader;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -36,7 +37,6 @@
import org.sonar.ce.task.projectanalysis.duplication.TextBlock;
import org.sonar.db.protobuf.DbFileSources;

import static com.google.common.collect.FluentIterable.from;
import static com.google.common.collect.Iterables.size;

public class DuplicationLineReader implements LineReader {
Expand Down Expand Up @@ -86,9 +86,9 @@ private static List<TextBlock> extractAllDuplicatedTextBlocks(Iterable<Duplicati
List<TextBlock> duplicatedBlock = new ArrayList<>(size(duplications));
for (Duplication duplication : duplications) {
duplicatedBlock.add(duplication.getOriginal());
for (InnerDuplicate duplicate : from(duplication.getDuplicates()).filter(InnerDuplicate.class)) {
duplicatedBlock.add(duplicate.getTextBlock());
}
Arrays.stream(duplication.getDuplicates())
.filter(d -> d instanceof InnerDuplicate)
.forEach(duplicate -> duplicatedBlock.add(duplicate.getTextBlock()));
}
return duplicatedBlock;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,17 @@ public Optional<ReadError> read(DbFileSources.Line.Builder lineBuilder) {
private void processSymbols(DbFileSources.Line.Builder lineBuilder) {
int line = lineBuilder.getLine();

List<ScannerReport.Symbol> lineSymbols = new ArrayList<>(this.symbolsPerLine.get(line));
// Sort symbols to have deterministic results and avoid false variation that would lead to an unnecessary update of the source files
// data
lineSymbols.sort(SymbolsComparator.INSTANCE);

StringBuilder symbolString = new StringBuilder();
for (ScannerReport.Symbol lineSymbol : lineSymbols) {
symbolsPerLine.get(line).stream().sorted(SymbolsComparator.INSTANCE).forEach(lineSymbol -> {
int symbolId = idsBySymbol.get(lineSymbol);

appendSymbol(symbolString, lineSymbol.getDeclaration(), line, symbolId, lineBuilder.getSource());
for (ScannerReport.TextRange range : lineSymbol.getReferenceList()) {
appendSymbol(symbolString, range, line, symbolId, lineBuilder.getSource());
}
}
});
if (symbolString.length() > 0) {
lineBuilder.setSymbols(symbolString.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonar.ce.task.projectanalysis.step;

import com.google.common.collect.ImmutableList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -129,7 +130,7 @@ private void initNewDuplicated(Component component, Set<Integer> changedLines) {
Iterable<Duplication> duplications = duplicationRepository.getDuplications(component);
for (Duplication duplication : duplications) {
duplicationCounters.addBlock(duplication.getOriginal());
duplication.getDuplicates().stream()
Arrays.stream(duplication.getDuplicates())
.filter(InnerDuplicate.class::isInstance)
.map(duplicate -> (InnerDuplicate) duplicate)
.forEach(duplicate -> duplicationCounters.addBlock(duplicate.getTextBlock()));
Expand Down

0 comments on commit 55a5f78

Please sign in to comment.