package basics.collections;

import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.List;
import basics.testing.Test;

/*
 * First in last out Stack.
 */
public class StackFILO<E>
{
private List<E> items;

public StackFILO()
{
   items = new ArrayList<E>();
}

public void push(E item)
{
   items.add(item);
}

public E pop()
{
   int size = items.size();
   if(size == 0)
      throw new EmptyStackException();
   return items.remove(size - 1);
}

public E peek()
{
   int size = items.size();
   if(size == 0)
      throw new EmptyStackException();
   return items.get(size - 1);
}

public boolean empty()
{
   return items.isEmpty();
}

public static void unittest()
{
   StackFILO<String> s = new StackFILO<String>();
   s.push("a");
   Test.assertEqualsTrue(s.peek(), "a", "peek not a but " + s.peek());
   s.push("b");
   Test.assertEqualsTrue(s.peek(), "b", "peek not b but " + s.peek());
   s.push("c");
   Test.assertEqualsTrue(s.peek(), "c", "peek not c but " + s.peek());
   Test.assertEqualsTrue(s.pop(), "c", "pop not c");
   Test.assertEqualsTrue(s.pop(), "b", "pop not b");
   Test.assertEqualsTrue(s.pop(), "a", "pop not a");
   Test.assertTrue(s.empty(), "stack not empty");
}
}
