Allow retrieving the last change from a change manager

This commit is contained in:
Kai S. K. Engelbart 2021-12-12 05:49:47 +01:00
parent 4b07626155
commit d80dd94e90
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
3 changed files with 31 additions and 5 deletions

View File

@ -1,6 +1,6 @@
package dev.kske.undoredo.core; 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 * A change manager keeps track of subsequent changes and allows un- and redoing them. A specific
@ -22,6 +22,12 @@ public interface ChangeManager<C extends Change> {
*/ */
void addChange(C change); void addChange(C change);
/**
* @return the change that was applied last
* @since 0.0.1
*/
Optional<Change> getLastChange();
/** /**
* Undoes the current change. * Undoes the current change.
* *

View File

@ -22,6 +22,11 @@ public final class UnlimitedChangeManager<C extends Change> implements ChangeMan
++index; ++index;
} }
@Override
public Optional<Change> getLastChange() {
return index == -1 ? Optional.empty() : Optional.of(changes.get(index));
}
@Override @Override
public boolean undo() { public boolean undo() {
if (isUndoAvailable()) { if (isUndoAvailable()) {

View File

@ -35,12 +35,25 @@ class ChangeManagerTest {
} }
/** /**
* Tests the consistency of the change list. * Tests retrieving the last change.
* *
* @since 0.0.1 * @since 0.0.1
*/ */
@Test @Test
@Order(2) @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() { void testGetChanges() {
assertTrue(manager.getChanges().isEmpty()); assertTrue(manager.getChanges().isEmpty());
manager.addChange(change); manager.addChange(change);
@ -54,7 +67,7 @@ class ChangeManagerTest {
* @since 0.0.1 * @since 0.0.1
*/ */
@Test @Test
@Order(2) @Order(4)
void testUndo() { void testUndo() {
assertFalse(manager.isUndoAvailable()); assertFalse(manager.isUndoAvailable());
assertFalse(manager.undo()); assertFalse(manager.undo());
@ -63,6 +76,7 @@ class ChangeManagerTest {
assertTrue(manager.undo()); assertTrue(manager.undo());
assertFalse(manager.isUndoAvailable()); assertFalse(manager.isUndoAvailable());
assertFalse(manager.undo()); assertFalse(manager.undo());
assertTrue(manager.getLastChange().isEmpty());
} }
/** /**
@ -71,7 +85,7 @@ class ChangeManagerTest {
* @since 0.0.1 * @since 0.0.1
*/ */
@Test @Test
@Order(4) @Order(5)
void testRedo() { void testRedo() {
assertFalse(manager.isRedoAvailable()); assertFalse(manager.isRedoAvailable());
assertFalse(manager.redo()); assertFalse(manager.redo());
@ -83,6 +97,7 @@ class ChangeManagerTest {
assertTrue(manager.redo()); assertTrue(manager.redo());
assertFalse(manager.isRedoAvailable()); assertFalse(manager.isRedoAvailable());
assertFalse(manager.redo()); assertFalse(manager.redo());
assertEquals(change, manager.getLastChange().get());
} }
/** /**
@ -91,7 +106,7 @@ class ChangeManagerTest {
* @since 0.0.1 * @since 0.0.1
*/ */
@Test @Test
@Order(5) @Order(6)
void testMark() { void testMark() {
assertTrue(manager.isAtMarkedIndex()); assertTrue(manager.isAtMarkedIndex());
manager.addChange(change); manager.addChange(change);