Phase.java 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. Preparation preparation = new Preparation();
  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.getIngredients().isEmpty() ?
  27. Duration.ZERO : preparation.getDuration();
  28. final Duration rest = this.rest.map(Rest::getDuration).orElse(Duration.ZERO);
  29. final Duration cooking = this.cooking.stream().map(Step::getDuration)
  30. .reduce(Duration.ZERO, Duration::plus);
  31. return new CollatedDuration(prep, cooking, prep.plus(cooking).plus(rest));
  32. }
  33. public void setPreparation(final @NonNull Preparation p) {
  34. preparation = new Preparation(p.duration, this::getIngredients);
  35. }
  36. }