use of javax.measure.UnitConverter in project uom-se by unitsofmeasurement.
the class PrefixTest method testNestedOperationsNotTheSame.
@Test
public void testNestedOperationsNotTheSame() {
Unit<Mass> m1 = MICRO(GRAM);
Unit<Mass> m2 = GRAM.divide(1000).divide(2000);
UnitConverter c1 = m1.getConverterTo(m2);
List steps1 = c1.getConversionSteps();
UnitConverter c2 = m2.getConverterTo(m1);
List steps2 = c2.getConversionSteps();
assertNotEquals(c1, c2);
assertNotEquals(m1, m2);
}
use of javax.measure.UnitConverter in project uom-se by unitsofmeasurement.
the class UnitsTest method testConverterToSI.
/**
* Test method for {@link javax.measure.Unit#toMetric()}.
*/
// @Test
// public void testToMetric() {
// AbstractUnit<? extends QuantityAmount> su = (AbstractUnit<? extends
// QuantityAmount>) one.toMetric();
// assertTrue(su.isUnscaledMetric());
// }
/**
* Test method for {@link javax.measure.Unit#getConverterTo}.
*/
@Test
public void testConverterToSI() {
Double factor = 10.0;
UnitConverter converter = one.getConverterTo(one);
Double result = converter.convert(factor.doubleValue());
assertEquals(result, factor);
logger.log(Level.FINER, result.toString());
}
use of javax.measure.UnitConverter in project n2a by frothga.
the class ExportJob method morphologyUnits.
public String morphologyUnits(String value) {
value = value.trim();
int unitIndex = UnitValue.findUnits(value);
// no number
if (unitIndex == 0)
return "0" + value;
// no unit
if (unitIndex >= value.length())
return value;
String unitString = value.substring(unitIndex).trim();
value = value.substring(0, unitIndex);
Unit<?> unit = UCUM.parse(unitString);
double v = 0;
try {
v = Double.valueOf(value);
UnitConverter converter = unit.getConverterToAny(um);
v = converter.convert(v);
} catch (Exception error) {
}
unitsUsed.put(um, "um");
// no unit string at end of number, because morphology units are always in um
return print(v);
}
use of javax.measure.UnitConverter 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());
}
}
}
Aggregations