From 3ffa5dfe0378f63f52a8b515220c69581bf5e5bf Mon Sep 17 00:00:00 2001 From: Constantin Costescu Date: Wed, 29 Jul 2020 00:36:31 +0200 Subject: [PATCH 1/3] #1135: Extends TextEnvelope when possible --- src/main/java/org/cactoos/text/HexOf.java | 44 +++++++--------- .../java/org/cactoos/text/Randomized.java | 51 +++++-------------- src/main/java/org/cactoos/text/Trimmed.java | 21 +++----- .../java/org/cactoos/text/TrimmedLeft.java | 35 ++++++------- .../java/org/cactoos/text/TrimmedRight.java | 31 +++++------ .../java/org/cactoos/text/UncheckedText.java | 4 ++ src/main/java/org/cactoos/text/Upper.java | 31 ++++------- .../java/org/cactoos/io/AppendToTest.java | 4 +- .../org/cactoos/iterator/ImmutableTest.java | 2 +- .../java/org/cactoos/text/RandomizedTest.java | 6 +-- 10 files changed, 89 insertions(+), 140 deletions(-) diff --git a/src/main/java/org/cactoos/text/HexOf.java b/src/main/java/org/cactoos/text/HexOf.java index 0b2d0b07c0..c4dde7faf1 100644 --- a/src/main/java/org/cactoos/text/HexOf.java +++ b/src/main/java/org/cactoos/text/HexOf.java @@ -24,7 +24,7 @@ package org.cactoos.text; import org.cactoos.Bytes; -import org.cactoos.Text; +import org.cactoos.Scalar; /** * Hexadecimal representation of Bytes. @@ -34,7 +34,7 @@ * @since 0.28 */ @SuppressWarnings("PMD.ConstructorShouldDoInitialization") -public final class HexOf implements Text { +public final class HexOf extends TextEnvelope { /** * The hexadecimal chars. @@ -44,31 +44,25 @@ public final class HexOf implements Text { '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', }; - /** - * The Bytes. - */ - private final Bytes bytes; - /** * Ctor. - * @param source The bytes + * @param bytes The bytes */ - public HexOf(final Bytes source) { - this.bytes = source; + public HexOf(final Bytes bytes) { + super(new Scalar() { + @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]; + } + return new String(hex); + } + }); } - - @Override - public String asString() throws Exception { - final byte[] bts = this.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); - } - } diff --git a/src/main/java/org/cactoos/text/Randomized.java b/src/main/java/org/cactoos/text/Randomized.java index 6a8297b908..e4fd11beaf 100644 --- a/src/main/java/org/cactoos/text/Randomized.java +++ b/src/main/java/org/cactoos/text/Randomized.java @@ -27,7 +27,6 @@ import java.util.List; import java.util.Random; import org.cactoos.Scalar; -import org.cactoos.Text; import org.cactoos.list.ListOf; import org.cactoos.scalar.Unchecked; @@ -37,33 +36,14 @@ *

There is no thread-safety guarantee. * * @since 0.32 - * @todo #1116:30min All classes implementing Text need to be refactored - * to extend TextEnvelope - asString() should be removed and implementation - * from TextEnvelope should be used. This to-do should be moved to another - * class which need to be refactored. */ -public final class Randomized implements Text { +public final class Randomized extends TextEnvelope { /** * Max length of generated text (if no length is specified). */ private static final int MAX_RANDOM_LENGTH = 255; - /** - * List of characters allowed for generating. - */ - private final List characters; - - /** - * Length of generated text. - */ - private final Scalar length; - - /** - * Characters index randomizer. - */ - private final Random indices; - /** * Ctor. */ @@ -159,21 +139,18 @@ public Randomized(final List chrs, final Scalar len) { * @param len Length of generated text. * @param rnd Characters index randomizer. */ - public Randomized(final List chrs, final Scalar len, - final Random rnd) { - this.characters = chrs; - this.length = len; - this.indices = rnd; - } - - @Override - public String asString() { - final int len = new Unchecked<>(this.length).value(); - final StringBuilder builder = new StringBuilder(len); - final int bound = this.characters.size(); - for (int index = 0; index < len; index = index + 1) { - builder.append(this.characters.get(this.indices.nextInt(bound))); - } - return builder.toString(); + public Randomized(final List chrs, final Scalar len, final Random rnd) { + super(new Scalar() { + @Override + public String value() { + final int length = new Unchecked<>(len).value(); + final StringBuilder builder = new StringBuilder(length); + final int bound = chrs.size(); + for (int index = 0; index < length; index = index + 1) { + builder.append(chrs.get(rnd.nextInt(bound))); + } + return builder.toString(); + } + }); } } diff --git a/src/main/java/org/cactoos/text/Trimmed.java b/src/main/java/org/cactoos/text/Trimmed.java index 4aa7f04dfd..309df542c2 100644 --- a/src/main/java/org/cactoos/text/Trimmed.java +++ b/src/main/java/org/cactoos/text/Trimmed.java @@ -23,6 +23,7 @@ */ package org.cactoos.text; +import org.cactoos.Scalar; import org.cactoos.Text; /** @@ -32,24 +33,18 @@ * * @since 0.1 */ -public final class Trimmed implements Text { - - /** - * The text. - */ - private final Text origin; +public final class Trimmed extends TextEnvelope { /** * Ctor. * @param text The text */ public Trimmed(final Text text) { - this.origin = text; + super(new Scalar() { + @Override + public String value() throws Exception { + return text.asString().trim(); + } + }); } - - @Override - public String asString() throws Exception { - return this.origin.asString().trim(); - } - } diff --git a/src/main/java/org/cactoos/text/TrimmedLeft.java b/src/main/java/org/cactoos/text/TrimmedLeft.java index ce6baf6c3a..cd79f148b7 100644 --- a/src/main/java/org/cactoos/text/TrimmedLeft.java +++ b/src/main/java/org/cactoos/text/TrimmedLeft.java @@ -23,6 +23,7 @@ */ package org.cactoos.text; +import org.cactoos.Scalar; import org.cactoos.Text; /** @@ -32,31 +33,25 @@ * * @since 0.12 */ -public final class TrimmedLeft implements Text { - - /** - * The text. - */ - private final Text origin; +public final class TrimmedLeft extends TextEnvelope { /** * Ctor. * @param text The text */ public TrimmedLeft(final Text text) { - this.origin = text; + super(new Scalar() { + @Override + public String value() throws Exception { + final String string = text.asString(); + int cursor = 0; + while (cursor < string.length() && Character.isWhitespace( + string.charAt(cursor) + )) { + cursor = cursor + 1; + } + return string.substring(cursor); + } + }); } - - @Override - public String asString() throws Exception { - final String text = this.origin.asString(); - int cursor = 0; - while (cursor < text.length() && Character.isWhitespace( - text.charAt(cursor) - )) { - cursor = cursor + 1; - } - return text.substring(cursor); - } - } diff --git a/src/main/java/org/cactoos/text/TrimmedRight.java b/src/main/java/org/cactoos/text/TrimmedRight.java index 23e0f67743..f97d51e81d 100644 --- a/src/main/java/org/cactoos/text/TrimmedRight.java +++ b/src/main/java/org/cactoos/text/TrimmedRight.java @@ -23,6 +23,7 @@ */ package org.cactoos.text; +import org.cactoos.Scalar; import org.cactoos.Text; /** @@ -32,29 +33,23 @@ * * @since 0.12 */ -public final class TrimmedRight implements Text { - - /** - * The text. - */ - private final Text origin; +public final class TrimmedRight extends TextEnvelope { /** * Ctor. * @param text The text */ public TrimmedRight(final Text text) { - this.origin = text; + super(new Scalar() { + @Override + public String value() throws Exception { + final String string = text.asString(); + int cursor = string.length() - 1; + while (cursor >= 0 && Character.isWhitespace(string.charAt(cursor))) { + cursor = cursor - 1; + } + return string.substring(0, cursor + 1); + } + }); } - - @Override - public String asString() throws Exception { - final String text = this.origin.asString(); - int cursor = text.length() - 1; - while (cursor >= 0 && Character.isWhitespace(text.charAt(cursor))) { - cursor = cursor - 1; - } - return text.substring(0, cursor + 1); - } - } diff --git a/src/main/java/org/cactoos/text/UncheckedText.java b/src/main/java/org/cactoos/text/UncheckedText.java index 784e886c3e..fef412abec 100644 --- a/src/main/java/org/cactoos/text/UncheckedText.java +++ b/src/main/java/org/cactoos/text/UncheckedText.java @@ -33,6 +33,10 @@ *

There is no thread-safety guarantee. * * @since 0.3 + * @todo #1135:30min All classes implementing Text need to be refactored + * to extend TextEnvelope - asString() should be removed and implementation + * from TextEnvelope should be used. This to-do should be moved to another + * class which need to be refactored. */ public final class UncheckedText implements Text { diff --git a/src/main/java/org/cactoos/text/Upper.java b/src/main/java/org/cactoos/text/Upper.java index 5636b0332b..36cb154b59 100644 --- a/src/main/java/org/cactoos/text/Upper.java +++ b/src/main/java/org/cactoos/text/Upper.java @@ -24,6 +24,7 @@ package org.cactoos.text; import java.util.Locale; +import org.cactoos.Scalar; import org.cactoos.Text; /** @@ -33,17 +34,7 @@ * * @since 0.1 */ -public final class Upper implements Text { - - /** - * The text. - */ - private final Text origin; - - /** - * The locale. - */ - private final Locale locale; +public final class Upper extends TextEnvelope { /** * Ctor. @@ -64,16 +55,14 @@ public Upper(final Text text) { /** * Ctor. * @param text The text - * @param lang Locale + * @param locale Locale */ - public Upper(final Text text, final Locale lang) { - this.origin = text; - this.locale = lang; - } - - @Override - public String asString() throws Exception { - return this.origin.asString().toUpperCase(this.locale); + public Upper(final Text text, final Locale locale) { + super(new Scalar() { + @Override + public String value() throws Exception { + return text.asString().toUpperCase(locale); + } + }); } - } diff --git a/src/test/java/org/cactoos/io/AppendToTest.java b/src/test/java/org/cactoos/io/AppendToTest.java index 96db27e5a7..3a8b408a54 100644 --- a/src/test/java/org/cactoos/io/AppendToTest.java +++ b/src/test/java/org/cactoos/io/AppendToTest.java @@ -53,10 +53,10 @@ public final class AppendToTest { * Ensures that AppendTo is failing on a negative predicate result. */ @Test - public void failsIfFileDoesNotExist() { + public void failsIfFileDoesNotExist() throws Exception { final File source = new File( new Randomized( - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ).asString() ); new Assertion<>( diff --git a/src/test/java/org/cactoos/iterator/ImmutableTest.java b/src/test/java/org/cactoos/iterator/ImmutableTest.java index e38119f885..7398d4829a 100644 --- a/src/test/java/org/cactoos/iterator/ImmutableTest.java +++ b/src/test/java/org/cactoos/iterator/ImmutableTest.java @@ -73,7 +73,7 @@ public void decoratesHasNext() { } @Test - public void decoratesToString() { + public void decoratesToString() throws Exception { final String string = new Randomized().asString(); final Iterator iterator = new Iterator() { public Object next() { diff --git a/src/test/java/org/cactoos/text/RandomizedTest.java b/src/test/java/org/cactoos/text/RandomizedTest.java index 7735274004..5c91bff8fc 100644 --- a/src/test/java/org/cactoos/text/RandomizedTest.java +++ b/src/test/java/org/cactoos/text/RandomizedTest.java @@ -41,7 +41,7 @@ public final class RandomizedTest { @Test - public void generatesRandomTextOfRandomLength() { + public void generatesRandomTextOfRandomLength() throws Exception { new Assertion<>( "Generated text is empty", new Randomized().asString().length(), @@ -50,7 +50,7 @@ public void generatesRandomTextOfRandomLength() { } @Test - public void generatesRandomTextOfSpecifiedLength() { + public void generatesRandomTextOfSpecifiedLength() throws Exception { new Assertion<>( "Generated text has incorrect length", new Randomized(512).asString().length(), @@ -59,7 +59,7 @@ public void generatesRandomTextOfSpecifiedLength() { } @Test - public void generatesRandomTextOfSpecifiedChars() { + public void generatesRandomTextOfSpecifiedChars() throws Exception { new Assertion<>( "Generated text contains not allowed characters", new Randomized('a') From 4d584084fc0a13e3af1cb36ca72e4841ee2002ed Mon Sep 17 00:00:00 2001 From: Constantin Costescu Date: Wed, 29 Jul 2020 19:36:08 +0200 Subject: [PATCH 2/3] #1135: fix puzzle comment formatting --- src/main/java/org/cactoos/text/UncheckedText.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/cactoos/text/UncheckedText.java b/src/main/java/org/cactoos/text/UncheckedText.java index fef412abec..c6b6c1cd2d 100644 --- a/src/main/java/org/cactoos/text/UncheckedText.java +++ b/src/main/java/org/cactoos/text/UncheckedText.java @@ -34,9 +34,9 @@ * * @since 0.3 * @todo #1135:30min All classes implementing Text need to be refactored - * to extend TextEnvelope - asString() should be removed and implementation - * from TextEnvelope should be used. This to-do should be moved to another - * class which need to be refactored. + * to extend TextEnvelope - asString() should be removed and implementation + * from TextEnvelope should be used. This to-do should be moved to another + * class which need to be refactored. */ public final class UncheckedText implements Text { From 2848c9405854f6f7ca7b7f7e1a0ecd198edf0c94 Mon Sep 17 00:00:00 2001 From: Constantin Costescu Date: Sat, 8 Aug 2020 14:08:50 +0200 Subject: [PATCH 3/3] #1135: remove puzzle --- src/main/java/org/cactoos/text/UncheckedText.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/org/cactoos/text/UncheckedText.java b/src/main/java/org/cactoos/text/UncheckedText.java index c6b6c1cd2d..784e886c3e 100644 --- a/src/main/java/org/cactoos/text/UncheckedText.java +++ b/src/main/java/org/cactoos/text/UncheckedText.java @@ -33,10 +33,6 @@ *

There is no thread-safety guarantee. * * @since 0.3 - * @todo #1135:30min All classes implementing Text need to be refactored - * to extend TextEnvelope - asString() should be removed and implementation - * from TextEnvelope should be used. This to-do should be moved to another - * class which need to be refactored. */ public final class UncheckedText implements Text {