1 package net.sf.openrocket.optimization.rocketoptimization.modifiers;
3 import java.util.ArrayList;
6 import javax.swing.event.ChangeEvent;
7 import javax.swing.event.ChangeListener;
9 import net.sf.openrocket.document.Simulation;
10 import net.sf.openrocket.optimization.general.OptimizationException;
11 import net.sf.openrocket.optimization.rocketoptimization.SimulationModifier;
12 import net.sf.openrocket.unit.UnitGroup;
13 import net.sf.openrocket.util.MathUtil;
16 * An abstract implementation of the SimulationModifier interface. An implementation
17 * needs only to implement the {@link #getCurrentValue(Simulation)} and
18 * {@link #modify(net.sf.openrocket.document.Simulation, double)} methods.
20 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
22 public abstract class AbstractSimulationModifier implements SimulationModifier {
24 private final String name;
25 private final Object relatedObject;
26 private final UnitGroup unitGroup;
28 private double minValue = 0.0;
29 private double maxValue = 1.0;
31 private final List<ChangeListener> listeners = new ArrayList<ChangeListener>();
37 * @param modifierName the name of this modifier (returned by {@link #getName()})
38 * @param relatedObject the related object (returned by {@link #getRelatedObject()})
39 * @param unitGroup the unit group (returned by {@link #getUnitGroup()})
41 public AbstractSimulationModifier(String modifierName, Object relatedObject, UnitGroup unitGroup) {
42 this.name = modifierName;
43 this.relatedObject = relatedObject;
44 this.unitGroup = unitGroup;
49 public String getName() {
54 public Object getRelatedObject() {
59 public double getCurrentScaledValue(Simulation simulation) throws OptimizationException {
60 double value = getCurrentValue(simulation);
61 return toScaledValue(value);
67 * Returns the scaled value (normally within [0...1]). If the min...max range is singular,
68 * this method returns 0.0, 1.0 or 0.5 depending on whether the value is less than,
69 * greater than or equal to the limit.
71 * @param value the value in SI units
72 * @return the value in scaled range (normally within [0...1])
74 protected double toScaledValue(double value) {
75 if (MathUtil.equals(minValue, maxValue)) {
83 return MathUtil.map(value, minValue, maxValue, 0.0, 1.0);
88 * Returns the base value (in SI units).
90 * @param value the value in scaled range (normally within [0...1])
91 * @return the value in SI units
93 protected double toBaseValue(double value) {
94 return MathUtil.map(value, 0.0, 1.0, minValue, maxValue);
100 public double getMinValue() {
105 public void setMinValue(double value) {
106 if (MathUtil.equals(minValue, value))
108 this.minValue = value;
109 if (maxValue < minValue)
115 public double getMaxValue() {
120 public void setMaxValue(double value) {
121 if (MathUtil.equals(maxValue, value))
123 this.maxValue = value;
124 if (minValue > maxValue)
130 public UnitGroup getUnitGroup() {
136 public void addChangeListener(ChangeListener listener) {
137 listeners.add(listener);
141 public void removeChangeListener(ChangeListener listener) {
142 listeners.remove(listener);
147 * Fire a change event to the listeners.
149 protected void fireChangeEvent() {
150 ChangeListener[] array = listeners.toArray(new ChangeListener[0]);
151 ChangeEvent event = new ChangeEvent(this);
152 for (ChangeListener l : array) {
153 l.stateChanged(event);