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