[BAEL-3090] Added example

This commit is contained in:
dupirefr 2019-07-24 08:17:58 +02:00
parent 0eb5179a6d
commit dd7b922f2b
4 changed files with 75 additions and 0 deletions

View File

@ -0,0 +1,12 @@
package com.baeldung.memento;
public class Coordinates {
private int x;
private int y;
public Coordinates(int x, int y) {
this.x = x;
this.y = y;
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.memento;
public class TextEditor {
private TextWindow textWindow;
private TextWindowState savedTextWindow;
public TextEditor(TextWindow textWindow) {
this.textWindow = textWindow;
}
public void hitSave() {
savedTextWindow = textWindow.save();
}
public void hitUndo() {
textWindow.restore(savedTextWindow);
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.memento;
public class TextWindow {
private StringBuilder currentText;
private Coordinates cursorPosition;
public TextWindow() {
this.currentText = new StringBuilder();
this.cursorPosition = new Coordinates(0, 0);
}
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());
cursorPosition = atTextEnd();
}
private Coordinates atTextEnd() {
String[] lines = currentText.toString().split("\n");
return new Coordinates(lines[lines.length - 1].length(), lines.length);
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.memento;
public class TextWindowState {
private String text;
public TextWindowState(String text) {
this.text = text;
}
public String getText() {
return text;
}
}