use of javax.measure.UnitConverter in project com.revolsys.open by revolsys.
the class UnitConverstionOperation method perform.
@Override
public void perform(final int sourceAxisCount, final double[] sourceCoordinates, final int targetAxisCount, final double[] targetCoordinates) {
final int numPoints = sourceCoordinates.length / sourceAxisCount;
final int axisCount = this.axisCount;
final UnitConverter converter = this.converter;
for (int vertexIndex = 0; vertexIndex < numPoints; vertexIndex++) {
for (int axisIndex = 0; axisIndex < targetAxisCount; axisIndex++) {
double value;
if (axisIndex < sourceAxisCount) {
value = sourceCoordinates[vertexIndex * sourceAxisCount + axisIndex];
if (axisIndex < axisCount) {
value = converter.convert(value);
}
} else {
value = Double.NaN;
}
targetCoordinates[vertexIndex * targetAxisCount + axisIndex] = value;
}
}
}
use of javax.measure.UnitConverter in project smarthome by eclipse.
the class QuantityType method toUnit.
/**
* Convert this QuantityType to a new {@link QuantityType} using the given target unit.
*
* @param targetUnit the unit to which this {@link QuantityType} will be converted to.
* @return the new {@link QuantityType} in the given {@link Unit} or {@code null} in case of a
*/
@SuppressWarnings("unchecked")
@Nullable
public QuantityType<T> toUnit(Unit<?> targetUnit) {
if (!targetUnit.equals(getUnit())) {
try {
UnitConverter uc = getUnit().getConverterToAny(targetUnit);
Quantity<?> result = Quantities.getQuantity(uc.convert(quantity.getValue()), targetUnit);
return new QuantityType<T>(result.getValue(), (Unit<T>) targetUnit);
} catch (UnconvertibleException | IncommensurableException e) {
logger.debug("Unable to convert unit from {} to {}", getUnit(), targetUnit);
return null;
}
}
return this;
}
use of javax.measure.UnitConverter in project indriya by unitsofmeasurement.
the class UnitConverterTest method testDouble.
@Test
public void testDouble() {
UnitConverter converter = sourceUnit.getConverterTo(targetUnit);
double length1 = 4.0;
double length2 = 6.0;
double result1 = converter.convert(length1);
double result2 = converter.convert(length2);
assertEquals(400, result1);
assertEquals(600, result2);
}
use of javax.measure.UnitConverter in project indriya by unitsofmeasurement.
the class TemporalQuantity method divide.
@Override
public ComparableQuantity<?> divide(Quantity<?> that) {
if (getUnit().equals(that.getUnit())) {
return TimeQuantities.getQuantity(value / that.getValue().intValue(), timeUnit);
}
Unit<?> divUnit = getUnit().divide(that.getUnit());
UnitConverter conv;
try {
conv = getUnit().getConverterToAny(divUnit);
return TimeQuantities.getQuantity(value / conv.convert(that.getValue()).intValue(), timeUnit);
} catch (UnconvertibleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return TimeQuantities.getQuantity(value / that.getValue().intValue(), timeUnit);
} catch (IncommensurableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return TimeQuantities.getQuantity(value / that.getValue().intValue(), timeUnit);
}
}
use of javax.measure.UnitConverter in project indriya by unitsofmeasurement.
the class ProductUnit method getSystemConverter.
@Override
public UnitConverter getSystemConverter() {
UnitConverter converter = AbstractConverter.IDENTITY;
for (Element e : elements) {
if (e.unit instanceof AbstractUnit) {
UnitConverter cvtr = ((AbstractUnit) e.unit).getSystemConverter();
if (!(cvtr.isLinear()))
throw new UnsupportedOperationException(e.unit + " is non-linear, cannot convert");
if (e.root != 1)
throw new UnsupportedOperationException(e.unit + " holds a base unit with fractional exponent");
int pow = e.pow;
if (pow < 0) {
// Negative power.
pow = -pow;
cvtr = cvtr.inverse();
}
for (int j = 0; j < pow; j++) {
converter = converter.concatenate(cvtr);
}
}
}
return converter;
}
Aggregations