Skip to content

Commit 304e2ae

Browse files
ahornaceVladimir Kotal
authored andcommitted
Use set for renamed files in history
fixes #3529
1 parent 769b4a1 commit 304e2ae

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/History.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
package org.opengrok.indexer.history;
2525

2626
import java.util.ArrayList;
27+
import java.util.Collections;
28+
import java.util.HashSet;
2729
import java.util.List;
30+
import java.util.Objects;
31+
import java.util.Set;
2832

2933
/**
3034
* Class representing the history of a file.
@@ -37,19 +41,19 @@ public class History {
3741
* SCMs) during cache creation.
3842
* These are relative to repository root.
3943
*/
40-
private List<String> renamedFiles = new ArrayList<>();
44+
private final Set<String> renamedFiles;
4145

4246
public History() {
4347
this(new ArrayList<>());
4448
}
4549

4650
History(List<HistoryEntry> entries) {
47-
this.entries = entries;
51+
this(entries, Collections.emptyList());
4852
}
4953

5054
History(List<HistoryEntry> entries, List<String> renamed) {
5155
this.entries = entries;
52-
this.renamedFiles = renamed;
56+
this.renamedFiles = new HashSet<>(renamed);
5357
}
5458

5559
/**
@@ -83,21 +87,18 @@ public List<HistoryEntry> getHistoryEntries(int limit, int offset) {
8387
offset = Math.max(offset, 0);
8488
limit = offset + limit > entries.size() ? entries.size() - offset : limit;
8589
return entries.subList(offset, offset + limit);
86-
}
87-
90+
}
91+
8892
/**
8993
* Check if at least one history entry has a file list.
9094
*
9195
* @return {@code true} if at least one of the entries has a non-empty
9296
* file list, {@code false} otherwise
9397
*/
9498
public boolean hasFileList() {
95-
for (HistoryEntry entry : entries) {
96-
if (!entry.getFiles().isEmpty()) {
97-
return true;
98-
}
99-
}
100-
return false;
99+
return entries.stream()
100+
.map(HistoryEntry::getFiles)
101+
.anyMatch(files -> !files.isEmpty());
101102
}
102103

103104
/**
@@ -107,24 +108,19 @@ public boolean hasFileList() {
107108
* tag list, {@code false} otherwise
108109
*/
109110
public boolean hasTags() {
110-
// TODO Use a private variable instead of for loop?
111-
for (HistoryEntry entry : entries) {
112-
if (entry.getTags() != null) {
113-
return true;
114-
}
115-
}
116-
return false;
111+
return entries.stream()
112+
.map(HistoryEntry::getTags)
113+
.anyMatch(Objects::nonNull);
117114
}
118115

119116
/**
120117
* Gets a value indicating if {@code file} is in the list of renamed files.
121-
* TODO: Warning -- this does a slow {@link List} search.
122118
*/
123119
public boolean isRenamed(String file) {
124120
return renamedFiles.contains(file);
125121
}
126122

127-
public List<String> getRenamedFiles() {
123+
public Set<String> getRenamedFiles() {
128124
return renamedFiles;
129125
}
130126
}

opengrok-indexer/src/test/java/org/opengrok/indexer/history/BitKeeperRepositoryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.Arrays;
3939
import java.util.List;
4040
import java.util.Scanner;
41+
import java.util.Set;
4142
import java.util.TreeSet;
4243

4344
import org.junit.jupiter.api.AfterEach;
@@ -91,7 +92,7 @@ public void tearDown() {
9192

9293
private static void validateHistory(History history) {
9394
final List<HistoryEntry> entries = history.getHistoryEntries();
94-
final List<String> renames = history.getRenamedFiles();
95+
final Set<String> renames = history.getRenamedFiles();
9596

9697
assertTrue(entries.size() > 0, "File history has no entries.");
9798

0 commit comments

Comments
 (0)