Przeglądaj źródła

Change the model to follow a significantly simpler system that doesn't depend on implicitly recursive polymorphism and type inspection.

Sam Jaffe 5 lat temu
rodzic
commit
3070b2f15d

+ 33 - 0
src/main/lombok/org/leumasjaffe/recipe/model/Card.java

@@ -0,0 +1,33 @@
+package org.leumasjaffe.recipe.model;
+
+import java.util.Collection;
+import java.util.Collections;
+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 {
+	int id; // TODO Fix this
+	int[] dependsOn; // decltype(id)[]
+	String vessel;
+	Optional<Step> preparation;
+	List<Step> cooking;
+	Optional<Rest> rest;
+	
+	@JsonIgnore
+	Collection<Ingredient> getIngredients() {
+		return getIngredientsAsStream().collect(Collectors.toList());
+	}
+	
+	@JsonIgnore
+	Stream<Ingredient> getIngredientsAsStream() {
+		return preparation.map(Collections::singletonList).orElse(cooking).stream()
+				.flatMap(s -> s.getIngredients().stream());
+	}
+}

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

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

+ 0 - 52
src/main/lombok/org/leumasjaffe/recipe/model/Product.java

@@ -1,52 +0,0 @@
-package org.leumasjaffe.recipe.model;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.Optional;
-
-import org.leumasjaffe.container.Pair;
-
-import lombok.Data;
-
-@Data
-public class Product implements CompoundRecipeComponent {
-	@Data
-	public static class Phase implements CompoundRecipeComponent {
-		List<Step> steps = new ArrayList<>();
-
-		@SuppressWarnings("unchecked")
-		@Override
-		public Collection<RecipeComponent> getComponents() {
-			return (Collection<RecipeComponent>)(List<?>) steps;
-		}
-	}
-	
-	@Data
-	public static class RecipePhase {
-		List<Pair<Phase, Rest>> additionalPhases = new ArrayList<>();
-		Phase phase;
-		Optional<Rest> rest = Optional.empty();
-		
-		void addComponents(Collection<RecipeComponent> comps) {
-			additionalPhases.forEach(pair -> {
-				comps.add(pair.getLeft());
-				comps.add(pair.getRight());
-			});
-			comps.add(phase);
-			rest.ifPresent(comps::add);
-		}
-	}
-	
-	String name;
-	RecipePhase prep;
-	RecipePhase cooking;
-	
-	@Override
-	public Collection<RecipeComponent> getComponents() {
-		Collection<RecipeComponent> comps = new ArrayList<>();
-		prep.addComponents(comps);
-		cooking.addComponents(comps);
-		return comps;
-	}
-}

+ 12 - 14
src/main/lombok/org/leumasjaffe/recipe/model/Recipe.java

@@ -1,30 +1,28 @@
 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 javax.swing.ImageIcon;
 
+import com.fasterxml.jackson.annotation.JsonIgnore;
+
 import lombok.Data;
 
-/**
- * Recipe handling logic::
- * 	if (num components == 1) {
- * 	  let c := components(0)
- * 	  create ingredient listing c.ingredients
- *    add product panel for c
- *  } else {
- *    for (each component c) {
- *    	create ingredient listing c.title => c.ingredients
- *      add product panel for c with heading c.title
- *    }
- *  }
- */
 @Data
 public class Recipe {
 	String title;
 	String description;
+	Object nutrition;
+	int servings;
 	Optional<ImageIcon> photo; // TODO JSONIZATION	
-	List<Product> products = new ArrayList<>();
+	List<Card> cards = new ArrayList<>();
+	
+	@JsonIgnore
+	Collection<Ingredient> getIngredients() {
+		return cards.stream().flatMap(Card::getIngredientsAsStream).collect(Collectors.toList());
+	}
 }

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

@@ -1,8 +0,0 @@
-package org.leumasjaffe.recipe.model;
-
-import java.util.Collection;
-
-public interface RecipeComponent {
-	Collection<Ingredient> getIngredients();
-	Duration getDuration();
-}

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

@@ -1,22 +1,13 @@
 package org.leumasjaffe.recipe.model;
 
-import java.util.Collection;
-import java.util.Collections;
-
 import lombok.Data;
 
 @Data
-public class Rest implements RecipeComponent {
+public class Rest {
 	public enum Where {
 		FREEZER, REFRIGERATOR, ROOM_TEMPERATURE, WARM_PLACE
 	}
 	
 	Where where;
 	Duration duration;
-	
-	@Override
-	public Collection<Ingredient> getIngredients() {
-		return Collections.emptyList();
-	}
-
 }

+ 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 implements RecipeComponent {
+public class Step {
 	Set<Ingredient> ingredients = new HashSet<>();
 	Duration duration;
 	String instruction;