17
17
import org .jabref .gui .actions .StandardActions ;
18
18
import org .jabref .gui .undo .NamedCompound ;
19
19
import org .jabref .logic .journals .Abbreviation ;
20
+ import org .jabref .logic .journals .JournalAbbreviationLoader ;
20
21
import org .jabref .logic .journals .JournalAbbreviationPreferences ;
21
22
import org .jabref .logic .journals .JournalAbbreviationRepository ;
22
- import org .jabref .logic .journals .JournalAbbreviationRepositoryManager ;
23
23
import org .jabref .logic .l10n .Localization ;
24
24
import org .jabref .logic .util .BackgroundTask ;
25
25
import org .jabref .logic .util .TaskExecutor ;
@@ -46,6 +46,9 @@ public class AbbreviateAction extends SimpleCommand {
46
46
private JournalAbbreviationRepository abbreviationRepository ;
47
47
private final TaskExecutor taskExecutor ;
48
48
private final UndoManager undoManager ;
49
+
50
+ private JournalAbbreviationPreferences lastUsedPreferences ;
51
+ private JournalAbbreviationRepository cachedRepository ;
49
52
50
53
private AbbreviationType abbreviationType ;
51
54
@@ -108,9 +111,7 @@ public void execute() {
108
111
}
109
112
110
113
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 ();
114
115
115
116
UndoableAbbreviator undoableAbbreviator = new UndoableAbbreviator (
116
117
abbreviationRepository ,
@@ -140,8 +141,7 @@ private String abbreviate(BibDatabaseContext databaseContext, List<BibEntry> ent
140
141
private String unabbreviate (BibDatabaseContext databaseContext , List <BibEntry > entries ) {
141
142
List <BibEntry > filteredEntries = new ArrayList <>();
142
143
143
- JournalAbbreviationRepository freshRepository = JournalAbbreviationRepositoryManager .getInstance ()
144
- .getRepository (journalAbbreviationPreferences );
144
+ JournalAbbreviationRepository freshRepository = getRepository ();
145
145
146
146
Map <String , Boolean > sourceEnabledStates = new HashMap <>();
147
147
String builtInId = JournalAbbreviationRepository .BUILTIN_LIST_ID ;
@@ -232,6 +232,82 @@ private void addToMap(Map<String, List<JournalAbbreviationRepository.Abbreviatio
232
232
map .computeIfAbsent (text , k -> new ArrayList <>()).add (abbrWithSource );
233
233
}
234
234
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
+
235
311
/**
236
312
* Checks if any journal abbreviation source is enabled in the preferences.
237
313
* This includes both the built-in list and any external journal lists.
0 commit comments