optimization bug fixes
[debian/openrocket] / src / net / sf / openrocket / optimization / rocketoptimization / modifiers / GenericModifier.java
1 package net.sf.openrocket.optimization.rocketoptimization.modifiers;
2
3 import net.sf.openrocket.document.Simulation;
4 import net.sf.openrocket.logging.LogHelper;
5 import net.sf.openrocket.optimization.general.OptimizationException;
6 import net.sf.openrocket.startup.Application;
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 /**
13  * A generic SimulationModifier that uses reflection to get and set a double value.
14  * Implementations need to implement the {@link #getModifiedObject(Simulation)} method
15  * to return which object is modified.
16  * 
17  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
18  */
19 public abstract class GenericModifier<T> extends AbstractSimulationModifier {
20         private static final LogHelper log = Application.getLogger();
21         
22         private final double multiplier;
23         
24         private final Class<? extends T> modifiedClass;
25         private final String methodName;
26         
27         private final Method getter;
28         private final Method setter;
29         
30         
31         /**
32          * Sole constructor.
33          * 
34          * @param modifierName          the name of this modifier (returned by {@link #getName()})
35          * @param modifierDescription   the description of this modifier (returned by {@link #getDescription()})
36          * @param relatedObject         the related object (returned by {@link #getRelatedObject()})
37          * @param unitGroup                     the unit group (returned by {@link #getUnitGroup()})
38          * @param multiplier            the multiplier by which the value returned by the getter is multiplied
39          *                                                      to obtain the desired value
40          * @param modifiedClass         the class type that {@link #getModifiedObject(Simulation)} returns
41          * @param methodName            the base name of the getter/setter methods (without "get"/"set")
42          */
43         public GenericModifier(String modifierName, String modifierDescription, Object relatedObject, UnitGroup unitGroup,
44                         double multiplier, Class<? extends T> modifiedClass, String methodName) {
45                 super(modifierName, modifierDescription, relatedObject, unitGroup);
46                 this.multiplier = multiplier;
47                 
48                 this.modifiedClass = modifiedClass;
49                 this.methodName = methodName;
50                 
51                 if (MathUtil.equals(multiplier, 0)) {
52                         throw new IllegalArgumentException("multiplier is zero");
53                 }
54                 
55                 try {
56                         methodName = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
57                         getter = new Method(modifiedClass.getMethod("get" + methodName));
58                         setter = new Method(modifiedClass.getMethod("set" + methodName, double.class));
59                 } catch (SecurityException e) {
60                         throw new BugException("Trying to find method get/set" + methodName + " in class " + modifiedClass, e);
61                 } catch (NoSuchMethodException e) {
62                         throw new BugException("Trying to find method get/set" + methodName + " in class " + modifiedClass, e);
63                 }
64         }
65         
66         
67
68         @Override
69         public double getCurrentSIValue(Simulation simulation) throws OptimizationException {
70                 T modifiable = getModifiedObject(simulation);
71                 if (modifiable == null) {
72                         throw new OptimizationException("BUG: getModifiedObject() returned null");
73                 }
74                 return ((Double) getter.invoke(modifiable)) * multiplier;
75         }
76         
77         
78         @Override
79         public void modify(Simulation simulation, double scaledValue) throws OptimizationException {
80                 T modifiable = getModifiedObject(simulation);
81                 if (modifiable == null) {
82                         throw new OptimizationException("BUG: getModifiedObject() returned null");
83                 }
84                 double siValue = toBaseValue(scaledValue) / multiplier;
85                 log.verbose("Setting setter=" + setter + " modifiable=" + modifiable + " siValue=" + siValue + "scaledValue=" + scaledValue);
86                 setter.invoke(modifiable, siValue);
87         }
88         
89         
90         /**
91          * Return the object from the simulation that will be modified.
92          * @param simulation    the simulation
93          * @return                              the object to modify
94          * 
95          * @throws OptimizationException        if the object cannot be found
96          */
97         protected abstract T getModifiedObject(Simulation simulation) throws OptimizationException;
98         
99         
100
101         @Override
102         public String toString() {
103                 return "GenericModifier[modifiedClass=" + modifiedClass.getCanonicalName() + ", methodName=" + methodName + ", multiplier=" + multiplier + "]";
104         }
105         
106 }