views:

329

answers:

3

Are there any open source libraries for representing cooking units such as Teaspoon and tablespoon in Java?

I have only found JSR-275 (http://jscience.org/jsr-275/) which is great but doesn't know about cooking units.

+1  A: 

I guess you can parse the wiki page for cooking measurements: Cooking Weights and Measures

It has all the measures organized in tables, so it should be pretty easy to parse them out.

CookieOfFortune
Boden
+10  A: 

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.

mtnygard
Thank you for this example, it helped a lot.
I82Much
+1  A: 

This might be of some use: JUnitConv. It's a Java applet for converting units (including cooking units), but it's GPL-licensed so you could download the source and adapt the relevant parts for your own use.

On the other hand, it looks like it shouldn't be hard to create a CookingUnits class compliant with JSR 275. (That's what I'd do)

David Zaslavsky