Skip to content

Commit

Permalink
(yegor256#1460) Introduce Flattened and use it where relevant
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Dec 27, 2020
1 parent 5580372 commit 80e0aa8
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 96 deletions.
40 changes: 17 additions & 23 deletions src/main/java/org/cactoos/text/Abbreviated.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
package org.cactoos.text;

import org.cactoos.Text;
import org.cactoos.scalar.ScalarOf;
import org.cactoos.scalar.Ternary;

/**
* Abbreviates a Text using ellipses.
Expand Down Expand Up @@ -80,32 +82,24 @@ public Abbreviated(final String text, final int max) {
* Ctor.
* @param text The Text
* @param max Max width of the result string
* @todo #1287:30min Introduce `text.Flatten` that takes a `Scalar` of `Text`
* and call `value()` then `asString()` on it. Add some tests for it (including
* for `equals`). Then replace the code below by a composition of `text.Flatten`
* and `scalar.Ternary`. Then do the same for `PrefixOf` and `SuffixOf` using
* `text.Sub`.
*/
public Abbreviated(final Text text, final int max) {
super(
new TextOf(
() -> {
final Text abbreviated;
if (text.asString().length() <= max) {
abbreviated = text;
} else {
abbreviated = new FormattedText(
"%s%s",
new Sub(
text,
0,
max - Abbreviated.ELLIPSES.length()
).asString(),
Abbreviated.ELLIPSES
);
}
return abbreviated.asString();
}
new Flattened(
new Ternary<>(
new ScalarOf<>(() -> new Sticky(text)),
(Text t) -> t.asString().length() <= max,
t -> t,
t -> new FormattedText(
"%s%s",
new Sub(
t,
0,
max - Abbreviated.ELLIPSES.length()
),
Abbreviated.ELLIPSES
)
)
)
);
}
Expand Down
35 changes: 19 additions & 16 deletions src/main/java/org/cactoos/text/Capitalized.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.cactoos.text;

import org.cactoos.Text;
import org.cactoos.scalar.ScalarOf;
import org.cactoos.scalar.Ternary;

/**
Expand All @@ -32,6 +33,7 @@
* no other characters are changed.
*
* @since 0.46
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
public final class Capitalized extends TextEnvelope {

Expand All @@ -50,23 +52,24 @@ public Capitalized(final String text) {
*/
public Capitalized(final Text text) {
super(
new TextOf(
() -> {
return new Ternary<Text>(
new IsBlank(text),
() -> text,
() -> new Joined(
"",
new TextOf(
Character.toChars(
Character.toTitleCase(
text.asString().codePointAt(0)
)
new Flattened(
new Ternary<>(
new ScalarOf<>(() -> new Sticky(text)),
(Text t) -> new IsBlank(t).value(),
t -> t,
t -> new Joined(
"",
new TextOf(
Character.toChars(
Character.toTitleCase(
t.asString().codePointAt(0)
)
),
new Sub(text, 1)
)
).value().asString();
}));
),
new Sub(t, 1)
)
)
)
);
}
}
45 changes: 45 additions & 0 deletions src/main/java/org/cactoos/text/Flattened.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2020 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.text;

import org.cactoos.Scalar;
import org.cactoos.Text;
import org.cactoos.scalar.Mapped;

/**
* {@link Text} from {@link Scalar}.
*
* <p>There is no thread-safety guarantee.
*
* @since 0.46
*/
public final class Flattened extends TextEnvelope {
/**
* Ctor.
* @param sclr The text
*/
public Flattened(final Scalar<Text> sclr) {
super(new TextOf(new Mapped<>(Text::asString, sclr)));
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/cactoos/text/PaddedStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ public final class PaddedStart extends TextEnvelope {
public PaddedStart(
final Text text, final int length, final char symbol) {
super(
new TextOf(
new Flattened(
() -> {
final String original = text.asString();
return new Joined(
new TextOf(""),
new Repeated(
new TextOf(symbol), length - original.length()
),
text
).asString();
new TextOf(original)
);
}
)
);
Expand Down
32 changes: 21 additions & 11 deletions src/main/java/org/cactoos/text/PrefixOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
*/
package org.cactoos.text;

import org.cactoos.Text;
import org.cactoos.func.FuncOf;
import org.cactoos.scalar.ScalarOf;
import org.cactoos.scalar.Ternary;

/**
* Returns a text that is before given boundary.
*
Expand All @@ -38,18 +43,23 @@ public final class PrefixOf extends TextEnvelope {
* @param boundary String to which text will be split
*/
public PrefixOf(final String text, final String boundary) {
this(new TextOf(text), boundary);
}

/**
* Ctor.
* @param text Text representing the text value
* @param boundary String to which text will be split
*/
public PrefixOf(final Text text, final String boundary) {
super(
new TextOf(
() -> {
final String prefix;
final int idx = text.indexOf(boundary);
if (idx >= 0) {
prefix = text.substring(0, idx);
} else {
prefix = text;
}
return prefix;
}
new Flattened(
new Ternary<>(
new ScalarOf<>(() -> new Sticky(text)),
(Text t) -> t.asString().indexOf(boundary) >= 0,
t -> new Sub(t, new FuncOf<>(0), s -> s.indexOf(boundary)),
t -> t
)
)
);
}
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/org/cactoos/text/Sub.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
*/
package org.cactoos.text;

import org.cactoos.Func;
import org.cactoos.Scalar;
import org.cactoos.Text;
import org.cactoos.scalar.Unchecked;
import org.cactoos.func.FuncOf;

/**
* Extract a substring from a Text.
Expand Down Expand Up @@ -60,7 +61,16 @@ public Sub(final String text, final int strt, final int finish) {
* @param strt Start position in the text
*/
public Sub(final Text text, final int strt) {
this(text, () -> strt, () -> text.asString().length());
this(text, new FuncOf<>(strt));
}

/**
* Ctor.
* @param text The Text
* @param strt Start position in the text
*/
public Sub(final Text text, final Func<String, Integer> strt) {
this(text, strt, s -> s.length());
}

/**
Expand All @@ -81,7 +91,7 @@ public Sub(final Text text, final int strt, final int finish) {
*/
public Sub(final Text text, final Scalar<Integer> strt,
final Scalar<Integer> finish) {
this(text, new Unchecked<>(strt), new Unchecked<>(finish));
this(text, new FuncOf<>(strt), new FuncOf<>(finish));
}

/**
Expand All @@ -90,16 +100,16 @@ public Sub(final Text text, final Scalar<Integer> strt,
* @param start Start position in the text
* @param end End position in the text
*/
public Sub(final Text text, final Unchecked<Integer> start,
final Unchecked<Integer> end) {
public Sub(final Text text, final Func<String, Integer> start,
final Func<String, Integer> end) {
super(
new Mapped(
origin -> {
int begin = start.value();
int begin = start.apply(origin);
if (begin < 0) {
begin = 0;
}
int finish = end.value();
int finish = end.apply(origin);
if (origin.length() < finish) {
finish = origin.length();
}
Expand Down
34 changes: 23 additions & 11 deletions src/main/java/org/cactoos/text/SuffixOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
*/
package org.cactoos.text;

import org.cactoos.Text;
import org.cactoos.scalar.ScalarOf;
import org.cactoos.scalar.Ternary;

/**
* Returns a text that is after given boundary.
*
Expand All @@ -38,18 +42,26 @@ public final class SuffixOf extends TextEnvelope {
* @param boundary String after which text will be split
*/
public SuffixOf(final String text, final String boundary) {
this(new TextOf(text), boundary);
}

/**
* Ctor.
* @param text Text representing the text value
* @param boundary String after which text will be split
*/
public SuffixOf(final Text text, final String boundary) {
super(
new TextOf(
() -> {
final String suffix;
final int idx = text.indexOf(boundary);
if (idx >= 0) {
suffix = text.substring(idx + boundary.length());
} else {
suffix = "";
}
return suffix;
}
new Flattened(
new Ternary<>(
new ScalarOf<>(() -> new Sticky(text)),
(Text t) -> t.asString().indexOf(boundary) >= 0,
t -> new Sub(
t,
s -> s.indexOf(boundary) + boundary.length()
),
t -> new TextOf("")
)
)
);
}
Expand Down
Loading

0 comments on commit 80e0aa8

Please sign in to comment.