Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed May 17, 2018
2 parents 4467355 + 06a677e commit 5cd1e3e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 106 deletions.
39 changes: 14 additions & 25 deletions src/main/java/org/cactoos/text/JoinedText.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,18 +35,11 @@
* <p>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<Text> texts;

/**
* The delimiter.
*/
private final Text delimiter;
public final class JoinedText extends TextEnvelope {

/**
* Ctor.
Expand All @@ -64,9 +58,7 @@ public JoinedText(final String delimit, final String... strs) {
public JoinedText(final String delimit, final Iterable<String> strs) {
this(
new TextOf(delimit),
new Mapped<>(
text -> new TextOf(text), strs
)
new Mapped<>(TextOf::new, strs)
);
}

Expand All @@ -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<Text> 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<String>) () -> {
final StringJoiner joint =
new StringJoiner(delimit.asString());
for (final Text text : txts) {
joint.add(text.asString());
}
return joint.toString();
});
}

}
21 changes: 3 additions & 18 deletions src/main/java/org/cactoos/text/LowerText.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.cactoos.text;

import java.util.Locale;
import org.cactoos.Scalar;
import org.cactoos.Text;

/**
Expand All @@ -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.
Expand All @@ -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<String>) () -> text.asString().toLowerCase(lang));
}

}
26 changes: 9 additions & 17 deletions src/main/java/org/cactoos/text/NormalizedText.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.cactoos.text;

import org.cactoos.Scalar;
import org.cactoos.Text;

/**
Expand All @@ -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.
Expand All @@ -51,17 +47,13 @@ public NormalizedText(final String text) {
* @param text A Text
*/
public NormalizedText(final Text text) {
this.origin = text;
super(
(Scalar<String>) () -> new ReplacedText(
new TrimmedText(text),
"\\s+",
" "
).asString()
);
}

@Override
public String asString() throws Exception {
return new ReplacedText(
new TrimmedText(this.origin),
"\\s+",
" "
).asString();
}

}

32 changes: 9 additions & 23 deletions src/main/java/org/cactoos/text/RepeatedText.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.cactoos.text;

import org.cactoos.Scalar;
import org.cactoos.Text;

/**
Expand All @@ -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.
Expand All @@ -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<String>) () -> {
final StringBuilder out = new StringBuilder();
for (int cnt = 0; cnt < count; ++cnt) {
out.append(text.asString());
}
return out.toString();
});
}

}
8 changes: 6 additions & 2 deletions src/main/java/org/cactoos/text/TextEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
* and equals methods.
* @checkstyle AbstractClassNameCheck (500 lines)
*/
@SuppressWarnings("PMD.AbstractNaming")
public abstract class TextEnvelope implements Text {

/**
Expand All @@ -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));
}

/**
Expand All @@ -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();
Expand Down
25 changes: 4 additions & 21 deletions src/main/java/org/cactoos/text/TextOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,12 +46,7 @@
*
* @since 0.12
*/
public final class TextOf implements Text {

/**
* The origin.
*/
private final Scalar<String> origin;
public final class TextOf extends TextEnvelope {

/**
* Ctor.
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -354,17 +347,7 @@ public TextOf(final InputStream input) {
* @param scalar The Scalar of String
*/
private TextOf(final Scalar<String> 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);
}

}
10 changes: 10 additions & 0 deletions src/test/java/org/cactoos/text/TextOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

1 comment on commit 5cd1e3e

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 5cd1e3e May 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 849-f712c12a discovered in src/main/java/org/cactoos/text/JoinedText.java and submitted as #878. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.