101 lines
1.9 KiB
Java
101 lines
1.9 KiB
Java
package dev.kske.undoredo;
|
|
|
|
import java.util.*;
|
|
|
|
/**
|
|
* @param <C> the change types to store in this change manager
|
|
* @author Maximilian Käfer
|
|
* @since 0.0.1
|
|
*/
|
|
public final class ChangeManager<C extends Change> {
|
|
|
|
private final List<C> changes = new LinkedList<>();
|
|
|
|
private int index;
|
|
private int markedIndex;
|
|
|
|
/**
|
|
* Adds a change to the changes list.
|
|
*
|
|
* @param change the change to add
|
|
* @since 0.0.1
|
|
*/
|
|
public void addChange(C change) {
|
|
change.apply();
|
|
changes.add(change);
|
|
}
|
|
|
|
/**
|
|
* Undoes the change at the current index position.
|
|
*
|
|
* @return whether the operation could be executed due to one being currently available
|
|
* @since 0.1.0
|
|
*/
|
|
public boolean undo() {
|
|
if (isUndoAvailable()) {
|
|
changes.get(index).invert().apply();
|
|
--index;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Applies the change that was undone before.
|
|
*
|
|
* @return whether the operation could be executed due to one being currently available
|
|
* @since 0.0.1
|
|
*/
|
|
public boolean redo() {
|
|
if (isRedoAvailable()) {
|
|
changes.get(index + 1).apply();
|
|
++index;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Marks the current index.
|
|
*
|
|
* @since 0.0.1
|
|
*/
|
|
public void mark() {
|
|
markedIndex = index;
|
|
}
|
|
|
|
/**
|
|
* @return whether the current index was marked
|
|
* @since 0.0.1
|
|
*/
|
|
public boolean isAtMarkedIndex() {
|
|
return markedIndex == index;
|
|
}
|
|
|
|
/**
|
|
* @return whether the undo operation is currently available
|
|
* @since 0.0.1
|
|
*/
|
|
public boolean isUndoAvailable() {
|
|
return index > 0;
|
|
}
|
|
|
|
/**
|
|
* @return whether the redo operation is currently available.
|
|
* @since 0.0.1
|
|
*/
|
|
public boolean isRedoAvailable() {
|
|
return index < changes.size() - 1;
|
|
}
|
|
|
|
/**
|
|
* Provides an unmodifiable view of the changes stored in this change manager.
|
|
*
|
|
* @return all stored changes
|
|
* @since 0.0.1
|
|
*/
|
|
public List<C> getChanges() {
|
|
return Collections.unmodifiableList(changes);
|
|
}
|
|
}
|