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 that is not user-defined.
160 public static Material newMaterial(Type type, String name, double density) {
161 return newMaterial(type, name, density, false);
165 * Return a new material of the specified type.
167 public static Material newMaterial(Type type, String name, double density,
168 boolean userDefined) {
171 return new Material.Line(name, density, userDefined);
174 return new Material.Surface(name, density, userDefined);
177 return new Material.Bulk(name, density, userDefined);
180 throw new IllegalArgumentException("Unknown material type: "+type);
185 public String toStorableString() {
186 return getType().name() + "|" + name.replace('|', ' ') + '|' + density;
189 public static Material fromStorableString(String str, boolean userDefined) {
190 String[] split = str.split("\\|",3);
191 if (split.length < 3)
192 throw new IllegalArgumentException("Illegal material string: "+str);
199 type = Type.valueOf(split[0]);
200 } catch (Exception e) {
201 throw new IllegalArgumentException("Illegal material string: "+str, e);
207 density = Double.parseDouble(split[2]);
208 } catch (NumberFormatException e) {
209 throw new IllegalArgumentException("Illegal material string: "+str, e);
214 return new Material.Bulk(name, density, userDefined);
217 return new Material.Surface(name, density, userDefined);
220 return new Material.Line(name, density, userDefined);
223 throw new IllegalArgumentException("Illegal material string: "+str);