1 package net.sf.openrocket.optimization.rocketoptimization.modifiers;
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;
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.
17 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
19 public abstract class GenericModifier<T> extends AbstractSimulationModifier {
20 private static final LogHelper log = Application.getLogger();
22 private final double multiplier;
24 private final Class<? extends T> modifiedClass;
25 private final String methodName;
27 private final Method getter;
28 private final Method setter;
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")
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;
48 this.modifiedClass = modifiedClass;
49 this.methodName = methodName;
51 if (MathUtil.equals(multiplier, 0)) {
52 throw new IllegalArgumentException("multiplier is zero");
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);
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");
74 return ((Double) getter.invoke(modifiable)) * multiplier;
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");
84 double siValue = toBaseValue(scaledValue) / multiplier;
85 log.verbose("Setting setter=" + setter + " modifiable=" + modifiable + " siValue=" + siValue + "scaledValue=" + scaledValue);
86 setter.invoke(modifiable, siValue);
91 * Return the object from the simulation that will be modified.
92 * @param simulation the simulation
93 * @return the object to modify
95 * @throws OptimizationException if the object cannot be found
97 protected abstract T getModifiedObject(Simulation simulation) throws OptimizationException;
102 public String toString() {
103 return "GenericModifier[modifiedClass=" + modifiedClass.getCanonicalName() + ", methodName=" + methodName + ", multiplier=" + multiplier + "]";