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> {
25 public static class Line extends Material {
26 public Line(String name, double density, boolean userDefined) {
27 super(name, density, userDefined);
31 public UnitGroup getUnitGroup() {
32 return UnitGroup.UNITS_DENSITY_LINE;
36 public Type getType() {
41 public static class Surface extends Material {
43 public Surface(String name, double density, boolean userDefined) {
44 super(name, density, userDefined);
48 public UnitGroup getUnitGroup() {
49 return UnitGroup.UNITS_DENSITY_SURFACE;
53 public Type getType() {
58 public String toStorableString() {
59 return super.toStorableString();
63 public static class Bulk extends Material {
64 public Bulk(String name, double density, boolean userDefined) {
65 super(name, density, userDefined);
69 public UnitGroup getUnitGroup() {
70 return UnitGroup.UNITS_DENSITY_BULK;
74 public Type getType() {
81 private final String name;
82 private final double density;
83 private final boolean userDefined;
86 public Material(String name, double density, boolean userDefined) {
88 this.density = density;
89 this.userDefined = userDefined;
94 public double getDensity() {
98 public String getName() {
102 public String getName(Unit u) {
103 return name + " (" + u.toStringUnit(density) + ")";
106 public boolean isUserDefined() {
110 public abstract UnitGroup getUnitGroup();
111 public abstract Type getType();
114 public String toString() {
115 return getName(getUnitGroup().getDefaultUnit());
120 * Compares this object to another object. Material objects are equal if and only if
121 * their types, names and densities are identical.
124 public boolean equals(Object o) {
127 if (this.getClass() != o.getClass())
129 Material m = (Material)o;
130 return ((m.name.equals(this.name)) &&
131 MathUtil.equals(m.density, this.density));
136 * A hashCode() method giving a hash code compatible with the equals() method.
139 public int hashCode() {
140 return name.hashCode() + (int)(density*1000);
145 * Order the materials according to their name, secondarily according to density.
147 public int compareTo(Material o) {
148 int c = this.name.compareTo(o.name);
152 return (int)((this.density - o.density)*1000);
158 * Return a new material of the specified type.
160 public static Material newMaterial(Type type, String name, double density,
161 boolean userDefined) {
164 return new Material.Line(name, density, userDefined);
167 return new Material.Surface(name, density, userDefined);
170 return new Material.Bulk(name, density, userDefined);
173 throw new IllegalArgumentException("Unknown material type: "+type);
178 public String toStorableString() {
179 return getType().name() + "|" + name.replace('|', ' ') + '|' + density;
184 * Return a material defined by the provided string.
186 * @param str the material storage string.
187 * @param userDefined whether the created material is user-defined.
188 * @return a new <code>Material</code> object.
189 * @throws IllegalArgumentException if <code>str</code> is invalid or null.
191 public static Material fromStorableString(String str, boolean userDefined) {
193 throw new IllegalArgumentException("Material string is null");
195 String[] split = str.split("\\|",3);
196 if (split.length < 3)
197 throw new IllegalArgumentException("Illegal material string: "+str);
204 type = Type.valueOf(split[0]);
205 } catch (Exception e) {
206 throw new IllegalArgumentException("Illegal material string: "+str, e);
212 density = Double.parseDouble(split[2]);
213 } catch (NumberFormatException e) {
214 throw new IllegalArgumentException("Illegal material string: "+str, e);
219 return new Material.Bulk(name, density, userDefined);
222 return new Material.Surface(name, density, userDefined);
225 return new Material.Line(name, density, userDefined);
228 throw new IllegalArgumentException("Illegal material string: "+str);