package jdk.internal.org.jline.reader.impl;
import java.util.function.Consumer;
public class UndoTree<T> {
private final Consumer<T> state;
private final Node parent;
private Node current;
public UndoTree(Consumer<T> s) {
state = s;
parent = new Node(null);
parent.left = parent;
clear();
}
public void clear() {
current = parent;
}
public void newState(T state) {
Node node = new Node(state);
current.right = node;
node.left = current;
current = node;
}
public boolean canUndo() {
return current.left != parent;
}
public boolean canRedo() {
return current.right != null;
}
public void undo() {
if (!canUndo()) {
throw new IllegalStateException("Cannot undo.");
}
current = current.left;
state.accept(current.state);
}
public void redo() {
if (!canRedo()) {
throw new IllegalStateException("Cannot redo.");
}
current = current.right;
state.accept(current.state);
}
private class Node {
private final T state;
private Node left = null;
private Node right = null;
public Node(T s) {
state = s;
}
}
}