Skip to content

Commit

Permalink
Implement test cases for search (JabRef#10193)
Browse files Browse the repository at this point in the history
* Move search folder to logic

* Add testEmptyLibrarySearch

* Add initializeDatabaseFromPath

* Add TestLibraryA & testUpperAndLowerWordSearch

* Add testSimpleSingleFieldSearch

* Add testSimpleMultipleFieldSearch

* Add testSensitiveWordSearch

* Add testSensitiveMutipleFieldSearch

* Add BibEntries for test-library-B

* Add testSimpleRegularExpression

* Add testSensitiveRegularExpression

* SimplePDFFulltextSearch WIP

* Change set.Files to add.File

* Add testSimplePDFNoteFulltextSearch WIP

* Move indexer

* Add missing @tests to SearchFunctionalityTest.java WIP

* WIP

* Exclude non-functional @tests

* Add withFiles

* Remove empty lines

* @NullMarked for LinkedFile

* Fix Logger parameter

* Streamline tests

* Fix checkstyle

* Refine tests

* Get rid of missing "identity" formatter

* Minimize test files

* Remove obsolete method (and make indexer class variable to speedup)

* Fix filenames

* Some refactorings

* Revert global indexer mapping (does not work)

ERROR: Could not retrieve search results.: org.apache.lucene.index.IndexNotFoundException: no segments* file found in NIOFSDirectory@C:\Users\koppor\AppData\Local\Temp\junit11457118934853053051 lockFactory=org.apache.lucene.store.NativeFSLockFactory@5d6751d7: files: [write.lock]

* Revert "Revert global indexer mapping (does not work)"

This reverts commit e081044.

* Small code updates

* Rename test files

* Some more logging

* Refine transaction boundaries (and some minor tweaks)

* Add TODO

* Fix off-by-one error

* Remove non-used .bib file

* Refine comments

* Fix variable assignment

* test-library-A -> test-library-title-casing

* Merge test cases of library-B.bib into title-casing.bib

* Increase transaction boundary for index addition/removal

* More readable directory names for index directory

* WIP: Introduce PdfIndexerManager

* Preparation for "fulltext search not checking all attached files upon start"

* Use "right" factory (and rename factor getter)

* Fix FullTextSeachRule (refactoring introduced a bug)

* Revert property for recheck of attached files

* Fix linting issues

* Fix Formatters optimization

* Fix test

* Exception for architecture

* Refine .gitignore

* Add missing }

* Do not search for PDF files in case of an exception of a search

* Remove duplicate code (and unneccsary pre-fetch of search results)

* Add exception for test

* Remove ".getMessage()"

* Move comment and remove obsolete variable

* Fix typo

* Fix name

* Add dot

* Add comment

* Call splitting method

* Add JavaDocComment

* Revert lambdas

---------

Co-authored-by: Oliver Kopp <[email protected]>
  • Loading branch information
Luggas4you and koppor authored Jan 8, 2024
1 parent bae698a commit 0c26a55
Show file tree
Hide file tree
Showing 76 changed files with 790 additions and 505 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
### Changed

- The Custom export format now uses the custom DOI base URI in the preferences for the `DOICheck`, if activated [forum#4084](https://discourse.jabref.org/t/export-html-disregards-custom-doi-base-uri/4084)
- The index directories for full text search have now more readable names to increase debugging possibilities using Apache Lucense's Lurk. [#10193](https://github.com/JabRef/jabref/issues/10193)
- The fulltext search also indexes files ending with .pdf (but do not having an explicit file type set). [#10193](https://github.com/JabRef/jabref/issues/10193)
- We changed the order of the lists in the "Citation relations" tab. `Cites` are now on the left and `Cited by` on the right [#10572](https://github.com/JabRef/jabref/pull/10752)

### Fixed

- We fixed an issue where attempting to cancel the importing/generation of an entry from id is ignored. [#10508](https://github.com/JabRef/jabref/issues/10508)
- We fixed an issue where the preview panel showing the wrong entry (an entry that is not selected in the entry table). [#9172](https://github.com/JabRef/jabref/issues/9172)
- The last page of a PDF is now indexed by the full text search. [#10193](https://github.com/JabRef/jabref/issues/10193)
- We fixed an issue where the duplicate check did not take umlauts or other LaTeX-encoded characters into account. [#10744](https://github.com/JabRef/jabref/pull/10744)
- We fixed the colors of the icon on hover for unset special fields. [#10431](https://github.com/JabRef/jabref/issues/10431)

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/jabref/gui/JabRefExecutorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.jabref.logic.pdf.search.PdfIndexerManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -146,6 +148,8 @@ public void shutdownEverything() {
gracefullyShutdown(this.executorService);
gracefullyShutdown(this.lowPriorityExecutorService);

PdfIndexerManager.shutdownAllIndexers();

timer.cancel();
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.jabref.logic.importer.ImportCleanup;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.pdf.search.PdfIndexerManager;
import org.jabref.logic.shared.DatabaseLocation;
import org.jabref.logic.undo.AddUndoableActionEvent;
import org.jabref.logic.undo.UndoChangeEvent;
Expand Down Expand Up @@ -862,6 +863,7 @@ public void closeTab(LibraryTab libraryTab) {
}
AutosaveManager.shutdown(context);
BackupManager.shutdown(context, prefs.getFilePreferences().getBackupDirectory(), prefs.getFilePreferences().shouldCreateBackup());
PdfIndexerManager.shutdownAllIndexers();
}

private void removeTab(LibraryTab libraryTab) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private void openDatabases() {
for (int tabNumber = 0; tabNumber < parserResults.size(); tabNumber++) {
// ToDo: Method needs to be rewritten, because the index of the parser result and of the libraryTab may not
// be identical, if there are also other tabs opened, that are not libraryTabs. Currently there are none,
// therefor for now this ok.
// therefore for now this ok.
ParserResult pr = parserResults.get(tabNumber);
if (pr.hasWarnings()) {
ParserResultWarningDialog.showParserResultWarningDialog(pr, mainFrame.getDialogService());
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@
import org.jabref.logic.importer.util.FileFieldParser;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.pdf.FileAnnotationCache;
import org.jabref.logic.pdf.search.indexing.IndexingTaskManager;
import org.jabref.logic.pdf.search.indexing.PdfIndexer;
import org.jabref.logic.pdf.search.IndexingTaskManager;
import org.jabref.logic.pdf.search.PdfIndexer;
import org.jabref.logic.pdf.search.PdfIndexerManager;
import org.jabref.logic.search.SearchQuery;
import org.jabref.logic.shared.DatabaseLocation;
import org.jabref.logic.util.UpdateField;
Expand Down Expand Up @@ -233,7 +234,7 @@ public void onDatabaseLoadingSucceed(ParserResult result) {

if (preferencesService.getFilePreferences().shouldFulltextIndexLinkedFiles()) {
try {
indexingTaskManager.updateIndex(PdfIndexer.of(bibDatabaseContext, preferencesService.getFilePreferences()), bibDatabaseContext);
indexingTaskManager.updateIndex(PdfIndexerManager.getIndexer(bibDatabaseContext, preferencesService.getFilePreferences()), bibDatabaseContext);
} catch (IOException e) {
LOGGER.error("Cannot access lucene index", e);
}
Expand Down Expand Up @@ -912,10 +913,8 @@ private class IndexUpdateListener {
public void listen(EntriesAddedEvent addedEntryEvent) {
if (preferencesService.getFilePreferences().shouldFulltextIndexLinkedFiles()) {
try {
PdfIndexer pdfIndexer = PdfIndexer.of(bibDatabaseContext, preferencesService.getFilePreferences());
for (BibEntry addedEntry : addedEntryEvent.getBibEntries()) {
indexingTaskManager.addToIndex(pdfIndexer, addedEntry, bibDatabaseContext);
}
PdfIndexer pdfIndexer = PdfIndexerManager.getIndexer(bibDatabaseContext, preferencesService.getFilePreferences());
indexingTaskManager.addToIndex(pdfIndexer, addedEntryEvent.getBibEntries());
} catch (IOException e) {
LOGGER.error("Cannot access lucene index", e);
}
Expand All @@ -926,7 +925,7 @@ public void listen(EntriesAddedEvent addedEntryEvent) {
public void listen(EntriesRemovedEvent removedEntriesEvent) {
if (preferencesService.getFilePreferences().shouldFulltextIndexLinkedFiles()) {
try {
PdfIndexer pdfIndexer = PdfIndexer.of(bibDatabaseContext, preferencesService.getFilePreferences());
PdfIndexer pdfIndexer = PdfIndexerManager.getIndexer(bibDatabaseContext, preferencesService.getFilePreferences());
for (BibEntry removedEntry : removedEntriesEvent.getBibEntries()) {
indexingTaskManager.removeFromIndex(pdfIndexer, removedEntry);
}
Expand All @@ -949,8 +948,9 @@ public void listen(FieldChangedEvent fieldChangedEvent) {
removedFiles.remove(newFileList);

try {
indexingTaskManager.addToIndex(PdfIndexer.of(bibDatabaseContext, preferencesService.getFilePreferences()), fieldChangedEvent.getBibEntry(), addedFiles, bibDatabaseContext);
indexingTaskManager.removeFromIndex(PdfIndexer.of(bibDatabaseContext, preferencesService.getFilePreferences()), fieldChangedEvent.getBibEntry(), removedFiles);
PdfIndexer indexer = PdfIndexerManager.getIndexer(bibDatabaseContext, preferencesService.getFilePreferences());
indexingTaskManager.addToIndex(indexer, fieldChangedEvent.getBibEntry(), addedFiles);
indexingTaskManager.removeFromIndex(indexer, removedFiles);
} catch (IOException e) {
LOGGER.warn("I/O error when writing lucene index", e);
}
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/org/jabref/gui/StateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
Expand All @@ -31,7 +30,6 @@
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.groups.GroupTreeNode;
import org.jabref.model.util.OptionalUtil;

import com.tobiasdiez.easybind.EasyBind;
import com.tobiasdiez.easybind.EasyBinding;
Expand Down Expand Up @@ -144,11 +142,6 @@ public void setActiveDatabase(BibDatabaseContext database) {
}
}

public List<BibEntry> getEntriesInCurrentDatabase() {
return OptionalUtil.flatMap(activeDatabase.get(), BibDatabaseContext::getEntries)
.collect(Collectors.toList());
}

public void clearSearchQuery() {
activeSearchQuery.setValue(Optional.empty());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/entryeditor/CommentsTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.pdf.search.indexing.IndexingTaskManager;
import org.jabref.logic.pdf.search.IndexingTaskManager;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.pdf.search.indexing.IndexingTaskManager;
import org.jabref.logic.pdf.search.IndexingTaskManager;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.jabref.gui.theme.ThemeManager;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.pdf.search.indexing.IndexingTaskManager;
import org.jabref.logic.pdf.search.IndexingTaskManager;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.pdf.search.indexing.IndexingTaskManager;
import org.jabref.logic.pdf.search.IndexingTaskManager;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.preferences.PreferencesService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.pdf.search.indexing.IndexingTaskManager;
import org.jabref.logic.pdf.search.IndexingTaskManager;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.preferences.PreferencesService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.pdf.search.indexing.IndexingTaskManager;
import org.jabref.logic.pdf.search.IndexingTaskManager;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.pdf.search.indexing.IndexingTaskManager;
import org.jabref.logic.pdf.search.IndexingTaskManager;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/entryeditor/PreviewTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.jabref.gui.theme.ThemeManager;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.pdf.search.indexing.IndexingTaskManager;
import org.jabref.logic.pdf.search.IndexingTaskManager;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.PreferencesService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.pdf.search.indexing.IndexingTaskManager;
import org.jabref.logic.pdf.search.IndexingTaskManager;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibEntryType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.jabref.gui.theme.ThemeManager;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.pdf.search.indexing.IndexingTaskManager;
import org.jabref.logic.pdf.search.IndexingTaskManager;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.jabref.logic.exporter.SelfContainedSaveConfiguration;
import org.jabref.logic.l10n.Encodings;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.pdf.search.PdfIndexerManager;
import org.jabref.logic.shared.DatabaseLocation;
import org.jabref.logic.shared.prefs.SharedDatabasePreferences;
import org.jabref.logic.util.StandardFileType;
Expand Down Expand Up @@ -137,13 +138,12 @@ public void saveSelectedAsPlain() {
boolean saveAs(Path file, SaveDatabaseMode mode) {
BibDatabaseContext context = libraryTab.getBibDatabaseContext();

// Close AutosaveManager and BackupManager for original library
Optional<Path> databasePath = context.getDatabasePath();
if (databasePath.isPresent()) {
final Path oldFile = databasePath.get();
context.setDatabasePath(oldFile);
// Close AutosaveManager, BackupManager, and PdfIndexer for original library
AutosaveManager.shutdown(context);
BackupManager.shutdown(context, this.preferences.getFilePreferences().getBackupDirectory(), preferences.getFilePreferences().shouldCreateBackup());
PdfIndexerManager.shutdownIndexer(context);
}

// Set new location
Expand All @@ -164,6 +164,7 @@ boolean saveAs(Path file, SaveDatabaseMode mode) {
// Reset (here: uninstall and install again) AutosaveManager and BackupManager for the new file name
libraryTab.resetChangeMonitor();
libraryTab.installAutosaveManagerAndBackupManager();
// PdfIndexerManager does not need to be called; the method {@link org.jabref.logic.pdf.search.PdfIndexerManager.get()} is called if a new indexer is needed

preferences.getGuiPreferences().getFileHistory().newFile(file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import org.jabref.logic.cleanup.MoveFilesCleanup;
import org.jabref.logic.cleanup.RenamePdfCleanup;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.pdf.search.indexing.IndexingTaskManager;
import org.jabref.logic.pdf.search.indexing.PdfIndexer;
import org.jabref.logic.pdf.search.IndexingTaskManager;
import org.jabref.logic.pdf.search.PdfIndexerManager;
import org.jabref.logic.util.io.FileNameCleaner;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabaseContext;
Expand Down Expand Up @@ -87,7 +87,7 @@ public void moveFilesToFileDirRenameAndAddToEntry(BibEntry entry, List<Path> fil
}

try {
indexingTaskManager.addToIndex(PdfIndexer.of(bibDatabaseContext, filePreferences), entry, bibDatabaseContext);
indexingTaskManager.addToIndex(PdfIndexerManager.getIndexer(bibDatabaseContext, filePreferences), entry);
} catch (IOException e) {
LOGGER.error("Could not access Fulltext-Index", e);
}
Expand All @@ -105,9 +105,9 @@ public void copyFilesToFileDirAndAddToEntry(BibEntry entry, List<Path> files, In
}

try {
indexingTaskManager.addToIndex(PdfIndexer.of(bibDatabaseContext, filePreferences), entry, bibDatabaseContext);
indexingTaskManager.addToIndex(PdfIndexerManager.getIndexer(bibDatabaseContext, filePreferences), entry);
} catch (IOException e) {
LOGGER.error("Could not access Fulltext-Index", e);
LOGGER.error("Could not access fulltext index", e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/preview/PreviewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.jabref.gui.theme.ThemeManager;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.pdf.search.indexing.IndexingTaskManager;
import org.jabref.logic.pdf.search.IndexingTaskManager;
import org.jabref.logic.preview.PreviewLayout;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.pdf.search.indexing.PdfIndexer;
import org.jabref.logic.pdf.search.PdfIndexer;
import org.jabref.logic.pdf.search.PdfIndexerManager;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.preferences.FilePreferences;

Expand Down Expand Up @@ -74,8 +75,8 @@ private void rebuildIndex() {
return;
}
try {
currentLibraryTab.get().getIndexingTaskManager().createIndex(PdfIndexer.of(databaseContext, filePreferences));
currentLibraryTab.get().getIndexingTaskManager().updateIndex(PdfIndexer.of(databaseContext, filePreferences), databaseContext);
PdfIndexer indexer = PdfIndexerManager.getIndexer(databaseContext, filePreferences);
currentLibraryTab.get().getIndexingTaskManager().rebuildIndex(indexer);
} catch (IOException e) {
dialogService.notify(Localization.lang("Failed to access fulltext search index"));
LOGGER.error("Failed to access fulltext search index", e);
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanups.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,16 @@ public static FieldFormatterCleanups parse(List<String> formatterMetaList) {
}

static Formatter getFormatterFromString(String formatterName) {
for (Formatter formatter : Formatters.getAll()) {
if (formatterName.equals(formatter.getKey())) {
return formatter;
}
}
LOGGER.info("Formatter {} not found.", formatterName);
return new IdentityFormatter();
return Formatters
.getFormatterForKey(formatterName)
.orElseGet(() -> {
if (!"identity".equals(formatterName)) {
// The identity formatter is not listed in the formatters list, but is still valid
// Therefore, we log errors in other cases only
LOGGER.info("Formatter {} not found.", formatterName);
}
return new IdentityFormatter();
});
}

@Override
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/org/jabref/logic/formatter/Formatters.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.jabref.logic.cleanup.Formatter;
import org.jabref.logic.formatter.bibtexfields.CleanupUrlFormatter;
Expand Down Expand Up @@ -40,6 +42,12 @@
public class Formatters {
private static final Pattern TRUNCATE_PATTERN = Pattern.compile("\\Atruncate\\d+\\z");

private static Map<String, Formatter> keyToFormatterMap;

static {
keyToFormatterMap = getAll().stream().collect(Collectors.toMap(Formatter::getKey, f -> f));
}

private Formatters() {
}

Expand Down Expand Up @@ -92,6 +100,11 @@ public static List<Formatter> getAll() {
return all;
}

public static Optional<Formatter> getFormatterForKey(String name) {
Objects.requireNonNull(name);
return keyToFormatterMap.containsKey(name) ? Optional.of(keyToFormatterMap.get(name)) : Optional.empty();
}

public static Optional<Formatter> getFormatterForModifier(String modifier) {
Objects.requireNonNull(modifier);

Expand All @@ -115,7 +128,7 @@ public static Optional<Formatter> getFormatterForModifier(String modifier) {
int truncateAfter = Integer.parseInt(modifier.substring(8));
return Optional.of(new TruncateFormatter(truncateAfter));
} else {
return getAll().stream().filter(f -> f.getKey().equals(modifier)).findAny();
return getFormatterForKey(modifier);
}
}
}
Loading

0 comments on commit 0c26a55

Please sign in to comment.