Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#1560) Delegate TextOf(Iterator) to TextOf(Iterable) #1599

Merged
merged 14 commits into from
May 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/main/java/org/cactoos/iterable/IterableOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.cactoos.scalar.ScalarWithFallback;
import org.cactoos.scalar.SumOfInt;
import org.cactoos.scalar.Unchecked;
import org.cactoos.text.TextOf;
import org.cactoos.text.Joined;
import org.cactoos.text.UncheckedText;

/**
Expand Down Expand Up @@ -134,6 +134,14 @@ public int hashCode() {

@Override
public String toString() {
return new UncheckedText(new TextOf(this)).asString();
return new UncheckedText(
new Joined(
", ",
new Mapped<>(
Object::toString,
this
)
)
).asString();
}
}
16 changes: 11 additions & 5 deletions src/main/java/org/cactoos/map/MapEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.cactoos.iterable.Mapped;
import org.cactoos.text.Concatenated;
import org.cactoos.text.Joined;
import org.cactoos.text.TextOf;

/**
Expand Down Expand Up @@ -122,11 +125,14 @@ public final Set<Map.Entry<X, Y>> entrySet() {

@Override
public final String toString() {
return new StringBuilder()
.append('{')
.append(new TextOf(this.entrySet()).toString())
.append('}')
.toString();
return new Concatenated(
new TextOf("{"),
new Joined(
", ",
new Mapped<>(Object::toString, this.entrySet())
),
new TextOf("}")
).toString();
}

@Override
Expand Down
40 changes: 13 additions & 27 deletions src/main/java/org/cactoos/text/TextOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.cactoos.Text;
import org.cactoos.bytes.BytesOf;
import org.cactoos.io.InputOf;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Mapped;

/**
Expand Down Expand Up @@ -316,39 +317,24 @@ public TextOf(final String input) {
/**
* Ctor.
*
* @param iterable The iterable to convert to string
* @todo #1461:30min We want constructors with {@code Iterable<?>} and
* {@code Iterator<Character>} to have same behaviour (simply concatenate list of strings).
* To do that, we should change {@code Iterable<?>} to {@code Iterable<Character>}
* and delegate the {@link Iterator} one to the {@link Iterable} one. After that, we need to
* modify other parts of cactoos that relied on this in order to preserve their behaviour
* by using {@link Joined} and {@link Concatenated}.
*/
public TextOf(final Iterable<?> iterable) {
super(
new Joined(
", ",
new Mapped<>(
Object::toString,
iterable
)
)
);
* @param iterator The iterable to convert to string
*/
public TextOf(final Iterator<Character> iterator) {
this(new IterableOf<>(iterator));
}

/**
* Ctor.
*
* @param iterator The iterable to convert to string
* @param iterable The iterable to convert to string
*/
public TextOf(final Iterator<Character> iterator) {
this(
new TextOfScalar(
() -> {
final StringBuilder buf = new StringBuilder();
iterator.forEachRemaining(buf::append);
return buf.toString();
}
public TextOf(final Iterable<Character> iterable) {
super(
new Concatenated(
new Mapped<>(
TextOf::new,
iterable
)
)
);
}
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/org/cactoos/text/TextOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import org.cactoos.io.InputOf;
import org.cactoos.iterable.IterableOfChars;
import org.cactoos.iterator.IteratorOfChars;
import org.hamcrest.core.AllOf;
import org.hamcrest.core.IsEqual;
Expand Down Expand Up @@ -307,10 +308,21 @@ void printsStackTraceFromArray() {
).affirm();
}

@Test
void readsIterableToText() {
new Assertion<>(
"Must read Iterable to Text",
new TextOf(
new IterableOfChars("hello")
),
new IsText("hello")
).affirm();
}

@Test
void readsIteratorToText() {
new Assertion<>(
"Can't read Iterator to Text",
"Must read Iterator to Text",
new TextOf(
new IteratorOfChars("qwer")
),
Expand Down