Phase.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package org.leumasjaffe.recipe.model;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Optional;
  5. import java.util.stream.Stream;
  6. import org.leumasjaffe.observer.Observable;
  7. import lombok.Data;
  8. import lombok.EqualsAndHashCode;
  9. import lombok.NonNull;
  10. @Data @EqualsAndHashCode(callSuper=false)
  11. public class Phase extends Observable.Instance implements CompoundRecipeComponent {
  12. int id = 0; // TODO Fix this
  13. int[] dependsOn = {}; // decltype(id)[]
  14. String vessel = "";
  15. Optional<Preparation> preparation = Optional.empty();
  16. List<Step> cooking = new ArrayList<>();
  17. Optional<Rest> rest = Optional.empty();
  18. public Stream<Ingredient> getIngredientsAsStream() {
  19. return cooking.stream().flatMap(RecipeComponent::getIngredientsAsStream);
  20. }
  21. // TODO Include Rest and Preparation
  22. public Stream<? extends RecipeComponent> getComponents() {
  23. return cooking.stream();
  24. }
  25. public CollatedDuration getCollatedDuration() {
  26. final Duration prep = preparation.map(Preparation::getDuration).orElse(Duration.ZERO);
  27. final Duration rest = this.rest.map(Rest::getDuration).orElse(Duration.ZERO);
  28. final Duration cooking = this.cooking.stream().map(Step::getDuration)
  29. .reduce(Duration.ZERO, Duration::plus);
  30. return new CollatedDuration(prep, cooking, prep.plus(cooking).plus(rest));
  31. }
  32. public void setPreparation(final @NonNull Preparation p) {
  33. preparation = Optional.of(new Preparation(p.duration, this::getIngredients));
  34. }
  35. }