Browse Source

Fix a bug in Product's ingredient merge regarding collating preparation methods.

Sam Jaffe 5 năm trước cách đây
mục cha
commit
81d735d8c9

+ 8 - 1
src/main/lombok/org/leumasjaffe/recipe/model/Product.java

@@ -17,7 +17,7 @@ public class Product extends Observable.Instance implements CompoundRecipeCompon
 	@Override
 	public Stream<Ingredient> getIngredientsAsStream() {
 		return Collator.collateBy(cards.stream().flatMap(Card::getIngredientsAsStream),
-				Product::key, Ingredient::plus).stream();
+				Product::key, Product::mergeIngredients).stream();
 	}
 
 	@Override
@@ -28,4 +28,11 @@ public class Product extends Observable.Instance implements CompoundRecipeCompon
 	private static String key(Ingredient ingredient) {
 		return ingredient.getName() + ingredient.getAmount().getUnit();
 	}
+	
+	static Ingredient mergeIngredients(final Ingredient lhs, final Ingredient rhs) {
+		if (!lhs.getName().equals(rhs.getName())) {
+			throw new IllegalArgumentException("Combining ingredients of differing types");
+		}
+		return new Ingredient(lhs.getName(), "", lhs.getAmount().plus(rhs.getAmount()));
+	}
 }

+ 2 - 2
src/main/lombok/org/leumasjaffe/recipe/util/Collator.java

@@ -3,7 +3,7 @@ package org.leumasjaffe.recipe.util;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.function.BiFunction;
+import java.util.function.BinaryOperator;
 import java.util.function.Function;
 import java.util.stream.Stream;
 
@@ -12,7 +12,7 @@ import lombok.experimental.UtilityClass;
 @UtilityClass
 public class Collator {
 	public <T> Collection<T> collateBy(final Stream<T> stream, final Function<T, String> getKey,
-			final BiFunction<T, T, T> folder) {
+			final BinaryOperator<T> folder) {
 		final Map<String, T> map = new HashMap<>();
         stream.forEach(value -> {
         	final String key = getKey.apply(value);

+ 38 - 0
src/test/resources/example.json

@@ -59,6 +59,44 @@
               "maxSeconds": 30
             }
           }
+        },
+        {
+        	"id": 1,
+        	"dependsOn": [],
+        	"vessel": "",
+        	"cooking": [
+           	{
+          		"ingredients": [
+          			{
+          				"name": "oil",
+          				"amount": "1 Tbsp"
+          			}
+          		],
+	            "duration": {
+	              "displayAs": "SECONDS",
+	              "approximate": true,
+	              "minSeconds": 30,
+	              "maxSeconds": 60
+	            },
+          		"instruction": "Heat oil over high heat"
+          	},
+        		{
+        			"ingredients": [
+          			{
+          				"name": "onion",
+          				"preparation": "sliced",
+          				"amount": "100 g"
+          			}
+          		],
+	            "duration": {
+	              "displayAs": "MINUTES",
+	              "approximate": true,
+	              "minSeconds": 300,
+	              "maxSeconds": 900
+	            },
+          		"instruction": "Sauté the onions until soft and translucent, stirring to prevent burning"
+        		}
+        	]
         }
       ]
     }