22

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.

mkobit
  • 43,979
  • 12
  • 156
  • 150
gregm
  • 12,019
  • 7
  • 56
  • 78

4 Answers4

14

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.

2

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 Z
  • 128,184
  • 27
  • 255
  • 279
2

I'm afraid, we can't help you with JSR-275 as it was rejected, but JSR-363 just went Final in September, so if you have a great idea for unit systems like "Cooking Units", please just let us know under UoM-Systems.

Werner Keil
  • 592
  • 5
  • 12
  • Looks like JSR-365 just got accepted, too. Is this already planned to be included? – mkobit Mar 08 '18 at 16:44
  • 1
    385 is the next version of JSR 363. The best place for those kinds of domain-specific units would be https://github.com/unitsofmeasurement/uom-domain. Please feel free to create a ticket as new feature request. – Werner Keil Mar 10 '18 at 22:22
1

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
  • 13,836
  • 8
  • 42
  • 58