]> git.gag.com Git - debian/openrocket/blob - src/net/sf/openrocket/gui/print/visitor/PartsListVisitorStrategy.java
merge fixing
[debian/openrocket] / src / net / sf / openrocket / gui / print / visitor / PartsListVisitorStrategy.java
1 /*
2  * PartsListVisitorStrategy.java
3  */
4 package net.sf.openrocket.gui.print.visitor;
5
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Set;
10
11 import net.sf.openrocket.rocketcomponent.BodyTube;
12 import net.sf.openrocket.rocketcomponent.Coaxial;
13 import net.sf.openrocket.rocketcomponent.ComponentVisitor;
14 import net.sf.openrocket.rocketcomponent.EllipticalFinSet;
15 import net.sf.openrocket.rocketcomponent.FreeformFinSet;
16 import net.sf.openrocket.rocketcomponent.InnerTube;
17 import net.sf.openrocket.rocketcomponent.LaunchLug;
18 import net.sf.openrocket.rocketcomponent.NoseCone;
19 import net.sf.openrocket.rocketcomponent.RadiusRingComponent;
20 import net.sf.openrocket.rocketcomponent.RingComponent;
21 import net.sf.openrocket.rocketcomponent.RocketComponent;
22 import net.sf.openrocket.rocketcomponent.Transition;
23 import net.sf.openrocket.rocketcomponent.TrapezoidFinSet;
24
25 import com.itextpdf.text.Document;
26 import com.itextpdf.text.pdf.PdfWriter;
27
28 /**
29  * A visitor strategy for creating documentation about a parts list.
30  */
31 public class PartsListVisitorStrategy extends BaseVisitorStrategy {
32         
33         /**
34          * Accumulator for parts data.
35          */
36         private Map<PartsAccumulator, PartsAccumulator> crap = new HashMap<PartsAccumulator, PartsAccumulator>();
37         
38         /**
39          * Construct a strategy for visiting a parts hierarchy for the purposes of collecting details on those parts.
40          *
41          * @param doc              The iText document
42          * @param theWriter        The direct iText writer
43          * @param theStagesToVisit The stages to be visited by this strategy
44          */
45         public PartsListVisitorStrategy(Document doc, PdfWriter theWriter, Set<Integer> theStagesToVisit) {
46                 super(doc, theWriter, theStagesToVisit);
47         }
48         
49         /**
50          * {@inheritDoc}
51          */
52         @Override
53         public void visit(final RingComponent visitable) {
54                 final PartsAccumulator key = new PartsAccumulator(visitable);
55                 PartsAccumulator pa = crap.get(key);
56                 if (pa == null) {
57                         pa = key;
58                         crap.put(pa, pa);
59                 }
60                 pa.increment();
61                 List<RocketComponent> rc = visitable.getChildren();
62                 goDeep(rc);
63         }
64         
65         /**
66          * {@inheritDoc}
67          */
68         @Override
69         public void visit(final InnerTube visitable) {
70                 final PartsAccumulator key = new PartsAccumulator(visitable);
71                 PartsAccumulator pa = crap.get(key);
72                 if (pa == null) {
73                         pa = key;
74                         crap.put(pa, pa);
75                 }
76                 pa.increment();
77                 List<RocketComponent> rc = visitable.getChildren();
78                 goDeep(rc);
79         }
80         
81         /**
82          * {@inheritDoc}
83          */
84         @Override
85         public void visit(final LaunchLug visitable) {
86                 final PartsAccumulator key = new PartsAccumulator(visitable);
87                 PartsAccumulator pa = crap.get(key);
88                 if (pa == null) {
89                         pa = key;
90                         crap.put(pa, pa);
91                 }
92                 pa.increment();
93         }
94         
95         /**
96          * {@inheritDoc}
97          */
98         @Override
99         public void visit(final Transition visitable) {
100                 final PartsAccumulator key = new PartsAccumulator(visitable);
101                 PartsAccumulator pa = crap.get(key);
102                 if (pa == null) {
103                         pa = key;
104                         crap.put(pa, pa);
105                 }
106                 pa.increment();
107                 List<RocketComponent> rc = visitable.getChildren();
108                 goDeep(rc);
109         }
110         
111         /**
112          * {@inheritDoc}
113          */
114         @Override
115         public void visit(final RadiusRingComponent visitable) {
116                 final PartsAccumulator key = new PartsAccumulator(visitable);
117                 PartsAccumulator pa = crap.get(key);
118                 if (pa == null) {
119                         pa = key;
120                         crap.put(pa, pa);
121                 }
122                 pa.increment();
123                 List<RocketComponent> rc = visitable.getChildren();
124                 goDeep(rc);
125         }
126         
127         /**
128          * {@inheritDoc}
129          */
130         @Override
131         public void visit(final NoseCone visitable) {
132                 final PartsAccumulator key = new PartsAccumulator(visitable);
133                 PartsAccumulator pa = crap.get(key);
134                 if (pa == null) {
135                         pa = key;
136                         crap.put(pa, pa);
137                 }
138                 pa.increment();
139                 List<RocketComponent> rc = visitable.getChildren();
140                 goDeep(rc);
141         }
142         
143         /**
144          * {@inheritDoc}
145          */
146         @Override
147         public void visit(final BodyTube visitable) {
148                 final PartsAccumulator key = new PartsAccumulator(visitable);
149                 PartsAccumulator pa = crap.get(key);
150                 if (pa == null) {
151                         pa = key;
152                         crap.put(pa, pa);
153                 }
154                 pa.increment();
155                 List<RocketComponent> rc = visitable.getChildren();
156                 goDeep(rc);
157         }
158         
159         /**
160          * {@inheritDoc}
161          */
162         @Override
163         public void visit(final TrapezoidFinSet visitable) {
164         }
165         
166         /**
167          * {@inheritDoc}
168          */
169         @Override
170         public void visit(final EllipticalFinSet visitable) {
171         }
172         
173         /**
174          * {@inheritDoc}
175          */
176         @Override
177         public void visit(final FreeformFinSet visitable) {
178         }
179         
180         /**
181          * {@inheritDoc}
182          */
183         @Override
184         public void setParent(final ComponentVisitor theParent) {
185                 parent = theParent;
186         }
187         
188         /**
189          * {@inheritDoc}
190          */
191         @Override
192         public void close() {
193                 for (PartsAccumulator partsAccumulator : crap.keySet()) {
194                         System.err.println(partsAccumulator.component.getComponentName() + " " + partsAccumulator.quantity);
195                 }
196         }
197         
198 }
199
200 class PartsAccumulator {
201         
202         int quantity = 0;
203         
204         RocketComponent component;
205         
206         PartsAccumulator(RocketComponent theComponent) {
207                 component = theComponent;
208         }
209         
210         void increment() {
211                 quantity++;
212         }
213         
214         int quantity() {
215                 return quantity;
216         }
217         
218         @Override
219         public boolean equals(final Object o1) {
220                 if (this == o1) {
221                         return true;
222                 }
223                 
224                 RocketComponent that;
225                 if (o1 instanceof net.sf.openrocket.gui.print.visitor.PartsAccumulator) {
226                         that = ((net.sf.openrocket.gui.print.visitor.PartsAccumulator) o1).component;
227                 } else if (o1 instanceof RocketComponent) {
228                         that = (RocketComponent) o1;
229                 } else {
230                         return false;
231                 }
232                 
233                 if (this.component.getClass().equals(that.getClass())) {
234                         //If 
235                         if (that.getLength() == this.component.getLength()) {
236                                 if (that.getMass() == this.component.getMass()) {
237                                         return true;
238                                 }
239                         }
240                         if (this.component instanceof Coaxial &&
241                                         that instanceof Coaxial) {
242                                 Coaxial cThis = (Coaxial) this.component;
243                                 Coaxial cThat = (Coaxial) that;
244                                 if (cThis.getInnerRadius() == cThat.getInnerRadius() &&
245                                                 cThis.getOuterRadius() == cThat.getOuterRadius()) {
246                                         return true;
247                                 }
248                         }
249                         return false;
250                 }
251                 return false;
252         }
253         
254         @Override
255         public int hashCode() {
256                 return component.getComponentName().hashCode();
257         }
258 }