From 4d2b9d053c9a78218cb0bd2470926243193cf653 Mon Sep 17 00:00:00 2001 From: mehyil Date: Fri, 2 Jun 2017 11:40:54 +0300 Subject: [PATCH] Replaced Text --- .../java/org/cactoos/text/ReplacedText.java | 48 +++++++++++++++++++ .../org/cactoos/text/ReplacedTextTest.java | 24 ++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/main/java/org/cactoos/text/ReplacedText.java create mode 100644 src/test/java/org/cactoos/text/ReplacedTextTest.java diff --git a/src/main/java/org/cactoos/text/ReplacedText.java b/src/main/java/org/cactoos/text/ReplacedText.java new file mode 100644 index 0000000000..12aa09b396 --- /dev/null +++ b/src/main/java/org/cactoos/text/ReplacedText.java @@ -0,0 +1,48 @@ +package org.cactoos.text; + +import org.cactoos.Text; + +import java.io.IOException; + +/** + * Replace the Text. + * + * @author Mehmet Yildirim (memoyil@gmail.com) + * @version $Id$ + * @since 0.1 + */ +public class ReplacedText implements Text { + + /** + * The text. + */ + private final Text origin; + + /** + * The old char. + */ + private String oldChar; + + /** + * The new char. + */ + private String newChar; + + /** + * Ctor. + * + * @param text The text + * @param oldChar The old char + * @param newChar The new char + */ + public ReplacedText(final Text text, String oldChar, String newChar) { + this.origin = text; + this.oldChar = oldChar; + this.newChar = newChar; + } + + @Override + public String asString() throws IOException { + return origin.asString().replace(oldChar, newChar); + } +} diff --git a/src/test/java/org/cactoos/text/ReplacedTextTest.java b/src/test/java/org/cactoos/text/ReplacedTextTest.java new file mode 100644 index 0000000000..c13f6c2e5a --- /dev/null +++ b/src/test/java/org/cactoos/text/ReplacedTextTest.java @@ -0,0 +1,24 @@ +package org.cactoos.text; + +import org.cactoos.TextHasString; +import org.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test case for {@link ReplacedText}. + * @author Mehmet Yildirim (memoyil@gmail.com) + * @version $Id$ + * @since 0.1 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class ReplacedTextTest { + + @Test + public void replaceText() { + MatcherAssert.assertThat( + "Can't replace a text", + new ReplacedText(new StringAsText("Hello!"),"ello","i"), + new TextHasString("Hi!") + ); + } +}