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;
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<C extends Change> {
*/
void addChange(C change);
/**
* @return the change that was applied last
* @since 0.0.1
*/
Optional<Change> getLastChange();
/**
* Undoes the current change.
*

View File

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