package leigh.eo.io;

import leigh.eo.EO;

import java.io.IOException;
import java.util.EmptyStackException;
import java.util.Stack;

/**
 * Implementation of <code>EOInputStream</code> based around a <code>Stack</code>. Reading from the stream
 * drains the stack.
 *
 * @author Alex Leigh
 */
public class StackEOInputStream implements EOInputStream {
    private final Stack<EO> stack;

    public StackEOInputStream(Stack<EO> stack) {
        this.stack = stack;
    }

    @Override
    public EO readEo() throws IOException {
        try {
            return stack.pop();
        } catch (EmptyStackException e) {
            return null;
        }
    }

    @Override
    public void close() throws Exception {
        // NOP
    }
}
