4 package net.sf.openrocket.gui.print.visitor;
6 import net.sf.openrocket.rocketcomponent.BodyComponent;
7 import net.sf.openrocket.rocketcomponent.BodyTube;
8 import net.sf.openrocket.rocketcomponent.ComponentVisitor;
9 import net.sf.openrocket.rocketcomponent.ComponentVisitorStrategy;
10 import net.sf.openrocket.rocketcomponent.EllipticalFinSet;
11 import net.sf.openrocket.rocketcomponent.ExternalComponent;
12 import net.sf.openrocket.rocketcomponent.FreeformFinSet;
13 import net.sf.openrocket.rocketcomponent.InnerTube;
14 import net.sf.openrocket.rocketcomponent.LaunchLug;
15 import net.sf.openrocket.rocketcomponent.NoseCone;
16 import net.sf.openrocket.rocketcomponent.RadiusRingComponent;
17 import net.sf.openrocket.rocketcomponent.RingComponent;
18 import net.sf.openrocket.rocketcomponent.Rocket;
19 import net.sf.openrocket.rocketcomponent.RocketComponent;
20 import net.sf.openrocket.rocketcomponent.Stage;
21 import net.sf.openrocket.rocketcomponent.Transition;
22 import net.sf.openrocket.rocketcomponent.TrapezoidFinSet;
24 import java.util.ArrayList;
25 import java.util.List;
28 * This visitor strategy accumulates Stage references in a Rocket hierarchy.
30 public class StageVisitorStrategy implements ComponentVisitorStrategy {
33 * The collection of stages, accumulated during a visitation.
35 private List<Double> stageComponents = new ArrayList<Double>();
37 private Double mass = 0d;
42 protected ComponentVisitor parent;
47 public StageVisitorStrategy () {
51 * Override the method that determines if the visiting should be going deep.
53 * @param stageNumber a stage number
55 * @return true, always
57 public boolean shouldVisitStage (int stageNumber) {
65 public void setParent (final ComponentVisitor theParent) {
73 public void visit (final Rocket visitable) {
81 public void visit (final Stage visitable) {
84 stageComponents.add(mass);
87 RocketComponent[] rc = visitable.getChildren();
95 public void visit (final RocketComponent visitable) {
96 mass += visitable.getMass();
101 * Recurse through the given rocket component.
103 * @param root the root component; all children will be visited recursively
105 protected void goDeep (final RocketComponent root) {
106 RocketComponent[] rc = root.getChildren();
112 * Recurse through the given rocket component.
114 * @param theRc an array of rocket components; all children will be visited recursively
116 protected void goDeep (final RocketComponent[] theRc) {
117 for (RocketComponent rocketComponent : theRc) {
118 rocketComponent.accept(parent);
127 public void visit (final ExternalComponent visitable) {
128 mass += visitable.getMass();
136 public void visit (final BodyComponent visitable) {
137 mass += visitable.getMass();
145 public void visit (final RingComponent visitable) {
146 mass += visitable.getMass();
154 public void visit (final InnerTube visitable) {
155 mass += visitable.getMass();
163 public void visit (final LaunchLug visitable) {
164 mass += visitable.getMass();
172 public void visit (final Transition visitable) {
173 mass += visitable.getMass();
181 public void visit (final RadiusRingComponent visitable) {
182 mass += visitable.getMass();
190 public void visit (final NoseCone visitable) {
191 mass += visitable.getMass();
199 public void visit (final BodyTube visitable) {
200 mass += visitable.getMass();
208 public void visit (final TrapezoidFinSet visitable) {
209 mass += visitable.getMass();
217 public void visit (final EllipticalFinSet visitable) {
218 mass += visitable.getMass();
226 public void visit (final FreeformFinSet visitable) {
227 mass += visitable.getMass();
232 * Get the list of stages, sort from Stage 1 .. Stage N.
234 * @return a sorted list of stages
236 public List<Double> getStages () {
237 return stageComponents;
241 * Close by setting the last stage.
243 public void close () {
245 stageComponents.add(mass);