2 * PartsListVisitorStrategy.java
4 package net.sf.openrocket.gui.print.visitor;
6 import java.util.HashMap;
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;
25 import com.itextpdf.text.Document;
26 import com.itextpdf.text.pdf.PdfWriter;
29 * A visitor strategy for creating documentation about a parts list.
31 public class PartsListVisitorStrategy extends BaseVisitorStrategy {
34 * Accumulator for parts data.
36 private Map<PartsAccumulator, PartsAccumulator> crap = new HashMap<PartsAccumulator, PartsAccumulator>();
39 * Construct a strategy for visiting a parts hierarchy for the purposes of collecting details on those parts.
41 * @param doc The iText document
42 * @param theWriter The direct iText writer
43 * @param theStagesToVisit The stages to be visited by this strategy
45 public PartsListVisitorStrategy(Document doc, PdfWriter theWriter, Set<Integer> theStagesToVisit) {
46 super(doc, theWriter, theStagesToVisit);
53 public void visit(final RingComponent visitable) {
54 final PartsAccumulator key = new PartsAccumulator(visitable);
55 PartsAccumulator pa = crap.get(key);
61 List<RocketComponent> rc = visitable.getChildren();
69 public void visit(final InnerTube visitable) {
70 final PartsAccumulator key = new PartsAccumulator(visitable);
71 PartsAccumulator pa = crap.get(key);
77 List<RocketComponent> rc = visitable.getChildren();
85 public void visit(final LaunchLug visitable) {
86 final PartsAccumulator key = new PartsAccumulator(visitable);
87 PartsAccumulator pa = crap.get(key);
99 public void visit(final Transition visitable) {
100 final PartsAccumulator key = new PartsAccumulator(visitable);
101 PartsAccumulator pa = crap.get(key);
107 List<RocketComponent> rc = visitable.getChildren();
115 public void visit(final RadiusRingComponent visitable) {
116 final PartsAccumulator key = new PartsAccumulator(visitable);
117 PartsAccumulator pa = crap.get(key);
123 List<RocketComponent> rc = visitable.getChildren();
131 public void visit(final NoseCone visitable) {
132 final PartsAccumulator key = new PartsAccumulator(visitable);
133 PartsAccumulator pa = crap.get(key);
139 List<RocketComponent> rc = visitable.getChildren();
147 public void visit(final BodyTube visitable) {
148 final PartsAccumulator key = new PartsAccumulator(visitable);
149 PartsAccumulator pa = crap.get(key);
155 List<RocketComponent> rc = visitable.getChildren();
163 public void visit(final TrapezoidFinSet visitable) {
170 public void visit(final EllipticalFinSet visitable) {
177 public void visit(final FreeformFinSet visitable) {
184 public void setParent(final ComponentVisitor theParent) {
192 public void close() {
193 for (PartsAccumulator partsAccumulator : crap.keySet()) {
194 System.err.println(partsAccumulator.component.getComponentName() + " " + partsAccumulator.quantity);
200 class PartsAccumulator {
204 RocketComponent component;
206 PartsAccumulator(RocketComponent theComponent) {
207 component = theComponent;
219 public boolean equals(final Object o1) {
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;
233 if (this.component.getClass().equals(that.getClass())) {
235 if (that.getLength() == this.component.getLength()) {
236 if (that.getMass() == this.component.getMass()) {
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()) {
255 public int hashCode() {
256 return component.getComponentName().hashCode();