Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Sep 13, 2020
2 parents 07b063e + 800fd3e commit 47aff6e
Show file tree
Hide file tree
Showing 28 changed files with 442 additions and 428 deletions.
51 changes: 28 additions & 23 deletions src/main/java/org/cactoos/text/Abbreviated.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.cactoos.text;

import org.cactoos.Scalar;
import org.cactoos.Text;

/**
Expand All @@ -41,9 +40,9 @@ public final class Abbreviated extends TextEnvelope {
private static final int MAX_WIDTH = 80;

/**
* The ellipses width.
* The ellipses.
*/
private static final int ELLIPSES_WIDTH = 3;
private static final String ELLIPSES = "...";

/**
* Ctor.
Expand Down Expand Up @@ -81,27 +80,33 @@ 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`.
*/
@SuppressWarnings({
"PMD.CallSuperInConstructor",
"PMD.ConstructorOnlyInitializesOrCallOtherConstructors"
})
public Abbreviated(final Text text, final int max) {
super((Scalar<String>) () -> {
final Text abbreviated;
if (text.asString().length() <= max) {
abbreviated = text;
} else {
abbreviated = new FormattedText(
"%s...",
new Sub(
text,
0,
max - Abbreviated.ELLIPSES_WIDTH
).asString()
);
}
return abbreviated.asString();
});
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();
}
)
);
}
}
5 changes: 2 additions & 3 deletions src/main/java/org/cactoos/text/Base64Encoded.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

package org.cactoos.text;

import org.cactoos.Scalar;
import org.cactoos.Text;
import org.cactoos.bytes.BytesBase64;
import org.cactoos.io.BytesOf;
Expand All @@ -51,10 +50,10 @@ public Base64Encoded(final String input) {
* @param origin Origin text
*/
public Base64Encoded(final Text origin) {
super((Scalar<String>) () -> new TextOf(
super(new TextOf(
new BytesBase64(
new BytesOf(origin)
)
).asString());
));
}
}
26 changes: 13 additions & 13 deletions src/main/java/org/cactoos/text/FormattedText.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Collection;
import java.util.Formatter;
import java.util.Locale;
import org.cactoos.Scalar;
import org.cactoos.Text;
import org.cactoos.list.ListOf;

Expand Down Expand Up @@ -136,18 +135,19 @@ public FormattedText(
final Locale locale,
final Collection<Object> args
) {
super(new Scalar<String>() {
@Override
public String value() throws Exception {
final StringBuilder out = new StringBuilder(0);
try (Formatter fmt = new Formatter(out, locale)) {
fmt.format(
ptn.asString(),
args.toArray()
);
super(
new TextOf(
() -> {
final StringBuilder out = new StringBuilder(0);
try (Formatter fmt = new Formatter(out, locale)) {
fmt.format(
ptn.asString(),
args.toArray()
);
}
return out.toString();
}
return out.toString();
}
});
)
);
}
}
31 changes: 15 additions & 16 deletions src/main/java/org/cactoos/text/HexOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.cactoos.text;

import org.cactoos.Bytes;
import org.cactoos.Scalar;

/**
* Hexadecimal representation of Bytes.
Expand All @@ -33,7 +32,6 @@
*
* @since 0.28
*/
@SuppressWarnings("PMD.ConstructorShouldDoInitialization")
public final class HexOf extends TextEnvelope {

/**
Expand All @@ -49,20 +47,21 @@ public final class HexOf extends TextEnvelope {
* @param bytes The bytes
*/
public HexOf(final Bytes bytes) {
super(new Scalar<String>() {
@Override
public String value() throws Exception {
final byte[] bts = bytes.asBytes();
final char[] hex = new char[bts.length * 2];
int chr = -1;
for (int idx = 0; idx < bts.length; ++idx) {
// @checkstyle MagicNumber (3 line)
final int value = 0xff & bts[idx];
hex[++chr] = HexOf.HEX_CHARS[value >>> 4];
hex[++chr] = HexOf.HEX_CHARS[value & 0x0f];
super(
new TextOf(
() -> {
final byte[] bts = bytes.asBytes();
final char[] hex = new char[bts.length * 2];
int chr = -1;
for (int idx = 0; idx < bts.length; ++idx) {
// @checkstyle MagicNumber (3 line)
final int value = 0xff & bts[idx];
hex[++chr] = HexOf.HEX_CHARS[value >>> 4];
hex[++chr] = HexOf.HEX_CHARS[value & 0x0f];
}
return new String(hex);
}
return new String(hex);
}
});
)
);
}
}
20 changes: 11 additions & 9 deletions src/main/java/org/cactoos/text/Joined.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.cactoos.text;

import java.util.StringJoiner;
import org.cactoos.Scalar;
import org.cactoos.Text;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Mapped;
Expand Down Expand Up @@ -83,13 +82,16 @@ public Joined(final String delimit, final Text... txts) {
* @param txts Texts to be joined
*/
public Joined(final Text delimit, final Iterable<? extends Text> txts) {
super((Scalar<String>) () -> {
final StringJoiner joint =
new StringJoiner(delimit.asString());
for (final Text text : txts) {
joint.add(text.asString());
}
return joint.toString();
});
super(
new TextOf(
() -> {
final StringJoiner joint = new StringJoiner(delimit.asString());
for (final Text text : txts) {
joint.add(text.asString());
}
return joint.toString();
}
)
);
}
}
9 changes: 6 additions & 3 deletions src/main/java/org/cactoos/text/Lowered.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.cactoos.text;

import java.util.Locale;
import org.cactoos.Scalar;
import org.cactoos.Text;

/**
Expand Down Expand Up @@ -56,9 +55,13 @@ public Lowered(final Text text) {
* Ctor.
* @param text The text
* @param locale The locale
* @todo #1287:30min Introduce `text.Mapped` that takes a `Text` and
* a `Func` from `String` to `String`. Add some tests (included for
* `equals`). Once it is done, replace the code below by the use of
* `text.Mapped`. Do the same for all the one-liner mapping of the
* `text` package (e.g., `Upper`, `Trimmed`).
*/
public Lowered(final Text text, final Locale locale) {
super((Scalar<String>) () -> text.asString().toLowerCase(locale));
super(new TextOf(() -> text.asString().toLowerCase(locale)));
}

}
34 changes: 17 additions & 17 deletions src/main/java/org/cactoos/text/NoNulls.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.cactoos.text;

import org.cactoos.Scalar;
import org.cactoos.Text;

/**
Expand All @@ -39,22 +38,23 @@ public final class NoNulls extends TextEnvelope {
* @param text The text
*/
public NoNulls(final Text text) {
super(new Scalar<String>() {
@Override
public String value() throws Exception {
if (text == null) {
throw new IllegalArgumentException(
"NULL instead of a valid text"
);
super(
new TextOf(
() -> {
if (text == null) {
throw new IllegalArgumentException(
"NULL instead of a valid text"
);
}
final String string = text.asString();
if (string == null) {
throw new IllegalStateException(
"NULL instead of a valid result string"
);
}
return string;
}
final String string = text.asString();
if (string == null) {
throw new IllegalStateException(
"NULL instead of a valid result string"
);
}
return string;
}
});
)
);
}
}
5 changes: 2 additions & 3 deletions src/main/java/org/cactoos/text/Normalized.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.cactoos.text;

import org.cactoos.Scalar;
import org.cactoos.Text;

/**
Expand All @@ -48,11 +47,11 @@ public Normalized(final String text) {
*/
public Normalized(final Text text) {
super(
(Scalar<String>) () -> new Replaced(
new Replaced(
new Trimmed(text),
"\\s+",
" "
).asString()
)
);
}
}
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/org/cactoos/text/PaddedStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.cactoos.text;

import org.cactoos.Scalar;
import org.cactoos.Text;

/**
Expand All @@ -43,16 +42,20 @@ public final class PaddedStart extends TextEnvelope {
*/
public PaddedStart(
final Text text, final int length, final char symbol) {
super((Scalar<String>) () -> {
final String original = text.asString();
return new Joined(
new TextOf(""),
new Repeated(
new TextOf(symbol), length - original.length()
),
text
).asString();
});
super(
new TextOf(
() -> {
final String original = text.asString();
return new Joined(
new TextOf(""),
new Repeated(
new TextOf(symbol), length - original.length()
),
text
).asString();
}
)
);
}
}

28 changes: 14 additions & 14 deletions src/main/java/org/cactoos/text/PrefixOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*/
package org.cactoos.text;

import org.cactoos.Scalar;

/**
* Returns a text that is before given boundary.
*
Expand All @@ -39,18 +37,20 @@ public final class PrefixOf extends TextEnvelope {
* @param text Text representing the text value
* @param boundary String to which text will be split
*/
@SuppressWarnings({"PMD.CallSuperInConstructor",
"PMD.ConstructorOnlyInitializesOrCallOtherConstructors"})
public PrefixOf(final String text, final String boundary) {
super((Scalar<String>) () -> {
final String prefix;
final int idx = text.indexOf(boundary);
if (idx >= 0) {
prefix = text.substring(0, idx);
} else {
prefix = text;
}
return prefix;
});
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;
}
)
);
}
}
Loading

3 comments on commit 47aff6e

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 47aff6e Sep 13, 2020

Choose a reason for hiding this comment

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

Puzzle 1287-014a5155 discovered in src/main/java/org/cactoos/text/Abbreviated.java and submitted as #1460. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 47aff6e Sep 13, 2020

Choose a reason for hiding this comment

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

Puzzle 1287-48d7a962 discovered in src/main/java/org/cactoos/text/TextOf.java and submitted as #1461. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 47aff6e Sep 13, 2020

Choose a reason for hiding this comment

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

Puzzle 1287-1c7c8289 discovered in src/main/java/org/cactoos/text/Lowered.java and submitted as #1462. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.