*/
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;
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;
}
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;
}
-
+
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;
}
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.
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);
}
}
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;
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);
}
}
-
+
}