]> git.gag.com Git - debian/openrocket/commitdiff
Added a simple stack implementation based on ArrayList since java.util.ArrayDeque...
authorkruland2607 <kruland2607@180e2498-e6e9-4542-8430-84ac67f01cd8>
Sat, 24 Mar 2012 01:56:16 +0000 (01:56 +0000)
committerkruland2607 <kruland2607@180e2498-e6e9-4542-8430-84ac67f01cd8>
Sat, 24 Mar 2012 01:56:16 +0000 (01:56 +0000)
git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/branches/froyo_12.03@484 180e2498-e6e9-4542-8430-84ac67f01cd8

core/src/net/sf/openrocket/util/SimpleStack.java [new file with mode: 0644]
core/test/net/sf/openrocket/util/SimpleStackTest.java [new file with mode: 0644]

diff --git a/core/src/net/sf/openrocket/util/SimpleStack.java b/core/src/net/sf/openrocket/util/SimpleStack.java
new file mode 100644 (file)
index 0000000..6bdd967
--- /dev/null
@@ -0,0 +1,29 @@
+package net.sf.openrocket.util;
+
+import java.util.NoSuchElementException;
+/**
+ * SimpleStack implementation backed by an ArrayList.
+ * 
+ */
+public class SimpleStack<T> extends ArrayList<T> {
+
+       public void push( T value ) {
+               this.add(value);
+       }
+       
+       public T peek() {
+               if ( size() <= 0 ) {
+                       return null;
+               }
+               return this.get( size() -1 );
+       }
+       
+       public T pop() {
+               if ( size() <= 0 ) {
+                       throw new NoSuchElementException();
+               }
+               T value = this.remove( size() -1 );
+               return value;
+       }
+       
+}
diff --git a/core/test/net/sf/openrocket/util/SimpleStackTest.java b/core/test/net/sf/openrocket/util/SimpleStackTest.java
new file mode 100644 (file)
index 0000000..16cd312
--- /dev/null
@@ -0,0 +1,43 @@
+package net.sf.openrocket.util;
+
+import java.util.NoSuchElementException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import org.junit.Test;
+
+public class SimpleStackTest {
+
+       @Test(expected=NoSuchElementException.class)
+       public void testEmptyStack() {
+               SimpleStack<Integer> s = new SimpleStack<Integer>();
+               
+               assertNull(s.peek());
+               
+               s.pop();
+       }
+       
+       @Test
+       public void testPushAndPop() {
+               
+               SimpleStack<Integer> s = new SimpleStack<Integer>();
+               
+               for( int i = 0; i< 10; i++ ) {
+                       s.push(i);
+                       assertEquals(i+1, s.size());
+               }
+       
+               for( int i=9; i>= 0; i-- ) {
+                       assertEquals( i, s.peek().intValue() );
+                       Integer val = s.pop();
+                       assertEquals( i, val.intValue() );
+                       assertEquals( i, s.size() );
+               }
+               
+               assertNull( s.peek() );
+               assertEquals( 0, s.size() );
+               
+       }
+       
+}