Extracted ChangeManager interface from concrete implementation
All checks were successful
zdm/undo-redo/pipeline/head This commit looks good
All checks were successful
zdm/undo-redo/pipeline/head This commit looks good
This commit is contained in:
parent
ee6015b353
commit
f82df2b979
@ -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ä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();
|
||||
}
|
||||
|
69
src/main/java/dev/kske/undoredo/UnlimitedChangeManager.java
Normal file
69
src/main/java/dev/kske/undoredo/UnlimitedChangeManager.java
Normal 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ä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);
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@ class ChangeManagerTest {
|
||||
|
||||
@BeforeEach
|
||||
void prepareChangeManager() {
|
||||
manager = new ChangeManager<>();
|
||||
manager = new UnlimitedChangeManager<>();
|
||||
wrapper = new IntWrapper();
|
||||
change = new IntChange(wrapper, 1);
|
||||
}
|
||||
|
Reference in New Issue
Block a user