Skip to content

Commit 50c3549

Browse files
committed
refactor: replace repository manager with instance-level caching
1 parent 19d1401 commit 50c3549

File tree

2 files changed

+82
-149
lines changed

2 files changed

+82
-149
lines changed

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

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
import org.jabref.gui.actions.StandardActions;
1818
import org.jabref.gui.undo.NamedCompound;
1919
import org.jabref.logic.journals.Abbreviation;
20+
import org.jabref.logic.journals.JournalAbbreviationLoader;
2021
import org.jabref.logic.journals.JournalAbbreviationPreferences;
2122
import org.jabref.logic.journals.JournalAbbreviationRepository;
22-
import org.jabref.logic.journals.JournalAbbreviationRepositoryManager;
2323
import org.jabref.logic.l10n.Localization;
2424
import org.jabref.logic.util.BackgroundTask;
2525
import org.jabref.logic.util.TaskExecutor;
@@ -46,6 +46,9 @@ public class AbbreviateAction extends SimpleCommand {
4646
private JournalAbbreviationRepository abbreviationRepository;
4747
private final TaskExecutor taskExecutor;
4848
private final UndoManager undoManager;
49+
50+
private JournalAbbreviationPreferences lastUsedPreferences;
51+
private JournalAbbreviationRepository cachedRepository;
4952

5053
private AbbreviationType abbreviationType;
5154

@@ -108,9 +111,7 @@ public void execute() {
108111
}
109112

110113
private String abbreviate(BibDatabaseContext databaseContext, List<BibEntry> entries) {
111-
// Use the repository manager to get a cached repository or build a new one if needed
112-
abbreviationRepository = JournalAbbreviationRepositoryManager.getInstance()
113-
.getRepository(journalAbbreviationPreferences);
114+
abbreviationRepository = getRepository();
114115

115116
UndoableAbbreviator undoableAbbreviator = new UndoableAbbreviator(
116117
abbreviationRepository,
@@ -140,8 +141,7 @@ private String abbreviate(BibDatabaseContext databaseContext, List<BibEntry> ent
140141
private String unabbreviate(BibDatabaseContext databaseContext, List<BibEntry> entries) {
141142
List<BibEntry> filteredEntries = new ArrayList<>();
142143

143-
JournalAbbreviationRepository freshRepository = JournalAbbreviationRepositoryManager.getInstance()
144-
.getRepository(journalAbbreviationPreferences);
144+
JournalAbbreviationRepository freshRepository = getRepository();
145145

146146
Map<String, Boolean> sourceEnabledStates = new HashMap<>();
147147
String builtInId = JournalAbbreviationRepository.BUILTIN_LIST_ID;
@@ -232,6 +232,82 @@ private void addToMap(Map<String, List<JournalAbbreviationRepository.Abbreviatio
232232
map.computeIfAbsent(text, k -> new ArrayList<>()).add(abbrWithSource);
233233
}
234234

235+
/**
236+
* Gets a repository instance, using cached version if preferences haven't changed.
237+
* This provides efficient repository creation without using static/singleton patterns.
238+
*
239+
*
240+
* @return A repository configured with current preferences
241+
*/
242+
private JournalAbbreviationRepository getRepository() {
243+
if (cachedRepository != null && !preferencesChanged()) {
244+
return cachedRepository;
245+
}
246+
247+
cachedRepository = JournalAbbreviationLoader.loadRepository(journalAbbreviationPreferences);
248+
lastUsedPreferences = clonePreferences();
249+
return cachedRepository;
250+
}
251+
252+
/**
253+
* Checks if preferences have changed since last repository creation
254+
*
255+
*
256+
* @return true if preferences have changed, false otherwise
257+
*/
258+
private boolean preferencesChanged() {
259+
if (lastUsedPreferences == null) {
260+
return true;
261+
}
262+
263+
if (lastUsedPreferences.shouldUseFJournalField() != journalAbbreviationPreferences.shouldUseFJournalField()) {
264+
return true;
265+
}
266+
267+
List<String> oldLists = lastUsedPreferences.getExternalJournalLists();
268+
List<String> newLists = journalAbbreviationPreferences.getExternalJournalLists();
269+
270+
if (oldLists.size() != newLists.size()) {
271+
return true;
272+
}
273+
274+
for (int i = 0; i < oldLists.size(); i++) {
275+
if (!oldLists.get(i).equals(newLists.get(i))) {
276+
return true;
277+
}
278+
}
279+
280+
Map<String, Boolean> oldEnabled = lastUsedPreferences.getEnabledExternalLists();
281+
Map<String, Boolean> newEnabled = journalAbbreviationPreferences.getEnabledExternalLists();
282+
283+
if (oldEnabled.size() != newEnabled.size()) {
284+
return true;
285+
}
286+
287+
for (Map.Entry<String, Boolean> entry : newEnabled.entrySet()) {
288+
Boolean oldValue = oldEnabled.get(entry.getKey());
289+
if (!java.util.Objects.equals(oldValue, entry.getValue())) {
290+
return true;
291+
}
292+
}
293+
294+
return false;
295+
}
296+
297+
/**
298+
* Creates a clone of the current preferences for comparison
299+
*
300+
*
301+
* @return A new preferences instance with the same settings
302+
*/
303+
private JournalAbbreviationPreferences clonePreferences() {
304+
return new JournalAbbreviationPreferences(
305+
journalAbbreviationPreferences.getExternalJournalLists(),
306+
journalAbbreviationPreferences.shouldUseFJournalField(),
307+
journalAbbreviationPreferences.getEnabledExternalLists()
308+
);
309+
}
310+
235311
/**
236312
* Checks if any journal abbreviation source is enabled in the preferences.
237313
* This includes both the built-in list and any external journal lists.

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

Lines changed: 0 additions & 143 deletions
This file was deleted.

0 commit comments

Comments
 (0)