From 0a154aff50a28df3da9c005f886fa447038272e9 Mon Sep 17 00:00:00 2001 From: Filip Ryblad Date: Tue, 10 Sep 2024 22:33:34 +0200 Subject: [PATCH] Do not make domain model deserialization undoable --- src/main/java/ninjabrainbot/gui/GUI.java | 1 + .../model/datastate/common/DetachedDomainModel.java | 4 ++++ .../java/ninjabrainbot/model/domainmodel/DomainModel.java | 5 +++++ .../model/domainmodel/DomainModelHistory.java | 8 ++++++++ .../ninjabrainbot/model/domainmodel/IDomainModel.java | 2 ++ 5 files changed, 20 insertions(+) diff --git a/src/main/java/ninjabrainbot/gui/GUI.java b/src/main/java/ninjabrainbot/gui/GUI.java index 63873cb..a481e6c 100644 --- a/src/main/java/ninjabrainbot/gui/GUI.java +++ b/src/main/java/ninjabrainbot/gui/GUI.java @@ -106,6 +106,7 @@ private void initModel() { dataState = modelState.dataState; domainModelImportExportService = new DomainModelImportExportService(domainModel, new TempFileAccessor("NinjabrainBot-save-state.txt"), preferences); domainModelImportExportService.triggerDeserialization(); + domainModel.deleteHistory(); Profiler.stop(); } diff --git a/src/main/java/ninjabrainbot/model/datastate/common/DetachedDomainModel.java b/src/main/java/ninjabrainbot/model/datastate/common/DetachedDomainModel.java index 2eb4418..1e1eab4 100644 --- a/src/main/java/ninjabrainbot/model/datastate/common/DetachedDomainModel.java +++ b/src/main/java/ninjabrainbot/model/datastate/common/DetachedDomainModel.java @@ -50,6 +50,10 @@ public void undoUnderWriteLock() { public void redoUnderWriteLock() { } + @Override + public void deleteHistory() { + } + @Override public boolean isReset() { return true; diff --git a/src/main/java/ninjabrainbot/model/domainmodel/DomainModel.java b/src/main/java/ninjabrainbot/model/domainmodel/DomainModel.java index 930cb6f..ceb4048 100644 --- a/src/main/java/ninjabrainbot/model/domainmodel/DomainModel.java +++ b/src/main/java/ninjabrainbot/model/domainmodel/DomainModel.java @@ -127,6 +127,11 @@ public void redoUnderWriteLock() { } } + @Override + public void deleteHistory() { + domainModelHistory.deleteHistory(); + } + @Override public boolean isReset() { return fundamentalComponents.stream().allMatch(IFundamentalComponent::isReset); diff --git a/src/main/java/ninjabrainbot/model/domainmodel/DomainModelHistory.java b/src/main/java/ninjabrainbot/model/domainmodel/DomainModelHistory.java index 3b38f62..532104b 100644 --- a/src/main/java/ninjabrainbot/model/domainmodel/DomainModelHistory.java +++ b/src/main/java/ninjabrainbot/model/domainmodel/DomainModelHistory.java @@ -51,11 +51,13 @@ private void saveSnapshotAfterCurrentSnapshot(DomainModelSnapshot domainModelSna } public DomainModelSnapshot moveToPreviousSnapshotAndGet() { + Assert.isTrue(isInitialized, "The DomainModelHistory has not been initialized."); currentIndex--; return snapshots.get(currentIndex); } public DomainModelSnapshot moveToNextSnapshotAndGet() { + Assert.isTrue(isInitialized, "The DomainModelHistory has not been initialized."); currentIndex++; return snapshots.get(currentIndex); } @@ -68,4 +70,10 @@ public boolean hasNextSnapshot() { return currentIndex < snapshots.size() - 1; } + public void deleteHistory() { + DomainModelSnapshot currentSnapshot = snapshots.get(currentIndex); + snapshots.removeIf(snapshot -> snapshot != currentSnapshot); + currentIndex = 0; + Assert.isEqual(snapshots.size(), 1, "The number of snapshots shall be 1 after deleting domain model history."); + } } diff --git a/src/main/java/ninjabrainbot/model/domainmodel/IDomainModel.java b/src/main/java/ninjabrainbot/model/domainmodel/IDomainModel.java index 34efa5f..c24d0d2 100644 --- a/src/main/java/ninjabrainbot/model/domainmodel/IDomainModel.java +++ b/src/main/java/ninjabrainbot/model/domainmodel/IDomainModel.java @@ -24,6 +24,8 @@ public interface IDomainModel extends IWriteLock { void redoUnderWriteLock(); + void deleteHistory(); + boolean isReset(); boolean isExternalSubscriptionRegistrationAllowed();