/* decorator pattern good choice for 
 * pre & post conditions:  
 * "design-by-contract"
 * 
 * each method will have a pre-condition and
 * a post-condition
 * 
 * you can use this decorator class, which
 * provides pre-condition wrappers, for 
 * debugging purposes
 * 
 * the post-condition wrappers can also
 * be written as well
 */

public class StackCheckDecorator2 
implements Stack {

	private Stack m_inner;
	
	public StackCheckDecorator2 (Stack inner) {
		m_inner = inner;
	}
	
	// pre_condition for push: o is not null
	// post: puts it on the stack
	public void push(Object o) {
		/* pre */
		if(o == null) {
			System.out.println("You are a bad person: push() does not like nullness.");
			System.exit(-1);
		}
		
		/* post */
		String pre_value = m_inner.toString();
		String o_pre_value = o.toString();
		String post_value = m_inner.toString();
		
		// compare
		// ... could also do this with cloning the vectors
		// ... rather than parsing through it with substrings
		// ... if doing this with sets instead of vectors,
		// ... would have to use cloning instead of substrings
		pre_value.substring(1);  // if start with:  <1, 2, 3>
								 // then you'll have:  1, 2, 3>
		String expectedValue = "<" +
								 o_pre_value +
								 pre_value.substring(1);
		
		if(!post_value.equals(expectedValue)) {
			System.out.println("Expected: "+ expectedValue + ", Got: " + post_value);
		}
		
		// to finish, check post_value of o ...
		
		m_inner.push(o);
	}

	// pre_condition for pop: o is non-empty
	// post: will be removed
	public Object pop() {
		if(m_inner.getLength() <= 0) {
			System.out.println("What are you thinking: pop() does not like to be called on an empty stack.");
			System.exit(-2);
		}
		return(m_inner.pop());
	}

	// pre_condition for getTop: o is non-empty
	// post: return the top
	public Object getTop() {
		if(m_inner.getLength() <= 0) {
			System.out.println("Something's wrong with you: getTop() does not like to be called on an empty stack.");
			System.exit(-2);
		}
		return(m_inner.getTop());
	}

	// pre_condition for getLength: none
	// post: returns the length
	public int getLength() {
		return(m_inner.getLength());
	}

	// pre_condition for getLength: none
	// post: clears it
	public void clear() {
		m_inner = null;
	}

}

