b22d9332945a517e6b34ab41f50826a0da465bc7
[debian/openrocket] / src / net / sf / openrocket / optimization / rocketoptimization / modifiers / GenericComponentModifier.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.rocketcomponent.RocketComponent;
6 import net.sf.openrocket.unit.UnitGroup;
7
8 /**
9  * A generic simulation modifier that modifies a value of a certain RocketComponent
10  * based on the component's ID and the value name.
11  * 
12  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
13  */
14 public class GenericComponentModifier extends GenericModifier<RocketComponent> {
15         
16         private final Class<? extends RocketComponent> componentClass;
17         private final String componentId;
18         
19         /**
20          * Sole constructor.
21          * 
22          * @param modifierName          the name of this modifier (returned by {@link #getName()})
23          * @param relatedObject         the related object (returned by {@link #getRelatedObject()})
24          * @param unitGroup                     the unit group (returned by {@link #getUnitGroup()})
25          * @param multiplier            the multiplier by which the value returned by the getter is multiplied
26          *                                                      to obtain the desired value
27          * @param componentClass        the RocketComponent class type that is being modified
28          * @param componentId           the ID of the component to modify
29          * @param methodName            the base name of the getter/setter methods (without "get"/"set")
30          */
31         public GenericComponentModifier(String modifierName, Object relatedObject, UnitGroup unitGroup,
32                         double multiplier, Class<? extends RocketComponent> componentClass, String componentId, String methodName) {
33                 super(modifierName, relatedObject, unitGroup, multiplier, componentClass, methodName);
34                 
35                 this.componentClass = componentClass;
36                 this.componentId = componentId;
37         }
38         
39         @Override
40         protected RocketComponent getModifiedObject(Simulation simulation) throws OptimizationException {
41                 RocketComponent c = simulation.getRocket().findComponent(componentId);
42                 if (c == null) {
43                         throw new OptimizationException("Could not find component of type " + componentClass.getSimpleName()
44                                         + " with correct ID");
45                 }
46                 return c;
47         }
48         
49 }