major optimization updates
[debian/openrocket] / src / net / sf / openrocket / optimization / rocketoptimization / modifiers / AbstractSimulationModifier.java
1 package net.sf.openrocket.optimization.rocketoptimization.modifiers;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.swing.event.ChangeEvent;
7 import javax.swing.event.ChangeListener;
8
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;
14
15 /**
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.
19  * 
20  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
21  */
22 public abstract class AbstractSimulationModifier implements SimulationModifier {
23         
24         private final String name;
25         private final String description;
26         private final Object relatedObject;
27         private final UnitGroup unitGroup;
28         
29         private double minValue = 0.0;
30         private double maxValue = 1.0;
31         
32         private final List<ChangeListener> listeners = new ArrayList<ChangeListener>();
33         
34         
35         /**
36          * Sole constructor.
37          * 
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()})
42          */
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;
49                 
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);
54                 }
55         }
56         
57         
58         @Override
59         public String getName() {
60                 return name;
61         }
62         
63         @Override
64         public String getDescription() {
65                 return description;
66         }
67         
68         @Override
69         public Object getRelatedObject() {
70                 return relatedObject;
71         }
72         
73         @Override
74         public double getCurrentScaledValue(Simulation simulation) throws OptimizationException {
75                 double value = getCurrentSIValue(simulation);
76                 return toScaledValue(value);
77         }
78         
79         
80
81         /**
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.
85          * 
86          * @param value         the value in SI units
87          * @return                      the value in scaled range (normally within [0...1])
88          */
89         protected double toScaledValue(double value) {
90                 if (MathUtil.equals(minValue, maxValue)) {
91                         if (value > maxValue)
92                                 return 1.0;
93                         if (value < minValue)
94                                 return 0.0;
95                         return 0.5;
96                 }
97                 
98                 return MathUtil.map(value, minValue, maxValue, 0.0, 1.0);
99         }
100         
101         
102         /**
103          * Returns the base value (in SI units).
104          * 
105          * @param value         the value in scaled range (normally within [0...1])
106          * @return                      the value in SI units
107          */
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);
111         }
112         
113         
114
115         @Override
116         public double getMinValue() {
117                 return minValue;
118         }
119         
120         @Override
121         public void setMinValue(double value) {
122                 if (MathUtil.equals(minValue, value))
123                         return;
124                 this.minValue = value;
125                 if (maxValue < minValue)
126                         maxValue = minValue;
127                 fireChangeEvent();
128         }
129         
130         @Override
131         public double getMaxValue() {
132                 return maxValue;
133         }
134         
135         @Override
136         public void setMaxValue(double value) {
137                 if (MathUtil.equals(maxValue, value))
138                         return;
139                 this.maxValue = value;
140                 if (minValue > maxValue)
141                         minValue = maxValue;
142                 fireChangeEvent();
143         }
144         
145         @Override
146         public UnitGroup getUnitGroup() {
147                 return unitGroup;
148         }
149         
150         
151         @Override
152         public void addChangeListener(ChangeListener listener) {
153                 listeners.add(listener);
154         }
155         
156         @Override
157         public void removeChangeListener(ChangeListener listener) {
158                 listeners.remove(listener);
159         }
160         
161         
162         /**
163          * Fire a change event to the listeners.
164          */
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);
170                 }
171         }
172         
173         @Override
174         public boolean equals(Object obj) {
175                 if (this == obj)
176                         return true;
177                 if (obj == null)
178                         return false;
179                 if (getClass() != obj.getClass())
180                         return false;
181                 
182                 AbstractSimulationModifier other = (AbstractSimulationModifier) obj;
183                 if (!this.description.equals(other.description))
184                         return false;
185                 if (!this.name.equals(other.name))
186                         return false;
187                 if (!this.relatedObject.equals(other.relatedObject))
188                         return false;
189                 if (!this.unitGroup.equals(other.unitGroup))
190                         return false;
191                 
192                 return true;
193         }
194         
195         
196
197         @Override
198         public int hashCode() {
199                 final int prime = 31;
200                 int result = 1;
201                 result = prime * result + (description.hashCode());
202                 result = prime * result + (name.hashCode());
203                 result = prime * result + (relatedObject.hashCode());
204                 result = prime * result + (unitGroup.hashCode());
205                 return result;
206         }
207         
208
209 }