[stack-arakaki commit] r3 - in trunk/src: . edu edu/hawaii edu/hawaii/stack

2 views
Skip to first unread message

codesite...@google.com

unread,
Oct 6, 2008, 4:52:33 AM10/6/08
to stack-arak...@googlegroups.com
Author: dan.m.arakaki
Date: Mon Oct 6 01:50:22 2008
New Revision: 3

Added:
trunk/src/
trunk/src/edu/
trunk/src/edu/hawaii/
trunk/src/edu/hawaii/stack/
trunk/src/edu/hawaii/stack/ClearStack.java (contents, props changed)
trunk/src/edu/hawaii/stack/EmptyStackException.java (contents, props
changed)
trunk/src/edu/hawaii/stack/Stack.java (contents, props changed)
trunk/src/edu/hawaii/stack/TestClearStack.java (contents, props
changed)
trunk/src/edu/hawaii/stack/TestStack.java (contents, props changed)
trunk/src/edu/hawaii/stack/overview.html (contents, props changed)
trunk/src/edu/hawaii/stack/package.html (contents, props changed)

Log:
Forgot to commit the src folder...

Added: trunk/src/edu/hawaii/stack/ClearStack.java
==============================================================================
--- (empty file)
+++ trunk/src/edu/hawaii/stack/ClearStack.java Mon Oct 6 01:50:22 2008
@@ -0,0 +1,40 @@
+package edu.hawaii.stack;
+
+import java.util.Iterator;
+
+/**
+ * Implements a "clear" stack ADT, in the sense that you can use an
iterator
+ * to see the internal contents of the stack.
+ * ClearStack is also a Bean with a read-only attributes called "iterator"
and "top".
+ *
+ * @author Philip Johnson
+ * @version $Id: ClearStack.java,v 1.5 2004/10/27 02:41:57 johnson Exp $
+ */
+public class ClearStack extends Stack implements Iterable<Object> {
+
+ /**
+ * Returns an iterator over this Stack.
+ * @return An Iterator over this stack.
+ */
+ public Iterator<Object> iterator() {
+ return this.stackElements.iterator();
+ }
+
+ /**
+ * Gets the top attribute of the ClearStack object.
+ *
+ * @return The top value.
+ * @exception EmptyStackException If the stack is empty.
+ */
+ public Object getTop() throws EmptyStackException {
+ return this.top();
+ }
+
+ /**
+ * Returns true if this stack is empty.
+ * @return True if empty.
+ */
+ public boolean isEmpty() {
+ return this.stackElements.size() == 0;
+ }
+}

Added: trunk/src/edu/hawaii/stack/EmptyStackException.java
==============================================================================
--- (empty file)
+++ trunk/src/edu/hawaii/stack/EmptyStackException.java Mon Oct 6 01:50:22
2008
@@ -0,0 +1,22 @@
+package edu.hawaii.stack;
+
+/**
+ * EmptyStackException is thrown when an attempt is made to pop an empty
stack.
+ *
+ * @author Philip M. Johnson
+ */
+public class EmptyStackException extends Exception {
+
+ /**
+ * The serial version UID, autogenerated by Eclipse.
+ */
+ private static final long serialVersionUID = 7236818507144587981L;
+
+ /**
+ * Indicates an illegal operation on an empty stack was performed.
+ * @param e The exception thrown.
+ */
+ public EmptyStackException(Exception e) {
+ super(e.getMessage());
+ }
+}

Added: trunk/src/edu/hawaii/stack/Stack.java
==============================================================================
--- (empty file)
+++ trunk/src/edu/hawaii/stack/Stack.java Mon Oct 6 01:50:22 2008
@@ -0,0 +1,79 @@
+package edu.hawaii.stack;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Implements the stack abstract data type. Since Java provides a Stack
ADT, this class is provided
+ * to help understand Ant build processes and automated QA.
+ *
+ * @author Philip M. Johnson; repaired by Daniel Arakaki
+ */
+public class Stack {
+
+ /**
+ * Holds the elements.
+ * Visibility is protected to allow subclass access to internal
representation.
+ */
+ protected List<Object> stackElements = new ArrayList<Object>();
+
+ /**
+ * Pushes obj onto the stack.
+ *
+ * @param stackObjects Any object.
+ */
+ public void push(Object stackObjects) {
+ this.stackElements.add(stackObjects);
+ }
+
+ /**
+ * Returns the last object pushed onto the stack and removes
+ * that object from the stack.
+ *
+ * @return The last object pushed.
+ * @throws EmptyStackException If the stack was empty at the time of the
pop.
+ */
+ public Object pop() throws EmptyStackException {
+ try {
+ Object aObject = this.stackElements.get(stackElements.size() - 1);
+ stackElements.remove(stackElements.size() - 1);
+ return aObject;
+ }
+ catch (Exception e) {
+ throw new EmptyStackException(e);
+ }
+ }
+
+ /**
+ * Returns the last object pushed onto the stack without removing it.
+ *
+ * @return Return the last object pushed into the stack.
+ * @throws EmptyStackException If the stack was empty at the time of the
top.
+ */
+ public Object top() throws EmptyStackException {
+ try {
+ return this.stackElements.get(this.stackElements.size() - 1);
+ }
+ catch (Exception e) {
+ throw new EmptyStackException(e);
+ }
+ }
+
+ /**
+ * Returns a shallow copy of the current stack as an array.
+ *
+ * @return An array containing the elements of the stack.
+ */
+ public Object[] toArray() {
+ return this.stackElements.toArray();
+ }
+
+ /**
+ * Provides a readable representation of the stack.
+ *
+ * @return The stack as a string.
+ */
+ public String toString() {
+ return "[Stack " + this.stackElements + "]";
+ }
+}

Added: trunk/src/edu/hawaii/stack/TestClearStack.java
==============================================================================
--- (empty file)
+++ trunk/src/edu/hawaii/stack/TestClearStack.java Mon Oct 6 01:50:22 2008
@@ -0,0 +1,68 @@
+package edu.hawaii.stack;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import java.util.ArrayList;
+
+/**
+ * Test case for the ClearStack abstract data type, which checks to see
that a ClearStack
+ * containing three elements can be iterated through and that this
iteration retrieves the
+ * elements in the correct order.
+ *
+ * @author Philip Johnson
+ */
+public class TestClearStack {
+
+ // Objects used for testing.
+ private final Integer one = Integer.valueOf(1);
+ private final Integer two = Integer.valueOf(2);
+ private final Integer three = Integer.valueOf(3);
+
+ /**
+ * Test the ClearStack iterator.
+ */
+ @Test
+ public void testNormalOperation() {
+ ClearStack stack = new ClearStack();
+ stack.push(one);
+ stack.push(two);
+ stack.push(three);
+ ArrayList<Object> list = new ArrayList<Object>();
+ // Go through the elements in the stack and save them in an ArrayList.
+ for (Object element : stack) {
+ list.add(element);
+ }
+ // Now check to see they were there and in the right order.
+ assertSame("Testing ClearStack[0]", one, list.get(0));
+ assertSame("Testing ClearStack[1]", two, list.get(1));
+ assertSame("Testing ClearStack[2]", three, list.get(2));
+ }
+
+ /**
+ * Testing the getTop method in ClearStack.
+ *
+ * @throws EmptyStackException Throws an EmptyStackException when
accessing an empty stack.
+ */
+ @Test
+ public void testGetTop() throws EmptyStackException {
+ ClearStack aStack = new ClearStack();
+ //try insert a try-catch...
+ aStack.push(this.one);
+ assertSame("Testing ClearStack[0]", one, aStack.getTop());
+ }
+
+ /**
+ * Testing isEmpty method from ClearStack.
+ */
+ @Test
+ public void testIsEmpty() {
+ ClearStack bStack = new ClearStack();
+ assertTrue("Testing ClearStack isEmpty", bStack.isEmpty());
+ bStack.push(this.one);
+ assertFalse("Testing ClearStack isEmpty", bStack.isEmpty());
+ }
+
+}

Added: trunk/src/edu/hawaii/stack/TestStack.java
==============================================================================
--- (empty file)
+++ trunk/src/edu/hawaii/stack/TestStack.java Mon Oct 6 01:50:22 2008
@@ -0,0 +1,77 @@
+package edu.hawaii.stack;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
+/**
+ * Test case for the Stack abstract data type, which checks to see that
you can push three
+ * objects onto the stack, then pop them off and get the exact same
objects in the correct order.
+ * This test also checks to see that popping an empty stack generates an
exception.
+ *
+ * @author Philip Johnson
+ * @version $Id: TestStack.java,v 1.6 2004/10/27 22:58:03 johnson Exp $
+ */
+public class TestStack {
+
+ // Objects used for testing.
+ private final Integer one = Integer.valueOf(1);
+ private final Integer two = Integer.valueOf(2);
+ private final Integer three = Integer.valueOf(3);
+
+
+ /**
+ * Test normal stack push.
+ *
+ * @exception EmptyStackException If errors during stack processing.
+ */
+ @Test
+ public void testNormalOperation() throws EmptyStackException {
+ Stack stack = new Stack();
+ stack.push(one);
+ stack.push(two);
+ stack.push(three);
+ assertEquals("Testing stack asArray", 3, stack.toArray().length);
+ assertSame("Testing stack top of three", three, stack.top());
+ assertSame("Testing stack pop of three", three, stack.pop());
+ assertSame("Testing stack pop of two", two, stack.pop());
+ assertSame("Testing stack pop of one", one, stack.pop());
+ }
+
+ /**
+ * Test stack toString on empty stack.
+ */
+ @Test
+ public void testEmptyStackToString() {
+ Stack stack = new Stack();
+ assertEquals("toString should return nothing in stack.", "[Stack []]",
stack.toString());
+ }
+
+ /**
+ * Test illegal pop of empty stack.
+ *
+ * @throws EmptyStackException Throw an EmptyStackException when trying
to pop an empty stack.
+ */
+ @Test(expected = EmptyStackException.class)
+ public void testIllegalPop() throws EmptyStackException {
+ Stack stack = new Stack();
+
+ stack.pop();
+ fail("Pop of empty stack did not generate exception.");
+ }
+
+ /**
+ * Test illegal top of empty stack.
+ *
+ * @throws EmptyStackException Throw an EmptyStackExecption when topping
an empty stack.
+ */
+ @Test(expected = EmptyStackException.class)
+ public void testTopEmptyStack() throws EmptyStackException {
+ Stack aStack = new Stack();
+
+ aStack.top();
+ fail("Top of empty stack did not generate exception");
+ }
+}

Added: trunk/src/edu/hawaii/stack/overview.html
==============================================================================
--- (empty file)
+++ trunk/src/edu/hawaii/stack/overview.html Mon Oct 6 01:50:22 2008
@@ -0,0 +1,6 @@
+ <body>
+Stack is a system designed to illustrate Ant-based build and packaging
techniques.
+<p>
+It is also used as a component in the StackMVC system.
+ </body>
+

Added: trunk/src/edu/hawaii/stack/package.html
==============================================================================
--- (empty file)
+++ trunk/src/edu/hawaii/stack/package.html Mon Oct 6 01:50:22 2008
@@ -0,0 +1,7 @@
+ <body>
+
+Provides a "classical" Stack abstract data type and a ClearStack ADT which
+is a Stack with an iterator.
+
+ </body>
+

Reply all
Reply to author
Forward
0 new messages