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