diff --git a/src/main/java/org/cactoos/text/JoinedText.java b/src/main/java/org/cactoos/text/JoinedText.java
index 814ab104a8..b736b85684 100644
--- a/src/main/java/org/cactoos/text/JoinedText.java
+++ b/src/main/java/org/cactoos/text/JoinedText.java
@@ -24,6 +24,7 @@
package org.cactoos.text;
import java.util.StringJoiner;
+import org.cactoos.Scalar;
import org.cactoos.Text;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Mapped;
@@ -34,18 +35,11 @@
*
There is no thread-safety guarantee.
*
* @since 0.9
+ * @todo #849:30min Continue refactoring all classes implementing Text to extend
+ * TextEnvelope - in most cases asString should be removed and implementation
+ * from TextEnvelope should be used.
*/
-public final class JoinedText implements Text {
-
- /**
- * The texts.
- */
- private final Iterable texts;
-
- /**
- * The delimiter.
- */
- private final Text delimiter;
+public final class JoinedText extends TextEnvelope {
/**
* Ctor.
@@ -64,9 +58,7 @@ public JoinedText(final String delimit, final String... strs) {
public JoinedText(final String delimit, final Iterable strs) {
this(
new TextOf(delimit),
- new Mapped<>(
- text -> new TextOf(text), strs
- )
+ new Mapped<>(TextOf::new, strs)
);
}
@@ -85,17 +77,14 @@ public JoinedText(final Text delimit, final Text... txts) {
* @param txts Texts to be joined
*/
public JoinedText(final Text delimit, final Iterable txts) {
- this.delimiter = delimit;
- this.texts = txts;
- }
-
- @Override
- public String asString() throws Exception {
- final StringJoiner joint = new StringJoiner(this.delimiter.asString());
- for (final Text text : this.texts) {
- joint.add(text.asString());
- }
- return joint.toString();
+ super((Scalar) () -> {
+ final StringJoiner joint =
+ new StringJoiner(delimit.asString());
+ for (final Text text : txts) {
+ joint.add(text.asString());
+ }
+ return joint.toString();
+ });
}
}
diff --git a/src/main/java/org/cactoos/text/LowerText.java b/src/main/java/org/cactoos/text/LowerText.java
index 7e121393c4..df44de081b 100644
--- a/src/main/java/org/cactoos/text/LowerText.java
+++ b/src/main/java/org/cactoos/text/LowerText.java
@@ -24,6 +24,7 @@
package org.cactoos.text;
import java.util.Locale;
+import org.cactoos.Scalar;
import org.cactoos.Text;
/**
@@ -33,17 +34,7 @@
*
* @since 0.1
*/
-public final class LowerText implements Text {
-
- /**
- * The text.
- */
- private final Text origin;
-
- /**
- * The locale.
- */
- private final Locale locale;
+public final class LowerText extends TextEnvelope {
/**
* Ctor.
@@ -59,13 +50,7 @@ public LowerText(final Text text) {
* @param lang Locale
*/
public LowerText(final Text text, final Locale lang) {
- this.origin = text;
- this.locale = lang;
- }
-
- @Override
- public String asString() throws Exception {
- return this.origin.asString().toLowerCase(this.locale);
+ super((Scalar) () -> text.asString().toLowerCase(lang));
}
}
diff --git a/src/main/java/org/cactoos/text/NormalizedText.java b/src/main/java/org/cactoos/text/NormalizedText.java
index fb7005f49e..8ec16160eb 100644
--- a/src/main/java/org/cactoos/text/NormalizedText.java
+++ b/src/main/java/org/cactoos/text/NormalizedText.java
@@ -23,6 +23,7 @@
*/
package org.cactoos.text;
+import org.cactoos.Scalar;
import org.cactoos.Text;
/**
@@ -31,12 +32,7 @@
*
* @since 0.9
*/
-public final class NormalizedText implements Text {
-
- /**
- * The text.
- */
- private final Text origin;
+public final class NormalizedText extends TextEnvelope {
/**
* Ctor.
@@ -51,17 +47,13 @@ public NormalizedText(final String text) {
* @param text A Text
*/
public NormalizedText(final Text text) {
- this.origin = text;
+ super(
+ (Scalar) () -> new ReplacedText(
+ new TrimmedText(text),
+ "\\s+",
+ " "
+ ).asString()
+ );
}
-
- @Override
- public String asString() throws Exception {
- return new ReplacedText(
- new TrimmedText(this.origin),
- "\\s+",
- " "
- ).asString();
- }
-
}
diff --git a/src/main/java/org/cactoos/text/RepeatedText.java b/src/main/java/org/cactoos/text/RepeatedText.java
index f69e55f065..56a5c9a035 100644
--- a/src/main/java/org/cactoos/text/RepeatedText.java
+++ b/src/main/java/org/cactoos/text/RepeatedText.java
@@ -23,6 +23,7 @@
*/
package org.cactoos.text;
+import org.cactoos.Scalar;
import org.cactoos.Text;
/**
@@ -32,17 +33,7 @@
*
* @since 0.9
*/
-public final class RepeatedText implements Text {
-
- /**
- * The text.
- */
- private final Text origin;
-
- /**
- * How many times repeat the Text.
- */
- private final int count;
+public final class RepeatedText extends TextEnvelope {
/**
* Ctor.
@@ -59,17 +50,12 @@ public RepeatedText(final String text, final int count) {
* @param count How many times repeat the Text
*/
public RepeatedText(final Text text, final int count) {
- this.origin = text;
- this.count = count;
- }
-
- @Override
- public String asString() throws Exception {
- final StringBuilder out = new StringBuilder();
- for (int cnt = 0; cnt < this.count; ++cnt) {
- out.append(this.origin.asString());
- }
- return out.toString();
+ super((Scalar) () -> {
+ final StringBuilder out = new StringBuilder();
+ for (int cnt = 0; cnt < count; ++cnt) {
+ out.append(text.asString());
+ }
+ return out.toString();
+ });
}
-
}
diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java
index 6a5fba78ca..a9dee93e2a 100644
--- a/src/main/java/org/cactoos/text/TextEnvelope.java
+++ b/src/main/java/org/cactoos/text/TextEnvelope.java
@@ -38,7 +38,6 @@
* and equals methods.
* @checkstyle AbstractClassNameCheck (500 lines)
*/
-@SuppressWarnings("PMD.AbstractNaming")
public abstract class TextEnvelope implements Text {
/**
@@ -51,7 +50,7 @@ public abstract class TextEnvelope implements Text {
* @param text Text representing the text value.
*/
public TextEnvelope(final Text text) {
- this(new IoCheckedScalar<>(() -> text.asString()));
+ this(new IoCheckedScalar<>(text::asString));
}
/**
@@ -67,6 +66,11 @@ public final String asString() throws IOException {
return this.origin.value();
}
+ @Override
+ public final String toString() {
+ return new UncheckedText(this).asString();
+ }
+
@Override
public final int hashCode() {
return new UncheckedScalar<>(this.origin).value().hashCode();
diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java
index e189305155..972498243d 100644
--- a/src/main/java/org/cactoos/text/TextOf.java
+++ b/src/main/java/org/cactoos/text/TextOf.java
@@ -35,11 +35,9 @@
import org.cactoos.Bytes;
import org.cactoos.Input;
import org.cactoos.Scalar;
-import org.cactoos.Text;
import org.cactoos.io.BytesOf;
import org.cactoos.io.InputOf;
import org.cactoos.iterable.Mapped;
-import org.cactoos.scalar.IoCheckedScalar;
/**
* TextOf
@@ -48,12 +46,7 @@
*
* @since 0.12
*/
-public final class TextOf implements Text {
-
- /**
- * The origin.
- */
- private final Scalar origin;
+public final class TextOf extends TextEnvelope {
/**
* Ctor.
@@ -162,10 +155,10 @@ public TextOf(final Reader rdr, final Charset cset) {
/**
* Ctor.
* @param rdr Reader
- * @param cset Charset
* @param max Buffer size
+ * @param cset Charset
*/
- public TextOf(final Reader rdr, final Charset cset, final int max) {
+ public TextOf(final Reader rdr, final int max, final Charset cset) {
this(new BytesOf(rdr, cset, max));
}
@@ -354,17 +347,7 @@ public TextOf(final InputStream input) {
* @param scalar The Scalar of String
*/
private TextOf(final Scalar scalar) {
- this.origin = scalar;
- }
-
- @Override
- public String asString() throws Exception {
- return new IoCheckedScalar<>(this.origin).value();
- }
-
- @Override
- public String toString() {
- return new UncheckedText(this).asString();
+ super(scalar);
}
}
diff --git a/src/test/java/org/cactoos/text/TextOfTest.java b/src/test/java/org/cactoos/text/TextOfTest.java
index 60dc8849aa..7ed20bc10b 100644
--- a/src/test/java/org/cactoos/text/TextOfTest.java
+++ b/src/test/java/org/cactoos/text/TextOfTest.java
@@ -92,6 +92,16 @@ public void readsInputIntoTextWithSmallBuffer() throws Exception {
);
}
+ @Test
+ public void readsReaderIntoTextWithSmallBuffer() {
+ final String text = "Hi there! with small buffer";
+ MatcherAssert.assertThat(
+ "Can't read text from Reader with a small reading buffer",
+ new TextOf(new StringReader(text), 2, StandardCharsets.UTF_8),
+ new TextHasString(text)
+ );
+ }
+
@Test
public void readsInputIntoTextWithSmallBufferAndDefaultCharset()
throws Exception {