Skip to content

Commit 2a6604e

Browse files
committed
fix: modernize collection usage, change return and param types, update JavaDoc
1 parent 153c8ad commit 2a6604e

File tree

8 files changed

+41
-30
lines changed

8 files changed

+41
-30
lines changed

src/main/java/org/jabref/gui/journals/AbbreviateAction.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.nio.file.Path;
44
import java.util.ArrayList;
5-
import java.util.Collections;
65
import java.util.HashMap;
76
import java.util.List;
87
import java.util.Map;
@@ -174,7 +173,7 @@ private String unabbreviate(BibDatabaseContext databaseContext, List<BibEntry> e
174173

175174
String text = entry.getFieldLatexFree(journalField).orElse("");
176175
List<JournalAbbreviationRepository.AbbreviationWithSource> possibleSources =
177-
textToSourceMap.getOrDefault(text, Collections.emptyList());
176+
textToSourceMap.getOrDefault(text, List.of());
178177

179178
if (!possibleSources.isEmpty()) {
180179
boolean allSourcesDisabled = true;

src/main/java/org/jabref/gui/preferences/journals/AbbreviationsFileViewModel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import java.nio.file.NoSuchFileException;
66
import java.nio.file.Path;
77
import java.util.ArrayList;
8-
import java.util.Collection;
98
import java.util.List;
109
import java.util.Objects;
1110
import java.util.Optional;
11+
import java.util.Set;
1212
import java.util.stream.Collectors;
1313

1414
import javafx.beans.property.BooleanProperty;
@@ -86,7 +86,7 @@ public void readAbbreviations() throws IOException {
8686
return;
8787
}
8888
try {
89-
Collection<Abbreviation> abbreviationsFromFile = JournalAbbreviationLoader.readAbbreviationsFromCsvFile(filePath);
89+
Set<Abbreviation> abbreviationsFromFile = JournalAbbreviationLoader.readAbbreviationsFromCsvFile(filePath);
9090

9191
List<AbbreviationViewModel> viewModels = abbreviationsFromFile.stream()
9292
.map(AbbreviationViewModel::new)
@@ -111,7 +111,7 @@ public void writeOrCreate() throws IOException {
111111

112112
List<Abbreviation> abbreviationList = abbreviationsProperty().stream()
113113
.map(AbbreviationViewModel::getAbbreviationObject)
114-
.collect(Collectors.toList());
114+
.toList();
115115
AbbreviationWriter.writeOrCreate(filePath, abbreviationList);
116116
}
117117

src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTabViewModel.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.nio.file.Path;
55
import java.util.List;
66
import java.util.Objects;
7-
import java.util.stream.Collectors;
87

98
import javafx.beans.property.SimpleBooleanProperty;
109
import javafx.beans.property.SimpleIntegerProperty;
@@ -150,7 +149,7 @@ public void addBuiltInList() {
150149
isLoading.setValue(false);
151150
List<AbbreviationViewModel> builtInViewModels = result.stream()
152151
.map(AbbreviationViewModel::new)
153-
.collect(Collectors.toList());
152+
.toList();
154153
AbbreviationsFileViewModel builtInListModel = new AbbreviationsFileViewModel(builtInViewModels, Localization.lang("JabRef built in list"));
155154

156155
boolean isEnabled = abbreviationsPreferences.isSourceEnabled(JournalAbbreviationRepository.BUILTIN_LIST_ID);
@@ -352,7 +351,7 @@ public void storeSettings() {
352351
.filter(path -> !path.isBuiltInListProperty().get())
353352
.filter(path -> path.getAbsolutePath().isPresent())
354353
.map(path -> path.getAbsolutePath().get().toAbsolutePath().toString())
355-
.collect(Collectors.toList());
354+
.toList();
356355

357356
abbreviationsPreferences.setExternalJournalLists(journalStringList);
358357
abbreviationsPreferences.setUseFJournalField(useFJournal.get());

src/main/java/org/jabref/logic/journals/AbbreviationParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import java.nio.file.Files;
77
import java.nio.file.Path;
88
import java.util.Arrays;
9-
import java.util.Collection;
109
import java.util.LinkedHashSet;
10+
import java.util.Set;
1111

1212
import org.apache.commons.csv.CSVParser;
1313
import org.apache.commons.csv.CSVRecord;
@@ -63,7 +63,7 @@ private char detectDelimiter(Path file) throws IOException {
6363
}
6464
}
6565

66-
public Collection<Abbreviation> getAbbreviations() {
66+
public Set<Abbreviation> getAbbreviations() {
6767
return abbreviations;
6868
}
6969
}

src/main/java/org/jabref/logic/journals/JournalAbbreviationLoader.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import java.nio.file.Files;
66
import java.nio.file.InvalidPathException;
77
import java.nio.file.Path;
8-
import java.util.Collection;
98
import java.util.Collections;
109
import java.util.List;
10+
import java.util.Set;
1111

1212
import org.jabref.logic.journals.ltwa.LtwaRepository;
1313

@@ -29,7 +29,14 @@ public class JournalAbbreviationLoader {
2929
private static final boolean USE_FJOURNAL_FIELD = true;
3030
private static final boolean BUILTIN_LIST_ENABLED_BY_DEFAULT = true;
3131

32-
public static Collection<Abbreviation> readAbbreviationsFromCsvFile(Path file) throws IOException {
32+
/**
33+
* Reads journal abbreviations from a CSV file.
34+
*
35+
* @param file Path to the CSV file containing journal abbreviations
36+
* @return A set of abbreviations read from the file
37+
* @throws IOException If an I/O error occurs while reading the file
38+
*/
39+
public static Set<Abbreviation> readAbbreviationsFromCsvFile(Path file) throws IOException {
3340
LOGGER.debug("Reading journal list from file {}", file);
3441
AbbreviationParser parser = new AbbreviationParser();
3542
parser.readJournalListFromFile(file);
@@ -102,11 +109,11 @@ public static JournalAbbreviationRepository loadRepository(JournalAbbreviationPr
102109
simpleFilename, enabled);
103110
}
104111

105-
Collection<Abbreviation> abbreviations = readAbbreviationsFromCsvFile(filePath);
112+
Set<Abbreviation> abbreviations = readAbbreviationsFromCsvFile(filePath);
106113

107114
repository.addCustomAbbreviations(abbreviations, simpleFilename, enabled);
108115

109-
repository.addCustomAbbreviations(Collections.emptyList(), prefixedKey, enabled);
116+
repository.addCustomAbbreviations(Set.of(), prefixedKey, enabled);
110117
} catch (IOException | InvalidPathException e) {
111118
// invalid path might come from unix/windows mixup of prefs
112119
LOGGER.error("Cannot read external journal list file {}", filename, e);
@@ -134,7 +141,7 @@ private static LtwaRepository loadLtwaRepository() throws IOException {
134141
}
135142

136143
public static JournalAbbreviationRepository loadBuiltInRepository() {
137-
JournalAbbreviationPreferences prefs = new JournalAbbreviationPreferences(Collections.emptyList(), USE_FJOURNAL_FIELD);
144+
JournalAbbreviationPreferences prefs = new JournalAbbreviationPreferences(List.of(), USE_FJOURNAL_FIELD);
138145

139146
prefs.setSourceEnabled(JournalAbbreviationRepository.BUILTIN_LIST_ID, BUILTIN_LIST_ENABLED_BY_DEFAULT);
140147
return loadRepository(prefs);

src/main/java/org/jabref/logic/journals/JournalAbbreviationRepository.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public Optional<Abbreviation> getForUnabbreviation(String input) {
209209
}
210210

211211
private Optional<Abbreviation> findAbbreviationFuzzyMatched(String input) {
212-
Collection<Abbreviation> enabledCustomAbbreviations = customAbbreviations.stream()
212+
List<Abbreviation> enabledCustomAbbreviations = customAbbreviations.stream()
213213
.filter(abbreviation -> isSourceEnabled(abbreviationSources.getOrDefault(abbreviation, BUILTIN_LIST_ID)))
214214
.toList();
215215

@@ -225,6 +225,13 @@ private Optional<Abbreviation> findAbbreviationFuzzyMatched(String input) {
225225
return findBestFuzzyMatched(fullToAbbreviationObject.values(), input);
226226
}
227227

228+
/**
229+
* Finds the best matching abbreviation by fuzzy matching from a collection of abbreviations
230+
*
231+
* @param abbreviations Collection of abbreviations to search
232+
* @param input The input string to match against
233+
* @return Optional containing the best matching abbreviation, or empty if no good match is found
234+
*/
228235
private Optional<Abbreviation> findBestFuzzyMatched(Collection<Abbreviation> abbreviations, String input) {
229236
// threshold for edit distance similarity comparison
230237
final double SIMILARITY_THRESHOLD = 1.0;
@@ -281,27 +288,27 @@ public void addCustomAbbreviation(@NonNull Abbreviation abbreviation, @NonNull S
281288
enabledSources.put(sourcePath, enabled);
282289
}
283290

284-
public Collection<Abbreviation> getCustomAbbreviations() {
291+
public Set<Abbreviation> getCustomAbbreviations() {
285292
return customAbbreviations;
286293
}
287294

288295
/**
289296
* Adds multiple custom abbreviations to the repository
290297
*
291-
* @param abbreviationsToAdd The abbreviations to add
298+
* @param abbreviationsToAdd The set of abbreviations to add
292299
*/
293-
public void addCustomAbbreviations(Collection<Abbreviation> abbreviationsToAdd) {
300+
public void addCustomAbbreviations(Set<Abbreviation> abbreviationsToAdd) {
294301
abbreviationsToAdd.forEach(this::addCustomAbbreviation);
295302
}
296303

297304
/**
298305
* Adds abbreviations with a specific source key and enabled state
299306
*
300-
* @param abbreviationsToAdd Collection of abbreviations to add
307+
* @param abbreviationsToAdd Set of abbreviations to add
301308
* @param sourceKey The key identifying the source of these abbreviations
302309
* @param enabled Whether the source should be enabled initially
303310
*/
304-
public void addCustomAbbreviations(Collection<Abbreviation> abbreviationsToAdd, String sourceKey, boolean enabled) {
311+
public void addCustomAbbreviations(Set<Abbreviation> abbreviationsToAdd, String sourceKey, boolean enabled) {
305312
enabledSources.put(sourceKey, enabled);
306313

307314
for (Abbreviation abbreviation : abbreviationsToAdd) {
@@ -350,8 +357,8 @@ public Set<String> getFullNames() {
350357
return fullToAbbreviationObject.keySet();
351358
}
352359

353-
public Collection<Abbreviation> getAllLoaded() {
354-
return fullToAbbreviationObject.values();
360+
public Set<Abbreviation> getAllLoaded() {
361+
return Set.copyOf(fullToAbbreviationObject.values());
355362
}
356363

357364
/**

src/main/java/org/jabref/logic/preferences/JabRefCliPreferences.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ static String convertListToString(List<String> value) {
730730
@VisibleForTesting
731731
static List<String> convertStringToList(String toConvert) {
732732
if (StringUtil.isBlank(toConvert)) {
733-
return Collections.emptyList();
733+
return List.of();
734734
}
735735

736736
return Splitter.on(STRINGLIST_DELIMITER).splitToList(toConvert);
@@ -1565,10 +1565,10 @@ public FieldPreferences getFieldPreferences() {
15651565
!getBoolean(DO_NOT_RESOLVE_STRINGS), // mind the !
15661566
getStringList(RESOLVE_STRINGS_FOR_FIELDS).stream()
15671567
.map(FieldFactory::parseField)
1568-
.collect(Collectors.toList()),
1568+
.toList(),
15691569
getStringList(NON_WRAPPABLE_FIELDS).stream()
15701570
.map(FieldFactory::parseField)
1571-
.collect(Collectors.toList()));
1571+
.toList());
15721572

15731573
EasyBind.listen(fieldPreferences.resolveStringsProperty(), (obs, oldValue, newValue) -> putBoolean(DO_NOT_RESOLVE_STRINGS, !newValue));
15741574
fieldPreferences.getResolvableFields().addListener((InvalidationListener) change ->
@@ -1810,7 +1810,7 @@ public CleanupPreferences getCleanupPreferences() {
18101810
FieldFormatterCleanups.parse(StringUtil.unifyLineBreaks(get(CLEANUP_FIELD_FORMATTERS), ""))));
18111811

18121812
cleanupPreferences.getObservableActiveJobs().addListener((SetChangeListener<CleanupPreferences.CleanupStep>) c ->
1813-
putStringList(CLEANUP_JOBS, cleanupPreferences.getActiveJobs().stream().map(Enum::name).collect(Collectors.toList())));
1813+
putStringList(CLEANUP_JOBS, cleanupPreferences.getActiveJobs().stream().map(Enum::name).toList()));
18141814

18151815
EasyBind.listen(cleanupPreferences.fieldFormatterCleanupsProperty(), (fieldFormatters, oldValue, newValue) -> {
18161816
putBoolean(CLEANUP_FIELD_FORMATTERS_ENABLED, newValue.isEnabled());
@@ -2017,7 +2017,7 @@ public XmpPreferences getXmpPreferences() {
20172017
xmpPreferences.getXmpPrivacyFilter().addListener((SetChangeListener<Field>) c ->
20182018
putStringList(XMP_PRIVACY_FILTERS, xmpPreferences.getXmpPrivacyFilter().stream()
20192019
.map(Field::getName)
2020-
.collect(Collectors.toList())));
2020+
.toList()));
20212021

20222022
return xmpPreferences;
20232023
}

src/test/java/org/jabref/gui/preferences/journals/JournalAbbreviationsViewModelTabTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.util.Arrays;
88
import java.util.List;
99
import java.util.Optional;
10-
import java.util.stream.Collectors;
1110
import java.util.stream.Stream;
1211

1312
import javafx.beans.property.SimpleBooleanProperty;
@@ -340,7 +339,7 @@ void builtInListsIncludeAllBuiltInAbbreviations() {
340339
ObservableList<Abbreviation> actualAbbreviations = FXCollections
341340
.observableArrayList(viewModel.abbreviationsProperty().stream()
342341
.map(AbbreviationViewModel::getAbbreviationObject)
343-
.collect(Collectors.toList()));
342+
.toList());
344343

345344
assertEquals(expected, actualAbbreviations);
346345
}

0 commit comments

Comments
 (0)