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