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) {
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) {
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) {
69 public UnitGroup getUnitGroup() {
70 return UnitGroup.UNITS_DENSITY_BULK;
74 public Type getType() {
81 private final String name;
82 private final double density;
85 public Material(String name, double density) {
87 this.density = density;
92 public double getDensity() {
96 public String getName() {
100 public String getName(Unit u) {
101 return name + " (" + u.toStringUnit(density) + ")";
104 public abstract UnitGroup getUnitGroup();
105 public abstract Type getType();
108 public String toString() {
109 return getName(getUnitGroup().getDefaultUnit());
114 * Compares this object to another object. Material objects are equal if and only if
115 * their types, names and densities are identical.
118 public boolean equals(Object o) {
121 if (this.getClass() != o.getClass())
123 Material m = (Material)o;
124 return ((m.name.equals(this.name)) &&
125 MathUtil.equals(m.density, this.density));
130 * A hashCode() method giving a hash code compatible with the equals() method.
133 public int hashCode() {
134 return name.hashCode() + (int)(density*1000);
139 * Order the materials according to their name, secondarily according to density.
141 public int compareTo(Material o) {
142 int c = this.name.compareTo(o.name);
146 return (int)((this.density - o.density)*1000);
152 public static Material newMaterial(Type type, String name, double density) {
155 return new Material.Line(name, density);
158 return new Material.Surface(name, density);
161 return new Material.Bulk(name, density);
164 throw new IllegalArgumentException("Unknown material type: "+type);
169 public String toStorableString() {
170 return getType().name() + "|" + name.replace('|', ' ') + '|' + density;
173 public static Material fromStorableString(String str) {
174 String[] split = str.split("\\|",3);
175 if (split.length < 3)
176 throw new IllegalArgumentException("Illegal material string: "+str);
183 type = Type.valueOf(split[0]);
184 } catch (Exception e) {
185 throw new IllegalArgumentException("Illegal material string: "+str, e);
191 density = Double.parseDouble(split[2]);
192 } catch (NumberFormatException e) {
193 throw new IllegalArgumentException("Illegal material string: "+str, e);
198 return new Material.Bulk(name, density);
201 return new Material.Surface(name, density);
204 return new Material.Line(name, density);
207 throw new IllegalArgumentException("Illegal material string: "+str);