1 package net.sf.openrocket.material;
3 import net.sf.openrocket.unit.Unit;
4 import net.sf.openrocket.unit.UnitGroup;
5 import net.sf.openrocket.util.MathUtil;
8 * A class for different material types. Each material has a name and density.
9 * The interpretation of the density depends on the material type. For
10 * {@link Type#BULK} it is kg/m^3, for {@link Type#SURFACE} km/m^2.
12 * Objects of this type are immutable.
14 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
17 public abstract class Material implements Comparable<Material> {
20 LINE("Line", UnitGroup.UNITS_DENSITY_LINE),
21 SURFACE("Surface", UnitGroup.UNITS_DENSITY_SURFACE),
22 BULK("Bulk", UnitGroup.UNITS_DENSITY_BULK);
24 private final String name;
25 private final UnitGroup units;
26 private Type(String name, UnitGroup units) {
30 public UnitGroup getUnitGroup() {
34 public String toString() {
40 ///// Definitions of different material types /////
42 public static class Line extends Material {
43 public Line(String name, double density, boolean userDefined) {
44 super(name, density, userDefined);
48 public Type getType() {
53 public static class Surface extends Material {
55 public Surface(String name, double density, boolean userDefined) {
56 super(name, density, userDefined);
60 public Type getType() {
65 public String toStorableString() {
66 return super.toStorableString();
70 public static class Bulk extends Material {
71 public Bulk(String name, double density, boolean userDefined) {
72 super(name, density, userDefined);
76 public Type getType() {
83 private final String name;
84 private final double density;
85 private final boolean userDefined;
88 public Material(String name, double density, boolean userDefined) {
90 this.density = density;
91 this.userDefined = userDefined;
96 public double getDensity() {
100 public String getName() {
104 public String getName(Unit u) {
105 return name + " (" + u.toStringUnit(density) + ")";
108 public boolean isUserDefined() {
112 public abstract Type getType();
115 public String toString() {
116 return this.getName(this.getType().getUnitGroup().getDefaultUnit());
121 * Compares this object to another object. Material objects are equal if and only if
122 * their types, names and densities are identical.
125 public boolean equals(Object o) {
128 if (this.getClass() != o.getClass())
130 Material m = (Material)o;
131 return ((m.name.equals(this.name)) &&
132 MathUtil.equals(m.density, this.density));
137 * A hashCode() method giving a hash code compatible with the equals() method.
140 public int hashCode() {
141 return name.hashCode() + (int)(density*1000);
146 * Order the materials according to their name, secondarily according to density.
148 public int compareTo(Material o) {
149 int c = this.name.compareTo(o.name);
153 return (int)((this.density - o.density)*1000);
159 * Return a new material of the specified type.
161 public static Material newMaterial(Type type, String name, double density,
162 boolean userDefined) {
165 return new Material.Line(name, density, userDefined);
168 return new Material.Surface(name, density, userDefined);
171 return new Material.Bulk(name, density, userDefined);
174 throw new IllegalArgumentException("Unknown material type: "+type);
179 public String toStorableString() {
180 return getType().name() + "|" + name.replace('|', ' ') + '|' + density;
185 * Return a material defined by the provided string.
187 * @param str the material storage string.
188 * @param userDefined whether the created material is user-defined.
189 * @return a new <code>Material</code> object.
190 * @throws IllegalArgumentException if <code>str</code> is invalid or null.
192 public static Material fromStorableString(String str, boolean userDefined) {
194 throw new IllegalArgumentException("Material string is null");
196 String[] split = str.split("\\|",3);
197 if (split.length < 3)
198 throw new IllegalArgumentException("Illegal material string: "+str);
205 type = Type.valueOf(split[0]);
206 } catch (Exception e) {
207 throw new IllegalArgumentException("Illegal material string: "+str, e);
213 density = Double.parseDouble(split[2]);
214 } catch (NumberFormatException e) {
215 throw new IllegalArgumentException("Illegal material string: "+str, e);
220 return new Material.Bulk(name, density, userDefined);
223 return new Material.Surface(name, density, userDefined);
226 return new Material.Line(name, density, userDefined);
229 throw new IllegalArgumentException("Illegal material string: "+str);