package com.mousetech.gourmetj; import com.mousetech.gourmetj.utils.IngredientDigester; import com.mousetech.gourmetj.utils.IngredientDigester.IngredientAmountFormat; public class ShopIngredient implements Comparable { /** * Constructor. * * @param shopCat * @param ingkey */ public ShopIngredient( String shopCat, String item, String ingkey) { this.shopCat = shopCat; this.ingkey = ingkey; } /** * Constructor. * * @param amount * @param unit * @param item * @param ingkey * @param shopCat */ public ShopIngredient( double amount, String unit, String item, String ingkey, String shopCat) { this.amount = amount; this.unit = unit; this.item = item; this.ingkey = ingkey; this.shopCat = shopCat; } private String shopCat; /** * @return the shopCat */ public String getShopCat() { return shopCat; } /** * @param shopCat the shopCat to set */ public void setShopCat(String shopCat) { this.shopCat = shopCat; } /** * @return the ingkey */ public String getIngkey() { return ingkey; } /** * @param ingkey the ingkey to set */ public void setIngkey(String ingkey) { this.ingkey = ingkey; } /** * @return the amount */ public double getAmount() { return amount; } /** * @param amount the amount to set */ public void setAmount(double amount) { this.amount = amount; } /** * @return the displayAmount */ public String getDisplayAmount() { return IngredientDigester.displayAmount( IngredientAmountFormat.IA_TEXT, this.getAmount(), null); } /** * @return the unit */ public String getUnit() { return unit; } /** * @param unit the unit to set */ public void setUnit(String unit) { this.unit = unit; } private String item; /** * @return the item */ public String getItem() { return item; } private String ingkey; private double amount; private String displayAmount; private String unit; @Override public int compareTo(Object o) { if ((o == null) || !(o instanceof ShopIngredient)) { throw new RuntimeException( "Invalid shipIngredient comparison"); } ShopIngredient o1 = (ShopIngredient) o; int i = relate(this.getItem(), o1.getItem()); if (i != 0) { return i; } i = relate(this.getShopCat(), o1.getShopCat()); if (i != 0) { return i; } // TODO: normalize case, singular/plural/abbreviations i = relate(this.getUnit(), o1.getUnit()); if (i != 0) { return i; } return i; // ZERO } private int relate(String item2, String item3) { if ((item2 == null) && (item3 == null)) { return 0; } if (item2 == null) { return -1; } if (item3 == null) { return 1; } return item2.compareTo(item3); } @Override public String toString() { return this.getAmount() + " " + this.getUnit() + " " + this.getItem() + " " + this.getIngkey() + " " + this.getShopCat(); } }