package senac_turma02.stack;

// import java.lang.reflect.Array;

public class StackImplArray<T extends Number> implements StackImpl<T> {
	/////////////////////////
	// Interface
	/////////////////////////
	@SuppressWarnings("unchecked")
	public T pop() throws StackEmptyException {
		if( isEmpty() )
			throw new StackEmptyException("Pilha Vazia!");
		return (T)data_[topIndex_--];
	}
	public void push(T n) throws StackFullException  {
		if( isFull() )
			throw new StackFullException("Pilha Cheia!");
		data_[++topIndex_] = n;
	}
	@SuppressWarnings("unchecked")
	public T top() throws StackEmptyException {
		if( isEmpty() )
			throw new StackEmptyException("Pilha Vazia!");
		return (T)data_[topIndex_];
	}
	public boolean isEmpty() {
		return topIndex_ == -1 ? true: false;
	}
	public boolean isFull() {
		return topIndex_ == size_-1 ? true: false;
	}
	
	/////////////////////////
	// Constructors
	/////////////////////////
	//
	// pilhaInicial[0] -> topo
	// pilhaInicial[1] -> topo - 1
	//
	@SuppressWarnings("unchecked")
	public StackImplArray(T pilhaInicial[]) {
		size_     = pilhaInicial.length;
		topIndex_ = size_ - 1;		
		data_     = new Object[size_];
		//data_ = (T[])Array.newInstance(data_.getClass().getComponentType(), size_);

		int i = topIndex_;
		for( T elem : pilhaInicial ) {
			data_[i--] = elem;
		}
	}
	
	@SuppressWarnings("unchecked")
	public StackImplArray(int size) {
		size_     = size;
		topIndex_ = -1;		
		data_     = new Object[size_];
		//data_ = (T[])Array.newInstance(data_.getClass().getComponentType(), size_);

	}
	public StackImplArray() {
		this(1);
	}	
	
	/////////////////////////
	// Implementation
	/////////////////////////
	private int size_;
	private int topIndex_;
	private Object[] data_;
}

