use of javax.measure.IncommensurableException in project unit-api by unitsofmeasurement.
the class ExceptionsTest method testIncommensurableException.
@Test
public void testIncommensurableException() {
IncommensurableException ie = new IncommensurableException("error");
assertEquals("error", ie.getMessage());
assertNull(ie.getCause());
}
use of javax.measure.IncommensurableException 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.IncommensurableException 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();
}
use of javax.measure.IncommensurableException in project unit-api by unitsofmeasurement.
the class ExceptionsTest method testIncommensurableExceptionWithCause.
@Test
public void testIncommensurableExceptionWithCause() {
Exception cause = new IllegalArgumentException();
IncommensurableException ie = new IncommensurableException(cause);
assertEquals(cause, ie.getCause());
}
use of javax.measure.IncommensurableException in project unit-api by unitsofmeasurement.
the class ExceptionsTest method testIncommensurableExceptionWithMessageAndCause.
@Test
public void testIncommensurableExceptionWithMessageAndCause() {
Exception cause = new IllegalArgumentException();
IncommensurableException ie = new IncommensurableException("yet another error", cause);
assertEquals("yet another error", ie.getMessage());
assertEquals(cause, ie.getCause());
}
Aggregations