DGP - 1st printing
[debian/openrocket] / src / net / sf / openrocket / gui / print / visitor / StageVisitorStrategy.java
1 /*
2  * StageVisitor.java
3  */
4 package net.sf.openrocket.gui.print.visitor;
5
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;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 /**
28  * This visitor strategy accumulates Stage references in a Rocket hierarchy.
29  */
30 public class StageVisitorStrategy implements ComponentVisitorStrategy {
31
32     /**
33      * The collection of stages, accumulated during a visitation.
34      */
35     private List<Double> stageComponents = new ArrayList<Double>();
36
37     private Double mass = 0d;
38
39     /**
40      * The owning visitor.
41      */
42     protected ComponentVisitor parent;
43
44     /**
45      * Constructor.
46      */
47     public StageVisitorStrategy () {
48     }
49
50     /**
51      * Override the method that determines if the visiting should be going deep.
52      *
53      * @param stageNumber a stage number
54      *
55      * @return true, always
56      */
57     public boolean shouldVisitStage (int stageNumber) {
58         return true;
59     }
60
61     /**
62      * {@inheritDoc}
63      */
64     @Override
65     public void setParent (final ComponentVisitor theParent) {
66         parent = theParent;
67     }
68
69     /**
70      * {@inheritDoc}
71      */
72     @Override
73     public void visit (final Rocket visitable) {
74         goDeep(visitable);
75     }
76
77     /**
78      * {@inheritDoc}
79      */
80     @Override
81     public void visit (final Stage visitable) {
82
83         if (mass > 0d) {
84             stageComponents.add(mass);
85         }
86         mass = 0d;
87         RocketComponent[] rc = visitable.getChildren();
88         goDeep(rc);
89     }
90
91     /**
92      * {@inheritDoc}
93      */
94     @Override
95     public void visit (final RocketComponent visitable) {
96         mass += visitable.getMass();
97         goDeep(visitable);
98     }
99
100     /**
101      * Recurse through the given rocket component.
102      *
103      * @param root the root component; all children will be visited recursively
104      */
105     protected void goDeep (final RocketComponent root) {
106         RocketComponent[] rc = root.getChildren();
107         goDeep(rc);
108     }
109
110
111     /**
112      * Recurse through the given rocket component.
113      *
114      * @param theRc an array of rocket components; all children will be visited recursively
115      */
116     protected void goDeep (final RocketComponent[] theRc) {
117         for (RocketComponent rocketComponent : theRc) {
118             rocketComponent.accept(parent);
119         }
120     }
121
122
123     /**
124      * {@inheritDoc}
125      */
126     @Override
127     public void visit (final ExternalComponent visitable) {
128         mass += visitable.getMass();
129         goDeep(visitable);
130     }
131
132     /**
133      * {@inheritDoc}
134      */
135     @Override
136     public void visit (final BodyComponent visitable) {
137         mass += visitable.getMass();
138         goDeep(visitable);
139     }
140
141     /**
142      * {@inheritDoc}
143      */
144     @Override
145     public void visit (final RingComponent visitable) {
146         mass += visitable.getMass();
147         goDeep(visitable);
148     }
149
150     /**
151      * {@inheritDoc}
152      */
153     @Override
154     public void visit (final InnerTube visitable) {
155         mass += visitable.getMass();
156         goDeep(visitable);
157     }
158
159     /**
160      * {@inheritDoc}
161      */
162     @Override
163     public void visit (final LaunchLug visitable) {
164         mass += visitable.getMass();
165         goDeep(visitable);
166     }
167
168     /**
169      * {@inheritDoc}
170      */
171     @Override
172     public void visit (final Transition visitable) {
173         mass += visitable.getMass();
174         goDeep(visitable);
175     }
176
177     /**
178      * {@inheritDoc}
179      */
180     @Override
181     public void visit (final RadiusRingComponent visitable) {
182         mass += visitable.getMass();
183         goDeep(visitable);
184     }
185
186     /**
187      * {@inheritDoc}
188      */
189     @Override
190     public void visit (final NoseCone visitable) {
191         mass += visitable.getMass();
192         goDeep(visitable);
193     }
194
195     /**
196      * {@inheritDoc}
197      */
198     @Override
199     public void visit (final BodyTube visitable) {
200         mass += visitable.getMass();
201         goDeep(visitable);
202     }
203
204     /**
205      * {@inheritDoc}
206      */
207     @Override
208     public void visit (final TrapezoidFinSet visitable) {
209         mass += visitable.getMass();
210         goDeep(visitable);
211     }
212
213     /**
214      * {@inheritDoc}
215      */
216     @Override
217     public void visit (final EllipticalFinSet visitable) {
218         mass += visitable.getMass();
219         goDeep(visitable);
220     }
221
222     /**
223      * {@inheritDoc}
224      */
225     @Override
226     public void visit (final FreeformFinSet visitable) {
227         mass += visitable.getMass();
228         goDeep(visitable);
229     }
230
231     /**
232      * Get the list of stages, sort from Stage 1 .. Stage N.
233      *
234      * @return a sorted list of stages
235      */
236     public List<Double> getStages () {
237         return stageComponents;
238     }
239
240     /**
241      * Close by setting the last stage.
242      */
243     public void close () {
244         if (mass > 0d) {
245             stageComponents.add(mass);
246         }
247     }
248
249 }