Search in sources :

Example 1 with IncommensurableException

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());
}
Also used : IncommensurableException(javax.measure.IncommensurableException) Test(org.junit.Test)

Example 2 with IncommensurableException

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);
}
Also used : IncommensurableException(javax.measure.IncommensurableException) UnitConverter(javax.measure.UnitConverter) UnconvertibleException(javax.measure.UnconvertibleException)

Example 3 with IncommensurableException

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();
}
Also used : IncommensurableException(javax.measure.IncommensurableException) UnitConverter(javax.measure.UnitConverter) UnconvertibleException(javax.measure.UnconvertibleException) Unit(javax.measure.Unit) IncommensurableException(javax.measure.IncommensurableException) UnconvertibleException(javax.measure.UnconvertibleException) ParseException(gov.sandia.n2a.language.ParseException)

Example 4 with IncommensurableException

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());
}
Also used : IncommensurableException(javax.measure.IncommensurableException) IncommensurableException(javax.measure.IncommensurableException) MeasurementException(javax.measure.MeasurementException) UnconvertibleException(javax.measure.UnconvertibleException) Test(org.junit.Test)

Example 5 with IncommensurableException

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());
}
Also used : IncommensurableException(javax.measure.IncommensurableException) IncommensurableException(javax.measure.IncommensurableException) MeasurementException(javax.measure.MeasurementException) UnconvertibleException(javax.measure.UnconvertibleException) Test(org.junit.Test)

Aggregations

IncommensurableException (javax.measure.IncommensurableException)6 UnconvertibleException (javax.measure.UnconvertibleException)4 UnitConverter (javax.measure.UnitConverter)3 Test (org.junit.Test)3 MeasurementException (javax.measure.MeasurementException)2 Unit (javax.measure.Unit)2 ParseException (gov.sandia.n2a.language.ParseException)1 BaseUnit (javax.measure.test.unit.BaseUnit)1