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..9dab491bd5 --- /dev/null +++ b/src/main/java/org/cactoos/func/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.func; + +import org.cactoos.Func; +import org.cactoos.Scalar; + +/** + * {@link Func} from {@link Func} of {@link Scalar}. + * + *

There is no thread-safety guarantee. + * + * @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..493466a4e6 --- /dev/null +++ b/src/main/java/org/cactoos/scalar/Flattened.java @@ -0,0 +1,43 @@ +/* + * 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. + * + * @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..3861ad50f0 100644 --- a/src/main/java/org/cactoos/scalar/ScalarOf.java +++ b/src/main/java/org/cactoos/scalar/ScalarOf.java @@ -38,6 +38,18 @@ public final class ScalarOf implements Scalar { */ private final Scalar origin; + /** + * Ctor. + * + * @param origin The scalar + */ + 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(); + } +}