I'm looking for a way in the JScience library to convert from one unit type to another. Given a factor between the base units of each types I should be able to convert. But it seems that JScience isn't being very nice about the conversion, and only allowing the conversion between units of the same base type.
Basically, I'm writing a diet app, and I need to be able to convert between calories, joules, kilojoules, grams, pounds, kg, etc. It is complicated by the macronutrient values - carbohyrates, protein, and fat content.
Example:
Each 1g of carb = 4 Calorie. That's 4000 calorie, or 16.736 Kilojoules, or 16736 joules (the base unit of energy).
Carb_Unit is then defined as: 16736 joules/gram, or 16736000joules/kilogram
Given this base values, I should be able to convert from any mass unit to any energy unit. But again, JScience won't allow this. Is there a way to tell the converter to return a converter with the correct conversion factor without it giving out the 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);
}