Selaa lähdekoodia

Add back RecipeComponent exclusively for internal use (providing default functions).

Sam Jaffe 5 vuotta sitten
vanhempi
commit
4d01bdf63f

+ 9 - 12
src/main/lombok/org/leumasjaffe/recipe/model/Card.java

@@ -1,18 +1,14 @@
 package org.leumasjaffe.recipe.model;
 
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
-import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
-import com.fasterxml.jackson.annotation.JsonIgnore;
-
 import lombok.Data;
 
 @Data
-public class Card {
+public class Card implements CompoundRecipeComponent {
 	int id = 0; // TODO Fix this
 	int[] dependsOn = {}; // decltype(id)[]
 	String vessel = "";
@@ -20,14 +16,15 @@ public class Card {
 	List<Step> cooking = new ArrayList<>();
 	Optional<Rest> rest = Optional.empty();
 	
-	@JsonIgnore
-	Collection<Ingredient> getIngredients() {
-		return getIngredientsAsStream().collect(Collectors.toList());
+	public Stream<Ingredient> getIngredientsAsStream() {
+		return getComponents().flatMap(RecipeComponent::getIngredientsAsStream);
 	}
 	
-	@JsonIgnore
-	Stream<Ingredient> getIngredientsAsStream() {
-		return preparation.map(p -> p.getIngredients().stream())
-				.orElse(cooking.stream().flatMap(s -> s.getIngredients().stream()));
+	public Stream<? extends RecipeComponent> getComponents() {
+		if (preparation.isPresent()) {
+			return Stream.of(preparation.get());
+		} else {
+			return cooking.stream();
+		}
 	}
 }

+ 31 - 0
src/main/lombok/org/leumasjaffe/recipe/model/CompoundRecipeComponent.java

@@ -0,0 +1,31 @@
+package org.leumasjaffe.recipe.model;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.Stream;
+
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
+
+@JsonIgnoreProperties({"duration", "ingredients", "components", "ingredientsAsStream"})
+interface CompoundRecipeComponent extends RecipeComponent {
+	Stream<? extends RecipeComponent> getComponents();
+	Stream<Ingredient> getIngredientsAsStream();
+	
+	@Override
+	default Duration getDuration() {
+		return getComponents().map(RecipeComponent::getDuration)
+				.reduce(Duration.ZERO, Duration::plus);
+	}
+	
+	@Override
+	default Collection<Ingredient> getIngredients() {
+		final Map<String, Ingredient> map = new HashMap<>();
+        getIngredientsAsStream().forEach(i -> {
+        	final String key = i.key();
+            map.computeIfPresent(key, (k, v) -> v.plus(i));
+            map.computeIfAbsent(key, k -> i);
+        });
+        return map.values();
+	}
+}

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

@@ -17,4 +17,8 @@ public class Ingredient {
 		}
 		return new Ingredient(name, preparation, amount.plus(rhs.amount));
 	}
+	
+	String key() {
+		return getName() + getPreparation() + amount.getUnit().toString();
+	}
 }

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

@@ -6,7 +6,7 @@ import java.util.Set;
 import lombok.Data;
 
 @Data
-public class Preparation {
+public class Preparation implements RecipeComponent {
 	Set<Ingredient> ingredients = new HashSet<>();
 	Duration duration;
 }

+ 9 - 13
src/main/lombok/org/leumasjaffe/recipe/model/Product.java

@@ -1,26 +1,22 @@
 package org.leumasjaffe.recipe.model;
 
-import java.util.Collection;
 import java.util.List;
-import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
-import com.fasterxml.jackson.annotation.JsonIgnore;
-
 import lombok.Data;
 
 @Data
-public class Product {
+public class Product implements CompoundRecipeComponent {
 	String name;
 	List<Card> cards;
-	
-	@JsonIgnore
-	Collection<Ingredient> getIngredients() {
-		return getIngredientsAsStream().collect(Collectors.toList());
-	}
-	
-	@JsonIgnore
-	Stream<Ingredient> getIngredientsAsStream() {
+
+	@Override
+	public Stream<Ingredient> getIngredientsAsStream() {
 		return cards.stream().flatMap(Card::getIngredientsAsStream);
 	}
+
+	@Override
+	public Stream<? extends RecipeComponent> getComponents() {
+		return cards.stream();
+	}
 }

+ 11 - 9
src/main/lombok/org/leumasjaffe/recipe/model/Recipe.java

@@ -1,28 +1,30 @@
 package org.leumasjaffe.recipe.model;
 
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
-import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import javax.swing.ImageIcon;
 
-import com.fasterxml.jackson.annotation.JsonIgnore;
-
 import lombok.Data;
 
 @Data
-public class Recipe {
+public class Recipe implements CompoundRecipeComponent {
 	String title;
 	String description;
-	Object nutrition;
 	int servings;
+	// TODO: Nutrition information
 	Optional<ImageIcon> photo; // TODO JSONIZATION	
 	List<Product> products = new ArrayList<>();
 	
-	@JsonIgnore
-	Collection<Ingredient> getIngredients() {
-		return products.stream().flatMap(Product::getIngredientsAsStream).collect(Collectors.toList());
+	@Override
+	public Stream<? extends RecipeComponent> getComponents() {
+		return products.stream();
+	}
+	
+	@Override
+	public Stream<Ingredient> getIngredientsAsStream() {
+		return products.stream().flatMap(Product::getIngredientsAsStream);
 	}
 }

+ 13 - 0
src/main/lombok/org/leumasjaffe/recipe/model/RecipeComponent.java

@@ -0,0 +1,13 @@
+package org.leumasjaffe.recipe.model;
+
+import java.util.Collection;
+import java.util.stream.Stream;
+
+interface RecipeComponent {
+	Collection<Ingredient> getIngredients();
+	Duration getDuration();
+
+	default Stream<Ingredient> getIngredientsAsStream() {
+		return getIngredients().stream();
+	}
+}

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

@@ -6,7 +6,7 @@ import java.util.Set;
 import lombok.Data;
 
 @Data
-public class Step {
+public class Step implements RecipeComponent {
 	Set<Ingredient> ingredients = new HashSet<>();
 	Duration duration;
 	String instruction;

+ 1 - 3
src/test/resources/example.json

@@ -13,6 +13,7 @@
             "ingredients": [
               {
                 "name": "onion",
+                "preparation": "",
                 "amount": "100 g"
               }
             ],
@@ -21,9 +22,6 @@
               "isApproximate": true,
               "minSeconds": 300,
               "maxSeconds": 600
-            },
-            "instructions": {
-              
             }
           }
         }