Merge branch 'master' into bael-16656

This commit is contained in:
Josh Cummings
2019-10-26 15:37:05 -06:00
committed by GitHub
parent db85c8f275
commit 0be2175c89
20539 changed files with 1643630 additions and 0 deletions
@@ -0,0 +1,27 @@
package com.baeldung.memento;
public class TextEditor {
private TextWindow textWindow;
private TextWindowState savedTextWindow;
public TextEditor(TextWindow textWindow) {
this.textWindow = textWindow;
}
public void write(String text) {
textWindow.addText(text);
}
public String print() {
return textWindow.getCurrentText();
}
public void hitSave() {
savedTextWindow = textWindow.save();
}
public void hitUndo() {
textWindow.restore(savedTextWindow);
}
}
@@ -0,0 +1,26 @@
package com.baeldung.memento;
public class TextWindow {
private StringBuilder currentText;
public TextWindow() {
this.currentText = new StringBuilder();
}
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());
}
}
@@ -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;
}
}
@@ -0,0 +1,20 @@
package com.baeldung.memento;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class TextEditorUnitTest {
@Test
void givenTextEditor_whenAddTextSaveAddMoreAndUndo_thenSavecStateRestored() {
TextEditor textEditor = new TextEditor(new TextWindow());
textEditor.write("The Memento Design Pattern\n");
textEditor.write("How to implement it in Java?\n");
textEditor.hitSave();
textEditor.write("Buy milk and eggs before coming home\n");
textEditor.hitUndo();
assertThat(textEditor.print()).isEqualTo("The Memento Design Pattern\nHow to implement it in Java?\n");
}
}