*/
public abstract class Material implements Comparable<Material> {
-
+
public enum Type {
LINE("Line", UnitGroup.UNITS_DENSITY_LINE),
SURFACE("Surface", UnitGroup.UNITS_DENSITY_SURFACE),
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;
public Line(String name, double density, boolean userDefined) {
super(name, density, userDefined);
}
-
+
@Override
public Type getType() {
return Type.LINE;
public Bulk(String name, double density, boolean userDefined) {
super(name, density, userDefined);
}
-
+
@Override
public Type getType() {
return Type.BULK;
}
-
+
private final String name;
private final double density;
private final boolean userDefined;
}
-
+
public double getDensity() {
return density;
}
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);
}
}
/**
* Return a new material of the specified type.
*/
- public static Material newMaterial(Type type, String name, double density,
+ public static Material newMaterial(Type type, String name, double density,
boolean userDefined) {
switch (type) {
case LINE:
return new Material.Bulk(name, density, userDefined);
default:
- throw new IllegalArgumentException("Unknown material type: "+type);
+ throw new IllegalArgumentException("Unknown material type: " + type);
}
}
if (str == null)
throw new IllegalArgumentException("Material string is null");
- String[] split = str.split("\\|",3);
+ 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) {
return new Material.Line(name, density, userDefined);
default:
- throw new IllegalArgumentException("Illegal material string: "+str);
+ throw new IllegalArgumentException("Illegal material string: " + str);
}
}
-
+
}