// 
// Decompiled by Procyon v0.6.0
// 

package 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(final Consumer<T> s) {
        this.state = s;
        (this.parent = new Node(null)).left = this.parent;
        this.clear();
    }
    
    public void clear() {
        this.current = this.parent;
    }
    
    public void newState(final T state) {
        final Node node = new Node(state);
        this.current.right = node;
        node.left = this.current;
        this.current = node;
    }
    
    public boolean canUndo() {
        return this.current.left != this.parent;
    }
    
    public boolean canRedo() {
        return this.current.right != null;
    }
    
    public void undo() {
        if (!this.canUndo()) {
            throw new IllegalStateException("Cannot undo.");
        }
        this.current = this.current.left;
        this.state.accept(this.current.state);
    }
    
    public void redo() {
        if (!this.canRedo()) {
            throw new IllegalStateException("Cannot redo.");
        }
        this.current = this.current.right;
        this.state.accept(this.current.state);
    }
    
    private class Node
    {
        private final T state;
        private Node left;
        private Node right;
        
        public Node(final T s) {
            this.left = null;
            this.right = null;
            this.state = s;
        }
    }
}
