[英]Converting different unit types in JScience library
我正在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);
}
看起來JScience
已經抓住您嘗試將Unit<Mass>
轉換為Unit<Energy>
,這在默認的PhysicalModel
是禁止的。
一種替代方法是為FoodEnergy
各個單位創建新的Quantity
:
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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.