浏览代码

Make Preparation no longer an optional element in a Phase. Instead, it will simply be hidden when not in use.

Sam Jaffe 5 年之前
父节点
当前提交
2355348354

+ 4 - 3
src/main/lombok/org/leumasjaffe/recipe/model/Phase.java

@@ -16,7 +16,7 @@ public class Phase extends Observable.Instance implements CompoundRecipeComponen
 	int id = 0; // TODO Fix this
 	int[] dependsOn = {}; // decltype(id)[]
 	String vessel = "";
-	Optional<Preparation> preparation = Optional.empty();
+	Preparation preparation = new Preparation();
 	List<Step> cooking = new ArrayList<>();
 	Optional<Rest> rest = Optional.empty();
 	
@@ -30,7 +30,8 @@ public class Phase extends Observable.Instance implements CompoundRecipeComponen
 	}
 	
 	public CollatedDuration getCollatedDuration() {
-		final Duration prep = preparation.map(Preparation::getDuration).orElse(Duration.ZERO);
+		final Duration prep = preparation.getIngredients().isEmpty() ?
+				Duration.ZERO : preparation.getDuration();
 		final Duration rest = this.rest.map(Rest::getDuration).orElse(Duration.ZERO);
 		final Duration cooking = this.cooking.stream().map(Step::getDuration)
 				.reduce(Duration.ZERO, Duration::plus);
@@ -38,6 +39,6 @@ public class Phase extends Observable.Instance implements CompoundRecipeComponen
 	}
 	
 	public void setPreparation(final @NonNull Preparation p) {
-		preparation = Optional.of(new Preparation(p.duration, this::getIngredients));
+		preparation = new Preparation(p.duration, this::getIngredients);
 	}
 }

+ 2 - 4
src/main/lombok/org/leumasjaffe/recipe/view/PhasePanel.java

@@ -28,9 +28,7 @@ public class PhasePanel extends JPanel {
 	public PhasePanel(final Phase phase) {		
 		setLayout(new VerticalLayout(5));
 		
-		if (phase.getPreparation().isPresent()) {
-			add(new PreparationPanel(phase));
-		}
+		add(new PreparationPanel(phase));
 		
 		panelSteps = new AutoGrowPanel<>(Step::new, StepPanel::new);
 		panelSteps.setGap(5);
@@ -49,7 +47,7 @@ public class PhasePanel extends JPanel {
 	
 	private static List<Observable> allChildren(Phase phase) {
 		List<Observable> children = new ArrayList<>(phase.getCooking());
-		phase.getPreparation().ifPresent(children::add);
+		children.add(phase.getPreparation());
 		phase.getRest().ifPresent(children::add);
 		return children;
 	}

+ 3 - 2
src/main/lombok/org/leumasjaffe/recipe/view/PreparationPanel.java

@@ -102,8 +102,9 @@ public class PreparationPanel extends JPanel {
 	}
 	
 	public void setModel(final Phase phase) {
-		childListener.setObserved(phase.getPreparation().get(), phase);
-		durationController.setObserved(phase.getPreparation().get());
+		final Preparation prep = phase.getPreparation();
+		childListener.setObserved(prep, phase);
+		durationController.setObserved(prep);
 	}
 	
 	@Override

+ 1 - 1
src/test/java/org/leumasjaffe/recipe/model/PhaseTest.java

@@ -87,7 +87,7 @@ class PhaseTest {
 		phase.setCooking(Arrays.asList(step));
 		phase.setPreparation(new Preparation());
 		assertThat(phase.getIngredients(), hasSize(2));
-		final Preparation prep = phase.getPreparation().get();
+		final Preparation prep = phase.getPreparation();
 		assertThat(prep.getIngredients(), hasSize(1));
 		assertThat(prep.getIngredients(), hasItem(new Ingredient("B", "TEST", _1g)));
 	}

+ 1 - 1
src/test/java/org/leumasjaffe/recipe/view/PhasePanelTest.java

@@ -34,7 +34,7 @@ class PhasePanelTest extends SwingTestCase {
 	@BeforeEach
 	void setUp() {
 		doReturn(Arrays.asList(stub)).when(stuff).getCooking();
-		doReturn(Optional.of(prep)).when(stuff).getPreparation();
+		doReturn(prep).when(stuff).getPreparation();
 		doReturn(Optional.of(rest)).when(stuff).getRest();
 		panel = new PhasePanel(stuff);
 		

+ 1 - 3
src/test/java/org/leumasjaffe/recipe/view/PreparationPanelTest.java

@@ -2,8 +2,6 @@ package org.leumasjaffe.recipe.view;
 
 import static org.mockito.Mockito.*;
 
-import java.util.Optional;
-
 import javax.swing.JFormattedTextField;
 
 import org.junit.jupiter.api.BeforeEach;
@@ -30,7 +28,7 @@ class PreparationPanelTest extends SwingTestCase {
 	
 	@BeforeEach
 	void setUp() {
-		doReturn(Optional.of(stuff)).when(parent).getPreparation();
+		doReturn(stuff).when(parent).getPreparation();
 		panel.setModel(parent);
 	}