-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathStep.java
More file actions
84 lines (65 loc) · 2.17 KB
/
Copy pathStep.java
File metadata and controls
84 lines (65 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.nighthacking.recipe;
import com.nighthacking.scales.Scale;
/**
* @author Stephen Chin <[email protected]>
*/
public class Step {
private final StepCommand command;
private Step(StepCommand command) {
this.command = command;
}
public Step() {
this.command = null;
}
public void execute(RecipeEnvironment env) throws InterruptedException {
command.execute(env);
}
public static Step say(String string) {
return new Step(e -> e.getDisplay().say(string));
}
public static Step say(String string, Object... args) {
return new Step(e -> e.getDisplay().say(string, args));
}
public static Step waitForFull() {
return new Step(e -> e.getScale().waitForStable(w -> w > 0));
}
public static Step waitForClear() {
return new Step(e -> e.getScale().waitFor(w -> w <= 0));
}
public static Step waitForContents() {
return new Step(e -> {
Scale s = e.getScale();
final double originalWeight = s.getWeight();
s.waitForStable(w -> w != originalWeight);
});
}
public static Step tare() {
return new Step(e -> {
try {
e.getScale().tare();
} catch (UnsupportedOperationException ex) {
e.getDisplay().say("Press the 'tare' button on the scale.");
e.getScale().waitFor(w -> w == 0);
}
});
}
public static Step waitFor(Ingredient ingredient) {
return waitFor(ingredient, ingredient.getWeight() / 10);
}
public static Step waitFor(Ingredient ingredient, double margin) {
return new Step(e -> e.getScale().waitForStable(w -> Math.abs(w - ingredient.getWeight()) < margin));
}
public static Step waitForAtLeast(Ingredient ingredient) {
return waitForAtLeast(ingredient, ingredient.getWeight() / 10);
}
public static Step waitForAtLeast(Ingredient ingredient, double margin) {
return new Step(e -> e.getScale().waitFor(w -> w > ingredient.getWeight() - margin / 2));
}
public static Step countdown(int seconds) {
return new Step(e -> e.getDisplay().countdown(seconds));
}
@FunctionalInterface
private static interface StepCommand {
public void execute(RecipeEnvironment environment) throws InterruptedException;
}
}