From a7deb7ce29d7b04f076e376e19331380ca3b077e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Cr=C3=A9mieux?= Date: Sat, 22 Jun 2024 18:40:57 +0200 Subject: [PATCH] Replace if construct by pattern matching for DatabaseChangeView creation in DatabaseChangeDetailsViewFactory ref: https://github.com/JabRef/jabref/issues/11220 --- .../DatabaseChangeDetailsViewFactory.java | 61 ++++++++++++------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/jabref/gui/collab/DatabaseChangeDetailsViewFactory.java b/src/main/java/org/jabref/gui/collab/DatabaseChangeDetailsViewFactory.java index a781817fd80..e20809388f0 100644 --- a/src/main/java/org/jabref/gui/collab/DatabaseChangeDetailsViewFactory.java +++ b/src/main/java/org/jabref/gui/collab/DatabaseChangeDetailsViewFactory.java @@ -57,28 +57,43 @@ public DatabaseChangeDetailsViewFactory(BibDatabaseContext databaseContext, } public DatabaseChangeDetailsView create(DatabaseChange databaseChange) { - // TODO: Use Pattern Matching for switch once it's out of preview - if (databaseChange instanceof EntryChange entryChange) { - return new EntryChangeDetailsView(entryChange.getOldEntry(), entryChange.getNewEntry(), databaseContext, dialogService, stateManager, themeManager, preferencesService, entryTypesManager, previewViewer, taskExecutor); - } else if (databaseChange instanceof EntryAdd entryAdd) { - return new EntryWithPreviewAndSourceDetailsView(entryAdd.getAddedEntry(), databaseContext, preferencesService, entryTypesManager, previewViewer); - } else if (databaseChange instanceof EntryDelete entryDelete) { - return new EntryWithPreviewAndSourceDetailsView(entryDelete.getDeletedEntry(), databaseContext, preferencesService, entryTypesManager, previewViewer); - } else if (databaseChange instanceof BibTexStringAdd stringAdd) { - return new BibTexStringAddDetailsView(stringAdd); - } else if (databaseChange instanceof BibTexStringDelete stringDelete) { - return new BibTexStringDeleteDetailsView(stringDelete); - } else if (databaseChange instanceof BibTexStringChange stringChange) { - return new BibTexStringChangeDetailsView(stringChange); - } else if (databaseChange instanceof BibTexStringRename stringRename) { - return new BibTexStringRenameDetailsView(stringRename); - } else if (databaseChange instanceof MetadataChange metadataChange) { - return new MetadataChangeDetailsView(metadataChange, preferencesService.getCitationKeyPatternPreferences().getKeyPatterns()); - } else if (databaseChange instanceof GroupChange groupChange) { - return new GroupChangeDetailsView(groupChange); - } else if (databaseChange instanceof PreambleChange preambleChange) { - return new PreambleChangeDetailsView(preambleChange); - } - throw new UnsupportedOperationException("Cannot preview the given change: " + databaseChange.getName()); + return switch (databaseChange) { + case EntryChange entryChange -> new EntryChangeDetailsView( + entryChange.getOldEntry(), + entryChange.getNewEntry(), + databaseContext, + dialogService, + stateManager, + themeManager, + preferencesService, + entryTypesManager, + previewViewer, + taskExecutor + ); + case EntryAdd entryAdd -> new EntryWithPreviewAndSourceDetailsView( + entryAdd.getAddedEntry(), + databaseContext, + preferencesService, + entryTypesManager, + previewViewer + ); + case EntryDelete entryDelete -> new EntryWithPreviewAndSourceDetailsView( + entryDelete.getDeletedEntry(), + databaseContext, + preferencesService, + entryTypesManager, + previewViewer + ); + case BibTexStringAdd stringAdd -> new BibTexStringAddDetailsView(stringAdd); + case BibTexStringDelete stringDelete -> new BibTexStringDeleteDetailsView(stringDelete); + case BibTexStringChange stringChange -> new BibTexStringChangeDetailsView(stringChange); + case BibTexStringRename stringRename -> new BibTexStringRenameDetailsView(stringRename); + case MetadataChange metadataChange -> new MetadataChangeDetailsView( + metadataChange, + preferencesService.getCitationKeyPatternPreferences().getKeyPatterns() + ); + case GroupChange groupChange -> new GroupChangeDetailsView(groupChange); + case PreambleChange preambleChange -> new PreambleChangeDetailsView(preambleChange); + }; } }