-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTextWindow.java
More file actions
42 lines (31 loc) · 904 Bytes
/
TextWindow.java
File metadata and controls
42 lines (31 loc) · 904 Bytes
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
package behavioral.memento;
import java.util.Stack;
public class TextWindow {
private StringBuilder currentText;
private Stack<TextWindowState> history;
public TextWindow() {
this.currentText = new StringBuilder();
this.history = new Stack<>();
}
public String getCurrentText() {
return currentText.toString();
}
public void addText(String text) {
currentText.append(text);
}
public TextWindowState save() {
return new TextWindowState(currentText.toString());
}
public void restore(TextWindowState save) {
currentText = new StringBuilder(save.getText());
}
public void undo() {
if (!history.isEmpty()) {
TextWindowState previousState = history.pop();
restore(previousState);
}
}
public void saveAndPush() {
history.push(save());
}
}