Skip to content

Commit

Permalink
(yegor256#1460) Introduce scalar.Flattened and func.Flattened
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Dec 28, 2020
1 parent b6ff2f6 commit affdb4a
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 8 deletions.
44 changes: 44 additions & 0 deletions src/main/java/org/cactoos/func/Flattened.java
Original file line number Diff line number Diff line change
@@ -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}.
*
* <p>There is no thread-safety guarantee.
*
* @since 0.48
*/
public final class Flattened<X, Y> extends FuncEnvelope<X, Y> {
/**
* Ctor.
* @param sclr The func
*/
public Flattened(final Func<X, Scalar<Y>> sclr) {
super(x -> sclr.apply(x).value());
}
}
15 changes: 8 additions & 7 deletions src/main/java/org/cactoos/func/FuncOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -49,15 +52,15 @@ public final class FuncOf<X, Y> implements Func<X, Y> {
* @param result The result
*/
public FuncOf(final Y result) {
this((Func<X, Y>) input -> result);
this(new Constant<>(result));
}

/**
* Ctor.
* @param callable The callable
*/
public FuncOf(final Callable<Y> callable) {
this((Func<X, Y>) input -> callable.call());
this(new ScalarOfCallable<>(callable));
}

/**
Expand All @@ -67,7 +70,7 @@ public FuncOf(final Callable<Y> callable) {
* @since 0.32
*/
public FuncOf(final Runnable runnable, final Y result) {
this(input -> runnable.run(), result);
this(new ScalarOf<>(runnable, result));
}

/**
Expand All @@ -89,16 +92,14 @@ public FuncOf(final Proc<X> proc, final Y result) {
* @param scalar Origin scalar
*/
public FuncOf(final Scalar<Y> scalar) {
this(input -> {
return scalar.value();
});
this(input -> scalar.value());
}

/**
* Ctor.
* @param fnc Func
*/
private FuncOf(final Func<X, Y> fnc) {
public FuncOf(final Func<X, Y> fnc) {
this.func = fnc;
}

Expand Down
43 changes: 43 additions & 0 deletions src/main/java/org/cactoos/scalar/Flattened.java
Original file line number Diff line number Diff line change
@@ -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}.
*
* <p>There is no thread-safety guarantee.
*
* @since 0.48
*/
public final class Flattened<X> extends ScalarEnvelope<X> {
/**
* Ctor.
* @param sclr The func
*/
public Flattened(final Scalar<Scalar<X>> sclr) {
super(() -> sclr.value().value());
}
}
12 changes: 12 additions & 0 deletions src/main/java/org/cactoos/scalar/ScalarOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ public final class ScalarOf<T> implements Scalar<T> {
*/
private final Scalar<T> origin;

/**
* Ctor.
*
* @param origin The scalar
*/
public ScalarOf(final Runnable runnable, final T result) {
this(() -> {
runnable.run();
return result;
});
}

/**
* Ctor.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/text/Capitalized.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"",
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/org/cactoos/func/FlattenedTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
53 changes: 53 additions & 0 deletions src/test/java/org/cactoos/scalar/FlattenedTest.java
Original file line number Diff line number Diff line change
@@ -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<Text> sclr = new Constant<>(txt);
new Assertion<>(
"must flatten",
new Flattened<>(
new ScalarOf<>(() -> sclr)
),
new ScalarHasValue<>(new TextIs(txt))
).affirm();
}
}

0 comments on commit affdb4a

Please sign in to comment.