Extracted ChangeManager interface from concrete implementation
zdm/undo-redo/pipeline/head This commit looks good Details

This commit is contained in:
Maximilian P. Käfer 2021-12-11 17:30:44 +01:00
parent ee6015b353
commit f82df2b979
Signed by: mpk
GPG Key ID: 035869C949377C5C
3 changed files with 81 additions and 44 deletions

View File

@ -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 <C> the change type to store in this change manager
* @author Maximilian K&auml;fer
* @author Kai S. K. Engelbart
* @since 0.0.1
*/
public final class ChangeManager<C extends Change> {
private final List<C> changes = new LinkedList<>();
private int index = -1;
private int markedIndex = -1;
public interface ChangeManager<C extends Change> {
/**
* Applies the given change and appends it to the change list.
@ -24,11 +20,7 @@ public final class ChangeManager<C extends Change> {
* @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<C extends Change> {
* @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<C extends Change> {
* @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<C extends Change> {
* @return all stored changes
* @since 0.0.1
*/
public List<C> getChanges() {
return Collections.unmodifiableList(changes);
}
List<C> getChanges();
}

View File

@ -0,0 +1,69 @@
package dev.kske.undoredo;
import java.util.*;
/**
* @param <C> the change type to store in this change manager
* @author Maximilian K&auml;fer
* @author Kai S. K. Engelbart
* @since 0.0.1
*/
public final class UnlimitedChangeManager<C extends Change> implements ChangeManager<C> {
private final List<C> 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<C> getChanges() {
return Collections.unmodifiableList(changes);
}
}

View File

@ -16,7 +16,7 @@ class ChangeManagerTest {
@BeforeEach
void prepareChangeManager() {
manager = new ChangeManager<>();
manager = new UnlimitedChangeManager<>();
wrapper = new IntWrapper();
change = new IntChange(wrapper, 1);
}