major optimization updates
[debian/openrocket] / src / net / sf / openrocket / unit / CaliberUnit.java
index 4d9784c82b32ce119151365388155470e675e2c5..cc93cb5114eb846d93c83529e078398ed1b314af 100644 (file)
@@ -1,39 +1,30 @@
 package net.sf.openrocket.unit;
 
-import java.util.Collection;
-import java.util.Collections;
 import java.util.Iterator;
 
-import javax.swing.event.ChangeEvent;
-import javax.swing.event.ChangeListener;
-
 import net.sf.openrocket.rocketcomponent.Configuration;
 import net.sf.openrocket.rocketcomponent.Rocket;
 import net.sf.openrocket.rocketcomponent.RocketComponent;
 import net.sf.openrocket.rocketcomponent.SymmetricComponent;
+import net.sf.openrocket.util.BugException;
 import net.sf.openrocket.util.MathUtil;
 
 
 public class CaliberUnit extends GeneralUnit {
-
+       
        public static final double DEFAULT_CALIBER = 0.01;
        
        private final Configuration configuration;
        private final Rocket rocket;
        
-       private double caliber = -1;
-       
-       
-       /* Listener for rocket and configuration, resets the caliber to -1. */
-       private final ChangeListener listener = new ChangeListener() {
-               @Override
-               public void stateChanged(ChangeEvent e) {
-                       caliber = -1;
-               }
-       };
+       private int rocketModId = -1;
+       private int configurationModId = -1;
        
+       private double caliber = -1;
        
        
+
+
        public CaliberUnit(Configuration configuration) {
                super(1.0, "cal");
                this.configuration = configuration;
@@ -42,7 +33,6 @@ public class CaliberUnit extends GeneralUnit {
                        this.rocket = null;
                } else {
                        this.rocket = configuration.getRocket();
-                       configuration.addChangeListener(listener);
                }
        }
        
@@ -50,53 +40,94 @@ public class CaliberUnit extends GeneralUnit {
                super(1.0, "cal");
                this.configuration = null;
                this.rocket = rocket;
-               if (rocket != null) {
-                       rocket.addChangeListener(listener);
+       }
+       
+       public CaliberUnit(double reference) {
+               super(1.0, "cal");
+               this.configuration = null;
+               this.rocket = null;
+               this.caliber = reference;
+               
+               if (reference <= 0) {
+                       throw new IllegalArgumentException("Illegal reference = " + reference);
                }
        }
        
        
        @Override
        public double fromUnit(double value) {
-               if (caliber < 0)
-                       calculateCaliber();
+               checkCaliber();
                
                return value * caliber;
        }
-
+       
        @Override
        public double toUnit(double value) {
-               if (caliber < 0)
-                       calculateCaliber();
+               checkCaliber();
                
                return value / caliber;
        }
-
        
-       // TODO: HIGH:  Check caliber calculation method...
-       private void calculateCaliber() {
-               caliber = 0;
-               
-               Iterator<RocketComponent> iterator;
-               if (configuration != null) {
-                       iterator = configuration.iterator();
-               } else if (rocket != null) {
-                       iterator = rocket.deepIterator();
-               } else {
-                       Collection<RocketComponent> set = Collections.emptyList();
-                       iterator = set.iterator();
+       
+
+       private void checkCaliber() {
+               if (configuration != null && configuration.getModID() != configurationModId) {
+                       caliber = -1;
+                       configurationModId = configuration.getModID();
                }
+               if (rocket != null && rocket.getModID() != rocketModId) {
+                       caliber = -1;
+                       rocketModId = rocket.getModID();
+               }
+               if (caliber < 0) {
+                       if (configuration != null) {
+                               caliber = calculateCaliber(configuration);
+                       } else if (rocket != null) {
+                               caliber = calculateCaliber(rocket);
+                       } else {
+                               throw new BugException("Both rocket and configuration are null");
+                       }
+               }
+       }
+       
+       
+       /**
+        * Calculate the caliber of a rocket configuration.
+        * 
+        * @param config        the rocket configuration
+        * @return                      the caliber of the rocket, or the default caliber.
+        */
+       public static double calculateCaliber(Configuration config) {
+               return calculateCaliber(config.iterator());
+       }
+       
+       /**
+        * Calculate the caliber of a rocket.
+        * 
+        * @param rocket        the rocket
+        * @return                      the caliber of the rocket, or the default caliber.
+        */
+       public static double calculateCaliber(Rocket rocket) {
+               return calculateCaliber(rocket.iterator());
+       }
+       
+       
+
+       private static double calculateCaliber(Iterator<RocketComponent> iterator) {
+               double cal = 0;
                
                while (iterator.hasNext()) {
                        RocketComponent c = iterator.next();
                        if (c instanceof SymmetricComponent) {
-                               double r1 = ((SymmetricComponent)c).getForeRadius() * 2;
-                               double r2 = ((SymmetricComponent)c).getAftRadius() * 2;
-                               caliber = MathUtil.max(caliber, r1, r2);
+                               double r1 = ((SymmetricComponent) c).getForeRadius() * 2;
+                               double r2 = ((SymmetricComponent) c).getAftRadius() * 2;
+                               cal = MathUtil.max(cal, r1, r2);
                        }
                }
                
-               if (caliber <= 0)
-                       caliber = DEFAULT_CALIBER;
+               if (cal < 0.0001)
+                       cal = DEFAULT_CALIBER;
+               
+               return cal;
        }
 }