-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dot.java
45 lines (35 loc) · 892 Bytes
/
Dot.java
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
package nz.ac.aut.wbz8656.dsa2017.assignment02;
import java.util.Objects;
/**
* A dot instruction. When executed, should take the top value of the stack
* and print it to the console.
* @author Guan Wang
* @version 1.0
*/
public class Dot implements Instruction {
//Private
private Printable printer;
private Dot(Printable printer) {
this.printer = printer;
}
private static void checkNotEmpty (Stack<Long> stack, String name) {
if (stack.size() == 0) {
throw new IllegalArgumentException(name + " must not be empty");
}
}
//Factory
/**
* Creates a new dot instruction
* @return a new Dot object
*/
public static Dot make(Printable printer) {
return new Dot(printer);
}
//Command
@Override
public void execute(Stack<Long> stack) {
Objects.requireNonNull(stack);
checkNotEmpty(stack, "Stack");
printer.println(stack.top());
}
}