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 #getCurrentSIValue(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 String description;
26 private final Object relatedObject;
27 private final UnitGroup unitGroup;
29 private double minValue = 0.0;
30 private double maxValue = 1.0;
32 private final List<ChangeListener> listeners = new ArrayList<ChangeListener>();
38 * @param modifierName the name of this modifier (returned by {@link #getName()})
39 * @param modifierDescription the description of this modifier (returned by {@link #getDescription()})
40 * @param relatedObject the related object (returned by {@link #getRelatedObject()})
41 * @param unitGroup the unit group (returned by {@link #getUnitGroup()})
43 public AbstractSimulationModifier(String modifierName, String modifierDescription, Object relatedObject,
44 UnitGroup unitGroup) {
45 this.name = modifierName;
46 this.description = modifierDescription;
47 this.relatedObject = relatedObject;
48 this.unitGroup = unitGroup;
50 if (this.name == null || this.description == null || this.relatedObject == null || this.unitGroup == null) {
51 throw new IllegalArgumentException("null value provided:" +
52 " name=" + this.name + " description=" + description + " relatedObject=" + relatedObject +
53 " unitGroup=" + unitGroup);
59 public String getName() {
64 public String getDescription() {
69 public Object getRelatedObject() {
74 public double getCurrentScaledValue(Simulation simulation) throws OptimizationException {
75 double value = getCurrentSIValue(simulation);
76 return toScaledValue(value);
82 * Returns the scaled value (normally within [0...1]). If the min...max range is singular,
83 * this method returns 0.0, 1.0 or 0.5 depending on whether the value is less than,
84 * greater than or equal to the limit.
86 * @param value the value in SI units
87 * @return the value in scaled range (normally within [0...1])
89 protected double toScaledValue(double value) {
90 if (MathUtil.equals(minValue, maxValue)) {
98 return MathUtil.map(value, minValue, maxValue, 0.0, 1.0);
103 * Returns the base value (in SI units).
105 * @param value the value in scaled range (normally within [0...1])
106 * @return the value in SI units
108 protected double toBaseValue(double value) {
109 System.out.println("value=" + value + " minValue=" + minValue + " maxValue=" + maxValue);
110 return MathUtil.map(value, 0.0, 1.0, minValue, maxValue);
116 public double getMinValue() {
121 public void setMinValue(double value) {
122 if (MathUtil.equals(minValue, value))
124 this.minValue = value;
125 if (maxValue < minValue)
131 public double getMaxValue() {
136 public void setMaxValue(double value) {
137 if (MathUtil.equals(maxValue, value))
139 this.maxValue = value;
140 if (minValue > maxValue)
146 public UnitGroup getUnitGroup() {
152 public void addChangeListener(ChangeListener listener) {
153 listeners.add(listener);
157 public void removeChangeListener(ChangeListener listener) {
158 listeners.remove(listener);
163 * Fire a change event to the listeners.
165 protected void fireChangeEvent() {
166 ChangeListener[] array = listeners.toArray(new ChangeListener[0]);
167 ChangeEvent event = new ChangeEvent(this);
168 for (ChangeListener l : array) {
169 l.stateChanged(event);
174 public boolean equals(Object obj) {
179 if (getClass() != obj.getClass())
182 AbstractSimulationModifier other = (AbstractSimulationModifier) obj;
183 if (!this.description.equals(other.description))
185 if (!this.name.equals(other.name))
187 if (!this.relatedObject.equals(other.relatedObject))
189 if (!this.unitGroup.equals(other.unitGroup))
198 public int hashCode() {
199 final int prime = 31;
201 result = prime * result + (description.hashCode());
202 result = prime * result + (name.hashCode());
203 result = prime * result + (relatedObject.hashCode());
204 result = prime * result + (unitGroup.hashCode());