Search in sources :

Example 1 with AbstractUOMConverter

use of com.ibm.streamsx.health.prepare.uomconverter.converters.AbstractUOMConverter in project streamsx.health by IBMStreams.

the class ConverterFactory method createConverter.

public AbstractUOMConverter createConverter(String inputUOM, String outputUOM) throws NoConverterFoundException {
    for (Class<? extends AbstractUOMConverter> converterClass : supportedConverters) {
        SupportedUOM supportedUoms = converterClass.getAnnotation(SupportedUOM.class);
        List<String> inputUOMList = Arrays.asList(supportedUoms.inputUOM());
        List<String> outputUOMList = Arrays.asList(supportedUoms.outputUOM());
        if (inputUOMList.contains(inputUOM) && outputUOMList.contains(outputUOM)) {
            try {
                Constructor<? extends AbstractUOMConverter> constructor = converterClass.getConstructor(String.class, String.class);
                AbstractUOMConverter converter = constructor.newInstance(inputUOM, outputUOM);
                registerConverter(converter);
                return converter;
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
        }
    }
    throw new NoConverterFoundException(inputUOM, outputUOM);
}
Also used : AbstractUOMConverter(com.ibm.streamsx.health.prepare.uomconverter.converters.AbstractUOMConverter) ObjectStreamException(java.io.ObjectStreamException) UnconvertibleException(javax.measure.UnconvertibleException)

Example 2 with AbstractUOMConverter

use of com.ibm.streamsx.health.prepare.uomconverter.converters.AbstractUOMConverter 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)

Aggregations

AbstractUOMConverter (com.ibm.streamsx.health.prepare.uomconverter.converters.AbstractUOMConverter)2 UnconvertibleException (javax.measure.UnconvertibleException)2 ObjectStreamException (java.io.ObjectStreamException)1