SafetyMutex and rocket optimization updates
[debian/openrocket] / src / net / sf / openrocket / optimization / rocketoptimization / modifiers / GenericModifier.java
1 package net.sf.openrocket.optimization.rocketoptimization.modifiers;
2
3 import javax.swing.event.ChangeListener;
4
5 import net.sf.openrocket.document.Simulation;
6 import net.sf.openrocket.optimization.rocketoptimization.SimulationModifier;
7 import net.sf.openrocket.unit.UnitGroup;
8 import net.sf.openrocket.util.BugException;
9 import net.sf.openrocket.util.MathUtil;
10 import net.sf.openrocket.util.Reflection.Method;
11
12 public class GenericModifier implements SimulationModifier {
13         
14         private final String name;
15         private final Object relatedObject;
16         private final UnitGroup unitGroup;
17         private final double multiplier;
18         private final Object modifiable;
19         
20         private final Method getter;
21         private final Method setter;
22         
23         private double minValue;
24         private double maxValue;
25         
26         
27
28
29
30         public GenericModifier(String modifierName, Object relatedObject, UnitGroup unitGroup, double multiplier,
31                         Object modifiable, String methodName) {
32                 this.name = modifierName;
33                 this.relatedObject = relatedObject;
34                 this.unitGroup = unitGroup;
35                 this.multiplier = multiplier;
36                 this.modifiable = modifiable;
37                 
38                 if (MathUtil.equals(multiplier, 0)) {
39                         throw new IllegalArgumentException("multiplier is zero");
40                 }
41                 
42                 try {
43                         methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
44                         getter = new Method(modifiable.getClass().getMethod("get" + methodName));
45                         setter = new Method(modifiable.getClass().getMethod("set" + methodName, double.class));
46                 } catch (SecurityException e) {
47                         throw new BugException("Trying to find method get/set" + methodName + " in class " + modifiable.getClass(), e);
48                 } catch (NoSuchMethodException e) {
49                         throw new BugException("Trying to find method get/set" + methodName + " in class " + modifiable.getClass(), e);
50                 }
51         }
52         
53         
54         @Override
55         public String getName() {
56                 return name;
57         }
58         
59         @Override
60         public Object getRelatedObject() {
61                 return relatedObject;
62         }
63         
64         @Override
65         public double getCurrentValue() {
66                 return ((Double) getter.invoke(modifiable)) * multiplier;
67         }
68         
69         
70         @Override
71         public double getCurrentScaledValue() {
72                 double value = getCurrentValue();
73                 return toScaledValue(value);
74         }
75         
76         @Override
77         public void modify(Simulation simulation, double scaledValue) {
78                 double siValue = toBaseValue(scaledValue) / multiplier;
79                 setter.invoke(modifiable, siValue);
80         }
81         
82         
83         /**
84          * Returns the scaled value (normally within [0...1]).
85          */
86         private double toScaledValue(double value) {
87                 if (MathUtil.equals(minValue, maxValue)) {
88                         if (value > maxValue)
89                                 return 1.0;
90                         if (value < minValue)
91                                 return 0.0;
92                         return 0.5;
93                 }
94                 
95                 return MathUtil.map(value, minValue, maxValue, 0.0, 1.0);
96         }
97         
98         
99         /**
100          * Returns the base value (in SI units).
101          */
102         private double toBaseValue(double value) {
103                 return MathUtil.map(value, 0.0, 1.0, minValue, maxValue);
104         }
105         
106         
107
108         @Override
109         public double getMinValue() {
110                 return minValue;
111         }
112         
113         @Override
114         public void setMinValue(double value) {
115                 if (MathUtil.equals(minValue, value))
116                         return;
117                 this.minValue = value;
118                 if (maxValue < minValue)
119                         maxValue = minValue;
120                 fireChangeEvent();
121         }
122         
123         @Override
124         public double getMaxValue() {
125                 return maxValue;
126         }
127         
128         @Override
129         public void setMaxValue(double value) {
130                 if (MathUtil.equals(maxValue, value))
131                         return;
132                 this.maxValue = value;
133                 if (minValue > maxValue)
134                         minValue = maxValue;
135                 fireChangeEvent();
136         }
137         
138         @Override
139         public UnitGroup getUnitGroup() {
140                 return unitGroup;
141         }
142         
143         
144         @Override
145         public void addChangeListener(ChangeListener listener) {
146                 // TODO Auto-generated method stub
147                 
148         }
149         
150         @Override
151         public void removeChangeListener(ChangeListener listener) {
152                 // TODO Auto-generated method stub
153                 
154         }
155         
156         
157         private void fireChangeEvent() {
158                 // TODO Auto-generated method stub
159                 
160         }
161         
162 }