use of javax.measure.Dimension in project n2a by frothga.
the class ExportJob method printDimension.
public String printDimension(Unit<?> unit) {
Dimension dimension = unit.getDimension();
String result = dimensionsNML.get(dimension);
// Not a standard NML dimension
if (result == null)
result = dimension.toString();
return result;
}
use of javax.measure.Dimension in project n2a by frothga.
the class ExportJob method appendUnits.
@SuppressWarnings({ "rawtypes", "unchecked" })
public void appendUnits(boolean assumeNML) {
// Purge pure dimensions (added independent of specific unit) if they are standard and NML dimensions are available.
if (assumeNML) {
List<String> keys = new ArrayList<String>(dimensionsUsed.keySet());
for (String key : keys) {
Dimension d = dimensionsUsed.get(key);
if (dimensionsNML.containsKey(d))
dimensionsUsed.remove(key);
}
}
for (Entry<Unit<?>, String> e : unitsUsed.entrySet()) {
Unit<?> key = e.getKey();
if (assumeNML && unitsNML.containsKey(key))
continue;
String symbol = e.getValue();
Dimension dimension = key.getDimension();
String dimensionName = dimensionsNML.get(dimension);
if (// Not a standard NML dimension
dimensionName == null) {
dimensionName = dimension.toString();
dimensionsUsed.put(dimensionName, dimension);
} else if (// Is a standard NML dimension, so only add if don't have NML dimensions available.
!assumeNML) {
dimensionsUsed.put(dimensionName, dimension);
}
Element unit = addElement("Unit", elements);
unit.setAttribute("symbol", symbol);
unit.setAttribute("dimension", dimensionName);
// Determine offset
Unit systemUnit = key.getSystemUnit();
UnitConverter converter = key.getConverterTo(systemUnit);
double offset = converter.convert(Integer.valueOf(0)).doubleValue();
if (offset != 0)
unit.setAttribute("offset", String.valueOf(offset));
// Determine power*scale
double scale = converter.convert(Integer.valueOf(1)).doubleValue() - offset;
int power = (int) Math.round(Math.log(scale) / Math.log(10));
if (Math.abs(scale - Math.pow(10, power)) < epsilon) {
unit.setAttribute("power", String.valueOf(power));
} else {
unit.setAttribute("scale", String.valueOf(scale));
}
}
for (Entry<String, Dimension> d : dimensionsUsed.entrySet()) {
String name = d.getKey();
Dimension value = d.getValue();
Element dimension = addElement("Dimension", elements);
dimension.setAttribute("name", name);
Map<? extends Dimension, Integer> bases = value.getBaseDimensions();
if (bases == null) {
Map<Dimension, Integer> temp = new HashMap<Dimension, Integer>();
temp.put(value, 1);
bases = temp;
}
for (Entry<? extends Dimension, Integer> e : bases.entrySet()) {
String base = e.getKey().toString().substring(1, 2).toLowerCase();
if (base.equals("θ"))
base = "k";
dimension.setAttribute(base, e.getValue().toString());
}
}
}
use of javax.measure.Dimension in project uom-se by unitsofmeasurement.
the class ProductUnit method getDimension.
@Override
public Dimension getDimension() {
Dimension dimension = QuantityDimension.NONE;
for (int i = 0; i < this.getUnitCount(); i++) {
Unit<?> unit = this.getUnit(i);
if (this.elements != null && unit.getDimension() != null) {
Dimension d = unit.getDimension().pow(this.getUnitPow(i)).root(this.getUnitRoot(i));
dimension = dimension.multiply(d);
}
}
return dimension;
}
Aggregations