[Major] Re-Implemented ..ParameterSet to represent the default more simply

- the default value is now simply an entry in the map
  - size() returns only the number of overrides
  - getIds() returns only the ids of overrides
- added ParemeterSetTest to test FlightConfigurableParameterSet
- removed (already defanged) event handling functions from MotorConfig
- removed (already defanged) event handling functions from DeploymentConfig
- removed (already defanged) event handling functions from IgnitionConfig
- FlightConfigurableParameter no longer extends ChangeSet
- fully parameterized DoubleModel to <T extends Enum<T> >
- fully parameterized EnumModel to <T extends Enum<T> >
This commit is contained in:
Daniel_M_Williams 2015-12-23 00:08:50 -05:00
parent 612f64fa4c
commit f59ebdd06e
17 changed files with 307 additions and 241 deletions

View File

@ -1,17 +1,12 @@
package net.sf.openrocket.motor;
import java.util.EventObject;
import java.util.List;
import net.sf.openrocket.rocketcomponent.FlightConfigurableParameter;
import net.sf.openrocket.rocketcomponent.IgnitionEvent;
import net.sf.openrocket.rocketcomponent.MotorMount;
import net.sf.openrocket.rocketcomponent.RocketComponent;
import net.sf.openrocket.simulation.MotorState;
import net.sf.openrocket.util.ArrayList;
import net.sf.openrocket.util.Coordinate;
import net.sf.openrocket.util.Inertia;
import net.sf.openrocket.util.StateChangeListener;
/**
* A single motor configuration. This includes the selected motor
@ -83,7 +78,6 @@ public class MotorConfiguration implements FlightConfigurableParameter<MotorConf
public void setMotor(Motor motor){
this.motor = motor;
fireChangeEvent();
}
public Motor getMotor() {
@ -104,7 +98,6 @@ public class MotorConfiguration implements FlightConfigurableParameter<MotorConf
public void setEjectionDelay(double delay) {
this.ejectionDelay = delay;
fireChangeEvent();
}
public Coordinate getPosition() {
@ -114,7 +107,6 @@ public class MotorConfiguration implements FlightConfigurableParameter<MotorConf
public void setPosition(Coordinate _position) {
this.position = _position;
modID++;
fireChangeEvent();
}
public double getIgnitionTime() {
@ -129,7 +121,6 @@ public class MotorConfiguration implements FlightConfigurableParameter<MotorConf
this.ignitionTime = _time;
this.ignitionOveride = true;
modID++;
fireChangeEvent();
}
public double getIgnitionDelay() {
@ -139,7 +130,6 @@ public class MotorConfiguration implements FlightConfigurableParameter<MotorConf
public void setIgnitionDelay(final double _delay) {
this.ignitionDelay = _delay;
this.ignitionOveride = true;
fireChangeEvent();
}
public IgnitionEvent getIgnitionEvent() {
@ -149,7 +139,6 @@ public class MotorConfiguration implements FlightConfigurableParameter<MotorConf
public void setIgnitionEvent(final IgnitionEvent _event) {
this.ignitionEvent = _event;
this.ignitionOveride = true;
fireChangeEvent();
}
public Coordinate getOffset( ){
@ -228,17 +217,6 @@ public class MotorConfiguration implements FlightConfigurableParameter<MotorConf
return clone;
}
@Override
public void addChangeListener(StateChangeListener listener) {
}
@Override
public void removeChangeListener(StateChangeListener listener) {
}
protected void fireChangeEvent() {
}
public int getModID() {
return modID;
}

View File

@ -13,18 +13,18 @@ public class MotorConfigurationSet extends FlightConfigurableParameterSet<MotorC
public static final int DEFAULT_MOTOR_EVENT_TYPE = ComponentChangeEvent.MOTOR_CHANGE | ComponentChangeEvent.EVENT_CHANGE;
public MotorConfigurationSet(RocketComponent component ) {
super(component, DEFAULT_MOTOR_EVENT_TYPE, new MotorConfiguration());
super( new MotorConfiguration());
}
/**
* Construct a copy of an existing FlightConfigurationSet.
*
* @param flightConfiguration another flightConfiguration to copy data from.
* @param configSet another flightConfiguration to copy data from.
* @param component the rocket component on which events are fired when the parameter values are changed
* @param eventType the event type that will be fired on changes
*/
public MotorConfigurationSet(FlightConfigurableParameterSet<MotorConfiguration> flightConfiguration, RocketComponent component) {
super(flightConfiguration, component, DEFAULT_MOTOR_EVENT_TYPE);
public MotorConfigurationSet(FlightConfigurableParameterSet<MotorConfiguration> configSet, RocketComponent component) {
super(configSet);
}
@Override
@ -35,8 +35,7 @@ public class MotorConfigurationSet extends FlightConfigurableParameterSet<MotorC
@Override
public String toDebug(){
StringBuilder buffer = new StringBuilder();
buffer.append("====== Dumping MotorConfigurationSet for mount '"+this.component.toDebugName()+" ======\n");
buffer.append(" >> motorSet ("+this.size()+ " motors)\n");
buffer.append("====== Dumping MotorConfigurationSet for mount ("+this.size()+ " motors)\n");
MotorConfiguration emptyInstance = this.getDefault();
buffer.append(" >> (["+emptyInstance.toString()+"]= @ "+ emptyInstance.getIgnitionEvent().name +" +"+emptyInstance.getIgnitionDelay()+"sec )\n");

View File

@ -17,8 +17,7 @@ public class AxialStage extends ComponentAssembly implements FlightConfigurableC
protected int stageNumber;
public AxialStage(){
this.separations = new FlightConfigurableParameterSet<StageSeparationConfiguration>(
this, ComponentChangeEvent.EVENT_CHANGE, new StageSeparationConfiguration());
this.separations = new FlightConfigurableParameterSet<StageSeparationConfiguration>( new StageSeparationConfiguration());
this.relativePosition = Position.AFTER;
this.stageNumber = 0;
}
@ -80,8 +79,7 @@ public class AxialStage extends ComponentAssembly implements FlightConfigurableC
@Override
protected RocketComponent copyWithOriginalID() {
AxialStage copy = (AxialStage) super.copyWithOriginalID();
copy.separations = new FlightConfigurableParameterSet<StageSeparationConfiguration>(separations,
copy, ComponentChangeEvent.EVENT_CHANGE);
copy.separations = new FlightConfigurableParameterSet<StageSeparationConfiguration>(separations);
return copy;
}

View File

@ -1,16 +1,12 @@
package net.sf.openrocket.rocketcomponent;
import java.util.EventObject;
import java.util.List;
import net.sf.openrocket.l10n.Translator;
import net.sf.openrocket.simulation.FlightEvent;
import net.sf.openrocket.startup.Application;
import net.sf.openrocket.unit.UnitGroup;
import net.sf.openrocket.util.ArrayList;
import net.sf.openrocket.util.MathUtil;
import net.sf.openrocket.util.Pair;
import net.sf.openrocket.util.StateChangeListener;
public class DeploymentConfiguration implements FlightConfigurableParameter<DeploymentConfiguration> {
@ -106,7 +102,6 @@ public class DeploymentConfiguration implements FlightConfigurableParameter<Depl
throw new NullPointerException("deployEvent is null");
}
this.deployEvent = deployEvent;
fireChangeEvent();
}
public double getDeployAltitude() {
@ -118,7 +113,6 @@ public class DeploymentConfiguration implements FlightConfigurableParameter<Depl
return;
}
this.deployAltitude = deployAltitude;
fireChangeEvent();
}
public double getDeployDelay() {
@ -130,7 +124,6 @@ public class DeploymentConfiguration implements FlightConfigurableParameter<Depl
return;
}
this.deployDelay = deployDelay;
fireChangeEvent();
}
@Override
@ -146,25 +139,6 @@ public class DeploymentConfiguration implements FlightConfigurableParameter<Depl
}
@Override
public void addChangeListener(StateChangeListener listener) {
}
@Override
public void removeChangeListener(StateChangeListener listener) {
}
private void fireChangeEvent() {
}
@Override
public DeploymentConfiguration clone() {
DeploymentConfiguration that = new DeploymentConfiguration();

View File

@ -8,7 +8,7 @@ import net.sf.openrocket.util.ChangeSource;
*
* @param <E> the parameter type
*/
public interface FlightConfigurableParameter<E> extends ChangeSource {
public interface FlightConfigurableParameter<E> {
/**
* Return a copy of this object. The listeners must not be copied

View File

@ -1,14 +1,12 @@
package net.sf.openrocket.rocketcomponent;
import java.util.Collections;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import net.sf.openrocket.util.ArrayList;
import net.sf.openrocket.util.StateChangeListener;
import net.sf.openrocket.util.Utils;
/**
@ -24,13 +22,7 @@ public class FlightConfigurableParameterSet<E extends FlightConfigurableParamete
//private static final Logger log = LoggerFactory.getLogger(ParameterSet.class);
protected final HashMap<FlightConfigurationId, E> map = new HashMap<FlightConfigurationId, E>();
protected E defaultValue;
protected final RocketComponent component;
protected final int eventType;
private final Listener listener = new Listener();
protected static final FlightConfigurationId defaultValueId = FlightConfigurationId.DEFAULT_VALUE_FCID;
/**
* Construct a FlightConfiguration that has no overrides.
@ -38,33 +30,25 @@ public class FlightConfigurableParameterSet<E extends FlightConfigurableParamete
* @param component the rocket component on which events are fired when the parameter values are changed
* @param eventType the event type that will be fired on changes
*/
public FlightConfigurableParameterSet(RocketComponent component, int eventType, E _defaultValue) {
this.component = component;
this.eventType = eventType;
this.defaultValue= _defaultValue;
addListener(_defaultValue);
public FlightConfigurableParameterSet(E _defaultValue) {
this.map.put( defaultValueId, _defaultValue);
}
/**
* Construct a copy of an existing FlightConfigurationImpl.
*
* @param component the rocket component on which events are fired when the parameter values are changed
* @param eventType the event type that will be fired on changes
*/
public FlightConfigurableParameterSet(FlightConfigurableParameterSet<E> configSet, RocketComponent component, int eventType) {
this.component = component;
this.eventType = eventType;
this.defaultValue= configSet.getDefault().clone();
public FlightConfigurableParameterSet(FlightConfigurableParameterSet<E> configSet ){
for (FlightConfigurationId key : configSet.map.keySet()) {
E cloneConfig = configSet.map.get(key).clone();
this.map.put(key, cloneConfig);
}
}
@Deprecated
// do we want to keep this? it shouldn't actually be called...
public boolean containsKey( final FlightConfigurationId fcid ){
return this.map.containsKey(fcid);
}
@ -77,7 +61,7 @@ public class FlightConfigurableParameterSet<E extends FlightConfigurableParamete
* @return the default parameter value (never null)
*/
public E getDefault(){
return this.defaultValue;
return this.map.get( defaultValueId);
}
/**
@ -94,7 +78,7 @@ public class FlightConfigurableParameterSet<E extends FlightConfigurableParamete
if( this.isDefault(nextDefaultValue)){
return;
}
this.defaultValue = nextDefaultValue;
this.map.put( defaultValueId, nextDefaultValue);
}
@Override
@ -102,13 +86,12 @@ public class FlightConfigurableParameterSet<E extends FlightConfigurableParamete
return map.values().iterator();
}
/**
* Return the number of specific flight configurations other than the default.
* @return
*/
public int size() {
return map.size();
return (map.size()-1);
}
/**
@ -119,7 +102,7 @@ public class FlightConfigurableParameterSet<E extends FlightConfigurableParamete
* @param value the parameter to find
* @return the flight configuration ID
*/
public FlightConfigurationId get(E testValue) {
public FlightConfigurationId getId(E testValue) {
if( null == testValue ){
return null;
}
@ -171,13 +154,13 @@ public class FlightConfigurableParameterSet<E extends FlightConfigurableParamete
}
/**
*
* @return a sorted list of all the contained FlightConfigurationIDs
*/
public List<FlightConfigurationId> getSortedConfigurationIDs(){
ArrayList<FlightConfigurationId> toReturn = new ArrayList<FlightConfigurationId>();
toReturn.addAll( this.map.keySet() );
toReturn.remove( defaultValueId );
// Java 1.8:
//toReturn.sort( null );
@ -187,7 +170,7 @@ public class FlightConfigurableParameterSet<E extends FlightConfigurableParamete
return toReturn;
}
public List<FlightConfigurationId> getIDs(){
public List<FlightConfigurationId> getIds(){
return this.getSortedConfigurationIDs();
}
@ -201,15 +184,12 @@ public class FlightConfigurableParameterSet<E extends FlightConfigurableParamete
public void set(FlightConfigurationId fcid, E nextValue) {
if ( nextValue == null) {
// null value means to delete this fcid
E previousValue = this.map.remove(fcid);
removeListener(previousValue);
this.map.remove(fcid);
}else{
E previousValue = this.map.put(fcid, nextValue);
removeListener(previousValue);
addListener(nextValue);
this.map.put(fcid, nextValue);
}
fireEvent();
update();
}
@ -241,7 +221,6 @@ public class FlightConfigurableParameterSet<E extends FlightConfigurableParamete
/*
* Clears all configuration-specific settings -- meaning querying the parameter for any configuration will return the default value.
*
*/
public void reset() {
E tempValue = this.getDefault();
@ -249,60 +228,32 @@ public class FlightConfigurableParameterSet<E extends FlightConfigurableParamete
setDefault(tempValue);
}
private void fireEvent() {
component.fireComponentChangeEvent(eventType);
}
public FlightConfigurationId cloneFlightConfiguration(FlightConfigurationId oldConfigId, FlightConfigurationId newConfigId) {
// clones the ENTRIES for the given fcid's.
// clones the ENTRIES for the given fcid's.
E oldValue = this.get(oldConfigId);
this.set(newConfigId, oldValue.clone());
fireEvent();
update();
return newConfigId;
}
private void addListener(E value) {
if (value != null) {
value.addChangeListener(listener);
}
}
private void removeListener(E value) {
if (value != null) {
value.removeChangeListener(listener);
}
}
private class Listener implements StateChangeListener {
@Override
public void stateChanged(EventObject e) {
fireEvent();
}
}
public String toDebug(){
StringBuilder buf = new StringBuilder();
buf.append(String.format("====== Dumping ConfigurationSet for: '%s' of type: %s ======\n", this.component.getName(), this.component.getClass().getSimpleName() ));
buf.append(String.format(" >> ParameterSet<%s> (%d configurations)\n", this.defaultValue.getClass().getSimpleName(), this.size() ));
buf.append(String.format(" >> [%s]= %s\n", "DEFAULT", this.getDefault().toString() ));
buf.append(String.format("====== Dumping ConfigurationSet<%s> (%d configurations)\n", this.getDefault().getClass().getSimpleName(), this.size() ));
final String fmt = " [%-12s]: %s\n";
for( FlightConfigurationId loopFCID : this.getSortedConfigurationIDs()){
String shortKey = loopFCID.toShortKey();
E inst = this.map.get(loopFCID);
if( this.isDefault(inst)){
shortKey = "*"+shortKey+"*";
}
buf.append(String.format(" >> [%s]= %s\n", shortKey, inst ));
buf.append(String.format(fmt, shortKey, inst ));
}
return buf.toString();
}
public void update(){
this.defaultValue.update();
for( E curValue: this.map.values() ){
curValue.update();
}

View File

@ -1,26 +1,17 @@
package net.sf.openrocket.rocketcomponent;
import java.util.EventObject;
import java.util.List;
import net.sf.openrocket.util.ArrayList;
import net.sf.openrocket.util.StateChangeListener;
public class IgnitionConfiguration implements FlightConfigurableParameter<IgnitionConfiguration> {
protected double ignitionDelay = 0.0;
protected IgnitionEvent ignitionEvent = IgnitionEvent.NEVER;
protected double ignitionTime = 0.0;
private final List<StateChangeListener> listeners = new ArrayList<StateChangeListener>();
public double getIgnitionDelay() {
return ignitionDelay;
}
public void setIgnitionDelay(double ignitionDelay) {
this.ignitionDelay = ignitionDelay;
fireChangeEvent();
}
public IgnitionEvent getIgnitionEvent() {
@ -29,7 +20,6 @@ public class IgnitionConfiguration implements FlightConfigurableParameter<Igniti
public void setIgnitionEvent(IgnitionEvent ignitionEvent) {
this.ignitionEvent = ignitionEvent;
fireChangeEvent();
}
public double getIgnitionTime() {
@ -38,7 +28,6 @@ public class IgnitionConfiguration implements FlightConfigurableParameter<Igniti
public void setIgnitionTime(double ignitionTime) {
this.ignitionTime = ignitionTime;
fireChangeEvent();
}
@Override
@ -50,24 +39,6 @@ public class IgnitionConfiguration implements FlightConfigurableParameter<Igniti
return clone;
}
@Override
public void addChangeListener(StateChangeListener listener) {
listeners.add(listener);
}
@Override
public void removeChangeListener(StateChangeListener listener) {
listeners.remove(listener);
}
private void fireChangeEvent() {
EventObject event = new EventObject(this);
Object[] list = listeners.toArray();
for (Object l : list) {
((StateChangeListener) l).stateChanged(event);
}
}
@Override
public void update(){

View File

@ -28,7 +28,7 @@ public abstract class RecoveryDevice extends MassObject implements FlightConfigu
private FlightConfigurableParameterSet<DeploymentConfiguration> deploymentConfigurations;
public RecoveryDevice() {
this.deploymentConfigurations = new FlightConfigurableParameterSet<DeploymentConfiguration>(this, ComponentChangeEvent.EVENT_CHANGE, new DeploymentConfiguration());
this.deploymentConfigurations = new FlightConfigurableParameterSet<DeploymentConfiguration>( new DeploymentConfiguration());
setMaterial(Application.getPreferences().getDefaultComponentMaterial(RecoveryDevice.class, Material.Type.SURFACE));
}
@ -113,8 +113,7 @@ public abstract class RecoveryDevice extends MassObject implements FlightConfigu
@Override
protected RocketComponent copyWithOriginalID() {
RecoveryDevice copy = (RecoveryDevice) super.copyWithOriginalID();
copy.deploymentConfigurations = new FlightConfigurableParameterSet<DeploymentConfiguration>(deploymentConfigurations,
copy, ComponentChangeEvent.EVENT_CHANGE);
copy.deploymentConfigurations = new FlightConfigurableParameterSet<DeploymentConfiguration>(deploymentConfigurations);
return copy;
}
}

View File

@ -85,7 +85,7 @@ public class Rocket extends RocketComponent {
functionalModID = modID;
FlightConfiguration defaultConfiguration = new FlightConfiguration( this, null);
this.configSet = new FlightConfigurableParameterSet<FlightConfiguration>(this, ComponentChangeEvent.CONFIG_CHANGE, defaultConfiguration);
this.configSet = new FlightConfigurableParameterSet<FlightConfiguration>( defaultConfiguration);
}
public String getDesigner() {
@ -301,8 +301,7 @@ public class Rocket extends RocketComponent {
@Override
public Rocket copyWithOriginalID() {
Rocket copy = (Rocket) super.copyWithOriginalID();
copy.configSet = new FlightConfigurableParameterSet<FlightConfiguration>(
this.configSet, copy, ComponentChangeEvent.CONFIG_CHANGE);
copy.configSet = new FlightConfigurableParameterSet<FlightConfiguration>( this.configSet);
copy.resetListeners();
return copy;
@ -339,7 +338,7 @@ public class Rocket extends RocketComponent {
this.refType = r.refType;
this.customReferenceLength = r.customReferenceLength;
this.configSet = new FlightConfigurableParameterSet<FlightConfiguration>( r.configSet, this, ComponentChangeEvent.CONFIG_CHANGE);
this.configSet = new FlightConfigurableParameterSet<FlightConfiguration>( r.configSet );
this.perfectFinish = r.perfectFinish;
this.checkComponentStructure();
@ -440,7 +439,7 @@ public class Rocket extends RocketComponent {
}
// notify all configurations
this.configSet.update();
this.update();
// Notify all listeners
// Copy the list before iterating to prevent concurrent modification exceptions.
@ -650,10 +649,14 @@ public class Rocket extends RocketComponent {
* @return a FlightConfiguration instance
*/
public FlightConfiguration getFlightConfiguration(final int configIndex) {
checkState();
return this.configSet.get(configIndex);
}
public void setDefaultConfiguration(final FlightConfiguration config) {
checkState();
configSet.setDefault( config);
fireComponentChangeEvent(ComponentChangeEvent.NONFUNCTIONAL_CHANGE);
}
public void setDefaultConfiguration(final FlightConfigurationId fcid) {
checkState();

View File

@ -313,7 +313,7 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
protected void componentChanged(ComponentChangeEvent e) {
// No-op
checkState();
this.update();
update();
}
@ -1089,6 +1089,13 @@ public abstract class RocketComponent implements ChangeSource, Cloneable, Iterab
protected void update() {
this.setAxialOffset(this.relativePosition, this.offset);
}
public final void updateChildren(){
this.update();
for( RocketComponent rc : children ){
rc.updateChildren();
}
}
public Coordinate getOffset() {
return this.position;

View File

@ -146,14 +146,6 @@ public class StageSeparationConfiguration implements FlightConfigurableParameter
return clone;
}
@Override
public void addChangeListener(StateChangeListener listener) {
}
@Override
public void removeChangeListener(StateChangeListener listener) {
}
private void fireChangeEvent() {

View File

@ -5,43 +5,242 @@ package net.sf.openrocket.rocketcomponent;
//import static org.junit.Assert.assertThat;
//import static org.junit.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import net.sf.openrocket.util.MathUtil;
import net.sf.openrocket.util.StateChangeListener;
import net.sf.openrocket.util.ArrayList;
import net.sf.openrocket.util.BaseTestCase.BaseTestCase;
public class ParameterSetTest extends BaseTestCase {
private final static double EPSILON = MathUtil.EPSILON*1E3;
static int gid=0;
FlightConfigurableParameterSet<TestParameter> testSet = null;
private class Parameter implements FlightConfigurableParameter<Parameter> {
public Parameter(){}
private class TestParameter implements FlightConfigurableParameter<TestParameter> {
public final int id;
@Override
public Parameter clone(){ return null; }
public TestParameter(){
id = gid++;
}
@Override
public void update(){}
@Override
public void addChangeListener(StateChangeListener listener){}
public boolean equals( Object other ){
if( other instanceof TestParameter){
return (this.id == ((TestParameter)other).id);
}
return false;
}
@Override
public void removeChangeListener(StateChangeListener listener){}
public int hashCode(){
return this.id;
}
@Override
public String toString(){
return "tp#:"+id;
}
@Override
public TestParameter clone(){
return new TestParameter();
}
};
@Before
public void localSetUp() {
gid=0;
TestParameter defaultParam = new TestParameter();
testSet = new FlightConfigurableParameterSet<TestParameter>( defaultParam );
}
// ================ Actual Tests ================
@Test
public void testEmptySet() {
assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 ));
TestParameter dtp = new TestParameter();
testSet.setDefault( dtp);
assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 ));
assertThat("set stores default value correctly: ", testSet.getDefault(), equalTo( dtp ));
}
@Test
public void testEmptyRocket() {
//FlightConfigurableParameterSet<Parameter> testSet = new FlightConfigurableParameterSet<Parameter>();
public void testRetrieveDefault(){
FlightConfigurationId fcid2 = new FlightConfigurationId();
// i.e. requesting the value for a non-existent config id should return the default
assertThat("set stores id-value pair correctly : ", testSet.get(fcid2), equalTo( testSet.getDefault() ));
assertThat("set contains wrong number of overrides: ", testSet.size(), equalTo( 0 ));
FlightConfigurationId fcid_def = FlightConfigurationId.DEFAULT_VALUE_FCID;
assertThat("retrieving the via the special default key should produce the default value: ", testSet.get(fcid_def), equalTo( testSet.getDefault() ));
assertThat("set should still contain zero overrides: ", testSet.size(), equalTo( 0 ));
}
@Test
public void testSetGetSecond(){
assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 ));
TestParameter tp2 = new TestParameter();
FlightConfigurationId fcid2 = new FlightConfigurationId();
testSet.set(fcid2, tp2);
// fcid <=> tp2 should be stored....
assertThat("set stores default value correctly: ", testSet.get(fcid2), equalTo( tp2 ));
assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 1 ));
}
@Test(expected=IndexOutOfBoundsException.class)
public void testGetByNegativeIndex() {
assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 ));
//assertThat
testSet.get(-1);
}
@Test(expected=IndexOutOfBoundsException.class)
public void testGetByTooHighIndex() {
assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 ));
TestParameter tp2 = new TestParameter();
FlightConfigurationId fcid2 = new FlightConfigurationId();
testSet.set(fcid2, tp2);
assertThat("set should contain one override: ", testSet.size(), equalTo( 1 ));
//assertThat
testSet.get(1); // this should be off-by-one (too high)
}
@Test
public void testGetIdsLength(){
assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 ));
TestParameter tp2 = new TestParameter();
FlightConfigurationId fcid2 = new FlightConfigurationId();
testSet.set(fcid2, tp2);
TestParameter tp3 = new TestParameter();
FlightConfigurationId fcid3 = new FlightConfigurationId();
testSet.set(fcid3, tp3);
assertThat("set should contain two overrides: ", testSet.size(), equalTo( 2 ));
// testSet.getSortedConfigurationIDs()
// >> this function should ONLY return ids for the overrides
assertThat("getIds() broken!\n"+testSet.toDebug(), testSet.getIds().size(), equalTo( testSet.size()));
assertThat("getIds() broken!\n"+testSet.toDebug(), testSet.getSortedConfigurationIDs().size(), equalTo( testSet.getIds().size() ) );
}
@Test
public void testGetByIndex(){
assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 ));
TestParameter tp1 = new TestParameter();
FlightConfigurationId fcid1 = new FlightConfigurationId();
testSet.set(fcid1, tp1);
TestParameter tp2 = new TestParameter();
FlightConfigurationId fcid2 = new FlightConfigurationId();
testSet.set(fcid2, tp2);
TestParameter tp3 = new TestParameter();
FlightConfigurationId fcid3 = new FlightConfigurationId();
testSet.set(fcid3, tp3);
TestParameter tp4 = new TestParameter();
FlightConfigurationId fcid4 = new FlightConfigurationId();
testSet.set(fcid4, tp4);
assertThat("set should contain two overrides: ", testSet.size(), equalTo( 4 ));
ArrayList<FlightConfigurationId> refList = new ArrayList<FlightConfigurationId>();
refList.add(fcid1);
refList.add(fcid2);
refList.add(fcid3);
refList.add(fcid4);
Collections.sort(refList); // Java 1.7:
//assertThat
assertThat("retrieve-by-index broken!\n"+testSet.toDebug(), testSet.get(0), equalTo( testSet.get( refList.get(0))));
assertThat("retrieve-by-index broken!\n"+testSet.toDebug(), testSet.get(1), equalTo( testSet.get( refList.get(1))));
assertThat("retrieve-by-index broken!\n"+testSet.toDebug(), testSet.get(2), equalTo( testSet.get( refList.get(2))));
assertThat("retrieve-by-index broken!\n"+testSet.toDebug(), testSet.get(3), equalTo( testSet.get( refList.get(3))));
}
@Test
public void testRemoveSecond(){
assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 ));
TestParameter tp2 = new TestParameter();
FlightConfigurationId fcid2 = new FlightConfigurationId();
testSet.set(fcid2, tp2);
// fcid <=> tp2 should be stored....
assertThat("set stores default value correctly: ", testSet.get(fcid2), equalTo( tp2 ));
assertThat("set should contain one override: ", testSet.size(), equalTo( 1 ));
testSet.set(fcid2, null);
// fcid <=> tp2 should be stored....
assertThat("set stores default value correctly: ", testSet.get(fcid2), equalTo( testSet.getDefault() ));
assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 ));
}
@Test
public void testGetByValue(){
assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 ));
assertThat("retrieving the default value should produce the special default key: ",
testSet.getId(testSet.getDefault()), equalTo( FlightConfigurationId.DEFAULT_VALUE_FCID));
TestParameter tp2 = new TestParameter();
FlightConfigurationId fcid2 = new FlightConfigurationId();
testSet.set(fcid2, tp2);
// fcid <=> tp2 should be stored....
assertThat("set should contain one override: ", testSet.size(), equalTo( 1 ));
assertThat("set stores default value correctly: ", testSet.get(fcid2), equalTo( tp2 ));
// now retrieve that same parameter by value
FlightConfigurationId fcid3 = testSet.getId(tp2);
assertThat("set should contain one override: ", testSet.size(), equalTo( 1 ));
assertThat("set stores default value correctly: ", fcid2, equalTo( fcid3 ));
assertThat("set stores default value correctly: ", testSet.get( fcid3), equalTo( tp2 ));
}
@Test
public void testCloneSecond(){
assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 0 ));
TestParameter tp2 = new TestParameter();
FlightConfigurationId fcid2 = new FlightConfigurationId();
testSet.set(fcid2, tp2);
// fcid <=> tp2 should be stored....
assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 1 ));
assertThat("set stores default value correctly: ", testSet.get(fcid2), equalTo( tp2 ));
FlightConfigurationId fcid3 = new FlightConfigurationId();
testSet.cloneFlightConfiguration(fcid2, fcid3);
// fcid <=> tp2 should be stored....
assertThat("set should contain zero overrides: ", testSet.size(), equalTo( 2 ));
assertThat("set stores default value correctly: ", testSet.get(fcid3), not( testSet.getDefault() ));
}
}

View File

@ -96,7 +96,7 @@ public abstract class Column {
*
* @return
*/
public Comparator getComparator() {
public Comparator<?> getComparator() {
return null;
}

View File

@ -548,7 +548,7 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat
* The main model handles all values in SI units, i.e. no conversion is made within the model.
*/
private final ChangeSource source;
private final Object source;
private final String valueName;
private final double multiplier;
@ -643,7 +643,7 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat
* @param min Minimum value allowed (in SI units)
* @param max Maximum value allowed (in SI units)
*/
public DoubleModel(ChangeSource source, String valueName, double multiplier, UnitGroup unit,
public DoubleModel(Object source, String valueName, double multiplier, UnitGroup unit,
double min, double max) {
this.source = source;
this.valueName = valueName;
@ -689,43 +689,42 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat
}
public DoubleModel(ChangeSource source, String valueName, double multiplier, UnitGroup unit,
public DoubleModel(Object source, String valueName, double multiplier, UnitGroup unit,
double min) {
this(source, valueName, multiplier, unit, min, Double.POSITIVE_INFINITY);
}
public DoubleModel(ChangeSource source, String valueName, double multiplier, UnitGroup unit) {
public DoubleModel(Object source, String valueName, double multiplier, UnitGroup unit) {
this(source, valueName, multiplier, unit, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
}
public DoubleModel(ChangeSource source, String valueName, UnitGroup unit,
public DoubleModel(Object source, String valueName, UnitGroup unit,
double min, double max) {
this(source, valueName, 1.0, unit, min, max);
}
public DoubleModel(ChangeSource source, String valueName, UnitGroup unit, double min) {
public DoubleModel(Object source, String valueName, UnitGroup unit, double min) {
this(source, valueName, 1.0, unit, min, Double.POSITIVE_INFINITY);
}
public DoubleModel(ChangeSource source, String valueName, UnitGroup unit) {
public DoubleModel(Object source, String valueName, UnitGroup unit) {
this(source, valueName, 1.0, unit, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
}
public DoubleModel(ChangeSource source, String valueName) {
public DoubleModel(Object source, String valueName) {
this(source, valueName, 1.0, UnitGroup.UNITS_NONE,
Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
}
public DoubleModel(ChangeSource source, String valueName, double min) {
public DoubleModel(Object source, String valueName, double min) {
this(source, valueName, 1.0, UnitGroup.UNITS_NONE, min, Double.POSITIVE_INFINITY);
}
public DoubleModel(ChangeSource source, String valueName, double min, double max) {
public DoubleModel(Object source, String valueName, double min, double max) {
this(source, valueName, 1.0, UnitGroup.UNITS_NONE, min, max);
}
/**
* Returns the value of the variable (in SI units).
*/
@ -880,7 +879,6 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat
if (listeners.isEmpty()) {
if (source != null) {
source.addChangeListener(this);
lastValue = getValue();
lastAutomatic = isAutomatic();
}
@ -909,9 +907,6 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat
checkState(false);
listeners.remove(l);
if (listeners.isEmpty() && source != null) {
source.removeChangeListener(this);
}
log.trace(this + " removing listener (total " + listeners.size() + "): " + l);
}
@ -930,9 +925,6 @@ public class DoubleModel implements StateChangeListener, ChangeSource, Invalidat
log.warn("Invalidating " + this + " while still having listeners " + listeners);
}
listeners.clear();
if (source != null) {
source.removeChangeListener(this);
}
MemoryManagement.collectable(this);
}

View File

@ -7,38 +7,37 @@ import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.MutableComboBoxModel;
import net.sf.openrocket.util.ChangeSource;
import net.sf.openrocket.util.Reflection;
import net.sf.openrocket.util.StateChangeListener;
public class EnumModel<T extends Enum<T>> extends AbstractListModel
implements ComboBoxModel, MutableComboBoxModel, StateChangeListener {
private final ChangeSource source;
public class EnumModel<T extends Enum<T>> extends AbstractListModel<T>
implements ComboBoxModel<T>, MutableComboBoxModel<T>, StateChangeListener {
private static final long serialVersionUID = 7766446027840316797L;
private final Object source;
private final String valueName;
private final String nullText;
private final Enum<T>[] values;
private Enum<T> currentValue = null;
private final T[] values;
private T currentValue = null;
ArrayList<Enum<T>> displayedValues = new ArrayList<Enum<T>>();
ArrayList<T> displayedValues = new ArrayList<T>();
private final Reflection.Method getMethod;
private final Reflection.Method setMethod;
public EnumModel(ChangeSource source, String valueName) {
public EnumModel(Object source, String valueName) {
this(source,valueName,null,null);
}
public EnumModel(ChangeSource source, String valueName, Enum<T>[] values) {
public EnumModel(Object source, String valueName, T[] values) {
this(source, valueName, values, null);
}
@SuppressWarnings("unchecked")
public EnumModel(ChangeSource source, String valueName, Enum<T>[] values, String nullText) {
public EnumModel(Object source, String valueName, T[] values, String nullText) {
Class<? extends Enum<T>> enumClass;
this.source = source;
this.valueName = valueName;
@ -62,15 +61,14 @@ public class EnumModel<T extends Enum<T>> extends AbstractListModel
if (values != null)
this.values = values;
else
this.values = enumClass.getEnumConstants();
this.values = (T[]) enumClass.getEnumConstants();
for (Enum<T> e : this.values){
for (T e : this.values){
this.displayedValues.add( e );
}
this.nullText = nullText;
stateChanged(null); // Update current value
source.addChangeListener(this);
}
@ -82,7 +80,7 @@ public class EnumModel<T extends Enum<T>> extends AbstractListModel
return currentValue;
}
@SuppressWarnings("unchecked")
@Override
public void setSelectedItem(Object item) {
if (item == null) {
@ -102,19 +100,19 @@ public class EnumModel<T extends Enum<T>> extends AbstractListModel
// Comparison with == ok, since both are enums
if (currentValue == item)
return;
// @SuppressWarnings("unchecked")
this.currentValue = (Enum<T>) item;
this.currentValue = (T) item;
setMethod.invoke(source, item);
}
@Override
public Object getElementAt(int index) {
public T getElementAt(int index) {
if( ( index < 0 ) || ( index >= this.displayedValues.size())){
return nullText; // bad parameter
return null; // bad parameter
}
if (values[index] == null)
return nullText;
return null;
return displayedValues.get( index);
}
@ -126,7 +124,7 @@ public class EnumModel<T extends Enum<T>> extends AbstractListModel
@SuppressWarnings("unchecked")
@Override
public void stateChanged(EventObject e) {
Enum<T> value = (Enum<T>) getMethod.invoke(source);
T value = (T) getMethod.invoke(source);
if (value != currentValue) {
currentValue = value;
this.fireContentsChanged(this, 0, values.length);
@ -139,7 +137,7 @@ public class EnumModel<T extends Enum<T>> extends AbstractListModel
}
@Override
public void addElement(Object item) {
public void addElement(T item) {
// Not actually allowed. The model starts out with all the enums, and only allows hiding some.
}
@ -152,7 +150,7 @@ public class EnumModel<T extends Enum<T>> extends AbstractListModel
}
@Override
public void insertElementAt(Object item, int index) {
public void insertElementAt(T item, int index) {
// Not actually allowed. The model starts out with all the enums, and only allows hiding some.
}

View File

@ -51,6 +51,7 @@ import net.sf.openrocket.util.StateChangeListener;
public class InnerTubeConfig extends RocketComponentConfig {
private static final long serialVersionUID = 7900041420864324470L;
private static final Translator trans = Application.getTranslator();
@ -95,11 +96,7 @@ public class InnerTubeConfig extends RocketComponentConfig {
panel.add(spin, "growx");
panel.add(new UnitSelector(m), "growx");
if (od == null)
panel.add(new BasicSlider(m.getSliderModel(0, 0.04, 0.2)), "w 100lp, wrap");
else
panel.add(new BasicSlider(m.getSliderModel(new DoubleModel(0), od)),
"w 100lp, wrap");
panel.add(new BasicSlider(m.getSliderModel(new DoubleModel(0), od)), "w 100lp, wrap");
if (m.isAutomaticAvailable()) {
JCheckBox check = new JCheckBox(m.getAutomaticAction());
@ -142,7 +139,7 @@ public class InnerTubeConfig extends RocketComponentConfig {
//// Position relative to:
panel.add(new JLabel(trans.get("ringcompcfg.Positionrelativeto")));
JComboBox combo = new JComboBox(
JComboBox<?> combo = new JComboBox<RocketComponent.Position>(
new EnumModel<RocketComponent.Position>(component, "RelativePosition",
new RocketComponent.Position[] {
RocketComponent.Position.TOP,
@ -389,6 +386,10 @@ public class InnerTubeConfig extends RocketComponentConfig {
class ClusterSelectionPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1804786106133398810L;
private static final int BUTTON_SIZE = 50;
private static final int MOTOR_DIAMETER = 10;
@ -417,6 +418,10 @@ class ClusterSelectionPanel extends JPanel {
private class ClusterButton extends JPanel implements StateChangeListener, MouseListener,
Resettable {
/**
*
*/
private static final long serialVersionUID = 3626386642481889629L;
private Clusterable component;
private ClusterConfiguration config;

View File

@ -138,7 +138,7 @@ public class ParachuteConfig extends RecoveryDeviceConfig {
//// Position relative to:
panel.add(new JLabel(trans.get("ParachuteCfg.lbl.Posrelativeto")));
combo = new JComboBox(
combo = new JComboBox<RocketComponent.Position>(
new EnumModel<RocketComponent.Position>(component, "RelativePosition",
new RocketComponent.Position[] {
RocketComponent.Position.TOP,