From f369b09c48eaae48f0177bbbed2a31a240a0ca51 Mon Sep 17 00:00:00 2001 From: Loay Ghreeb Date: Thu, 29 Aug 2024 10:42:28 +0300 Subject: [PATCH] Use BibEntry.getId instead of System.identityHashCode --- ...s.md => 0038-use-entryId-for-bibentries.md} | 8 ++++---- .../gui/duplicationFinder/DuplicateSearch.java | 6 +++--- .../jabref/gui/groups/GroupNodeViewModel.java | 18 +++++++++--------- .../org/jabref/model/groups/SearchGroup.java | 10 +++++----- 4 files changed, 21 insertions(+), 21 deletions(-) rename docs/decisions/{0038-use-identityhashcode-for-bibentries.md => 0038-use-entryId-for-bibentries.md} (60%) diff --git a/docs/decisions/0038-use-identityhashcode-for-bibentries.md b/docs/decisions/0038-use-entryId-for-bibentries.md similarity index 60% rename from docs/decisions/0038-use-identityhashcode-for-bibentries.md rename to docs/decisions/0038-use-entryId-for-bibentries.md index d5c9fb25062..f709ccf9d1d 100644 --- a/docs/decisions/0038-use-identityhashcode-for-bibentries.md +++ b/docs/decisions/0038-use-entryId-for-bibentries.md @@ -1,11 +1,11 @@ --- -title: Use System.identityHashCode for BibEntry at indexing +title: Use BibEntry.getId for BibEntry at indexing nav_order: 38 parent: Decision Records --- -# Use `System.identityHashCode` for BibEntries at Indexing +# Use `BibEntry.getId` for BibEntries at Indexing ## Context and Problem Statement @@ -22,9 +22,9 @@ This, however, is not useful in the UI, where equal entries are not the same ent ## Considered Options -* Use `System.identityHashCode` for indexing `BibEntry` +* Use `BibEntry.getId` for indexing `BibEntry` * Rewrite `BibEntry` logic ## Decision Outcome -Chosen option: "Use `System.identityHashCode` for indexing `BibEntry`", because is the "natural" thing to ensure distinction between two instances of a `BibEntry` object - regardless of equality. +Chosen option: "Use `BibEntry.getId` for indexing `BibEntry`", because is the "natural" thing to ensure distinction between two instances of a `BibEntry` object - regardless of equality. diff --git a/src/main/java/org/jabref/gui/duplicationFinder/DuplicateSearch.java b/src/main/java/org/jabref/gui/duplicationFinder/DuplicateSearch.java index de084e65aff..96aa9ec959b 100644 --- a/src/main/java/org/jabref/gui/duplicationFinder/DuplicateSearch.java +++ b/src/main/java/org/jabref/gui/duplicationFinder/DuplicateSearch.java @@ -219,7 +219,7 @@ private void handleDuplicates(DuplicateSearchResult result) { */ static class DuplicateSearchResult { - private final Map toRemove = new HashMap<>(); + private final Map toRemove = new HashMap<>(); private final List toAdd = new ArrayList<>(); private int duplicates = 0; @@ -234,7 +234,7 @@ public synchronized List getToAdd() { public synchronized void remove(BibEntry entry) { // ADR-0038 - toRemove.put(System.identityHashCode(entry), entry); + toRemove.put(entry.getId(), entry); duplicates++; } @@ -252,7 +252,7 @@ public synchronized void replace(BibEntry entry, BibEntry replacement) { public synchronized boolean isToRemove(BibEntry entry) { // ADR-0038 - return toRemove.containsKey(System.identityHashCode(entry)); + return toRemove.containsKey(entry.getId()); } public synchronized int getDuplicateCount() { diff --git a/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java b/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java index b5fc1fc11df..37fdca01663 100644 --- a/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java +++ b/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java @@ -64,7 +64,7 @@ public class GroupNodeViewModel { private final BibDatabaseContext databaseContext; private final StateManager stateManager; private final GroupTreeNode groupNode; - private final ObservableMap matchedEntries = FXCollections.observableHashMap(); + private final ObservableMap matchedEntries = FXCollections.observableHashMap(); private final SimpleBooleanProperty hasChildren; private final SimpleBooleanProperty expandedProperty = new SimpleBooleanProperty(); private final BooleanBinding anySelectedEntriesMatched; @@ -258,21 +258,21 @@ private void onDatabaseChanged(ListChangeListener.Change cha for (BibEntry changedEntry : change.getList().subList(change.getFrom(), change.getTo())) { if (groupNode.matches(changedEntry)) { // ADR-0038 - matchedEntries.put(System.identityHashCode(changedEntry), changedEntry); + matchedEntries.put(changedEntry.getId(), changedEntry); } else { // ADR-0038 - matchedEntries.remove(System.identityHashCode(changedEntry)); + matchedEntries.remove(changedEntry.getId()); } } } else { for (BibEntry removedEntry : change.getRemoved()) { // ADR-0038 - matchedEntries.remove(System.identityHashCode(removedEntry)); + matchedEntries.remove(removedEntry.getId()); } for (BibEntry addedEntry : change.getAddedSubList()) { if (groupNode.matches(addedEntry)) { // ADR-0038 - matchedEntries.put(System.identityHashCode(addedEntry), addedEntry); + matchedEntries.put(addedEntry.getId(), addedEntry); } } } @@ -300,7 +300,7 @@ private void updateMatchedEntries() { .onSuccess(entries -> { matchedEntries.clear(); // ADR-0038 - entries.forEach(entry -> matchedEntries.put(System.identityHashCode(entry), entry)); + entries.forEach(entry -> matchedEntries.put(entry.getId(), entry)); }) .executeWith(taskExecutor); } @@ -549,10 +549,10 @@ public void listen(IndexAddedOrUpdatedEvent event) { searchGroup.updateMatches(entry); if (groupNode.matches(entry)) { // ADR-0038 - matchedEntries.put(System.identityHashCode(entry), entry); + matchedEntries.put(entry.getId(), entry); } else { // ADR-0038 - matchedEntries.remove(System.identityHashCode(entry)); + matchedEntries.remove(entry.getId()); } } }).executeWith(taskExecutor); @@ -566,7 +566,7 @@ public void listen(IndexRemovedEvent event) { for (BibEntry entry : event.entries()) { searchGroup.updateMatches(entry); // ADR-0038 - matchedEntries.remove(System.identityHashCode(entry)); + matchedEntries.remove(entry.getId()); } }).executeWith(taskExecutor); } diff --git a/src/main/java/org/jabref/model/groups/SearchGroup.java b/src/main/java/org/jabref/model/groups/SearchGroup.java index eaf0e9412de..ba865148d65 100644 --- a/src/main/java/org/jabref/model/groups/SearchGroup.java +++ b/src/main/java/org/jabref/model/groups/SearchGroup.java @@ -28,7 +28,7 @@ public class SearchGroup extends AbstractGroup { public static final Version VERSION_6_0_ALPHA = Version.parse("6.0-alpha"); private static final Logger LOGGER = LoggerFactory.getLogger(SearchGroup.class); - private final ObservableMap matchedEntries = FXCollections.observableHashMap(); + private final ObservableMap matchedEntries = FXCollections.observableHashMap(); private SearchQuery query; private LuceneManager luceneManager; @@ -75,7 +75,7 @@ public void updateMatches() { matchedEntries.clear(); // TODO: Search should be done in a background thread // ADR-0038 - luceneManager.search(query).getMatchedEntries().forEach(entry -> matchedEntries.put(System.identityHashCode(entry), entry)); + luceneManager.search(query).getMatchedEntries().forEach(entry -> matchedEntries.put(entry.getId(), entry)); } public void updateMatches(BibEntry entry) { @@ -84,10 +84,10 @@ public void updateMatches(BibEntry entry) { } if (luceneManager.isMatched(entry, query)) { // ADR-0038 - matchedEntries.put(System.identityHashCode(entry), entry); + matchedEntries.put(entry.getId(), entry); } else { // ADR-0038 - matchedEntries.remove(System.identityHashCode(entry)); + matchedEntries.remove(entry.getId()); } } @@ -108,7 +108,7 @@ public boolean equals(Object o) { @Override public boolean contains(BibEntry entry) { // ADR-0038 - return matchedEntries.containsKey(System.identityHashCode(entry)); + return matchedEntries.containsKey(entry.getId()); } @Override