Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jan 10, 2021
2 parents 7138346 + 5541aac commit 0960e39
Show file tree
Hide file tree
Showing 15 changed files with 484 additions and 103 deletions.
46 changes: 46 additions & 0 deletions src/main/java/org/cactoos/func/Flattened.java
Original file line number Diff line number Diff line change
@@ -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}.
*
* <p>There is no thread-safety guarantee.
*
* @param <X> Type of input
* @param <Y> Type of output
* @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
44 changes: 44 additions & 0 deletions src/main/java/org/cactoos/scalar/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.scalar;

import org.cactoos.Scalar;

/**
* {@link Scalar} from {@link Scalar} of {@link Scalar}.
*
* <p>There is no thread-safety guarantee.
*
* @param <X> Element type
* @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());
}
}
13 changes: 13 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,19 @@ public final class ScalarOf<T> implements Scalar<T> {
*/
private final Scalar<T> 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.
*
Expand Down
42 changes: 19 additions & 23 deletions src/main/java/org/cactoos/text/Abbreviated.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@
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.
*
* <p>There is no thread-safety guarantee.
*
* @since 0.29
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
public final class Abbreviated extends TextEnvelope {

Expand Down Expand Up @@ -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
)
)
)
);
}
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)),
new org.cactoos.func.Flattened<>(IsBlank::new),
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
Loading

1 comment on commit 0960e39

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 0960e39 Jan 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1287-014a5155 disappeared from src/main/java/org/cactoos/text/Abbreviated.java, that's why I closed #1460. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.