use of javax.measure.UnconvertibleException in project unit-api by unitsofmeasurement.
the class ExceptionsTest method testUnconvertibleException.
@Test(expected = UnconvertibleException.class)
public void testUnconvertibleException() {
UnconvertibleException e = new UnconvertibleException("error");
assertEquals("error", e.getMessage());
assertNull(e.getCause());
throw e;
}
use of javax.measure.UnconvertibleException in project unit-api by unitsofmeasurement.
the class ExceptionsTest method testUnconvertibleExceptionWithMessageAndCause.
@Test(expected = UnconvertibleException.class)
public void testUnconvertibleExceptionWithMessageAndCause() {
Exception cause = new IllegalStateException();
UnconvertibleException e = new UnconvertibleException("state error", cause);
assertEquals("state error", e.getMessage());
assertEquals(cause, e.getCause());
throw e;
}
use of javax.measure.UnconvertibleException in project unit-api by unitsofmeasurement.
the class TestUnit method getConverterTo.
public UnitConverter getConverterTo(Unit<Q> that) throws UnconvertibleException {
if ((this == that) || this.equals(that))
// Shortcut.
return TestConverter.IDENTITY;
Unit<Q> thisSystemUnit = this.getSystemUnit();
Unit<Q> thatSystemUnit = that.getSystemUnit();
if (!thisSystemUnit.equals(thatSystemUnit))
try {
return getConverterToAny(that);
} catch (IncommensurableException e) {
throw new UnconvertibleException(e);
}
UnitConverter thisToSI = this.getSystemConverter();
UnitConverter thatToSI = that.getConverterTo(thatSystemUnit);
return thatToSI.inverse().concatenate(thisToSI);
}
use of javax.measure.UnconvertibleException in project streamsx.health by IBMStreams.
the class ConverterFactory method convert.
public ConversionResult convert(double value, String inputUOM) throws UnconvertibleException {
AbstractUOMConverter converter = converterMap.get(inputUOM);
if (converter == null) {
throw new UnconvertibleException("No converter found for UOM: '" + inputUOM + "'");
}
double convertedValue = converter.convert(value);
String outputUOM = converter.getOutputUOM();
return new ConversionResult(inputUOM, outputUOM, convertedValue);
}
use of javax.measure.UnconvertibleException in project n2a by frothga.
the class ExportJob method biophysicalUnits.
public String biophysicalUnits(String value) {
value = value.trim();
int unitIndex = findUnits(value);
// no number or no units, so probably something else
if (unitIndex == 0 || unitIndex >= value.length())
return value;
String unitString = value.substring(unitIndex).trim();
String numberString = value.substring(0, unitIndex);
Unit<?> unit;
try {
unit = UCUM.parse(unitString);
} catch (Exception e) {
return value;
}
double v = 0;
try {
v = Double.valueOf(numberString);
} catch (NumberFormatException error) {
return value;
}
// Determine power in numberString itself
double power = 1;
if (v != 0)
power = Math.pow(10, Math.floor((Math.getExponent(v) + 1) / baseRatio));
// Find closest matching unit
Entry<Unit<?>, String> closest = null;
// like closest, but only ratios >= 1
Entry<Unit<?>, String> closestAbove = null;
double closestRatio = Double.POSITIVE_INFINITY;
double closestAboveRatio = Double.POSITIVE_INFINITY;
for (Entry<Unit<?>, String> e : nmlUnits.entrySet()) {
Unit<?> u = e.getKey();
if (u.isCompatible(unit)) {
try {
UnitConverter converter = unit.getConverterToAny(u);
double ratio = converter.convert(power);
if (ratio < 1) {
ratio = 1 / ratio;
} else {
if (ratio < closestAboveRatio) {
closestAboveRatio = ratio;
closestAbove = e;
}
}
if (ratio < closestRatio) {
closestRatio = ratio;
closest = e;
}
} catch (UnconvertibleException | IncommensurableException e1) {
}
}
}
if (closest == null) {
// completely give up on conversion
return value;
}
if (closestAboveRatio <= 1000 + epsilon)
closest = closestAbove;
try {
UnitConverter converter = unit.getConverterToAny(closest.getKey());
v = converter.convert(v);
} catch (Exception error) {
}
return print(v) + closest.getValue();
}
Aggregations