Search in sources :

Example 1 with UnconvertibleException

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

Example 2 with UnconvertibleException

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

Example 3 with UnconvertibleException

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

Example 4 with UnconvertibleException

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);
}
Also used : AbstractUOMConverter(com.ibm.streamsx.health.prepare.uomconverter.converters.AbstractUOMConverter) UnconvertibleException(javax.measure.UnconvertibleException)

Example 5 with UnconvertibleException

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();
}
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)

Aggregations

UnconvertibleException (javax.measure.UnconvertibleException)6 IncommensurableException (javax.measure.IncommensurableException)4 Test (org.junit.Test)3 MeasurementException (javax.measure.MeasurementException)2 UnitConverter (javax.measure.UnitConverter)2 AbstractUOMConverter (com.ibm.streamsx.health.prepare.uomconverter.converters.AbstractUOMConverter)1 ParseException (gov.sandia.n2a.language.ParseException)1 Unit (javax.measure.Unit)1