Skip to content

Commit

Permalink
Replaced Text
Browse files Browse the repository at this point in the history
  • Loading branch information
mehyil committed Jun 2, 2017
1 parent 65d6d67 commit 4d2b9d0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/main/java/org/cactoos/text/ReplacedText.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
24 changes: 24 additions & 0 deletions src/test/java/org/cactoos/text/ReplacedTextTest.java
Original file line number Diff line number Diff line change
@@ -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!")
);
}
}

0 comments on commit 4d2b9d0

Please sign in to comment.