From dd7b922f2bb0e738a3926583cb5199ed888ef793 Mon Sep 17 00:00:00 2001 From: dupirefr Date: Wed, 24 Jul 2019 08:17:58 +0200 Subject: [PATCH 1/5] [BAEL-3090] Added example --- .../com/baeldung/memento/Coordinates.java | 12 ++++++++ .../java/com/baeldung/memento/TextEditor.java | 19 ++++++++++++ .../java/com/baeldung/memento/TextWindow.java | 30 +++++++++++++++++++ .../com/baeldung/memento/TextWindowState.java | 14 +++++++++ 4 files changed, 75 insertions(+) create mode 100644 patterns/design-patterns-2/src/main/java/com/baeldung/memento/Coordinates.java create mode 100644 patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextEditor.java create mode 100644 patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindow.java create mode 100644 patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindowState.java diff --git a/patterns/design-patterns-2/src/main/java/com/baeldung/memento/Coordinates.java b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/Coordinates.java new file mode 100644 index 0000000000..19c1798a93 --- /dev/null +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/Coordinates.java @@ -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; + } +} diff --git a/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextEditor.java b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextEditor.java new file mode 100644 index 0000000000..cf08e2b226 --- /dev/null +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextEditor.java @@ -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); + } +} diff --git a/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindow.java b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindow.java new file mode 100644 index 0000000000..5fdfda1998 --- /dev/null +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindow.java @@ -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); + } +} diff --git a/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindowState.java b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindowState.java new file mode 100644 index 0000000000..10015a84d1 --- /dev/null +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindowState.java @@ -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; + } +} From 83d8443fef41488ac1dbfa250f947ec26ecd29be Mon Sep 17 00:00:00 2001 From: dupirefr Date: Sat, 27 Jul 2019 11:24:45 +0200 Subject: [PATCH 2/5] [BAEL-3090] Added test + removed cursor --- patterns/design-patterns-2/pom.xml | 7 +++++++ .../main/java/com/baeldung/memento/TextEditor.java | 8 ++++++++ .../main/java/com/baeldung/memento/TextWindow.java | 12 ++++-------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/patterns/design-patterns-2/pom.xml b/patterns/design-patterns-2/pom.xml index 392035ab84..574edecb36 100644 --- a/patterns/design-patterns-2/pom.xml +++ b/patterns/design-patterns-2/pom.xml @@ -31,6 +31,12 @@ commons-lang3 ${commons-lang3.version} + + org.assertj + assertj-core + ${assertj.version} + test + @@ -38,5 +44,6 @@ 1.8 1.8 16.0.2 + 3.12.2 diff --git a/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextEditor.java b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextEditor.java index cf08e2b226..37ac962773 100644 --- a/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextEditor.java +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextEditor.java @@ -9,6 +9,14 @@ public class TextEditor { this.textWindow = textWindow; } + public void write(String text) { + textWindow.addText(text); + } + + public String print() { + return textWindow.getCurrentText(); + } + public void hitSave() { savedTextWindow = textWindow.save(); } diff --git a/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindow.java b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindow.java index 5fdfda1998..08778561b0 100644 --- a/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindow.java +++ b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/TextWindow.java @@ -3,11 +3,13 @@ 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 String getCurrentText() { + return currentText.toString(); } public void addText(String text) { @@ -20,11 +22,5 @@ public class TextWindow { 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); } } From c3cc9a83367d21fa0b8499218e6d703d9532831e Mon Sep 17 00:00:00 2001 From: dupirefr Date: Sat, 27 Jul 2019 11:34:42 +0200 Subject: [PATCH 3/5] [BAEL-3090] Removed Coordinates --- .../main/java/com/baeldung/memento/Coordinates.java | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 patterns/design-patterns-2/src/main/java/com/baeldung/memento/Coordinates.java diff --git a/patterns/design-patterns-2/src/main/java/com/baeldung/memento/Coordinates.java b/patterns/design-patterns-2/src/main/java/com/baeldung/memento/Coordinates.java deleted file mode 100644 index 19c1798a93..0000000000 --- a/patterns/design-patterns-2/src/main/java/com/baeldung/memento/Coordinates.java +++ /dev/null @@ -1,12 +0,0 @@ -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; - } -} From b9089ce06796d67f89c404749790e203a33fc8fe Mon Sep 17 00:00:00 2001 From: dupirefr Date: Sat, 27 Jul 2019 11:35:16 +0200 Subject: [PATCH 4/5] [BAEL-3090] Really added test this time --- .../com/baeldung/memento/TextEditorTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 patterns/design-patterns-2/src/test/java/com/baeldung/memento/TextEditorTest.java diff --git a/patterns/design-patterns-2/src/test/java/com/baeldung/memento/TextEditorTest.java b/patterns/design-patterns-2/src/test/java/com/baeldung/memento/TextEditorTest.java new file mode 100644 index 0000000000..b985753d91 --- /dev/null +++ b/patterns/design-patterns-2/src/test/java/com/baeldung/memento/TextEditorTest.java @@ -0,0 +1,20 @@ +package com.baeldung.memento; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class TextEditorTest { + + @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"); + } +} \ No newline at end of file From dcc8d524412637e00cb7ee23d9fe8af70c2e471f Mon Sep 17 00:00:00 2001 From: dupirefr Date: Sat, 27 Jul 2019 12:01:37 +0200 Subject: [PATCH 5/5] [BAEL-3090] Renamed test to match PMD --- .../memento/{TextEditorTest.java => TextEditorUnitTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename patterns/design-patterns-2/src/test/java/com/baeldung/memento/{TextEditorTest.java => TextEditorUnitTest.java} (96%) diff --git a/patterns/design-patterns-2/src/test/java/com/baeldung/memento/TextEditorTest.java b/patterns/design-patterns-2/src/test/java/com/baeldung/memento/TextEditorUnitTest.java similarity index 96% rename from patterns/design-patterns-2/src/test/java/com/baeldung/memento/TextEditorTest.java rename to patterns/design-patterns-2/src/test/java/com/baeldung/memento/TextEditorUnitTest.java index b985753d91..09f6df70a2 100644 --- a/patterns/design-patterns-2/src/test/java/com/baeldung/memento/TextEditorTest.java +++ b/patterns/design-patterns-2/src/test/java/com/baeldung/memento/TextEditorUnitTest.java @@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -class TextEditorTest { +class TextEditorUnitTest { @Test void givenTextEditor_whenAddTextSaveAddMoreAndUndo_thenSavecStateRestored() {