[Refactor]ed SimpleStack class to JDK 'ArrayDeque' class

This commit is contained in:
Daniel_M_Williams 2015-12-03 09:45:10 -05:00
parent defcf24c86
commit 890d390b4b
6 changed files with 17 additions and 87 deletions

View File

@ -1,10 +1,11 @@
package net.sf.openrocket.file.simplesax;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import net.sf.openrocket.aerodynamics.Warning;
import net.sf.openrocket.aerodynamics.WarningSet;
import net.sf.openrocket.util.SimpleStack;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
@ -17,9 +18,9 @@ import org.xml.sax.helpers.DefaultHandler;
class DelegatorHandler extends DefaultHandler {
private final WarningSet warnings;
private final SimpleStack<ElementHandler> handlerStack = new SimpleStack<ElementHandler>();
private final SimpleStack<StringBuilder> elementData = new SimpleStack<StringBuilder>();
private final SimpleStack<HashMap<String, String>> elementAttributes = new SimpleStack<HashMap<String, String>>();
private final Deque<ElementHandler> handlerStack = new ArrayDeque<ElementHandler>();
private final Deque<StringBuilder> elementData = new ArrayDeque<StringBuilder>();
private final Deque<HashMap<String, String>> elementAttributes = new ArrayDeque<HashMap<String, String>>();
// Ignore all elements as long as ignore > 0

View File

@ -189,6 +189,10 @@ public class Rocket extends RocketComponent {
return this.stageMap.values();
}
public AxialStage getTopmostStage(){
return (AxialStage) getChild(0);
}
private int getNewStageNumber() {
int guess = 0;
while (stageMap.containsKey(guess)) {

View File

@ -4,6 +4,7 @@ import java.util.Collection;
import java.util.EventObject;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import java.util.NoSuchElementException;
import org.slf4j.Logger;
@ -23,7 +24,6 @@ import net.sf.openrocket.util.Invalidator;
import net.sf.openrocket.util.LineStyle;
import net.sf.openrocket.util.MathUtil;
import net.sf.openrocket.util.SafetyMutex;
import net.sf.openrocket.util.SimpleStack;
import net.sf.openrocket.util.StateChangeListener;
import net.sf.openrocket.util.UniqueID;
@ -2029,7 +2029,7 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
*/
private static class RocketComponentIterator implements Iterator<RocketComponent> {
// Stack holds iterators which still have some components left.
private final SimpleStack<Iterator<RocketComponent>> iteratorStack = new SimpleStack<Iterator<RocketComponent>>();
private final Stack<Iterator<RocketComponent>> iteratorStack = new Stack<Iterator<RocketComponent>>();
private final Rocket root;
private final int treeModID;

View File

@ -1,6 +1,8 @@
package net.sf.openrocket.simulation;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -28,7 +30,6 @@ import net.sf.openrocket.startup.Application;
import net.sf.openrocket.util.Coordinate;
import net.sf.openrocket.util.MathUtil;
import net.sf.openrocket.util.Pair;
import net.sf.openrocket.util.SimpleStack;
public class BasicEventSimulationEngine implements SimulationEngine {
@ -55,7 +56,10 @@ public class BasicEventSimulationEngine implements SimulationEngine {
private FlightConfigurationID fcid;
private SimpleStack<SimulationStatus> stages = new SimpleStack<SimulationStatus>();
// was a stack, but parallel staging breaks that
protected Stack<SimulationStatus> stages = new Stack<SimulationStatus>();
// protected ArrayList<SimulationStatus> burningStages = new ArrayList<SimulationStatus>();
// protected ArrayList<SimulationStatus> carriedStages = new ArrayList<SimulationStatus>();
@Override

View File

@ -1,36 +0,0 @@
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;
}
public String toString() {
StringBuilder sb = new StringBuilder("SimpleStack count=" + size() + "\n");
for( T element: this ) {
sb.append(" ").append(element.toString());
}
return sb.toString();
}
}

View File

@ -1,43 +0,0 @@
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() );
}
}