From f82df2b979fe0fe4648c9582b19e6e29e4c10d6b Mon Sep 17 00:00:00 2001 From: DieGurke Date: Sat, 11 Dec 2021 17:30:44 +0100 Subject: [PATCH] Extracted ChangeManager interface from concrete implementation --- .../java/dev/kske/undoredo/ChangeManager.java | 54 +++------------ .../kske/undoredo/UnlimitedChangeManager.java | 69 +++++++++++++++++++ .../dev/kske/undoredo/ChangeManagerTest.java | 2 +- 3 files changed, 81 insertions(+), 44 deletions(-) create mode 100644 src/main/java/dev/kske/undoredo/UnlimitedChangeManager.java diff --git a/src/main/java/dev/kske/undoredo/ChangeManager.java b/src/main/java/dev/kske/undoredo/ChangeManager.java index 4d45bd5..25e73f6 100644 --- a/src/main/java/dev/kske/undoredo/ChangeManager.java +++ b/src/main/java/dev/kske/undoredo/ChangeManager.java @@ -1,6 +1,6 @@ package dev.kske.undoredo; -import java.util.*; +import java.util.List; /** * A change manager keeps track of subsequent changes and allows un- and redoing them. A specific @@ -9,14 +9,10 @@ import java.util.*; * * @param the change type to store in this change manager * @author Maximilian Käfer + * @author Kai S. K. Engelbart * @since 0.0.1 */ -public final class ChangeManager { - - private final List changes = new LinkedList<>(); - - private int index = -1; - private int markedIndex = -1; +public interface ChangeManager { /** * Applies the given change and appends it to the change list. @@ -24,11 +20,7 @@ public final class ChangeManager { * @param change the change to add * @since 0.0.1 */ - public void addChange(C change) { - change.apply(); - changes.add(change); - ++index; - } + void addChange(C change); /** * Undoes the current change. @@ -36,14 +28,7 @@ public final class ChangeManager { * @return whether an action was performed * @since 0.1.0 */ - public boolean undo() { - if (isUndoAvailable()) { - changes.get(index).invert().apply(); - --index; - return true; - } - return false; - } + boolean undo(); /** * Applies the change that was undone before. @@ -51,47 +36,32 @@ public final class ChangeManager { * @return whether an action was performed * @since 0.0.1 */ - public boolean redo() { - if (isRedoAvailable()) { - changes.get(index + 1).apply(); - ++index; - return true; - } - return false; - } + boolean redo(); /** * Marks the current change. * * @since 0.0.1 */ - public void mark() { - markedIndex = index; - } + void mark(); /** * @return whether the current change is marked * @since 0.0.1 */ - public boolean isAtMarkedIndex() { - return markedIndex == index; - } + boolean isAtMarkedIndex(); /** * @return whether a change is present that can be undone * @since 0.0.1 */ - public boolean isUndoAvailable() { - return index > -1; - } + boolean isUndoAvailable(); /** * @return whether a change is present that can be redone * @since 0.0.1 */ - public boolean isRedoAvailable() { - return index < changes.size() - 1; - } + boolean isRedoAvailable(); /** * Provides an unmodifiable view of the changes stored in this change manager. @@ -99,7 +69,5 @@ public final class ChangeManager { * @return all stored changes * @since 0.0.1 */ - public List getChanges() { - return Collections.unmodifiableList(changes); - } + List getChanges(); } diff --git a/src/main/java/dev/kske/undoredo/UnlimitedChangeManager.java b/src/main/java/dev/kske/undoredo/UnlimitedChangeManager.java new file mode 100644 index 0000000..d927ee7 --- /dev/null +++ b/src/main/java/dev/kske/undoredo/UnlimitedChangeManager.java @@ -0,0 +1,69 @@ +package dev.kske.undoredo; + +import java.util.*; + +/** + * @param the change type to store in this change manager + * @author Maximilian Käfer + * @author Kai S. K. Engelbart + * @since 0.0.1 + */ +public final class UnlimitedChangeManager implements ChangeManager { + + private final List changes = new LinkedList<>(); + + private int index = -1; + private int markedIndex = -1; + + @Override + public void addChange(C change) { + change.apply(); + changes.add(change); + ++index; + } + + @Override + public boolean undo() { + if (isUndoAvailable()) { + changes.get(index).invert().apply(); + --index; + return true; + } + return false; + } + + @Override + public boolean redo() { + if (isRedoAvailable()) { + changes.get(index + 1).apply(); + ++index; + return true; + } + return false; + } + + @Override + public void mark() { + markedIndex = index; + } + + @Override + public boolean isAtMarkedIndex() { + return markedIndex == index; + } + + @Override + public boolean isUndoAvailable() { + return index > -1; + } + + @Override + public boolean isRedoAvailable() { + return index < changes.size() - 1; + } + + @Override + public List getChanges() { + return Collections.unmodifiableList(changes); + } +} diff --git a/src/test/java/dev/kske/undoredo/ChangeManagerTest.java b/src/test/java/dev/kske/undoredo/ChangeManagerTest.java index b3e1b72..db18ba5 100644 --- a/src/test/java/dev/kske/undoredo/ChangeManagerTest.java +++ b/src/test/java/dev/kske/undoredo/ChangeManagerTest.java @@ -16,7 +16,7 @@ class ChangeManagerTest { @BeforeEach void prepareChangeManager() { - manager = new ChangeManager<>(); + manager = new UnlimitedChangeManager<>(); wrapper = new IntWrapper(); change = new IntChange(wrapper, 1); }