From 910ce3d34ac9868f2448f73a90102e777b7e413a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Sun, 27 Dec 2020 16:36:17 +0100 Subject: [PATCH 1/2] (#1460) Introduce text.Flattened and use it where relevant --- .../java/org/cactoos/text/Abbreviated.java | 42 +++++----- .../java/org/cactoos/text/Capitalized.java | 35 ++++---- src/main/java/org/cactoos/text/Flattened.java | 45 ++++++++++ .../java/org/cactoos/text/PaddedStart.java | 6 +- src/main/java/org/cactoos/text/PrefixOf.java | 32 ++++--- src/main/java/org/cactoos/text/Sub.java | 24 ++++-- src/main/java/org/cactoos/text/SuffixOf.java | 34 +++++--- .../org/cactoos/text/AbbreviatedTest.java | 83 +++++++++++++------ .../java/org/cactoos/text/FlattenedTest.java | 68 +++++++++++++++ 9 files changed, 273 insertions(+), 96 deletions(-) create mode 100644 src/main/java/org/cactoos/text/Flattened.java create mode 100644 src/test/java/org/cactoos/text/FlattenedTest.java diff --git a/src/main/java/org/cactoos/text/Abbreviated.java b/src/main/java/org/cactoos/text/Abbreviated.java index aba60ad044..9aecf94dec 100644 --- a/src/main/java/org/cactoos/text/Abbreviated.java +++ b/src/main/java/org/cactoos/text/Abbreviated.java @@ -24,6 +24,9 @@ package org.cactoos.text; import org.cactoos.Text; +import org.cactoos.scalar.LengthOf; +import org.cactoos.scalar.ScalarOf; +import org.cactoos.scalar.Ternary; /** * Abbreviates a Text using ellipses. @@ -31,6 +34,7 @@ *

There is no thread-safety guarantee. * * @since 0.29 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ public final class Abbreviated extends TextEnvelope { @@ -80,32 +84,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) -> new LengthOf(t).longValue() <= max, + t -> t, + t -> new FormattedText( + "%s%s", + new Sub( + t, + 0, + max - Abbreviated.ELLIPSES.length() + ), + Abbreviated.ELLIPSES + ) + ) ) ); } diff --git a/src/main/java/org/cactoos/text/Capitalized.java b/src/main/java/org/cactoos/text/Capitalized.java index 79584fa8e8..9e89452979 100644 --- a/src/main/java/org/cactoos/text/Capitalized.java +++ b/src/main/java/org/cactoos/text/Capitalized.java @@ -24,6 +24,7 @@ package org.cactoos.text; import org.cactoos.Text; +import org.cactoos.scalar.ScalarOf; import org.cactoos.scalar.Ternary; /** @@ -32,6 +33,7 @@ * no other characters are changed. * * @since 0.46 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ public final class Capitalized extends TextEnvelope { @@ -50,23 +52,24 @@ public Capitalized(final String text) { */ public Capitalized(final Text text) { super( - new TextOf( - () -> { - return new Ternary( - 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) + ) + ) + ) + ); } } diff --git a/src/main/java/org/cactoos/text/Flattened.java b/src/main/java/org/cactoos/text/Flattened.java new file mode 100644 index 0000000000..7d0a340c22 --- /dev/null +++ b/src/main/java/org/cactoos/text/Flattened.java @@ -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}. + * + *

There is no thread-safety guarantee. + * + * @since 0.46 + */ +public final class Flattened extends TextEnvelope { + /** + * Ctor. + * @param sclr The text + */ + public Flattened(final Scalar sclr) { + super(new TextOf(new Mapped<>(Text::asString, sclr))); + } +} diff --git a/src/main/java/org/cactoos/text/PaddedStart.java b/src/main/java/org/cactoos/text/PaddedStart.java index 0acd168c3b..6f0711d8d3 100644 --- a/src/main/java/org/cactoos/text/PaddedStart.java +++ b/src/main/java/org/cactoos/text/PaddedStart.java @@ -43,7 +43,7 @@ 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( @@ -51,8 +51,8 @@ public PaddedStart( new Repeated( new TextOf(symbol), length - original.length() ), - text - ).asString(); + new TextOf(original) + ); } ) ); diff --git a/src/main/java/org/cactoos/text/PrefixOf.java b/src/main/java/org/cactoos/text/PrefixOf.java index 4b46b979b9..2225732133 100644 --- a/src/main/java/org/cactoos/text/PrefixOf.java +++ b/src/main/java/org/cactoos/text/PrefixOf.java @@ -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. * @@ -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 + ) ) ); } diff --git a/src/main/java/org/cactoos/text/Sub.java b/src/main/java/org/cactoos/text/Sub.java index 8a9975deb8..6dcda238bc 100644 --- a/src/main/java/org/cactoos/text/Sub.java +++ b/src/main/java/org/cactoos/text/Sub.java @@ -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. @@ -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 strt) { + this(text, strt, s -> s.length()); } /** @@ -81,7 +91,7 @@ public Sub(final Text text, final int strt, final int finish) { */ public Sub(final Text text, final Scalar strt, final Scalar finish) { - this(text, new Unchecked<>(strt), new Unchecked<>(finish)); + this(text, new FuncOf<>(strt), new FuncOf<>(finish)); } /** @@ -90,16 +100,16 @@ public Sub(final Text text, final Scalar strt, * @param start Start position in the text * @param end End position in the text */ - public Sub(final Text text, final Unchecked start, - final Unchecked end) { + public Sub(final Text text, final Func start, + final Func 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(); } diff --git a/src/main/java/org/cactoos/text/SuffixOf.java b/src/main/java/org/cactoos/text/SuffixOf.java index 8d064c278c..42acaec477 100644 --- a/src/main/java/org/cactoos/text/SuffixOf.java +++ b/src/main/java/org/cactoos/text/SuffixOf.java @@ -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. * @@ -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("") + ) ) ); } diff --git a/src/test/java/org/cactoos/text/AbbreviatedTest.java b/src/test/java/org/cactoos/text/AbbreviatedTest.java index 4c550a7af8..9b9fdf74ae 100644 --- a/src/test/java/org/cactoos/text/AbbreviatedTest.java +++ b/src/test/java/org/cactoos/text/AbbreviatedTest.java @@ -23,14 +23,20 @@ */ package org.cactoos.text; +import java.util.concurrent.atomic.AtomicInteger; +import org.cactoos.Text; +import org.cactoos.iterable.IterableOf; +import org.hamcrest.core.AllOf; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; -import org.llorllale.cactoos.matchers.TextHasString; +import org.llorllale.cactoos.matchers.TextIs; /** * Test case for {@link Abbreviated}. * @since 0.29 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ @SuppressWarnings("PMD.TooManyMethods") final class AbbreviatedTest { @@ -39,30 +45,27 @@ final class AbbreviatedTest { void abbreviatesAnEmptyText() { final String msg = ""; new Assertion<>( - "Can't abbreviate an msg text", - // @checkstyle MagicNumber (1 line) + "Must abbreviate an msg text", new Abbreviated(msg, 8), - new TextHasString(msg) + new TextIs(msg) ).affirm(); } @Test void abbreviatesText() { new Assertion<>( - "Can't abbreviate a text", - // @checkstyle MagicNumber (1 line) + "Must abbreviate a text", new Abbreviated("hello world", 8), - new TextHasString("hello...") + new TextIs("hello...") ).affirm(); } @Test void abbreviatesTextOneCharSmaller() { new Assertion<>( - "Can't abbreviate a text one char smaller", - // @checkstyle MagicNumber (1 line) + "Must abbreviate a text one char smaller", new Abbreviated("oo programming", 10), - new TextHasString("oo prog...") + new TextIs("oo prog...") ).affirm(); } @@ -70,10 +73,9 @@ void abbreviatesTextOneCharSmaller() { void abbreviatesTextWithSameLength() { final String msg = "elegant objects"; new Assertion<>( - "Can't abbreviate a text with same length", - // @checkstyle MagicNumber (1 line) + "Must abbreviate a text with same length", new Abbreviated(msg, 15), - new TextHasString(msg) + new TextIs(msg) ).affirm(); } @@ -81,10 +83,9 @@ void abbreviatesTextWithSameLength() { void abbreviatesTextOneCharBigger() { final String msg = "the old mcdonald"; new Assertion<>( - "Can't abbreviate a text one char bigger", - // @checkstyle MagicNumber (1 line) + "Must abbreviate a text one char bigger", new Abbreviated(msg, 17), - new TextHasString(msg) + new TextIs(msg) ).affirm(); } @@ -92,10 +93,9 @@ void abbreviatesTextOneCharBigger() { void abbreviatesTextTwoCharsBigger() { final String msg = "hi everybody!"; new Assertion<>( - "Can't abbreviate a text two chars bigger", - // @checkstyle MagicNumber (1 line) + "Must abbreviate a text two chars bigger", new Abbreviated(msg, 15), - new TextHasString(msg) + new TextIs(msg) ).affirm(); } @@ -103,25 +103,58 @@ void abbreviatesTextTwoCharsBigger() { void abbreviatesTextWithWidthBiggerThanLength() { final String msg = "cactoos framework"; new Assertion<>( - "Can't abbreviate a text with width bigger than length", - // @checkstyle MagicNumber (1 line) + "Must abbreviate a text with width bigger than length", new Abbreviated(msg, 50), - new TextHasString(msg) + new TextIs(msg) ).affirm(); } @Test void abbreviatesTextBiggerThanDefaultMaxWidth() { - // @checkstyle LineLengthCheck (10 line) new Assertion<>( - "Can't abbreviate a text bigger than default max width", + "Must abbreviate a text bigger than default max width", new Abbreviated( + // @checkstyle LineLengthCheck (1 line) "The quick brown fox jumps over the lazy black dog and after that returned to the cave" ), - new TextHasString( + new TextIs( + // @checkstyle LineLengthCheck (1 line) "The quick brown fox jumps over the lazy black dog and after that returned to ..." ) ).affirm(); } + @Test + @SuppressWarnings("unchecked") + void abbreviatesTextThatChanges() { + final AtomicInteger counter = new AtomicInteger(0); + final Text txt = new TextOf( + () -> { + final String result; + if (counter.getAndIncrement() == 0) { + result = "The quick brown fox jumps"; + } else { + result = "The lazy black dog"; + } + return result; + } + ); + new Assertion<>( + "Must abbreviate a text that changes", + new Abbreviated( + txt, + 15 + ), + new AllOf<>( + new IterableOf<>( + new TextIs( + "The quick br..." + ), + new TextIs( + "The lazy bla..." + ) + ) + ) + ).affirm(); + } } diff --git a/src/test/java/org/cactoos/text/FlattenedTest.java b/src/test/java/org/cactoos/text/FlattenedTest.java new file mode 100644 index 0000000000..20a5a0f158 --- /dev/null +++ b/src/test/java/org/cactoos/text/FlattenedTest.java @@ -0,0 +1,68 @@ +/* + * 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.Text; +import org.cactoos.iterable.IterableOf; +import org.cactoos.iterable.Mapped; +import org.cactoos.scalar.ScalarOf; +import org.hamcrest.core.AllOf; +import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.TextIs; + +/** +* Tests for {@link Flattened}. +* +* @since 0.49 +* @checkstyle ClassDataAbstractionCouplingCheck (500 lines) +*/ +final class FlattenedTest { + @Test + void flattens() { + final Text txt = new TextOf("txt"); + new Assertion<>( + "must flatten", + new Flattened( + new ScalarOf<>(() -> txt) + ), + new TextIs(txt) + ).affirm(); + } + + @Test + void flattensTextThatChanges() { + final Iterable txts = new IterableOf<>( + new TextOf("txt1"), + new TextOf("txt2") + ); + new Assertion<>( + "must flatten a scalar that changes", + new Flattened( + new ScalarOf<>(txts.iterator()::next) + ), + new AllOf(new Mapped<>(TextIs::new, txts)) + ).affirm(); + } +} From 5541aacd72db25bcfe49375723e4ad26529a0f6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Mon, 28 Dec 2020 15:23:27 +0100 Subject: [PATCH 2/2] (#1460) Introduce scalar.Flattened and func.Flattened --- src/main/java/org/cactoos/func/Flattened.java | 46 ++++++++++++++++ src/main/java/org/cactoos/func/FuncOf.java | 15 +++--- .../java/org/cactoos/scalar/Flattened.java | 44 +++++++++++++++ .../java/org/cactoos/scalar/ScalarOf.java | 13 +++++ .../java/org/cactoos/text/Capitalized.java | 2 +- .../java/org/cactoos/func/FlattenedTest.java | 47 ++++++++++++++++ .../org/cactoos/scalar/FlattenedTest.java | 53 +++++++++++++++++++ 7 files changed, 212 insertions(+), 8 deletions(-) create mode 100644 src/main/java/org/cactoos/func/Flattened.java create mode 100644 src/main/java/org/cactoos/scalar/Flattened.java create mode 100644 src/test/java/org/cactoos/func/FlattenedTest.java create mode 100644 src/test/java/org/cactoos/scalar/FlattenedTest.java diff --git a/src/main/java/org/cactoos/func/Flattened.java b/src/main/java/org/cactoos/func/Flattened.java new file mode 100644 index 0000000000..762583f33e --- /dev/null +++ b/src/main/java/org/cactoos/func/Flattened.java @@ -0,0 +1,46 @@ +/* + * 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.func; + +import org.cactoos.Func; +import org.cactoos.Scalar; + +/** + * {@link Func} from {@link Func} of {@link Scalar}. + * + *

There is no thread-safety guarantee. + * + * @param Type of input + * @param Type of output + * @since 0.48 + */ +public final class Flattened extends FuncEnvelope { + /** + * Ctor. + * @param sclr The func + */ + public Flattened(final Func> sclr) { + super(x -> sclr.apply(x).value()); + } +} diff --git a/src/main/java/org/cactoos/func/FuncOf.java b/src/main/java/org/cactoos/func/FuncOf.java index 7e464ba71c..a332bea62e 100644 --- a/src/main/java/org/cactoos/func/FuncOf.java +++ b/src/main/java/org/cactoos/func/FuncOf.java @@ -27,6 +27,9 @@ import org.cactoos.Func; import org.cactoos.Proc; import org.cactoos.Scalar; +import org.cactoos.scalar.Constant; +import org.cactoos.scalar.ScalarOf; +import org.cactoos.scalar.ScalarOfCallable; /** * Represents many possible inputs as {@link Func}. @@ -49,7 +52,7 @@ public final class FuncOf implements Func { * @param result The result */ public FuncOf(final Y result) { - this((Func) input -> result); + this(new Constant<>(result)); } /** @@ -57,7 +60,7 @@ public FuncOf(final Y result) { * @param callable The callable */ public FuncOf(final Callable callable) { - this((Func) input -> callable.call()); + this(new ScalarOfCallable<>(callable)); } /** @@ -67,7 +70,7 @@ public FuncOf(final Callable callable) { * @since 0.32 */ public FuncOf(final Runnable runnable, final Y result) { - this(input -> runnable.run(), result); + this(new ScalarOf<>(runnable, result)); } /** @@ -89,16 +92,14 @@ public FuncOf(final Proc proc, final Y result) { * @param scalar Origin scalar */ public FuncOf(final Scalar scalar) { - this(input -> { - return scalar.value(); - }); + this(input -> scalar.value()); } /** * Ctor. * @param fnc Func */ - private FuncOf(final Func fnc) { + public FuncOf(final Func fnc) { this.func = fnc; } diff --git a/src/main/java/org/cactoos/scalar/Flattened.java b/src/main/java/org/cactoos/scalar/Flattened.java new file mode 100644 index 0000000000..d66fc2134d --- /dev/null +++ b/src/main/java/org/cactoos/scalar/Flattened.java @@ -0,0 +1,44 @@ +/* + * 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.scalar; + +import org.cactoos.Scalar; + +/** + * {@link Scalar} from {@link Scalar} of {@link Scalar}. + * + *

There is no thread-safety guarantee. + * + * @param Element type + * @since 0.48 + */ +public final class Flattened extends ScalarEnvelope { + /** + * Ctor. + * @param sclr The func + */ + public Flattened(final Scalar> sclr) { + super(() -> sclr.value().value()); + } +} diff --git a/src/main/java/org/cactoos/scalar/ScalarOf.java b/src/main/java/org/cactoos/scalar/ScalarOf.java index 1d6d350eb7..d7b8b65a08 100644 --- a/src/main/java/org/cactoos/scalar/ScalarOf.java +++ b/src/main/java/org/cactoos/scalar/ScalarOf.java @@ -38,6 +38,19 @@ public final class ScalarOf implements Scalar { */ private final Scalar origin; + /** + * Ctor. + * + * @param runnable The runnable + * @param result Result to return + */ + public ScalarOf(final Runnable runnable, final T result) { + this(() -> { + runnable.run(); + return result; + }); + } + /** * Ctor. * diff --git a/src/main/java/org/cactoos/text/Capitalized.java b/src/main/java/org/cactoos/text/Capitalized.java index 9e89452979..904fb28e5a 100644 --- a/src/main/java/org/cactoos/text/Capitalized.java +++ b/src/main/java/org/cactoos/text/Capitalized.java @@ -55,7 +55,7 @@ public Capitalized(final Text text) { new Flattened( new Ternary<>( new ScalarOf<>(() -> new Sticky(text)), - (Text t) -> new IsBlank(t).value(), + new org.cactoos.func.Flattened<>(IsBlank::new), t -> t, t -> new Joined( "", diff --git a/src/test/java/org/cactoos/func/FlattenedTest.java b/src/test/java/org/cactoos/func/FlattenedTest.java new file mode 100644 index 0000000000..04722eb6b7 --- /dev/null +++ b/src/test/java/org/cactoos/func/FlattenedTest.java @@ -0,0 +1,47 @@ +/* + * 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.func; + +import org.cactoos.scalar.BoolOf; +import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.FuncApplies; + +/** +* Tests for {@link Flattened}. +* +* @since 0.49 +*/ +final class FlattenedTest { + @Test + void flattens() { + new Assertion<>( + "must flatten", + new Flattened<>( + new FuncOf<>(x -> new BoolOf(x)) + ), + new FuncApplies<>("true", true) + ).affirm(); + } +} diff --git a/src/test/java/org/cactoos/scalar/FlattenedTest.java b/src/test/java/org/cactoos/scalar/FlattenedTest.java new file mode 100644 index 0000000000..d2029cfee0 --- /dev/null +++ b/src/test/java/org/cactoos/scalar/FlattenedTest.java @@ -0,0 +1,53 @@ +/* + * 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.scalar; + +import org.cactoos.Scalar; +import org.cactoos.Text; +import org.cactoos.text.TextOf; +import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.ScalarHasValue; +import org.llorllale.cactoos.matchers.TextIs; + +/** +* Tests for {@link Flattened}. +* +* @since 0.49 +* @checkstyle ClassDataAbstractionCouplingCheck (500 lines) +*/ +final class FlattenedTest { + @Test + void flattens() { + final Text txt = new TextOf("txt"); + final Scalar sclr = new Constant<>(txt); + new Assertion<>( + "must flatten", + new Flattened<>( + new ScalarOf<>(() -> sclr) + ), + new ScalarHasValue<>(new TextIs(txt)) + ).affirm(); + } +}