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;
|
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
|
* 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
|
* @param <C> the change type to store in this change manager
|
||||||
* @author Maximilian Käfer
|
* @author Maximilian Käfer
|
||||||
|
* @author Kai S. K. Engelbart
|
||||||
* @since 0.0.1
|
* @since 0.0.1
|
||||||
*/
|
*/
|
||||||
public final class ChangeManager<C extends Change> {
|
public interface ChangeManager<C extends Change> {
|
||||||
|
|
||||||
private final List<C> changes = new LinkedList<>();
|
|
||||||
|
|
||||||
private int index = -1;
|
|
||||||
private int markedIndex = -1;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Applies the given change and appends it to the change list.
|
* 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
|
* @param change the change to add
|
||||||
* @since 0.0.1
|
* @since 0.0.1
|
||||||
*/
|
*/
|
||||||
public void addChange(C change) {
|
void addChange(C change);
|
||||||
change.apply();
|
|
||||||
changes.add(change);
|
|
||||||
++index;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Undoes the current change.
|
* Undoes the current change.
|
||||||
@ -36,14 +28,7 @@ public final class ChangeManager<C extends Change> {
|
|||||||
* @return whether an action was performed
|
* @return whether an action was performed
|
||||||
* @since 0.1.0
|
* @since 0.1.0
|
||||||
*/
|
*/
|
||||||
public boolean undo() {
|
boolean undo();
|
||||||
if (isUndoAvailable()) {
|
|
||||||
changes.get(index).invert().apply();
|
|
||||||
--index;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Applies the change that was undone before.
|
* Applies the change that was undone before.
|
||||||
@ -51,47 +36,32 @@ public final class ChangeManager<C extends Change> {
|
|||||||
* @return whether an action was performed
|
* @return whether an action was performed
|
||||||
* @since 0.0.1
|
* @since 0.0.1
|
||||||
*/
|
*/
|
||||||
public boolean redo() {
|
boolean redo();
|
||||||
if (isRedoAvailable()) {
|
|
||||||
changes.get(index + 1).apply();
|
|
||||||
++index;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Marks the current change.
|
* Marks the current change.
|
||||||
*
|
*
|
||||||
* @since 0.0.1
|
* @since 0.0.1
|
||||||
*/
|
*/
|
||||||
public void mark() {
|
void mark();
|
||||||
markedIndex = index;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return whether the current change is marked
|
* @return whether the current change is marked
|
||||||
* @since 0.0.1
|
* @since 0.0.1
|
||||||
*/
|
*/
|
||||||
public boolean isAtMarkedIndex() {
|
boolean isAtMarkedIndex();
|
||||||
return markedIndex == index;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return whether a change is present that can be undone
|
* @return whether a change is present that can be undone
|
||||||
* @since 0.0.1
|
* @since 0.0.1
|
||||||
*/
|
*/
|
||||||
public boolean isUndoAvailable() {
|
boolean isUndoAvailable();
|
||||||
return index > -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return whether a change is present that can be redone
|
* @return whether a change is present that can be redone
|
||||||
* @since 0.0.1
|
* @since 0.0.1
|
||||||
*/
|
*/
|
||||||
public boolean isRedoAvailable() {
|
boolean isRedoAvailable();
|
||||||
return index < changes.size() - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides an unmodifiable view of the changes stored in this change manager.
|
* 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
|
* @return all stored changes
|
||||||
* @since 0.0.1
|
* @since 0.0.1
|
||||||
*/
|
*/
|
||||||
public List<C> getChanges() {
|
List<C> getChanges();
|
||||||
return Collections.unmodifiableList(changes);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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
|
@BeforeEach
|
||||||
void prepareChangeManager() {
|
void prepareChangeManager() {
|
||||||
manager = new ChangeManager<>();
|
manager = new UnlimitedChangeManager<>();
|
||||||
wrapper = new IntWrapper();
|
wrapper = new IntWrapper();
|
||||||
change = new IntChange(wrapper, 1);
|
change = new IntChange(wrapper, 1);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user