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!") + ); + } +}