From d80dd94e906529a2e9ea0d2e720bf262d111a8dd Mon Sep 17 00:00:00 2001 From: kske Date: Sun, 12 Dec 2021 05:49:47 +0100 Subject: [PATCH] Allow retrieving the last change from a change manager --- .../dev/kske/undoredo/core/ChangeManager.java | 8 ++++++- .../undoredo/core/UnlimitedChangeManager.java | 5 ++++ .../kske/undoredo/core/ChangeManagerTest.java | 23 +++++++++++++++---- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/dev/kske/undoredo/core/ChangeManager.java b/core/src/main/java/dev/kske/undoredo/core/ChangeManager.java index 7b887e7..be9424b 100644 --- a/core/src/main/java/dev/kske/undoredo/core/ChangeManager.java +++ b/core/src/main/java/dev/kske/undoredo/core/ChangeManager.java @@ -1,6 +1,6 @@ package dev.kske.undoredo.core; -import java.util.List; +import java.util.*; /** * A change manager keeps track of subsequent changes and allows un- and redoing them. A specific @@ -22,6 +22,12 @@ public interface ChangeManager { */ void addChange(C change); + /** + * @return the change that was applied last + * @since 0.0.1 + */ + Optional getLastChange(); + /** * Undoes the current change. * diff --git a/core/src/main/java/dev/kske/undoredo/core/UnlimitedChangeManager.java b/core/src/main/java/dev/kske/undoredo/core/UnlimitedChangeManager.java index 8a0ff35..b353a7d 100644 --- a/core/src/main/java/dev/kske/undoredo/core/UnlimitedChangeManager.java +++ b/core/src/main/java/dev/kske/undoredo/core/UnlimitedChangeManager.java @@ -22,6 +22,11 @@ public final class UnlimitedChangeManager implements ChangeMan ++index; } + @Override + public Optional getLastChange() { + return index == -1 ? Optional.empty() : Optional.of(changes.get(index)); + } + @Override public boolean undo() { if (isUndoAvailable()) { diff --git a/core/src/test/java/dev/kske/undoredo/core/ChangeManagerTest.java b/core/src/test/java/dev/kske/undoredo/core/ChangeManagerTest.java index 4643970..2508b56 100644 --- a/core/src/test/java/dev/kske/undoredo/core/ChangeManagerTest.java +++ b/core/src/test/java/dev/kske/undoredo/core/ChangeManagerTest.java @@ -35,12 +35,25 @@ class ChangeManagerTest { } /** - * Tests the consistency of the change list. + * Tests retrieving the last change. * * @since 0.0.1 */ @Test @Order(2) + void testLastChange() { + assertTrue(manager.getLastChange().isEmpty()); + manager.addChange(change); + assertEquals(change, manager.getLastChange().get()); + } + + /** + * Tests the consistency of the change list. + * + * @since 0.0.1 + */ + @Test + @Order(3) void testGetChanges() { assertTrue(manager.getChanges().isEmpty()); manager.addChange(change); @@ -54,7 +67,7 @@ class ChangeManagerTest { * @since 0.0.1 */ @Test - @Order(2) + @Order(4) void testUndo() { assertFalse(manager.isUndoAvailable()); assertFalse(manager.undo()); @@ -63,6 +76,7 @@ class ChangeManagerTest { assertTrue(manager.undo()); assertFalse(manager.isUndoAvailable()); assertFalse(manager.undo()); + assertTrue(manager.getLastChange().isEmpty()); } /** @@ -71,7 +85,7 @@ class ChangeManagerTest { * @since 0.0.1 */ @Test - @Order(4) + @Order(5) void testRedo() { assertFalse(manager.isRedoAvailable()); assertFalse(manager.redo()); @@ -83,6 +97,7 @@ class ChangeManagerTest { assertTrue(manager.redo()); assertFalse(manager.isRedoAvailable()); assertFalse(manager.redo()); + assertEquals(change, manager.getLastChange().get()); } /** @@ -91,7 +106,7 @@ class ChangeManagerTest { * @since 0.0.1 */ @Test - @Order(5) + @Order(6) void testMark() { assertTrue(manager.isAtMarkedIndex()); manager.addChange(change);