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