Merge branch 'upstream' into debian
[debian/openrocket] / src / net / sf / openrocket / material / Material.java
index f59885da900a38d93ce9146e3dc3ee90ee3bc350..00be68b1c3cdce191dfe53b035d13d31fc086a60 100644 (file)
@@ -15,23 +15,38 @@ import net.sf.openrocket.util.MathUtil;
  */
 
 public abstract class Material implements Comparable<Material> {
-
+       
        public enum Type {
-               LINE,
-               SURFACE,
-               BULK
+               LINE("Line", UnitGroup.UNITS_DENSITY_LINE),
+               SURFACE("Surface", UnitGroup.UNITS_DENSITY_SURFACE),
+               BULK("Bulk", UnitGroup.UNITS_DENSITY_BULK);
+               
+               private final String name;
+               private final UnitGroup units;
+               
+               private Type(String name, UnitGroup units) {
+                       this.name = name;
+                       this.units = units;
+               }
+               
+               public UnitGroup getUnitGroup() {
+                       return units;
+               }
+               
+               @Override
+               public String toString() {
+                       return name;
+               }
        }
        
+       
+       /////  Definitions of different material types  /////
+       
        public static class Line extends Material {
-               public Line(String name, double density) {
-                       super(name, density);
+               public Line(String name, double density, boolean userDefined) {
+                       super(name, density, userDefined);
                }
-
-               @Override
-               public UnitGroup getUnitGroup() {
-                       return UnitGroup.UNITS_DENSITY_LINE;
-               }
-
+               
                @Override
                public Type getType() {
                        return Type.LINE;
@@ -40,15 +55,10 @@ public abstract class Material implements Comparable<Material> {
        
        public static class Surface extends Material {
                
-               public Surface(String name, double density) {
-                       super(name, density);
+               public Surface(String name, double density, boolean userDefined) {
+                       super(name, density, userDefined);
                }
                
-               @Override
-               public UnitGroup getUnitGroup() {
-                       return UnitGroup.UNITS_DENSITY_SURFACE;
-               }
-
                @Override
                public Type getType() {
                        return Type.SURFACE;
@@ -61,15 +71,10 @@ public abstract class Material implements Comparable<Material> {
        }
        
        public static class Bulk extends Material {
-               public Bulk(String name, double density) {
-                       super(name, density);
+               public Bulk(String name, double density, boolean userDefined) {
+                       super(name, density, userDefined);
                }
-
-               @Override
-               public UnitGroup getUnitGroup() {
-                       return UnitGroup.UNITS_DENSITY_BULK;
-               }
-
+               
                @Override
                public Type getType() {
                        return Type.BULK;
@@ -77,18 +82,20 @@ public abstract class Material implements Comparable<Material> {
        }
        
        
-       
+
        private final String name;
        private final double density;
+       private final boolean userDefined;
        
        
-       public Material(String name, double density) {
+       public Material(String name, double density, boolean userDefined) {
                this.name = name;
                this.density = density;
+               this.userDefined = userDefined;
        }
        
        
-       
+
        public double getDensity() {
                return density;
        }
@@ -101,15 +108,18 @@ public abstract class Material implements Comparable<Material> {
                return name + " (" + u.toStringUnit(density) + ")";
        }
        
-       public abstract UnitGroup getUnitGroup();
+       public boolean isUserDefined() {
+               return userDefined;
+       }
+       
        public abstract Type getType();
        
        @Override
        public String toString() {
-               return getName(getUnitGroup().getDefaultUnit());
+               return this.getName(this.getType().getUnitGroup().getDefaultUnit());
        }
        
-
+       
        /**
         * Compares this object to another object.  Material objects are equal if and only if
         * their types, names and densities are identical.
@@ -120,48 +130,51 @@ public abstract class Material implements Comparable<Material> {
                        return false;
                if (this.getClass() != o.getClass())
                        return false;
-               Material m = (Material)o;
-               return ((m.name.equals(this.name)) && 
-                               MathUtil.equals(m.density, this.density)); 
+               Material m = (Material) o;
+               return ((m.name.equals(this.name)) && MathUtil.equals(m.density, this.density));
        }
-
-
+       
+       
        /**
         * A hashCode() method giving a hash code compatible with the equals() method.
         */
        @Override
        public int hashCode() {
-               return name.hashCode() + (int)(density*1000);
+               return name.hashCode() + (int) (density * 1000);
        }
-
+       
        
        /**
         * Order the materials according to their name, secondarily according to density.
         */
+       @Override
        public int compareTo(Material o) {
                int c = this.name.compareTo(o.name);
                if (c != 0) {
                        return c;
                } else {
-                       return (int)((this.density - o.density)*1000);
+                       return (int) ((this.density - o.density) * 1000);
                }
        }
        
        
-       
-       public static Material newMaterial(Type type, String name, double density) {
+       /**
+        * Return a new material of the specified type.
+        */
+       public static Material newMaterial(Type type, String name, double density,
+                       boolean userDefined) {
                switch (type) {
                case LINE:
-                       return new Material.Line(name, density);
+                       return new Material.Line(name, density, userDefined);
                        
                case SURFACE:
-                       return new Material.Surface(name, density);
+                       return new Material.Surface(name, density, userDefined);
                        
                case BULK:
-                       return new Material.Bulk(name, density);
+                       return new Material.Bulk(name, density, userDefined);
                        
                default:
-                       throw new IllegalArgumentException("Unknown material type: "+type);
+                       throw new IllegalArgumentException("Unknown material type: " + type);
                }
        }
        
@@ -170,11 +183,23 @@ public abstract class Material implements Comparable<Material> {
                return getType().name() + "|" + name.replace('|', ' ') + '|' + density;
        }
        
-       public static Material fromStorableString(String str) {
-               String[] split = str.split("\\|",3);
+       
+       /**
+        * Return a material defined by the provided string.
+        * 
+        * @param str                   the material storage string.
+        * @param userDefined   whether the created material is user-defined.
+        * @return                              a new <code>Material</code> object.
+        * @throws IllegalArgumentException             if <code>str</code> is invalid or null.
+        */
+       public static Material fromStorableString(String str, boolean userDefined) {
+               if (str == null)
+                       throw new IllegalArgumentException("Material string is null");
+               
+               String[] split = str.split("\\|", 3);
                if (split.length < 3)
-                       throw new IllegalArgumentException("Illegal material string: "+str);
-
+                       throw new IllegalArgumentException("Illegal material string: " + str);
+               
                Type type = null;
                String name;
                double density;
@@ -182,30 +207,30 @@ public abstract class Material implements Comparable<Material> {
                try {
                        type = Type.valueOf(split[0]);
                } catch (Exception e) {
-                       throw new IllegalArgumentException("Illegal material string: "+str, e);
+                       throw new IllegalArgumentException("Illegal material string: " + str, e);
                }
-
+               
                name = split[1];
                
                try {
                        density = Double.parseDouble(split[2]);
                } catch (NumberFormatException e) {
-                       throw new IllegalArgumentException("Illegal material string: "+str, e);
+                       throw new IllegalArgumentException("Illegal material string: " + str, e);
                }
                
                switch (type) {
                case BULK:
-                       return new Material.Bulk(name, density);
+                       return new Material.Bulk(name, density, userDefined);
                        
                case SURFACE:
-                       return new Material.Surface(name, density);
+                       return new Material.Surface(name, density, userDefined);
                        
                case LINE:
-                       return new Material.Line(name, density);
+                       return new Material.Line(name, density, userDefined);
                        
                default:
-                       throw new IllegalArgumentException("Illegal material string: "+str);
+                       throw new IllegalArgumentException("Illegal material string: " + str);
                }
        }
-
+       
 }