From 76eb49f5edc88a03f48b67f0c62c47bd6bc3e723 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Mon, 7 May 2018 10:35:04 -0300 Subject: [PATCH 01/25] Using Text as Map key is impossible #788. Created TextEnvelope and TextEnvelopeTest for Text comparison with String values. --- .../java/org/cactoos/text/TextEnvelope.java | 76 +++++++++++++++++++ .../org/cactoos/text/TextEnvelopeTest.java | 64 ++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 src/main/java/org/cactoos/text/TextEnvelope.java create mode 100644 src/test/java/org/cactoos/text/TextEnvelopeTest.java diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java new file mode 100644 index 0000000000..d43be0aff7 --- /dev/null +++ b/src/main/java/org/cactoos/text/TextEnvelope.java @@ -0,0 +1,76 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 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 java.io.IOException; +import org.cactoos.Text; + +/** + * Text envelope for comparisons. + * @author Paulo Lobo (pauloeduardolobo@gmail.com) + * @version $Id$ + * @since 0.31 + */ +public final class TextEnvelope implements Text { + + /** + * Enveloped {@link Text}. + */ + private final Text origin; + + /** + * Ctor. + * @param origin Text to be envelooped. + */ + public TextEnvelope(final Text origin) { + this.origin = origin; + } + + @Override + public int hashCode() { + int result = 0; + try { + result = this.origin.asString().hashCode(); + } catch (final IOException ioexc) { + result = -1; + } + return result; + } + + @Override + public boolean equals(final Object obj) { + boolean result; + try { + result = this.origin.asString().equals(obj); + } catch (final IOException ioexc) { + result = false; + } + return result; + } + + @Override + public String asString() throws IOException { + return this.origin.asString(); + } +} diff --git a/src/test/java/org/cactoos/text/TextEnvelopeTest.java b/src/test/java/org/cactoos/text/TextEnvelopeTest.java new file mode 100644 index 0000000000..1e16243d95 --- /dev/null +++ b/src/test/java/org/cactoos/text/TextEnvelopeTest.java @@ -0,0 +1,64 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 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.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Tests for {@link TextEnvelope}. + * @author Paulo Lobo (pauloeduardolobo@gmail.com) + * @version $Id$ + * @since 0.31 + */ +public final class TextEnvelopeTest { + /** + * Test for {@link TextEnvelope#equals(Object)} method. Must assert that + * the enveloped {@link TextOf} value is equal to its string. + */ + @Test + public void testEquals() { + final String equals = "equals"; + MatcherAssert.assertThat( + "Enveloped value does not match its represented String value", + new TextEnvelope(new TextOf(equals)), + Matchers.is(equals) + ); + } + /** + * Test for {@link TextEnvelope#hashCode()} method. Must assert that the + * {@link TextEnvelope} hashCode is equals to the hashCode of the String + * it represents. + */ + @Test + public void testHashCode() { + final String hash = "hashCode"; + MatcherAssert.assertThat( + "Enveloped hashCode does not match its represented String hashcode", + new TextEnvelope(new TextOf(hash)).hashCode(), + Matchers.is(hash.hashCode()) + ); + } +} From dcd703b73186144d9395926292d9a9774f8a3718 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Mon, 7 May 2018 13:39:03 -0300 Subject: [PATCH 02/25] Using Text as Map key is impossible #788. Improved test coverage. --- .../java/org/cactoos/text/TextEnvelope.java | 29 ++++++------------- .../org/cactoos/text/TextEnvelopeTest.java | 13 +++++++++ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java index d43be0aff7..08635d7e14 100644 --- a/src/main/java/org/cactoos/text/TextEnvelope.java +++ b/src/main/java/org/cactoos/text/TextEnvelope.java @@ -23,11 +23,12 @@ */ package org.cactoos.text; -import java.io.IOException; import org.cactoos.Text; /** - * Text envelope for comparisons. + * Text decorator for comparisons between {@link Text} implementations against + * its {@link #asString()} value using {@link #equals(Object)} and + * {@link #hashCode()} methods. * @author Paulo Lobo (pauloeduardolobo@gmail.com) * @version $Id$ * @since 0.31 @@ -37,40 +38,28 @@ public final class TextEnvelope implements Text { /** * Enveloped {@link Text}. */ - private final Text origin; + private final UncheckedText origin; /** * Ctor. - * @param origin Text to be envelooped. + * @param origin Text to be enveloped. */ public TextEnvelope(final Text origin) { - this.origin = origin; + this.origin = new UncheckedText(origin); } @Override public int hashCode() { - int result = 0; - try { - result = this.origin.asString().hashCode(); - } catch (final IOException ioexc) { - result = -1; - } - return result; + return this.origin.asString().hashCode(); } @Override public boolean equals(final Object obj) { - boolean result; - try { - result = this.origin.asString().equals(obj); - } catch (final IOException ioexc) { - result = false; - } - return result; + return this.origin.asString().equals(obj); } @Override - public String asString() throws IOException { + public String asString() { return this.origin.asString(); } } diff --git a/src/test/java/org/cactoos/text/TextEnvelopeTest.java b/src/test/java/org/cactoos/text/TextEnvelopeTest.java index 1e16243d95..a5a91aadb1 100644 --- a/src/test/java/org/cactoos/text/TextEnvelopeTest.java +++ b/src/test/java/org/cactoos/text/TextEnvelopeTest.java @@ -34,6 +34,19 @@ * @since 0.31 */ public final class TextEnvelopeTest { + /** + * Test for {@link TextEnvelope#asString()} method. Must assert that + * the enveloped {@link TextOf} value is equal to {@link TextOf} value. + */ + @Test + public void testAsString() { + final String equals = "asString"; + MatcherAssert.assertThat( + "Enveloped value does not match its TextOf value", + new TextEnvelope(new TextOf(equals)).asString(), + Matchers.is(new UncheckedText(new TextOf(equals)).asString()) + ); + } /** * Test for {@link TextEnvelope#equals(Object)} method. Must assert that * the enveloped {@link TextOf} value is equal to its string. From afeee99675b03ea8cd9f8d1df99d28ec6ef2c0cb Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Tue, 8 May 2018 13:58:31 -0300 Subject: [PATCH 03/25] Added changes suggested in review #828 for resolution of #788: Using Text as Map key is impossible. --- ...nvelope.java => AbstractTextEnvelope.java} | 33 ++++++---- src/main/java/org/cactoos/text/TextOf.java | 18 +---- .../org/cactoos/text/TextEnvelopeTest.java | 66 +++++++++++++++---- 3 files changed, 74 insertions(+), 43 deletions(-) rename src/main/java/org/cactoos/text/{TextEnvelope.java => AbstractTextEnvelope.java} (60%) diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/AbstractTextEnvelope.java similarity index 60% rename from src/main/java/org/cactoos/text/TextEnvelope.java rename to src/main/java/org/cactoos/text/AbstractTextEnvelope.java index 08635d7e14..46389f766c 100644 --- a/src/main/java/org/cactoos/text/TextEnvelope.java +++ b/src/main/java/org/cactoos/text/AbstractTextEnvelope.java @@ -23,43 +23,50 @@ */ package org.cactoos.text; +import java.io.IOException; +import org.cactoos.Scalar; import org.cactoos.Text; +import org.cactoos.scalar.IoCheckedScalar; +import org.cactoos.scalar.UncheckedScalar; /** - * Text decorator for comparisons between {@link Text} implementations against + * Text envelope for comparisons between {@link Text} implementations against * its {@link #asString()} value using {@link #equals(Object)} and * {@link #hashCode()} methods. * @author Paulo Lobo (pauloeduardolobo@gmail.com) * @version $Id$ * @since 0.31 + * @todo #828:30min Refactor classes in text package to use + * {@link AbstractTextEnvelope} allowing comparison using hashCode and equals + * methods. */ -public final class TextEnvelope implements Text { +public abstract class AbstractTextEnvelope implements Text { /** - * Enveloped {@link Text}. + * String value of the envelope. */ - private final UncheckedText origin; + private final IoCheckedScalar origin; /** * Ctor. - * @param origin Text to be enveloped. + * @param scalar Scalar representing the text value. */ - public TextEnvelope(final Text origin) { - this.origin = new UncheckedText(origin); + public AbstractTextEnvelope(final Scalar scalar) { + this.origin = new IoCheckedScalar<>(scalar); } @Override - public int hashCode() { - return this.origin.asString().hashCode(); + public final String asString() throws IOException { + return this.origin.value(); } @Override - public boolean equals(final Object obj) { - return this.origin.asString().equals(obj); + public final int hashCode() { + return new UncheckedScalar<>(this.origin).value().hashCode(); } @Override - public String asString() { - return this.origin.asString(); + public final boolean equals(final Object obj) { + return new UncheckedScalar<>(this.origin).value().equals(obj); } } diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index 9cbf73c661..4eb0e293f4 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -24,7 +24,6 @@ package org.cactoos.text; import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; @@ -36,11 +35,9 @@ import org.cactoos.Bytes; import org.cactoos.Input; import org.cactoos.Scalar; -import org.cactoos.Text; import org.cactoos.io.BytesOf; import org.cactoos.io.InputOf; import org.cactoos.iterable.Mapped; -import org.cactoos.scalar.IoCheckedScalar; /** * TextOf @@ -51,12 +48,7 @@ * @version $Id$ * @since 0.12 */ -public final class TextOf implements Text { - - /** - * The origin. - */ - private final Scalar origin; +public final class TextOf extends AbstractTextEnvelope { /** * Ctor. @@ -357,17 +349,11 @@ public TextOf(final InputStream input) { * @param scalar The Scalar of String */ private TextOf(final Scalar scalar) { - this.origin = scalar; - } - - @Override - public String asString() throws IOException { - return new IoCheckedScalar<>(this.origin).value(); + super(scalar); } @Override public String toString() { return new UncheckedText(this).asString(); } - } diff --git a/src/test/java/org/cactoos/text/TextEnvelopeTest.java b/src/test/java/org/cactoos/text/TextEnvelopeTest.java index a5a91aadb1..a709621b76 100644 --- a/src/test/java/org/cactoos/text/TextEnvelopeTest.java +++ b/src/test/java/org/cactoos/text/TextEnvelopeTest.java @@ -23,55 +23,93 @@ */ package org.cactoos.text; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import org.cactoos.Scalar; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; /** - * Tests for {@link TextEnvelope}. + * Tests for {@link AbstractTextEnvelope}. * @author Paulo Lobo (pauloeduardolobo@gmail.com) * @version $Id$ * @since 0.31 */ public final class TextEnvelopeTest { /** - * Test for {@link TextEnvelope#asString()} method. Must assert that + * Test for {@link AbstractTextEnvelope#asString()} method. Must assert that * the enveloped {@link TextOf} value is equal to {@link TextOf} value. + * @throws Exception Throws from asString. */ @Test - public void testAsString() { + public void testAsString() throws Exception { final String equals = "asString"; MatcherAssert.assertThat( - "Enveloped value does not match its TextOf value", - new TextEnvelope(new TextOf(equals)).asString(), - Matchers.is(new UncheckedText(new TextOf(equals)).asString()) + "Envelope value does not match String value", + new TextEnvelopeDummy(equals).asString(), + Matchers.is(equals) ); } /** - * Test for {@link TextEnvelope#equals(Object)} method. Must assert that - * the enveloped {@link TextOf} value is equal to its string. + * Test for {@link AbstractTextEnvelope#equals(Object)} method. Must assert + * that the envelope value is equal to its string value. */ @Test public void testEquals() { final String equals = "equals"; MatcherAssert.assertThat( - "Enveloped value does not match its represented String value", - new TextEnvelope(new TextOf(equals)), + "Envelope value does not match its represented String value", + new TextEnvelopeDummy(equals), Matchers.is(equals) ); } /** - * Test for {@link TextEnvelope#hashCode()} method. Must assert that the - * {@link TextEnvelope} hashCode is equals to the hashCode of the String - * it represents. + * Test for {@link AbstractTextEnvelope#hashCode()} method. Must assert that + * the {@link AbstractTextEnvelope} hashCode is equals to the hashCode of + * the String it represents. */ @Test public void testHashCode() { final String hash = "hashCode"; MatcherAssert.assertThat( "Enveloped hashCode does not match its represented String hashcode", - new TextEnvelope(new TextOf(hash)).hashCode(), + new TextEnvelopeDummy(hash).hashCode(), Matchers.is(hash.hashCode()) ); } + + /** + * Dummy class for {@link AbstractTextEnvelope} testing. + */ + private final class TextEnvelopeDummy extends AbstractTextEnvelope { + + /** + * Ctor. + * + * @param scalar Text to be enveloped. + */ + TextEnvelopeDummy(final Scalar scalar) { + super(scalar); + } + /** + * Ctor. + * + * @param input The String + */ + TextEnvelopeDummy(final String input) { + this(input, StandardCharsets.UTF_8); + } + + /** + * Ctor. + * + * @param input The String + * @param cset The Charset + */ + TextEnvelopeDummy(final String input, + final Charset cset) { + this(() -> new String(input.getBytes(cset), cset)); + } + } } From 7513780dad277022a101523c3a9c9ff4148a1c78 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Tue, 8 May 2018 17:36:10 -0300 Subject: [PATCH 04/25] Added changes suggested in review #828 for resolution of #788: Using Text as Map key is impossible. --- ...actTextEnvelope.java => TextEnvelope.java} | 25 +++++++++++------ src/main/java/org/cactoos/text/TextOf.java | 2 +- .../org/cactoos/text/TextEnvelopeTest.java | 28 +++++++++---------- 3 files changed, 32 insertions(+), 23 deletions(-) rename src/main/java/org/cactoos/text/{AbstractTextEnvelope.java => TextEnvelope.java} (76%) diff --git a/src/main/java/org/cactoos/text/AbstractTextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java similarity index 76% rename from src/main/java/org/cactoos/text/AbstractTextEnvelope.java rename to src/main/java/org/cactoos/text/TextEnvelope.java index 46389f766c..764545a00f 100644 --- a/src/main/java/org/cactoos/text/AbstractTextEnvelope.java +++ b/src/main/java/org/cactoos/text/TextEnvelope.java @@ -30,17 +30,18 @@ import org.cactoos.scalar.UncheckedScalar; /** - * Text envelope for comparisons between {@link Text} implementations against - * its {@link #asString()} value using {@link #equals(Object)} and - * {@link #hashCode()} methods. + * Text envelope that provides {@link #equals(Object)} and {@link #hashCode()} + * methods. * @author Paulo Lobo (pauloeduardolobo@gmail.com) * @version $Id$ - * @since 0.31 - * @todo #828:30min Refactor classes in text package to use - * {@link AbstractTextEnvelope} allowing comparison using hashCode and equals + * @since 0.32 + * @todo #788:30min Refactor classes in text package to use + * {@link TextEnvelope} allowing comparison using hashCode and equals * methods. + * @checkstyle AbstractClassNameCheck (500 lines) */ -public abstract class AbstractTextEnvelope implements Text { +@SuppressWarnings("PMD.AbstractNaming") +public abstract class TextEnvelope implements Text { /** * String value of the envelope. @@ -51,10 +52,18 @@ public abstract class AbstractTextEnvelope implements Text { * Ctor. * @param scalar Scalar representing the text value. */ - public AbstractTextEnvelope(final Scalar scalar) { + public TextEnvelope(final Scalar scalar) { this.origin = new IoCheckedScalar<>(scalar); } + /** + * Ctor. + * @param text Text representing the text value. + */ + public TextEnvelope(final Text text) { + this(new IoCheckedScalar<>(() -> text.asString())); + } + @Override public final String asString() throws IOException { return this.origin.value(); diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index 4eb0e293f4..88f2e819f6 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -48,7 +48,7 @@ * @version $Id$ * @since 0.12 */ -public final class TextOf extends AbstractTextEnvelope { +public final class TextOf extends TextEnvelope { /** * Ctor. diff --git a/src/test/java/org/cactoos/text/TextEnvelopeTest.java b/src/test/java/org/cactoos/text/TextEnvelopeTest.java index a709621b76..ca0a0608d1 100644 --- a/src/test/java/org/cactoos/text/TextEnvelopeTest.java +++ b/src/test/java/org/cactoos/text/TextEnvelopeTest.java @@ -27,32 +27,32 @@ import java.nio.charset.StandardCharsets; import org.cactoos.Scalar; import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; +import org.hamcrest.core.IsEqual; import org.junit.Test; /** - * Tests for {@link AbstractTextEnvelope}. + * Tests for {@link TextEnvelope}. * @author Paulo Lobo (pauloeduardolobo@gmail.com) * @version $Id$ - * @since 0.31 + * @since 0.32 */ public final class TextEnvelopeTest { /** - * Test for {@link AbstractTextEnvelope#asString()} method. Must assert that + * Test for {@link TextEnvelope#asString()} method. Must assert that * the enveloped {@link TextOf} value is equal to {@link TextOf} value. * @throws Exception Throws from asString. */ @Test public void testAsString() throws Exception { - final String equals = "asString"; + final String text = "asString"; MatcherAssert.assertThat( "Envelope value does not match String value", - new TextEnvelopeDummy(equals).asString(), - Matchers.is(equals) + new TextEnvelopeDummy(text).asString(), + new IsEqual<>(text) ); } /** - * Test for {@link AbstractTextEnvelope#equals(Object)} method. Must assert + * Test for {@link TextEnvelope#equals(Object)} method. Must assert * that the envelope value is equal to its string value. */ @Test @@ -61,12 +61,12 @@ public void testEquals() { MatcherAssert.assertThat( "Envelope value does not match its represented String value", new TextEnvelopeDummy(equals), - Matchers.is(equals) + new IsEqual<>(equals) ); } /** - * Test for {@link AbstractTextEnvelope#hashCode()} method. Must assert that - * the {@link AbstractTextEnvelope} hashCode is equals to the hashCode of + * Test for {@link TextEnvelope#hashCode()} method. Must assert that + * the {@link TextEnvelope} hashCode is equals to the hashCode of * the String it represents. */ @Test @@ -75,14 +75,14 @@ public void testHashCode() { MatcherAssert.assertThat( "Enveloped hashCode does not match its represented String hashcode", new TextEnvelopeDummy(hash).hashCode(), - Matchers.is(hash.hashCode()) + new IsEqual<>(hash.hashCode()) ); } /** - * Dummy class for {@link AbstractTextEnvelope} testing. + * Dummy class for {@link TextEnvelope} testing. */ - private final class TextEnvelopeDummy extends AbstractTextEnvelope { + private final class TextEnvelopeDummy extends TextEnvelope { /** * Ctor. From c94febff23b536c7718ebd11772c880377656417 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Tue, 8 May 2018 17:52:44 -0300 Subject: [PATCH 05/25] Correction of @todo tag description. --- src/main/java/org/cactoos/text/TextEnvelope.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java index 764545a00f..bddf5cfada 100644 --- a/src/main/java/org/cactoos/text/TextEnvelope.java +++ b/src/main/java/org/cactoos/text/TextEnvelope.java @@ -35,9 +35,9 @@ * @author Paulo Lobo (pauloeduardolobo@gmail.com) * @version $Id$ * @since 0.32 - * @todo #788:30min Refactor classes in text package to use - * {@link TextEnvelope} allowing comparison using hashCode and equals - * methods. + * @todo #788:30min Refactor all classes in text package to extend from + * TextEnvelope, allowing direct comparison with string objects using hashCode + * and equals methods. * @checkstyle AbstractClassNameCheck (500 lines) */ @SuppressWarnings("PMD.AbstractNaming") From a1843b2f289b4c19e565ab700f274e1babc218df Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Wed, 9 May 2018 09:27:35 -0300 Subject: [PATCH 06/25] Changes suggested in PR #819, reverting TextOf. --- .../java/org/cactoos/text/TextEnvelope.java | 12 ++++++------ src/main/java/org/cactoos/text/TextOf.java | 18 ++++++++++++++++-- .../org/cactoos/text/TextEnvelopeTest.java | 6 +++--- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java index bddf5cfada..f13770a19c 100644 --- a/src/main/java/org/cactoos/text/TextEnvelope.java +++ b/src/main/java/org/cactoos/text/TextEnvelope.java @@ -50,18 +50,18 @@ public abstract class TextEnvelope implements Text { /** * Ctor. - * @param scalar Scalar representing the text value. + * @param text Text representing the text value. */ - public TextEnvelope(final Scalar scalar) { - this.origin = new IoCheckedScalar<>(scalar); + public TextEnvelope(final Text text) { + this(new IoCheckedScalar<>(() -> text.asString())); } /** * Ctor. - * @param text Text representing the text value. + * @param scalar Scalar representing the text value. */ - public TextEnvelope(final Text text) { - this(new IoCheckedScalar<>(() -> text.asString())); + public TextEnvelope(final Scalar scalar) { + this.origin = new IoCheckedScalar<>(scalar); } @Override diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index 93547b44c0..189fd64d78 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -24,6 +24,7 @@ package org.cactoos.text; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; @@ -35,9 +36,11 @@ import org.cactoos.Bytes; import org.cactoos.Input; import org.cactoos.Scalar; +import org.cactoos.Text; import org.cactoos.io.BytesOf; import org.cactoos.io.InputOf; import org.cactoos.iterable.Mapped; +import org.cactoos.scalar.IoCheckedScalar; /** * TextOf @@ -48,7 +51,12 @@ * @version $Id$ * @since 0.12 */ -public final class TextOf extends TextEnvelope { +public final class TextOf implements Text { + + /** + * The origin. + */ + private final Scalar origin; /** * Ctor. @@ -349,11 +357,17 @@ public TextOf(final InputStream input) { * @param scalar The Scalar of String */ private TextOf(final Scalar scalar) { - super(scalar); + this.origin = scalar; + } + + @Override + public String asString() throws IOException { + return new IoCheckedScalar<>(this.origin).value(); } @Override public String toString() { return new UncheckedText(this).asString(); } + } diff --git a/src/test/java/org/cactoos/text/TextEnvelopeTest.java b/src/test/java/org/cactoos/text/TextEnvelopeTest.java index ca0a0608d1..3aad0c1acd 100644 --- a/src/test/java/org/cactoos/text/TextEnvelopeTest.java +++ b/src/test/java/org/cactoos/text/TextEnvelopeTest.java @@ -57,11 +57,11 @@ public void testAsString() throws Exception { */ @Test public void testEquals() { - final String equals = "equals"; + final String text = "equals"; MatcherAssert.assertThat( "Envelope value does not match its represented String value", - new TextEnvelopeDummy(equals), - new IsEqual<>(equals) + new TextEnvelopeDummy(text), + new IsEqual<>(text) ); } /** From 2dd9502f610bda7b3d428239ca783b8acd5750a8 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Wed, 9 May 2018 10:09:11 -0300 Subject: [PATCH 07/25] Changes suggested in PR #819, reverting TextOf. --- .../java/org/cactoos/text/TextEnvelope.java | 12 +++++------ src/main/java/org/cactoos/text/TextOf.java | 20 ++++++++++++++++--- .../org/cactoos/text/TextEnvelopeTest.java | 6 +++--- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java index bddf5cfada..f13770a19c 100644 --- a/src/main/java/org/cactoos/text/TextEnvelope.java +++ b/src/main/java/org/cactoos/text/TextEnvelope.java @@ -50,18 +50,18 @@ public abstract class TextEnvelope implements Text { /** * Ctor. - * @param scalar Scalar representing the text value. + * @param text Text representing the text value. */ - public TextEnvelope(final Scalar scalar) { - this.origin = new IoCheckedScalar<>(scalar); + public TextEnvelope(final Text text) { + this(new IoCheckedScalar<>(() -> text.asString())); } /** * Ctor. - * @param text Text representing the text value. + * @param scalar Scalar representing the text value. */ - public TextEnvelope(final Text text) { - this(new IoCheckedScalar<>(() -> text.asString())); + public TextEnvelope(final Scalar scalar) { + this.origin = new IoCheckedScalar<>(scalar); } @Override diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index 88f2e819f6..189fd64d78 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -24,6 +24,7 @@ package org.cactoos.text; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; @@ -35,9 +36,11 @@ import org.cactoos.Bytes; import org.cactoos.Input; import org.cactoos.Scalar; +import org.cactoos.Text; import org.cactoos.io.BytesOf; import org.cactoos.io.InputOf; import org.cactoos.iterable.Mapped; +import org.cactoos.scalar.IoCheckedScalar; /** * TextOf @@ -48,7 +51,12 @@ * @version $Id$ * @since 0.12 */ -public final class TextOf extends TextEnvelope { +public final class TextOf implements Text { + + /** + * The origin. + */ + private final Scalar origin; /** * Ctor. @@ -340,7 +348,7 @@ public TextOf(final Iterable iterable) { * @since 0.21 */ public TextOf(final InputStream input) { - this(new InputOf(new InputStreamReader(input))); + this(new InputOf(new InputStreamReader(input, StandardCharsets.UTF_8))); } /** @@ -349,11 +357,17 @@ public TextOf(final InputStream input) { * @param scalar The Scalar of String */ private TextOf(final Scalar scalar) { - super(scalar); + this.origin = scalar; + } + + @Override + public String asString() throws IOException { + return new IoCheckedScalar<>(this.origin).value(); } @Override public String toString() { return new UncheckedText(this).asString(); } + } diff --git a/src/test/java/org/cactoos/text/TextEnvelopeTest.java b/src/test/java/org/cactoos/text/TextEnvelopeTest.java index ca0a0608d1..3aad0c1acd 100644 --- a/src/test/java/org/cactoos/text/TextEnvelopeTest.java +++ b/src/test/java/org/cactoos/text/TextEnvelopeTest.java @@ -57,11 +57,11 @@ public void testAsString() throws Exception { */ @Test public void testEquals() { - final String equals = "equals"; + final String text = "equals"; MatcherAssert.assertThat( "Envelope value does not match its represented String value", - new TextEnvelopeDummy(equals), - new IsEqual<>(equals) + new TextEnvelopeDummy(text), + new IsEqual<>(text) ); } /** From 3df5efa6d16624210e48a720ca9e9eea3a46331f Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Wed, 9 May 2018 10:20:36 -0300 Subject: [PATCH 08/25] Changes suggested in PR #819, reverting TextOf. --- src/main/java/org/cactoos/text/TextEnvelope.java | 4 ++-- src/test/java/org/cactoos/text/TextEnvelopeTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java index f13770a19c..975c03b146 100644 --- a/src/main/java/org/cactoos/text/TextEnvelope.java +++ b/src/main/java/org/cactoos/text/TextEnvelope.java @@ -36,8 +36,8 @@ * @version $Id$ * @since 0.32 * @todo #788:30min Refactor all classes in text package to extend from - * TextEnvelope, allowing direct comparison with string objects using hashCode - * and equals methods. + * TextEnvelope, allowing direct comparison with string objects using hashCode + * and equals methods. * @checkstyle AbstractClassNameCheck (500 lines) */ @SuppressWarnings("PMD.AbstractNaming") diff --git a/src/test/java/org/cactoos/text/TextEnvelopeTest.java b/src/test/java/org/cactoos/text/TextEnvelopeTest.java index 3aad0c1acd..a88aeffdbb 100644 --- a/src/test/java/org/cactoos/text/TextEnvelopeTest.java +++ b/src/test/java/org/cactoos/text/TextEnvelopeTest.java @@ -39,7 +39,7 @@ public final class TextEnvelopeTest { /** * Test for {@link TextEnvelope#asString()} method. Must assert that - * the enveloped {@link TextOf} value is equal to {@link TextOf} value. + * the envelope asString value is equal to its string value. * @throws Exception Throws from asString. */ @Test From 2df24c0e7b3389c5fe0c67a59b2f56dac9dcb63a Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Mon, 7 May 2018 10:35:04 -0300 Subject: [PATCH 09/25] Using Text as Map key is impossible #788. Created TextEnvelope and TextEnvelopeTest for Text comparison with String values. --- .../java/org/cactoos/text/TextEnvelope.java | 76 +++++++++++++++++++ .../org/cactoos/text/TextEnvelopeTest.java | 64 ++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 src/main/java/org/cactoos/text/TextEnvelope.java create mode 100644 src/test/java/org/cactoos/text/TextEnvelopeTest.java diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java new file mode 100644 index 0000000000..d43be0aff7 --- /dev/null +++ b/src/main/java/org/cactoos/text/TextEnvelope.java @@ -0,0 +1,76 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 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 java.io.IOException; +import org.cactoos.Text; + +/** + * Text envelope for comparisons. + * @author Paulo Lobo (pauloeduardolobo@gmail.com) + * @version $Id$ + * @since 0.31 + */ +public final class TextEnvelope implements Text { + + /** + * Enveloped {@link Text}. + */ + private final Text origin; + + /** + * Ctor. + * @param origin Text to be envelooped. + */ + public TextEnvelope(final Text origin) { + this.origin = origin; + } + + @Override + public int hashCode() { + int result = 0; + try { + result = this.origin.asString().hashCode(); + } catch (final IOException ioexc) { + result = -1; + } + return result; + } + + @Override + public boolean equals(final Object obj) { + boolean result; + try { + result = this.origin.asString().equals(obj); + } catch (final IOException ioexc) { + result = false; + } + return result; + } + + @Override + public String asString() throws IOException { + return this.origin.asString(); + } +} diff --git a/src/test/java/org/cactoos/text/TextEnvelopeTest.java b/src/test/java/org/cactoos/text/TextEnvelopeTest.java new file mode 100644 index 0000000000..1e16243d95 --- /dev/null +++ b/src/test/java/org/cactoos/text/TextEnvelopeTest.java @@ -0,0 +1,64 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 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.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Tests for {@link TextEnvelope}. + * @author Paulo Lobo (pauloeduardolobo@gmail.com) + * @version $Id$ + * @since 0.31 + */ +public final class TextEnvelopeTest { + /** + * Test for {@link TextEnvelope#equals(Object)} method. Must assert that + * the enveloped {@link TextOf} value is equal to its string. + */ + @Test + public void testEquals() { + final String equals = "equals"; + MatcherAssert.assertThat( + "Enveloped value does not match its represented String value", + new TextEnvelope(new TextOf(equals)), + Matchers.is(equals) + ); + } + /** + * Test for {@link TextEnvelope#hashCode()} method. Must assert that the + * {@link TextEnvelope} hashCode is equals to the hashCode of the String + * it represents. + */ + @Test + public void testHashCode() { + final String hash = "hashCode"; + MatcherAssert.assertThat( + "Enveloped hashCode does not match its represented String hashcode", + new TextEnvelope(new TextOf(hash)).hashCode(), + Matchers.is(hash.hashCode()) + ); + } +} From 39133f29cd3b6ec653c370e031daf8abb9853041 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Mon, 7 May 2018 13:39:03 -0300 Subject: [PATCH 10/25] Using Text as Map key is impossible #788. Improved test coverage. --- .../java/org/cactoos/text/TextEnvelope.java | 29 ++++++------------- .../org/cactoos/text/TextEnvelopeTest.java | 13 +++++++++ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java index d43be0aff7..08635d7e14 100644 --- a/src/main/java/org/cactoos/text/TextEnvelope.java +++ b/src/main/java/org/cactoos/text/TextEnvelope.java @@ -23,11 +23,12 @@ */ package org.cactoos.text; -import java.io.IOException; import org.cactoos.Text; /** - * Text envelope for comparisons. + * Text decorator for comparisons between {@link Text} implementations against + * its {@link #asString()} value using {@link #equals(Object)} and + * {@link #hashCode()} methods. * @author Paulo Lobo (pauloeduardolobo@gmail.com) * @version $Id$ * @since 0.31 @@ -37,40 +38,28 @@ public final class TextEnvelope implements Text { /** * Enveloped {@link Text}. */ - private final Text origin; + private final UncheckedText origin; /** * Ctor. - * @param origin Text to be envelooped. + * @param origin Text to be enveloped. */ public TextEnvelope(final Text origin) { - this.origin = origin; + this.origin = new UncheckedText(origin); } @Override public int hashCode() { - int result = 0; - try { - result = this.origin.asString().hashCode(); - } catch (final IOException ioexc) { - result = -1; - } - return result; + return this.origin.asString().hashCode(); } @Override public boolean equals(final Object obj) { - boolean result; - try { - result = this.origin.asString().equals(obj); - } catch (final IOException ioexc) { - result = false; - } - return result; + return this.origin.asString().equals(obj); } @Override - public String asString() throws IOException { + public String asString() { return this.origin.asString(); } } diff --git a/src/test/java/org/cactoos/text/TextEnvelopeTest.java b/src/test/java/org/cactoos/text/TextEnvelopeTest.java index 1e16243d95..a5a91aadb1 100644 --- a/src/test/java/org/cactoos/text/TextEnvelopeTest.java +++ b/src/test/java/org/cactoos/text/TextEnvelopeTest.java @@ -34,6 +34,19 @@ * @since 0.31 */ public final class TextEnvelopeTest { + /** + * Test for {@link TextEnvelope#asString()} method. Must assert that + * the enveloped {@link TextOf} value is equal to {@link TextOf} value. + */ + @Test + public void testAsString() { + final String equals = "asString"; + MatcherAssert.assertThat( + "Enveloped value does not match its TextOf value", + new TextEnvelope(new TextOf(equals)).asString(), + Matchers.is(new UncheckedText(new TextOf(equals)).asString()) + ); + } /** * Test for {@link TextEnvelope#equals(Object)} method. Must assert that * the enveloped {@link TextOf} value is equal to its string. From e4f56a7d8e865292ceeb1a47837442e97be22778 Mon Sep 17 00:00:00 2001 From: Roman Proshin Date: Mon, 7 May 2018 20:04:34 +0300 Subject: [PATCH 11/25] #790: Create RandomText --- .../java/org/cactoos/text/RandomText.java | 124 ++++++++++++++++++ .../java/org/cactoos/text/RandomTextTest.java | 84 ++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 src/main/java/org/cactoos/text/RandomText.java create mode 100644 src/test/java/org/cactoos/text/RandomTextTest.java diff --git a/src/main/java/org/cactoos/text/RandomText.java b/src/main/java/org/cactoos/text/RandomText.java new file mode 100644 index 0000000000..a58da06b12 --- /dev/null +++ b/src/main/java/org/cactoos/text/RandomText.java @@ -0,0 +1,124 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 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 java.io.IOException; +import java.security.SecureRandom; +import java.util.List; +import java.util.Random; +import org.cactoos.Text; +import org.cactoos.list.ListOf; + +/** + * Random text. + * + *

There is no thread-safety guarantee. + * + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 0.32 + */ +public final class RandomText implements Text { + + /** + * 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 int length; + + /** + * Characters index randomizer. + */ + private final Random random; + + /** + * Ctor. + */ + public RandomText() { + this(new SecureRandom().nextInt(RandomText.MAX_RANDOM_LENGTH - 1) + 1); + } + + /** + * Ctor. + * @param len Length of generated text. + */ + public RandomText(final int len) { + this( + new ListOf<>( + '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', + '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', + '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', + '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', + 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', + ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', + 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', + '{', '|', '}', '~' + ), + len + ); + } + + /** + * Ctor. + * @param chrs List of characters allowed for generating. + */ + public RandomText(final List chrs) { + this( + chrs, + new SecureRandom().nextInt(RandomText.MAX_RANDOM_LENGTH - 1) + 1 + ); + } + + /** + * Ctor. + * @param chrs List of characters allowed for generating. + * @param len Length of generated text. + */ + public RandomText(final List chrs, final int len) { + this.characters = chrs; + this.length = len; + this.random = new SecureRandom(); + } + + @Override + public String asString() throws IOException { + final StringBuilder builder = new StringBuilder(this.length); + final int bound = this.characters.size(); + for (int index = 0; index < this.length; index = index + 1) { + builder.append(this.characters.get(this.random.nextInt(bound))); + } + return builder.toString(); + } +} diff --git a/src/test/java/org/cactoos/text/RandomTextTest.java b/src/test/java/org/cactoos/text/RandomTextTest.java new file mode 100644 index 0000000000..d7ac6fec7b --- /dev/null +++ b/src/test/java/org/cactoos/text/RandomTextTest.java @@ -0,0 +1,84 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 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.list.ListOf; +import org.cactoos.matchers.TextHasString; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.hamcrest.core.IsEqual; +import org.junit.Test; + +/** + * Test for {@link RandomText}. + * + *

There is no thread-safety guarantee. + * + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 0.32 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) + */ +public final class RandomTextTest { + + @Test + public void generatesRandomTextOfRandomLength() throws Exception { + MatcherAssert.assertThat( + "Generated text is empty", + new RandomText().asString().length(), + Matchers.greaterThan(0) + ); + } + + @Test + public void generatesRandomTextOfSpecifiedLength() throws Exception { + MatcherAssert.assertThat( + "Generated text has incorrect length", + new RandomText(512).asString().length(), + new IsEqual<>(512) + ); + } + + @Test + public void generatesRandomTextOfSpecifiedChars() throws Exception { + MatcherAssert.assertThat( + "Generated text contains not allowed characters", + new RandomText(new ListOf<>('a')) + .asString() + .replaceAll("a", "") + .length(), + new IsEqual<>(0) + ); + } + + @Test + public void generatesRandomTextOfSpecifiedCharsAndLength() { + MatcherAssert.assertThat( + "Generated text doesn't match specification", + new RandomText(new ListOf<>('a'), 10), + new TextHasString("aaaaaaaaaa") + ); + } +} From 28a84607466c08dc80a6564edca0f85e3386f916 Mon Sep 17 00:00:00 2001 From: Roman Proshin Date: Mon, 7 May 2018 23:20:16 +0300 Subject: [PATCH 12/25] #790: Create RandomText - fix PR comments --- .../java/org/cactoos/text/RandomText.java | 35 ++++++++++++++++--- .../java/org/cactoos/text/RandomTextTest.java | 5 ++- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/cactoos/text/RandomText.java b/src/main/java/org/cactoos/text/RandomText.java index a58da06b12..1322a94208 100644 --- a/src/main/java/org/cactoos/text/RandomText.java +++ b/src/main/java/org/cactoos/text/RandomText.java @@ -23,7 +23,6 @@ */ package org.cactoos.text; -import java.io.IOException; import java.security.SecureRandom; import java.util.List; import java.util.Random; @@ -72,7 +71,7 @@ public RandomText() { * Ctor. * @param len Length of generated text. */ - public RandomText(final int len) { + public RandomText(final Integer len) { this( new ListOf<>( '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', @@ -90,6 +89,14 @@ public RandomText(final int len) { ); } + /** + * Ctor. + * @param chrs Array of characters allowed for generating. + */ + public RandomText(final Character... chrs) { + this(new ListOf<>(chrs)); + } + /** * Ctor. * @param chrs List of characters allowed for generating. @@ -101,19 +108,39 @@ public RandomText(final List chrs) { ); } + /** + * Ctor. + * @param len Length of generated text. + * @param chrs Array of characters allowed for generating. + */ + public RandomText(final Integer len, final Character... chrs) { + this(new ListOf<>(chrs), len); + } + /** * Ctor. * @param chrs List of characters allowed for generating. * @param len Length of generated text. */ public RandomText(final List chrs, final int len) { + this(chrs, len, new SecureRandom()); + } + + /** + * Ctor. + * @param chrs List of characters allowed for generating. + * @param len Length of generated text. + * @param rnd Characters index randomizer. + */ + public RandomText(final List chrs, final int len, + final Random rnd) { this.characters = chrs; this.length = len; - this.random = new SecureRandom(); + this.random = rnd; } @Override - public String asString() throws IOException { + public String asString() { final StringBuilder builder = new StringBuilder(this.length); final int bound = this.characters.size(); for (int index = 0; index < this.length; index = index + 1) { diff --git a/src/test/java/org/cactoos/text/RandomTextTest.java b/src/test/java/org/cactoos/text/RandomTextTest.java index d7ac6fec7b..71db5b446f 100644 --- a/src/test/java/org/cactoos/text/RandomTextTest.java +++ b/src/test/java/org/cactoos/text/RandomTextTest.java @@ -23,7 +23,6 @@ */ package org.cactoos.text; -import org.cactoos.list.ListOf; import org.cactoos.matchers.TextHasString; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; @@ -65,7 +64,7 @@ public void generatesRandomTextOfSpecifiedLength() throws Exception { public void generatesRandomTextOfSpecifiedChars() throws Exception { MatcherAssert.assertThat( "Generated text contains not allowed characters", - new RandomText(new ListOf<>('a')) + new RandomText('a') .asString() .replaceAll("a", "") .length(), @@ -77,7 +76,7 @@ public void generatesRandomTextOfSpecifiedChars() throws Exception { public void generatesRandomTextOfSpecifiedCharsAndLength() { MatcherAssert.assertThat( "Generated text doesn't match specification", - new RandomText(new ListOf<>('a'), 10), + new RandomText(10, 'a'), new TextHasString("aaaaaaaaaa") ); } From 6d61ff5cd245632f9b07258f3e911fbc1c7f892d Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Tue, 8 May 2018 13:58:31 -0300 Subject: [PATCH 13/25] Added changes suggested in review #828 for resolution of #788: Using Text as Map key is impossible. --- ...nvelope.java => AbstractTextEnvelope.java} | 33 ++++++---- src/main/java/org/cactoos/text/TextOf.java | 18 +---- .../org/cactoos/text/TextEnvelopeTest.java | 66 +++++++++++++++---- 3 files changed, 74 insertions(+), 43 deletions(-) rename src/main/java/org/cactoos/text/{TextEnvelope.java => AbstractTextEnvelope.java} (60%) diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/AbstractTextEnvelope.java similarity index 60% rename from src/main/java/org/cactoos/text/TextEnvelope.java rename to src/main/java/org/cactoos/text/AbstractTextEnvelope.java index 08635d7e14..46389f766c 100644 --- a/src/main/java/org/cactoos/text/TextEnvelope.java +++ b/src/main/java/org/cactoos/text/AbstractTextEnvelope.java @@ -23,43 +23,50 @@ */ package org.cactoos.text; +import java.io.IOException; +import org.cactoos.Scalar; import org.cactoos.Text; +import org.cactoos.scalar.IoCheckedScalar; +import org.cactoos.scalar.UncheckedScalar; /** - * Text decorator for comparisons between {@link Text} implementations against + * Text envelope for comparisons between {@link Text} implementations against * its {@link #asString()} value using {@link #equals(Object)} and * {@link #hashCode()} methods. * @author Paulo Lobo (pauloeduardolobo@gmail.com) * @version $Id$ * @since 0.31 + * @todo #828:30min Refactor classes in text package to use + * {@link AbstractTextEnvelope} allowing comparison using hashCode and equals + * methods. */ -public final class TextEnvelope implements Text { +public abstract class AbstractTextEnvelope implements Text { /** - * Enveloped {@link Text}. + * String value of the envelope. */ - private final UncheckedText origin; + private final IoCheckedScalar origin; /** * Ctor. - * @param origin Text to be enveloped. + * @param scalar Scalar representing the text value. */ - public TextEnvelope(final Text origin) { - this.origin = new UncheckedText(origin); + public AbstractTextEnvelope(final Scalar scalar) { + this.origin = new IoCheckedScalar<>(scalar); } @Override - public int hashCode() { - return this.origin.asString().hashCode(); + public final String asString() throws IOException { + return this.origin.value(); } @Override - public boolean equals(final Object obj) { - return this.origin.asString().equals(obj); + public final int hashCode() { + return new UncheckedScalar<>(this.origin).value().hashCode(); } @Override - public String asString() { - return this.origin.asString(); + public final boolean equals(final Object obj) { + return new UncheckedScalar<>(this.origin).value().equals(obj); } } diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index 189fd64d78..8f8a14b33e 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -24,7 +24,6 @@ package org.cactoos.text; import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; @@ -36,11 +35,9 @@ import org.cactoos.Bytes; import org.cactoos.Input; import org.cactoos.Scalar; -import org.cactoos.Text; import org.cactoos.io.BytesOf; import org.cactoos.io.InputOf; import org.cactoos.iterable.Mapped; -import org.cactoos.scalar.IoCheckedScalar; /** * TextOf @@ -51,12 +48,7 @@ * @version $Id$ * @since 0.12 */ -public final class TextOf implements Text { - - /** - * The origin. - */ - private final Scalar origin; +public final class TextOf extends AbstractTextEnvelope { /** * Ctor. @@ -357,17 +349,11 @@ public TextOf(final InputStream input) { * @param scalar The Scalar of String */ private TextOf(final Scalar scalar) { - this.origin = scalar; - } - - @Override - public String asString() throws IOException { - return new IoCheckedScalar<>(this.origin).value(); + super(scalar); } @Override public String toString() { return new UncheckedText(this).asString(); } - } diff --git a/src/test/java/org/cactoos/text/TextEnvelopeTest.java b/src/test/java/org/cactoos/text/TextEnvelopeTest.java index a5a91aadb1..a709621b76 100644 --- a/src/test/java/org/cactoos/text/TextEnvelopeTest.java +++ b/src/test/java/org/cactoos/text/TextEnvelopeTest.java @@ -23,55 +23,93 @@ */ package org.cactoos.text; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import org.cactoos.Scalar; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; /** - * Tests for {@link TextEnvelope}. + * Tests for {@link AbstractTextEnvelope}. * @author Paulo Lobo (pauloeduardolobo@gmail.com) * @version $Id$ * @since 0.31 */ public final class TextEnvelopeTest { /** - * Test for {@link TextEnvelope#asString()} method. Must assert that + * Test for {@link AbstractTextEnvelope#asString()} method. Must assert that * the enveloped {@link TextOf} value is equal to {@link TextOf} value. + * @throws Exception Throws from asString. */ @Test - public void testAsString() { + public void testAsString() throws Exception { final String equals = "asString"; MatcherAssert.assertThat( - "Enveloped value does not match its TextOf value", - new TextEnvelope(new TextOf(equals)).asString(), - Matchers.is(new UncheckedText(new TextOf(equals)).asString()) + "Envelope value does not match String value", + new TextEnvelopeDummy(equals).asString(), + Matchers.is(equals) ); } /** - * Test for {@link TextEnvelope#equals(Object)} method. Must assert that - * the enveloped {@link TextOf} value is equal to its string. + * Test for {@link AbstractTextEnvelope#equals(Object)} method. Must assert + * that the envelope value is equal to its string value. */ @Test public void testEquals() { final String equals = "equals"; MatcherAssert.assertThat( - "Enveloped value does not match its represented String value", - new TextEnvelope(new TextOf(equals)), + "Envelope value does not match its represented String value", + new TextEnvelopeDummy(equals), Matchers.is(equals) ); } /** - * Test for {@link TextEnvelope#hashCode()} method. Must assert that the - * {@link TextEnvelope} hashCode is equals to the hashCode of the String - * it represents. + * Test for {@link AbstractTextEnvelope#hashCode()} method. Must assert that + * the {@link AbstractTextEnvelope} hashCode is equals to the hashCode of + * the String it represents. */ @Test public void testHashCode() { final String hash = "hashCode"; MatcherAssert.assertThat( "Enveloped hashCode does not match its represented String hashcode", - new TextEnvelope(new TextOf(hash)).hashCode(), + new TextEnvelopeDummy(hash).hashCode(), Matchers.is(hash.hashCode()) ); } + + /** + * Dummy class for {@link AbstractTextEnvelope} testing. + */ + private final class TextEnvelopeDummy extends AbstractTextEnvelope { + + /** + * Ctor. + * + * @param scalar Text to be enveloped. + */ + TextEnvelopeDummy(final Scalar scalar) { + super(scalar); + } + /** + * Ctor. + * + * @param input The String + */ + TextEnvelopeDummy(final String input) { + this(input, StandardCharsets.UTF_8); + } + + /** + * Ctor. + * + * @param input The String + * @param cset The Charset + */ + TextEnvelopeDummy(final String input, + final Charset cset) { + this(() -> new String(input.getBytes(cset), cset)); + } + } } From 5c0dc141b4879931b5cde19af79c434f57bb60d6 Mon Sep 17 00:00:00 2001 From: George Aristy Date: Tue, 8 May 2018 13:32:55 -0400 Subject: [PATCH 14/25] Fix for #815 --- .rultor.yml | 9 ++++----- .travis.yml | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/.rultor.yml b/.rultor.yml index 3d667e93f1..db3a115fa3 100644 --- a/.rultor.yml +++ b/.rultor.yml @@ -5,20 +5,19 @@ assets: pubring.gpg: zerocracy/home#assets/pubring.gpg secring.gpg: zerocracy/home#assets/secring.gpg env: - MAVEN_OPTS: -XX:MaxPermSize=256m -Xmx1g - JAVA_OPTS: -XX:MaxPermSize=256m -Xmx1g + MAVEN_OPTS: -Xmx1g + JAVA_OPTS: -Xmx1g install: | sudo gem install --no-rdoc --no-ri pdd sudo gem install --no-rdoc --no-ri xcop architect: -- yegor256 - llorllale +- yegor256 merge: script: | + pdd --file=/dev/null mvn clean install -Pqulice --errors --settings ../settings.xml mvn clean site -Psite --errors --settings ../settings.xml - mvn clean - pdd --source=$(pwd) --verbose --file=/dev/null deploy: script: | mvn clean deploy -Pqulice --errors --settings ../settings.xml diff --git a/.travis.yml b/.travis.yml index 0f521cad1f..c1286d0309 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,11 +5,11 @@ cache: - $HOME/.m2 script: - set -e - - mvn clean site -Psite --errors --batch-mode + - pdd --file=/dev/null - mvn clean install -Pqulice --errors --batch-mode + - mvn clean site -Psite --errors --batch-mode install: - gem install pdd - - gem install est - gem install xcop env: global: From e18dbc09cb81a34d23a38ed1e319d24f37956c81 Mon Sep 17 00:00:00 2001 From: Roman Proshin Date: Tue, 8 May 2018 21:39:16 +0300 Subject: [PATCH 15/25] #790: Create RandomText - replace int with Scalar --- .../java/org/cactoos/text/RandomText.java | 40 +++++++++++++++---- .../java/org/cactoos/text/RandomTextTest.java | 6 +-- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/cactoos/text/RandomText.java b/src/main/java/org/cactoos/text/RandomText.java index 1322a94208..00b01f6ec3 100644 --- a/src/main/java/org/cactoos/text/RandomText.java +++ b/src/main/java/org/cactoos/text/RandomText.java @@ -26,8 +26,10 @@ import java.security.SecureRandom; 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.UncheckedScalar; /** * Random text. @@ -53,7 +55,7 @@ public final class RandomText implements Text { /** * Length of generated text. */ - private final int length; + private final Scalar length; /** * Characters index randomizer. @@ -64,7 +66,11 @@ public final class RandomText implements Text { * Ctor. */ public RandomText() { - this(new SecureRandom().nextInt(RandomText.MAX_RANDOM_LENGTH - 1) + 1); + this( + () -> new SecureRandom().nextInt( + RandomText.MAX_RANDOM_LENGTH - 1 + ) + 1 + ); } /** @@ -72,6 +78,14 @@ public RandomText() { * @param len Length of generated text. */ public RandomText(final Integer len) { + this(() -> len); + } + + /** + * Ctor. + * @param len Length of generated text. + */ + public RandomText(final Scalar len) { this( new ListOf<>( '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', @@ -104,7 +118,9 @@ public RandomText(final Character... chrs) { public RandomText(final List chrs) { this( chrs, - new SecureRandom().nextInt(RandomText.MAX_RANDOM_LENGTH - 1) + 1 + () -> new SecureRandom().nextInt( + RandomText.MAX_RANDOM_LENGTH - 1 + ) + 1 ); } @@ -114,6 +130,15 @@ public RandomText(final List chrs) { * @param chrs Array of characters allowed for generating. */ public RandomText(final Integer len, final Character... chrs) { + this(() -> len, chrs); + } + + /** + * Ctor. + * @param len Length of generated text. + * @param chrs Array of characters allowed for generating. + */ + public RandomText(final Scalar len, final Character... chrs) { this(new ListOf<>(chrs), len); } @@ -122,7 +147,7 @@ public RandomText(final Integer len, final Character... chrs) { * @param chrs List of characters allowed for generating. * @param len Length of generated text. */ - public RandomText(final List chrs, final int len) { + public RandomText(final List chrs, final Scalar len) { this(chrs, len, new SecureRandom()); } @@ -132,7 +157,7 @@ public RandomText(final List chrs, final int len) { * @param len Length of generated text. * @param rnd Characters index randomizer. */ - public RandomText(final List chrs, final int len, + public RandomText(final List chrs, final Scalar len, final Random rnd) { this.characters = chrs; this.length = len; @@ -141,9 +166,10 @@ public RandomText(final List chrs, final int len, @Override public String asString() { - final StringBuilder builder = new StringBuilder(this.length); + final int len = new UncheckedScalar<>(this.length).value(); + final StringBuilder builder = new StringBuilder(len); final int bound = this.characters.size(); - for (int index = 0; index < this.length; index = index + 1) { + for (int index = 0; index < len; index = index + 1) { builder.append(this.characters.get(this.random.nextInt(bound))); } return builder.toString(); diff --git a/src/test/java/org/cactoos/text/RandomTextTest.java b/src/test/java/org/cactoos/text/RandomTextTest.java index 71db5b446f..2cf5929d59 100644 --- a/src/test/java/org/cactoos/text/RandomTextTest.java +++ b/src/test/java/org/cactoos/text/RandomTextTest.java @@ -43,7 +43,7 @@ public final class RandomTextTest { @Test - public void generatesRandomTextOfRandomLength() throws Exception { + public void generatesRandomTextOfRandomLength() { MatcherAssert.assertThat( "Generated text is empty", new RandomText().asString().length(), @@ -52,7 +52,7 @@ public void generatesRandomTextOfRandomLength() throws Exception { } @Test - public void generatesRandomTextOfSpecifiedLength() throws Exception { + public void generatesRandomTextOfSpecifiedLength() { MatcherAssert.assertThat( "Generated text has incorrect length", new RandomText(512).asString().length(), @@ -61,7 +61,7 @@ public void generatesRandomTextOfSpecifiedLength() throws Exception { } @Test - public void generatesRandomTextOfSpecifiedChars() throws Exception { + public void generatesRandomTextOfSpecifiedChars() { MatcherAssert.assertThat( "Generated text contains not allowed characters", new RandomText('a') From a8f9f65ea0f0423bac96af24f75d4b156f7443d5 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Tue, 8 May 2018 17:36:10 -0300 Subject: [PATCH 16/25] Added changes suggested in review #828 for resolution of #788: Using Text as Map key is impossible. --- ...actTextEnvelope.java => TextEnvelope.java} | 25 +++++++++++------ src/main/java/org/cactoos/text/TextOf.java | 2 +- .../org/cactoos/text/TextEnvelopeTest.java | 28 +++++++++---------- 3 files changed, 32 insertions(+), 23 deletions(-) rename src/main/java/org/cactoos/text/{AbstractTextEnvelope.java => TextEnvelope.java} (76%) diff --git a/src/main/java/org/cactoos/text/AbstractTextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java similarity index 76% rename from src/main/java/org/cactoos/text/AbstractTextEnvelope.java rename to src/main/java/org/cactoos/text/TextEnvelope.java index 46389f766c..764545a00f 100644 --- a/src/main/java/org/cactoos/text/AbstractTextEnvelope.java +++ b/src/main/java/org/cactoos/text/TextEnvelope.java @@ -30,17 +30,18 @@ import org.cactoos.scalar.UncheckedScalar; /** - * Text envelope for comparisons between {@link Text} implementations against - * its {@link #asString()} value using {@link #equals(Object)} and - * {@link #hashCode()} methods. + * Text envelope that provides {@link #equals(Object)} and {@link #hashCode()} + * methods. * @author Paulo Lobo (pauloeduardolobo@gmail.com) * @version $Id$ - * @since 0.31 - * @todo #828:30min Refactor classes in text package to use - * {@link AbstractTextEnvelope} allowing comparison using hashCode and equals + * @since 0.32 + * @todo #788:30min Refactor classes in text package to use + * {@link TextEnvelope} allowing comparison using hashCode and equals * methods. + * @checkstyle AbstractClassNameCheck (500 lines) */ -public abstract class AbstractTextEnvelope implements Text { +@SuppressWarnings("PMD.AbstractNaming") +public abstract class TextEnvelope implements Text { /** * String value of the envelope. @@ -51,10 +52,18 @@ public abstract class AbstractTextEnvelope implements Text { * Ctor. * @param scalar Scalar representing the text value. */ - public AbstractTextEnvelope(final Scalar scalar) { + public TextEnvelope(final Scalar scalar) { this.origin = new IoCheckedScalar<>(scalar); } + /** + * Ctor. + * @param text Text representing the text value. + */ + public TextEnvelope(final Text text) { + this(new IoCheckedScalar<>(() -> text.asString())); + } + @Override public final String asString() throws IOException { return this.origin.value(); diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index 8f8a14b33e..93547b44c0 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -48,7 +48,7 @@ * @version $Id$ * @since 0.12 */ -public final class TextOf extends AbstractTextEnvelope { +public final class TextOf extends TextEnvelope { /** * Ctor. diff --git a/src/test/java/org/cactoos/text/TextEnvelopeTest.java b/src/test/java/org/cactoos/text/TextEnvelopeTest.java index a709621b76..ca0a0608d1 100644 --- a/src/test/java/org/cactoos/text/TextEnvelopeTest.java +++ b/src/test/java/org/cactoos/text/TextEnvelopeTest.java @@ -27,32 +27,32 @@ import java.nio.charset.StandardCharsets; import org.cactoos.Scalar; import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; +import org.hamcrest.core.IsEqual; import org.junit.Test; /** - * Tests for {@link AbstractTextEnvelope}. + * Tests for {@link TextEnvelope}. * @author Paulo Lobo (pauloeduardolobo@gmail.com) * @version $Id$ - * @since 0.31 + * @since 0.32 */ public final class TextEnvelopeTest { /** - * Test for {@link AbstractTextEnvelope#asString()} method. Must assert that + * Test for {@link TextEnvelope#asString()} method. Must assert that * the enveloped {@link TextOf} value is equal to {@link TextOf} value. * @throws Exception Throws from asString. */ @Test public void testAsString() throws Exception { - final String equals = "asString"; + final String text = "asString"; MatcherAssert.assertThat( "Envelope value does not match String value", - new TextEnvelopeDummy(equals).asString(), - Matchers.is(equals) + new TextEnvelopeDummy(text).asString(), + new IsEqual<>(text) ); } /** - * Test for {@link AbstractTextEnvelope#equals(Object)} method. Must assert + * Test for {@link TextEnvelope#equals(Object)} method. Must assert * that the envelope value is equal to its string value. */ @Test @@ -61,12 +61,12 @@ public void testEquals() { MatcherAssert.assertThat( "Envelope value does not match its represented String value", new TextEnvelopeDummy(equals), - Matchers.is(equals) + new IsEqual<>(equals) ); } /** - * Test for {@link AbstractTextEnvelope#hashCode()} method. Must assert that - * the {@link AbstractTextEnvelope} hashCode is equals to the hashCode of + * Test for {@link TextEnvelope#hashCode()} method. Must assert that + * the {@link TextEnvelope} hashCode is equals to the hashCode of * the String it represents. */ @Test @@ -75,14 +75,14 @@ public void testHashCode() { MatcherAssert.assertThat( "Enveloped hashCode does not match its represented String hashcode", new TextEnvelopeDummy(hash).hashCode(), - Matchers.is(hash.hashCode()) + new IsEqual<>(hash.hashCode()) ); } /** - * Dummy class for {@link AbstractTextEnvelope} testing. + * Dummy class for {@link TextEnvelope} testing. */ - private final class TextEnvelopeDummy extends AbstractTextEnvelope { + private final class TextEnvelopeDummy extends TextEnvelope { /** * Ctor. From 18ea4957da0b530cfd1583f5a780a3d194bb4326 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Tue, 8 May 2018 17:52:44 -0300 Subject: [PATCH 17/25] Correction of @todo tag description. --- src/main/java/org/cactoos/text/TextEnvelope.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java index 764545a00f..bddf5cfada 100644 --- a/src/main/java/org/cactoos/text/TextEnvelope.java +++ b/src/main/java/org/cactoos/text/TextEnvelope.java @@ -35,9 +35,9 @@ * @author Paulo Lobo (pauloeduardolobo@gmail.com) * @version $Id$ * @since 0.32 - * @todo #788:30min Refactor classes in text package to use - * {@link TextEnvelope} allowing comparison using hashCode and equals - * methods. + * @todo #788:30min Refactor all classes in text package to extend from + * TextEnvelope, allowing direct comparison with string objects using hashCode + * and equals methods. * @checkstyle AbstractClassNameCheck (500 lines) */ @SuppressWarnings("PMD.AbstractNaming") From cfaa93e71222abd786c4f52b31f9604a0d5f6720 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Wed, 9 May 2018 09:27:35 -0300 Subject: [PATCH 18/25] Changes suggested in PR #819, reverting TextOf. --- .../java/org/cactoos/text/TextEnvelope.java | 12 ++++++------ src/main/java/org/cactoos/text/TextOf.java | 18 ++++++++++++++++-- .../org/cactoos/text/TextEnvelopeTest.java | 6 +++--- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java index bddf5cfada..f13770a19c 100644 --- a/src/main/java/org/cactoos/text/TextEnvelope.java +++ b/src/main/java/org/cactoos/text/TextEnvelope.java @@ -50,18 +50,18 @@ public abstract class TextEnvelope implements Text { /** * Ctor. - * @param scalar Scalar representing the text value. + * @param text Text representing the text value. */ - public TextEnvelope(final Scalar scalar) { - this.origin = new IoCheckedScalar<>(scalar); + public TextEnvelope(final Text text) { + this(new IoCheckedScalar<>(() -> text.asString())); } /** * Ctor. - * @param text Text representing the text value. + * @param scalar Scalar representing the text value. */ - public TextEnvelope(final Text text) { - this(new IoCheckedScalar<>(() -> text.asString())); + public TextEnvelope(final Scalar scalar) { + this.origin = new IoCheckedScalar<>(scalar); } @Override diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index 93547b44c0..189fd64d78 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -24,6 +24,7 @@ package org.cactoos.text; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; @@ -35,9 +36,11 @@ import org.cactoos.Bytes; import org.cactoos.Input; import org.cactoos.Scalar; +import org.cactoos.Text; import org.cactoos.io.BytesOf; import org.cactoos.io.InputOf; import org.cactoos.iterable.Mapped; +import org.cactoos.scalar.IoCheckedScalar; /** * TextOf @@ -48,7 +51,12 @@ * @version $Id$ * @since 0.12 */ -public final class TextOf extends TextEnvelope { +public final class TextOf implements Text { + + /** + * The origin. + */ + private final Scalar origin; /** * Ctor. @@ -349,11 +357,17 @@ public TextOf(final InputStream input) { * @param scalar The Scalar of String */ private TextOf(final Scalar scalar) { - super(scalar); + this.origin = scalar; + } + + @Override + public String asString() throws IOException { + return new IoCheckedScalar<>(this.origin).value(); } @Override public String toString() { return new UncheckedText(this).asString(); } + } diff --git a/src/test/java/org/cactoos/text/TextEnvelopeTest.java b/src/test/java/org/cactoos/text/TextEnvelopeTest.java index ca0a0608d1..3aad0c1acd 100644 --- a/src/test/java/org/cactoos/text/TextEnvelopeTest.java +++ b/src/test/java/org/cactoos/text/TextEnvelopeTest.java @@ -57,11 +57,11 @@ public void testAsString() throws Exception { */ @Test public void testEquals() { - final String equals = "equals"; + final String text = "equals"; MatcherAssert.assertThat( "Envelope value does not match its represented String value", - new TextEnvelopeDummy(equals), - new IsEqual<>(equals) + new TextEnvelopeDummy(text), + new IsEqual<>(text) ); } /** From 3115a488c7477c64bf7e8d367e0a614dcfa80bfe Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Wed, 9 May 2018 10:20:36 -0300 Subject: [PATCH 19/25] Changes suggested in PR #819, reverting TextOf. --- src/main/java/org/cactoos/text/TextEnvelope.java | 4 ++-- src/test/java/org/cactoos/text/TextEnvelopeTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java index f13770a19c..975c03b146 100644 --- a/src/main/java/org/cactoos/text/TextEnvelope.java +++ b/src/main/java/org/cactoos/text/TextEnvelope.java @@ -36,8 +36,8 @@ * @version $Id$ * @since 0.32 * @todo #788:30min Refactor all classes in text package to extend from - * TextEnvelope, allowing direct comparison with string objects using hashCode - * and equals methods. + * TextEnvelope, allowing direct comparison with string objects using hashCode + * and equals methods. * @checkstyle AbstractClassNameCheck (500 lines) */ @SuppressWarnings("PMD.AbstractNaming") diff --git a/src/test/java/org/cactoos/text/TextEnvelopeTest.java b/src/test/java/org/cactoos/text/TextEnvelopeTest.java index 3aad0c1acd..a88aeffdbb 100644 --- a/src/test/java/org/cactoos/text/TextEnvelopeTest.java +++ b/src/test/java/org/cactoos/text/TextEnvelopeTest.java @@ -39,7 +39,7 @@ public final class TextEnvelopeTest { /** * Test for {@link TextEnvelope#asString()} method. Must assert that - * the enveloped {@link TextOf} value is equal to {@link TextOf} value. + * the envelope asString value is equal to its string value. * @throws Exception Throws from asString. */ @Test From 0b138e25ca2880d6b576c3dc423b4e7e4643806a Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Tue, 8 May 2018 13:58:31 -0300 Subject: [PATCH 20/25] Added changes suggested in review #828 for resolution of #788: Using Text as Map key is impossible. --- ...Envelope.java => AbstractTextEnvelope.java} | 0 src/main/java/org/cactoos/text/TextOf.java | 18 ++---------------- 2 files changed, 2 insertions(+), 16 deletions(-) rename src/main/java/org/cactoos/text/{TextEnvelope.java => AbstractTextEnvelope.java} (100%) diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/AbstractTextEnvelope.java similarity index 100% rename from src/main/java/org/cactoos/text/TextEnvelope.java rename to src/main/java/org/cactoos/text/AbstractTextEnvelope.java diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index 189fd64d78..8f8a14b33e 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -24,7 +24,6 @@ package org.cactoos.text; import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; @@ -36,11 +35,9 @@ import org.cactoos.Bytes; import org.cactoos.Input; import org.cactoos.Scalar; -import org.cactoos.Text; import org.cactoos.io.BytesOf; import org.cactoos.io.InputOf; import org.cactoos.iterable.Mapped; -import org.cactoos.scalar.IoCheckedScalar; /** * TextOf @@ -51,12 +48,7 @@ * @version $Id$ * @since 0.12 */ -public final class TextOf implements Text { - - /** - * The origin. - */ - private final Scalar origin; +public final class TextOf extends AbstractTextEnvelope { /** * Ctor. @@ -357,17 +349,11 @@ public TextOf(final InputStream input) { * @param scalar The Scalar of String */ private TextOf(final Scalar scalar) { - this.origin = scalar; - } - - @Override - public String asString() throws IOException { - return new IoCheckedScalar<>(this.origin).value(); + super(scalar); } @Override public String toString() { return new UncheckedText(this).asString(); } - } From 8acd6a6f9e442d914101d26750e50e5fdcdd95d6 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Tue, 8 May 2018 17:36:10 -0300 Subject: [PATCH 21/25] Added changes suggested in review #828 for resolution of #788: Using Text as Map key is impossible. --- .../text/{AbstractTextEnvelope.java => TextEnvelope.java} | 0 src/main/java/org/cactoos/text/TextOf.java | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename src/main/java/org/cactoos/text/{AbstractTextEnvelope.java => TextEnvelope.java} (100%) diff --git a/src/main/java/org/cactoos/text/AbstractTextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java similarity index 100% rename from src/main/java/org/cactoos/text/AbstractTextEnvelope.java rename to src/main/java/org/cactoos/text/TextEnvelope.java diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index 8f8a14b33e..93547b44c0 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -48,7 +48,7 @@ * @version $Id$ * @since 0.12 */ -public final class TextOf extends AbstractTextEnvelope { +public final class TextOf extends TextEnvelope { /** * Ctor. From 1de377fd4beb4f795c072665d7b3f3a3303612c7 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Wed, 9 May 2018 09:27:35 -0300 Subject: [PATCH 22/25] Changes suggested in PR #819, reverting TextOf. --- src/main/java/org/cactoos/text/TextOf.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index 93547b44c0..189fd64d78 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -24,6 +24,7 @@ package org.cactoos.text; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; @@ -35,9 +36,11 @@ import org.cactoos.Bytes; import org.cactoos.Input; import org.cactoos.Scalar; +import org.cactoos.Text; import org.cactoos.io.BytesOf; import org.cactoos.io.InputOf; import org.cactoos.iterable.Mapped; +import org.cactoos.scalar.IoCheckedScalar; /** * TextOf @@ -48,7 +51,12 @@ * @version $Id$ * @since 0.12 */ -public final class TextOf extends TextEnvelope { +public final class TextOf implements Text { + + /** + * The origin. + */ + private final Scalar origin; /** * Ctor. @@ -349,11 +357,17 @@ public TextOf(final InputStream input) { * @param scalar The Scalar of String */ private TextOf(final Scalar scalar) { - super(scalar); + this.origin = scalar; + } + + @Override + public String asString() throws IOException { + return new IoCheckedScalar<>(this.origin).value(); } @Override public String toString() { return new UncheckedText(this).asString(); } + } From ba7392ee54c539e61e7af3dfbc89ab76b6318788 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Tue, 8 May 2018 13:58:31 -0300 Subject: [PATCH 23/25] Added changes suggested in review #828 for resolution of #788: Using Text as Map key is impossible. --- ...Envelope.java => AbstractTextEnvelope.java} | 0 src/main/java/org/cactoos/text/TextOf.java | 18 ++---------------- 2 files changed, 2 insertions(+), 16 deletions(-) rename src/main/java/org/cactoos/text/{TextEnvelope.java => AbstractTextEnvelope.java} (100%) diff --git a/src/main/java/org/cactoos/text/TextEnvelope.java b/src/main/java/org/cactoos/text/AbstractTextEnvelope.java similarity index 100% rename from src/main/java/org/cactoos/text/TextEnvelope.java rename to src/main/java/org/cactoos/text/AbstractTextEnvelope.java diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index 189fd64d78..8f8a14b33e 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -24,7 +24,6 @@ package org.cactoos.text; import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; @@ -36,11 +35,9 @@ import org.cactoos.Bytes; import org.cactoos.Input; import org.cactoos.Scalar; -import org.cactoos.Text; import org.cactoos.io.BytesOf; import org.cactoos.io.InputOf; import org.cactoos.iterable.Mapped; -import org.cactoos.scalar.IoCheckedScalar; /** * TextOf @@ -51,12 +48,7 @@ * @version $Id$ * @since 0.12 */ -public final class TextOf implements Text { - - /** - * The origin. - */ - private final Scalar origin; +public final class TextOf extends AbstractTextEnvelope { /** * Ctor. @@ -357,17 +349,11 @@ public TextOf(final InputStream input) { * @param scalar The Scalar of String */ private TextOf(final Scalar scalar) { - this.origin = scalar; - } - - @Override - public String asString() throws IOException { - return new IoCheckedScalar<>(this.origin).value(); + super(scalar); } @Override public String toString() { return new UncheckedText(this).asString(); } - } From 59042c7c934ebdc8b79da3db45e125b09ad23069 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Tue, 8 May 2018 17:36:10 -0300 Subject: [PATCH 24/25] Added changes suggested in review #828 for resolution of #788: Using Text as Map key is impossible. --- .../text/{AbstractTextEnvelope.java => TextEnvelope.java} | 0 src/main/java/org/cactoos/text/TextOf.java | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename src/main/java/org/cactoos/text/{AbstractTextEnvelope.java => TextEnvelope.java} (100%) diff --git a/src/main/java/org/cactoos/text/AbstractTextEnvelope.java b/src/main/java/org/cactoos/text/TextEnvelope.java similarity index 100% rename from src/main/java/org/cactoos/text/AbstractTextEnvelope.java rename to src/main/java/org/cactoos/text/TextEnvelope.java diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index 8f8a14b33e..93547b44c0 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -48,7 +48,7 @@ * @version $Id$ * @since 0.12 */ -public final class TextOf extends AbstractTextEnvelope { +public final class TextOf extends TextEnvelope { /** * Ctor. From a60101c72e9309c7b8fbba821aa7316a22d82851 Mon Sep 17 00:00:00 2001 From: Paulo Lobo Date: Wed, 9 May 2018 09:27:35 -0300 Subject: [PATCH 25/25] Changes suggested in PR #819, reverting TextOf. --- src/main/java/org/cactoos/text/TextOf.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index 93547b44c0..189fd64d78 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -24,6 +24,7 @@ package org.cactoos.text; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; @@ -35,9 +36,11 @@ import org.cactoos.Bytes; import org.cactoos.Input; import org.cactoos.Scalar; +import org.cactoos.Text; import org.cactoos.io.BytesOf; import org.cactoos.io.InputOf; import org.cactoos.iterable.Mapped; +import org.cactoos.scalar.IoCheckedScalar; /** * TextOf @@ -48,7 +51,12 @@ * @version $Id$ * @since 0.12 */ -public final class TextOf extends TextEnvelope { +public final class TextOf implements Text { + + /** + * The origin. + */ + private final Scalar origin; /** * Ctor. @@ -349,11 +357,17 @@ public TextOf(final InputStream input) { * @param scalar The Scalar of String */ private TextOf(final Scalar scalar) { - super(scalar); + this.origin = scalar; + } + + @Override + public String asString() throws IOException { + return new IoCheckedScalar<>(this.origin).value(); } @Override public String toString() { return new UncheckedText(this).asString(); } + }