Explorar o código

Move preparation into Ingredient, allow repeating ingredients with different preparations.

Sam Jaffe %!s(int64=5) %!d(string=hai) anos
pai
achega
aa56816a08

+ 12 - 18
src/main/lombok/org/leumasjaffe/recipe/model/Amount.java

@@ -39,11 +39,10 @@ public class Amount {
 		COUNT, WEIGHT, VOLUME;
 	}
 	
-	Unit type;
+	Unit unit;
 	double value;
 	Volume vol;
 	Weight wgt;
-	String unit;
 	
 	@JsonCreator
 	public Amount(final String serial) {
@@ -61,50 +60,45 @@ public class Amount {
 			wgt = rw.get();
 			return;
 		}
-		unit = tokens[1];
 	}
 	
 	@JsonValue @Override
 	public String toString() {
 		StringBuilder build = new StringBuilder();
 		build.append(value);
-		build.append(' ');
-		build.append(unitName());
+		if (unit != Unit.COUNT) {
+			build.append(' ');
+			build.append(unitName());
+		}
 		return build.toString();
 	}
 	
 	private String unitName() {
-		switch (type) {
-		case COUNT:
-			return unit;
+		switch (unit) {
 		case WEIGHT:
 			return wgt.displayName;
 		case VOLUME:
 			return vol.displayName;
 		default:
-			throw new IllegalStateException("Unknown enum value: " + type);
+			return "";
 		}
 	}
 
 	public Amount plus(final Amount amount) {
-		if (type != amount.type) {
-			throw new IllegalArgumentException("Cannot merge mass and volume together");
-		} else if (!unit.equals(amount.unit)) {
-			throw new IllegalArgumentException("Cannot merge objects with different units");
+		if (unit != amount.unit) {
+			throw new IllegalArgumentException("Cannot merge mass/volume/count amounts together");
 		}
-		return new Amount(type, value + amount.value * scale(amount), vol, wgt, unit);
+		return new Amount(unit, value + amount.value * scale(amount), vol, wgt);
 	}
 
 	private double scale(final Amount amount) {
-		switch (type) {
-		case COUNT:
-			return 1.0;
+		switch (unit) {
 		case WEIGHT:
 			return amount.wgt.atomicUnits / wgt.atomicUnits;
 		case VOLUME:
 			return amount.vol.atomicUnits / vol.atomicUnits;
 		default:
-			throw new IllegalStateException("Unknown enum value: " + type);
+			return 1.0;
 		}
 	}
 }

+ 4 - 3
src/main/lombok/org/leumasjaffe/recipe/model/Ingredient.java

@@ -6,14 +6,15 @@ import lombok.Data;
 @Data @AllArgsConstructor
 public class Ingredient {
 	String name;
+	String preparation;
 	Amount amount;
 	
 	Ingredient plus(final Ingredient rhs) {
 		if (!name.equals(rhs.name)) {
 			throw new IllegalArgumentException("Combining ingredients of differing types");
-		} else if (amount.type != rhs.amount.type) {
-			throw new IllegalArgumentException("Cannot merge mass and volume together");
+		} else if (!preparation.equals(rhs.preparation)) {
+			throw new IllegalArgumentException("Cannot combine items with different preparations");
 		}
-		return new Ingredient(name, amount.plus(rhs.amount));
+		return new Ingredient(name, preparation, amount.plus(rhs.amount));
 	}
 }

+ 0 - 3
src/main/lombok/org/leumasjaffe/recipe/model/Preparation.java

@@ -1,8 +1,6 @@
 package org.leumasjaffe.recipe.model;
 
-import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Map;
 import java.util.Set;
 
 import lombok.Data;
@@ -10,6 +8,5 @@ import lombok.Data;
 @Data
 public class Preparation {
 	Set<Ingredient> ingredients = new HashSet<>();
-	Map<String, String> instructions = new HashMap<>();
 	Duration duration;
 }