在JScience库中转换不同的单元类型

时间:2017-06-07 08:25:51

标签: java jscience

我正在寻找JScience库中的一种方式,从一种单位类型转换为另一种单位类型。鉴于每种类型的基本单位之间的因素我应该能够转换。但似乎JScience对转换并不是很好,只允许在相同基类型的单元之间进行转换。

基本上,我正在写一个饮食应用程序,我需要能够在卡路里,焦耳,千焦耳,克,磅,公斤等之间进行转换。它的常量营养素值很复杂 - 碳水化合物,蛋白质和脂肪内容。

示例:

每1克碳水化合物= 4卡路里。这是4000卡路里,或16.736千焦耳,或16736焦耳(基本能量单位)。

Carb_Unit定义为:16736焦耳/克,或16736000焦耳/千克

鉴于此基本值,我应该能够从任何质量单位转换为任何能量单位。但同样,JScience不会允许这样做。有没有办法告诉转换器返回具有正确转换因子的转换器而不发出ConversionException?

CustomUnits.java:

public class CustomUnits extends SystemOfUnits {

    /**
     * Holds collection of CustomUnits.
     */
    private static HashSet<Unit<?>> UNITS = new HashSet<Unit<?>>();

    private static <U extends Unit<?>> U customUnits(U unit) {
        UNITS.add(unit);
        return unit;
    }

    @Override
    public Set<Unit<?>> getUnits() {
        return Collections.unmodifiableSet(UNITS);
    }

    public static final Unit<Energy> KILOCALORIE = customUnits(SI.JOULE.times(4184));
    public static final Unit<Energy> KILOJOULE = customUnits(SI.JOULE.times(1000));

    // Food units expressed as energy
    public static final Unit<Energy> CARBOHYDRATE_ENERGY = customUnits(KILOCALORIE.times(4));
    public static final Unit<Energy> PROTEIN_ENERGY = customUnits(KILOCALORIE.times(4));
    public static final Unit<Energy> FAT_ENERGY = customUnits(KILOCALORIE.times(9));

    // Food units expressed as mass
    public static final Unit<Mass> CARBOHYDRATE_MASS = customUnits(SI.GRAM);
    public static final Unit<Mass> PROTEIN_MASS = customUnits(SI.GRAM);
    public static final Unit<Mass> FAT_MASS = customUnits(SI.GRAM);
}

Main.java:

public static void main(String[] args) {

    Amount<?> carbEnergyUnit = Amount.valueOf(1, CustomUnits.CARBOHYDRATE_ENERGY);
    Amount<?> carbEnergyCalorie = carbEnergyUnit.to(CustomUnits.KILOCALORIE);
    Amount<?> carbEnergyKJ = carbEnergyUnit.to(CustomUnits.KILOJOULE);
    Amount<?> carbEnergyJoules = carbEnergyUnit.to(SI.JOULE);

    System.out.println(carbEnergyUnit.getExactValue() + "g of carb");       // 1g of carb
    System.out.println(carbEnergyCalorie.getExactValue() + " Calorie");     // 4 Calorie
    System.out.println(carbEnergyKJ.getEstimatedValue() + " KiloJoules");   // 16.735999999999997 KiloJoules
    System.out.println(carbEnergyJoules.getExactValue() + " Joules");       // 16736 Joules

    // Exception in thread "main" javax.measure.converter.ConversionException: lb is not compatible with J*16736
    UnitConverter toCarb = NonSI.POUND.getConverterTo(CustomUnits.CARBOHYDRATE_ENERGY);
    double result = toCarb.convert(4);
    System.out.println(result);
}

1 个答案:

答案 0 :(得分:2)

JScience似乎已经抓住您尝试将Unit<Mass>转换为Unit<Energy>,这在默认PhysicalModel中是被禁止的。

另一种方法是为各种Quantity单位创建新的FoodEnergy

public static final Unit<Energy> KILOCALORIE = SI.JOULE.times(4184);

public interface FoodEnergy extends Quantity {

    public final static Unit<FoodEnergy> UNIT
        = (Unit<FoodEnergy>) SI.GRAM.times(KILOCALORIE);
}

private static final Unit<FoodEnergy> PROTEIN_ENERGY = FoodEnergy.UNIT.times(4);
private static final Unit<FoodEnergy> ETHANOL_ENERGY = FoodEnergy.UNIT.times(7);
…

然后,您可以结合特定能源的贡献:

Amount<FoodEnergy> beer =
    Amount.valueOf(2, PROTEIN_ENERGY).plus(
    Amount.valueOf(14, ETHANOL_ENERGY));
System.out.println(beer.to(FoodEnergy.UNIT).getEstimatedValue() + " Calories");

打印105.99999999999997 Calories。通过将NonSI.POUND转换为SI.GRAM,您可以找到一磅蛋白质中的卡路里:

double grams = NonSI.POUND.getConverterTo(SI.GRAM).convert(1);
Amount<FoodEnergy> pound = Amount.valueOf(grams, PROTEIN_ENERGY);
System.out.println(pound.to(FoodEnergy.UNIT).getEstimatedValue() + " Calories");

打印1814.3694799999998 Calories。最后,您可以从FoodEnergy.UNIT

中恢复焦耳数
System.out.println(FoodEnergy.UNIT.divide(SI.GRAM));

打印J*4184

System.out.println(FoodEnergy.UNIT.divide(SI.GRAM).toStandardUnit().convert(1));

打印4184.0