Cooking Units in Java

java, units-of-measurement

Solution

JScience is extensible, so you should be able to create a subclass of javax.measure.unit.SystemOfUnits. You'll create a number of public static final declarations like this:

public final class Cooking extends SystemOfUnits {
  private static HashSet<Unit<?>> UNITS = new HashSet<Unit<?>>();

  private Cooking() {
  }

  public static Cooking getInstance() {
    return INSTANCE;
  }
  private static final Cooking INSTANCE = new SI();

  public static final BaseUnit<CookingVolume> TABLESPOON = si(new BaseUnit<CookingVolume>("Tbsp"));

  ...

   public static final Unit<CookingVolume> GRAM = TABLESPOON.divide(1000);

}

public interface CookingVolume extends Quantity {
  public final static Unit<CookingVolume> UNIT = Cooking.TABLESPOON;
}

It's pretty straightforward to define the other units and conversions, just as long as you know what the conversion factors are.

Problem

Are there any open source libraries for representing cooking units such as Teaspoon and tablespoon in Java? I have only found JSR-275 (https://jcp.org/en/jsr/detail?id=275) which is great but doesn't know about cooking units.

Original source