From b8cbd5212d19a57fc6234239788de16d8c5698f1 Mon Sep 17 00:00:00 2001 From: "Olivier B. OURA" Date: Thu, 25 Feb 2021 18:24:22 +0000 Subject: [PATCH 01/29] Introduce Text implementations #1461 --- src/main/java/org/cactoos/text/TextOf.java | 332 ++---------------- .../java/org/cactoos/text/TextOfDateTime.java | 282 +++++++++++++++ .../java/org/cactoos/text/TextOfScalar.java | 85 +++++ .../java/org/cactoos/text/TextOfString.java | 82 +++++ .../org/cactoos/text/TextOfDateTimeTest.java | 291 +++++++++++++++ .../java/org/cactoos/text/TextOfTest.java | 249 ------------- 6 files changed, 765 insertions(+), 556 deletions(-) create mode 100644 src/main/java/org/cactoos/text/TextOfDateTime.java create mode 100644 src/main/java/org/cactoos/text/TextOfScalar.java create mode 100644 src/main/java/org/cactoos/text/TextOfString.java create mode 100644 src/test/java/org/cactoos/text/TextOfDateTimeTest.java diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index ad9e2c8db2..660f645aef 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -32,16 +32,7 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Path; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.OffsetDateTime; -import java.time.ZoneId; -import java.time.ZonedDateTime; -import java.time.format.DateTimeFormatter; -import java.util.Date; import java.util.Iterator; -import java.util.Locale; import org.cactoos.Bytes; import org.cactoos.Input; import org.cactoos.Scalar; @@ -49,10 +40,6 @@ import org.cactoos.bytes.BytesOf; import org.cactoos.io.InputOf; import org.cactoos.iterable.Mapped; -import org.cactoos.scalar.And; -import org.cactoos.scalar.Or; -import org.cactoos.scalar.Unchecked; -import org.cactoos.time.Iso; /** * TextOf @@ -60,20 +47,10 @@ *

There is no thread-safety guarantee. * * @since 0.12 - * @todo #1287:30min Introduce `Text` implementations `TextOfScalar`, - * `TextOfBytes` and `TextOfDateTime` (i.e., for all of the constructor - * below that delegates to a `Scalar`) and have `TextOf` extends `TextEnvelope` - * to delegate to those new classes. If possible remove the checkstyle exclusions - * below. * @checkstyle ClassDataAbstractionCouplingCheck (1000 lines) * @checkstyle ClassFanOutComplexityCheck (1000 lines) */ -public final class TextOf implements Text { - - /** - * String value of the envelope. - */ - private final Scalar origin; +public final class TextOf extends TextEnvelope { /** * Ctor. @@ -310,7 +287,9 @@ public TextOf(final Bytes bytes) { * @param cset The Charset */ public TextOf(final Bytes bytes, final Charset cset) { - this(() -> new String(bytes.asBytes(), cset)); + this( + new TextOfScalar(() -> new String(bytes.asBytes(), cset)) + ); } /** @@ -320,7 +299,9 @@ public TextOf(final Bytes bytes, final Charset cset) { * @param cset The Charset */ public TextOf(final Bytes bytes, final String cset) { - this(() -> new String(bytes.asBytes(), cset)); + this( + new TextOfScalar(() -> new String(bytes.asBytes(), cset)) + ); } /** @@ -335,27 +316,17 @@ public TextOf(final String input) { /** * Ctor. * - * @param input The String - * @param cset The Charset - */ - public TextOf(final String input, final Charset cset) { - this(() -> new String(input.getBytes(cset), cset)); - } - - /** - * Ctor. * @param iterable The iterable to convert to string - * @since 0.21 */ public TextOf(final Iterable iterable) { - this( - () -> new Joined( + super( + new Joined( ", ", new Mapped<>( Object::toString, iterable ) - ).asString() + ) ); } @@ -366,11 +337,13 @@ public TextOf(final Iterable iterable) { */ public TextOf(final Iterator iterator) { this( - () -> { - final StringBuilder buf = new StringBuilder(); - iterator.forEachRemaining(buf::append); - return buf.toString(); - } + new TextOfScalar( + () -> { + final StringBuilder buf = new StringBuilder(); + iterator.forEachRemaining(buf::append); + return buf.toString(); + } + ) ); } @@ -384,276 +357,21 @@ public TextOf(final InputStream input) { this(new InputOf(new InputStreamReader(input, StandardCharsets.UTF_8))); } - /** - * Formats date using ISO date time format. - * @param date The date to format. - * @since 1.0 - */ - public TextOf(final LocalDate date) { - this(date, new Iso().value()); - } - - /** - * Formats date using provided date time format string using default locale. - * @param date The date to format. - * @param format The format to use. - * @since 1.0 - */ - public TextOf(final LocalDate date, final String format) { - this(date, format, Locale.getDefault(Locale.Category.FORMAT)); - } - - /** - * Formats the date using the provided format string using the provided - * locale. - * @param date The date to format. - * @param format The format string to use. - * @param locale The locale to use. - * @since 1.0 - */ - public TextOf( - final LocalDate date, - final String format, - final Locale locale - ) { - this(date, DateTimeFormatter.ofPattern(format, locale)); - } - - /** - * Formats the date using the provided formatter. - * @param date The date to format. - * @param formatter The formatter to use. - * @since 1.0 - */ - public TextOf(final LocalDate date, final DateTimeFormatter formatter) { - this( - () -> formatter.format( - ZonedDateTime.of( - date, LocalTime.MIN, ZoneId.systemDefault() - ) - ) - ); - } - - /** - * Formats the date using the provided formatter. - * @param date The date to format. - * @param formatter The formatter to use. - */ - public TextOf( - final LocalDateTime date, - final DateTimeFormatter formatter - ) { - this(() -> formatter.format(date.atZone(ZoneId.systemDefault()))); - } - - /** - * Formats date using ISO date time format. - * @param date The date to format. - */ - public TextOf(final LocalDateTime date) { - this(date, new Iso().value()); - } - - /** - * Formats date using provided date time format string using default locale. - * @param date The date to format. - * @param format The format to use. - */ - public TextOf(final LocalDateTime date, final String format) { - this(date, format, Locale.getDefault(Locale.Category.FORMAT)); - } - - /** - * Formats the date using the provided format string using the provided - * locale. - * @param date The date to format. - * @param format The format string to use. - * @param locale The locale to use. - */ - public TextOf( - final LocalDateTime date, - final String format, - final Locale locale - ) { - this(date, DateTimeFormatter.ofPattern(format, locale)); - } - - /** - * Formats the date with ISO format using the system zone. - * @param date The date to format. - */ - public TextOf(final Date date) { - this(date, new Iso().value()); - } - - /** - * Formats the date with to format using the default locale and the system - * zone. - * @param date The date to format. - * @param format The format to use. - */ - public TextOf(final Date date, final String format) { - this(date, format, Locale.getDefault(Locale.Category.FORMAT)); - } - - /** - * Formats the date using the format and locale using the system default - * zone. - * @param date The date to format. - * @param format The format to use. - * @param locale The locale to use. - */ - public TextOf( - final Date date, - final String format, - final Locale locale - ) { - this(date, DateTimeFormatter.ofPattern(format, locale)); - } - - /** - * Formats the date using the format and locale using the system default - * zone. - * @param date The date to format. - * @param formatter The formatter to use. - */ - public TextOf(final Date date, final DateTimeFormatter formatter) { - this( - () -> formatter.format( - ZonedDateTime.ofInstant( - date.toInstant(), - ZoneId.systemDefault() - ) - ) - ); - } - - /** - * Formats date using ISO date time format. - * @param date The date to format. - */ - public TextOf(final OffsetDateTime date) { - this(date, new Iso().value()); - } - - /** - * Formats date using provided date time format string using default locale. - * @param date The date to format. - * @param format The format to use. - */ - public TextOf(final OffsetDateTime date, final String format) { - this(date, format, Locale.getDefault(Locale.Category.FORMAT)); - } - - /** - * Formats the date using the provided format string using the provided - * locale. - * @param date The date to format. - * @param format The format string to use. - * @param locale The locale to use. - */ - public TextOf( - final OffsetDateTime date, - final String format, - final Locale locale - ) { - this(date, DateTimeFormatter.ofPattern(format, locale)); - } - - /** - * Formats the date using the provided formatter. - * @param date The date to format. - * @param formatter The formatter to use. - */ - public TextOf( - final OffsetDateTime date, - final DateTimeFormatter formatter - ) { - this(() -> formatter.format(date)); - } - - /** - * Formats date using ISO date time format. - * @param date The date to format. - */ - public TextOf(final ZonedDateTime date) { - this(date, new Iso().value()); - } - - /** - * Formats date using provided date time format string using default locale. - * @param date The date to format. - * @param format The format to use. - */ - public TextOf(final ZonedDateTime date, final String format) { - this(date, format, Locale.getDefault(Locale.Category.FORMAT)); - } - - /** - * Formats the date using the provided format string using the provided - * locale. - * @param date The date to format. - * @param format The format string to use. - * @param locale The locale to use. - */ - public TextOf( - final ZonedDateTime date, - final String format, - final Locale locale - ) { - this(date, DateTimeFormatter.ofPattern(format, locale)); - } - - /** - * Formats the date using the provided formatter. - * @param date The date to format. - * @param formatter The formatter to use. - */ - public TextOf( - final ZonedDateTime date, - final DateTimeFormatter formatter - ) { - this(() -> formatter.format(date)); - } - /** * Ctor. * * @param scalar The Scalar of String */ public TextOf(final Scalar scalar) { - this.origin = scalar; - } - - @Override - public String asString() throws Exception { - return this.origin.value(); - } - - @Override - public String toString() { - return new UncheckedText(this).asString(); - } - - @Override - public int hashCode() { - return new Unchecked<>(this.origin).value().hashCode(); + this(new TextOfScalar(scalar)); } - @Override - @edu.umd.cs.findbugs.annotations.SuppressFBWarnings("EQ_UNUSUAL") - public boolean equals(final Object obj) { - return new Unchecked<>( - new Or( - () -> this == obj, - new And( - () -> obj instanceof Text, - () -> new UncheckedText(this) - .asString() - .equals(new UncheckedText((Text) obj).asString()) - ) - ) - ).value(); + /** + * Ctor. + * + * @param text Text + */ + private TextOf(final Text text) { + super(text); } } diff --git a/src/main/java/org/cactoos/text/TextOfDateTime.java b/src/main/java/org/cactoos/text/TextOfDateTime.java new file mode 100644 index 0000000000..4b520383fb --- /dev/null +++ b/src/main/java/org/cactoos/text/TextOfDateTime.java @@ -0,0 +1,282 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.text; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Date; +import java.util.Locale; +import org.cactoos.time.Iso; + +/** + * Text of date time + * + *

There is no thread-safety guarantee. + * + * @since 1.0.0 + */ +public final class TextOfDateTime extends TextEnvelope { + + /** + * Formats date using ISO date time format. + * @param date The date to format. + */ + public TextOfDateTime(final LocalDate date) { + this(new Iso().value(), date); + } + + /** + * Formats date using provided date time format string using default locale. + * @param format The format to use. + * @param date The date to format. + */ + public TextOfDateTime(final String format, final LocalDate date) { + this(format, date, Locale.getDefault(Locale.Category.FORMAT)); + } + + /** + * Formats the date using the provided format string using the provided + * locale. + * @param format The format string to use. + * @param date The date to format. + * @param locale The locale to use. + */ + public TextOfDateTime( + final String format, + final LocalDate date, + final Locale locale + ) { + this(DateTimeFormatter.ofPattern(format, locale), date); + } + + /** + * Formats date using ISO date time format. + * @param date The date to format. + */ + public TextOfDateTime(final LocalDateTime date) { + this(new Iso().value(), date); + } + + /** + * Formats date using provided date time format string using default locale. + * @param format The format to use. + * @param date The date to format. + */ + public TextOfDateTime(final String format, final LocalDateTime date) { + this(format, date, Locale.getDefault(Locale.Category.FORMAT)); + } + + /** + * Formats the date using the provided format string using the provided + * locale. + * @param format The format string to use. + * @param date The date to format. + * @param locale The locale to use. + */ + public TextOfDateTime( + final String format, + final LocalDateTime date, + final Locale locale + ) { + this(DateTimeFormatter.ofPattern(format, locale), date); + } + + /** + * Formats the date with ISO format using the system zone. + * @param date The date to format. + */ + public TextOfDateTime(final Date date) { + this(new Iso().value(), date); + } + + /** + * Formats the date with to format using the default locale and the system + * zone. + * @param format The format to use. + * @param date The date to format. + */ + public TextOfDateTime(final String format, final Date date) { + this(format, date, Locale.getDefault(Locale.Category.FORMAT)); + } + + /** + * Formats the date using the format and locale using the system default + * zone. + * @param format The format to use. + * @param date The date to format. + * @param locale The locale to use. + */ + public TextOfDateTime( + final String format, + final Date date, + final Locale locale + ) { + this(DateTimeFormatter.ofPattern(format, locale), date); + } + + /** + * Formats date using ISO date time format. + * @param date The date to format. + */ + public TextOfDateTime(final OffsetDateTime date) { + this(new Iso().value(), date); + } + + /** + * Formats date using provided date time format string using default locale. + * @param format The format to use. + * @param date The date to format. + */ + public TextOfDateTime(final String format, final OffsetDateTime date) { + this(format, date, Locale.getDefault(Locale.Category.FORMAT)); + } + + /** + * Formats the date using the provided format string using the provided + * locale. + * @param format The format string to use. + * @param date The date to format. + * @param locale The locale to use. + */ + public TextOfDateTime( + final String format, + final OffsetDateTime date, + final Locale locale + ) { + this(DateTimeFormatter.ofPattern(format, locale), date); + } + + /** + * Formats date using ISO date time format. + * @param date The date to format. + */ + public TextOfDateTime(final ZonedDateTime date) { + this(new Iso().value(), date); + } + + /** + * Formats date using provided date time format string using default locale. + * @param format The format to use. + * @param date The date to format. + */ + public TextOfDateTime(final String format, final ZonedDateTime date) { + this(format, date, Locale.getDefault(Locale.Category.FORMAT)); + } + + /** + * Formats the date using the provided format string using the provided + * locale. + * @param format The format to use. + * @param date The date to format. + * @param locale The locale to use. + */ + public TextOfDateTime( + final String format, + final ZonedDateTime date, + final Locale locale + ) { + this(DateTimeFormatter.ofPattern(format, locale), date); + } + + /** + * Formats the date using the provided formatter. + * @param formatter The formatter to use. + * @param date The date to format. + */ + public TextOfDateTime(final DateTimeFormatter formatter, final LocalDate date) { + super( + new TextOfScalar( + () -> formatter.format( + ZonedDateTime.of( + date, LocalTime.MIN, ZoneId.systemDefault() + ) + ) + ) + ); + } + + /** + * Formats the date using the provided formatter. + * @param formatter The formatter to use. + * @param date The date to format. + */ + public TextOfDateTime( + final DateTimeFormatter formatter, + final LocalDateTime date + ) { + super( + new TextOfScalar( + () -> formatter.format(date.atZone(ZoneId.systemDefault())) + ) + ); + } + + /** + * Formats the date using the format and locale using the system default + * zone. + * @param formatter The formatter to use. + * @param date The date to format. + */ + public TextOfDateTime(final DateTimeFormatter formatter, final Date date) { + super( + new TextOfScalar( + () -> formatter.format( + ZonedDateTime.ofInstant( + date.toInstant(), + ZoneId.systemDefault() + ) + ) + ) + ); + } + + /** + * Formats the date using the provided formatter. + * @param formatter The formatter to use. + * @param date The date to format. + */ + public TextOfDateTime( + final DateTimeFormatter formatter, + final OffsetDateTime date + ) { + super(new TextOfScalar(() -> formatter.format(date))); + } + + /** + * Formats the date using the provided formatter. + * @param formatter The formatter to use. + * @param date The date to format. + */ + public TextOfDateTime( + final DateTimeFormatter formatter, + final ZonedDateTime date + ) { + super(new TextOfScalar(() -> formatter.format(date))); + } +} diff --git a/src/main/java/org/cactoos/text/TextOfScalar.java b/src/main/java/org/cactoos/text/TextOfScalar.java new file mode 100644 index 0000000000..42d9ed31c0 --- /dev/null +++ b/src/main/java/org/cactoos/text/TextOfScalar.java @@ -0,0 +1,85 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.text; + +import org.cactoos.Scalar; +import org.cactoos.Text; +import org.cactoos.scalar.And; +import org.cactoos.scalar.Or; +import org.cactoos.scalar.Unchecked; + +/** + * Text of {@link Scalar} + * + *

There is no thread-safety guarantee. + * + * @since 1.0.0 + */ +public final class TextOfScalar implements Text { + + /** + * String value of the envelope. + */ + private final Scalar origin; + + /** + * Ctor. + * + * @param scalar The Scalar of String + */ + public TextOfScalar(final Scalar scalar) { + this.origin = scalar; + } + + @Override + public String asString() throws Exception { + return this.origin.value(); + } + + @Override + public String toString() { + return new UncheckedText(this).asString(); + } + + @Override + public int hashCode() { + return new Unchecked<>(this.origin).value().hashCode(); + } + + @Override + @edu.umd.cs.findbugs.annotations.SuppressFBWarnings("EQ_UNUSUAL") + public boolean equals(final Object obj) { + return new Unchecked<>( + new Or( + () -> this == obj, + new And( + () -> obj instanceof Text, + () -> new UncheckedText(this) + .asString() + .equals(new UncheckedText((Text) obj).asString()) + ) + ) + ).value(); + } +} diff --git a/src/main/java/org/cactoos/text/TextOfString.java b/src/main/java/org/cactoos/text/TextOfString.java new file mode 100644 index 0000000000..f1ff7060d4 --- /dev/null +++ b/src/main/java/org/cactoos/text/TextOfString.java @@ -0,0 +1,82 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.text; + +import org.cactoos.Text; +import org.cactoos.scalar.And; +import org.cactoos.scalar.Or; +import org.cactoos.scalar.Unchecked; + +/** + * Text of {@link String} + * + *

There is no thread-safety guarantee. + * + * @since 1.0.0 + */ +public final class TextOfString implements Text { + + /** + * Input text. + */ + private final String input; + + /** + * Ctor. + * + * @param input The String + */ + public TextOfString(final String input) { + this.input = input; + } + + @Override + public String asString() throws Exception { + return this.input; + } + + @Override + public String toString() { + return this.input; + } + + @Override + public int hashCode() { + return this.input.hashCode(); + } + + @Override + @edu.umd.cs.findbugs.annotations.SuppressFBWarnings("EQ_UNUSUAL") + public boolean equals(final Object obj) { + return new Unchecked<>( + new Or( + () -> this == obj, + new And( + () -> obj instanceof Text, + () -> this.input.equals(new UncheckedText((Text) obj).asString()) + ) + ) + ).value(); + } +} diff --git a/src/test/java/org/cactoos/text/TextOfDateTimeTest.java b/src/test/java/org/cactoos/text/TextOfDateTimeTest.java new file mode 100644 index 0000000000..8afab67016 --- /dev/null +++ b/src/test/java/org/cactoos/text/TextOfDateTimeTest.java @@ -0,0 +1,291 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.text; + +import java.io.IOException; +import java.text.MessageFormat; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.util.Calendar; +import java.util.Locale; +import java.util.TimeZone; +import org.hamcrest.core.IsNot; +import org.hamcrest.core.IsNull; +import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.IsBlank; +import org.llorllale.cactoos.matchers.IsText; + +/** + * Test case for {@link TextOfDateTime}. + * + * @since 1.0.0 + * @checkstyle ClassDataAbstractionCouplingCheck (1000 lines) + * @checkstyle MagicNumberCheck (1000 lines) + * @checkstyle StringLiteralsConcatenationCheck (1000 lines) + */ +@SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals"}) +final class TextOfDateTimeTest { + + @Test + void readsLocalDateFormattedWithFormatString() { + final LocalDate date = LocalDate.of(2017, 12, 13); + new Assertion<>( + "Must format a LocalDate with format.", + new TextOfDateTime("yyyy-MM-dd HH:mm:ss", date), + new IsText("2017-12-13 00:00:00") + ).affirm(); + } + + @Test + void readsLocalDateFormattedWithFormatStringWithLocale() { + final LocalDate date = LocalDate.of(2017, 12, 13); + new Assertion<>( + "Must format a LocalDate with format using locale.", + new TextOfDateTime( + "yyyy MMM dd. HH.mm.ss", date, Locale.FRENCH + ), + new IsText("2017 déc. 13. 00.00.00") + ).affirm(); + } + + @Test + void readsLocalDateFormattedAsIsoDateTime() throws IOException { + final LocalDate date = LocalDate.of(2017, 12, 13); + new Assertion<>( + "Must format a LocalDate with default/ISO format.", + new TextOfDateTime(date), + new IsText( + MessageFormat.format( + "2017-12-13T00:00:00{0}", + date.atTime(LocalTime.MIN).atZone(ZoneId.systemDefault()) + .getOffset().toString() + ) + ) + ).affirm(); + } + + @Test + void readsCurrentLocalDateAsText() throws Exception { + new Assertion<>( + "Must format a LocalDate with ISO format.", + new TextOfDateTime(LocalDate.now()).asString(), + new IsNot<>(new IsBlank()) + ).affirm(); + } + + @Test + void localDateTimeFormattedAsIsoDateTime() { + final LocalDateTime date = LocalDateTime.of( + 2017, 12, 13, 14, 15, 16, 17 + ); + new Assertion<>( + "Must format a LocalDateTime with default/ISO format.", + new TextOfDateTime(date), + new IsText( + MessageFormat.format( + "2017-12-13T14:15:16.000000017{0}", + date.atZone(ZoneId.systemDefault()).getOffset().toString() + ) + ) + ).affirm(); + } + + @Test + void localDateTimeFormattedWithFormatString() { + final LocalDateTime date = LocalDateTime.of( + 2017, 12, 13, 14, 15, 16, 17 + ); + new Assertion<>( + "Must format a LocalDateTime with format.", + new TextOfDateTime("yyyy-MM-dd HH:mm:ss", date), + new IsText("2017-12-13 14:15:16") + ).affirm(); + } + + @Test + void localDateTimeFormattedWithFormatStringWithLocale() { + final LocalDateTime date = LocalDateTime.of( + 2017, 12, 13, 14, 15, 16, 17 + ); + new Assertion<>( + "Must format a LocalDateTime with format using locale.", + new TextOfDateTime( + "yyyy MMM dd. HH.mm.ss", date, Locale.FRENCH + ), + new IsText("2017 déc. 13. 14.15.16") + ).affirm(); + } + + @Test + void currentLocalDateTimeAsText() throws Exception { + new Assertion<>( + "Must format a LocalDateTime with ISO format.", + new TextOfDateTime(LocalDateTime.now()).asString(), + new IsNot<>(new IsNull<>()) + ).affirm(); + } + + @Test + void dateFormattedUsingIsoFormatter() { + final Calendar calendar = + Calendar.getInstance(TimeZone.getDefault()); + calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16); + calendar.set(Calendar.MILLISECOND, 17); + final ZoneOffset offset = calendar.getTimeZone().toZoneId() + .getRules().getOffset(calendar.toInstant()); + new Assertion<>( + "Must format a java.util.Date with ISO format.", + new TextOfDateTime(calendar.getTime()), + new IsText("2017-12-13T14:15:16.017" + offset) + ).affirm(); + } + + @Test + void dateFormattedUsingCustomFormat() { + final Calendar calendar = + Calendar.getInstance(TimeZone.getDefault()); + calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16); + new Assertion<>( + "Must format a java.util.Date with custom format.", + new TextOfDateTime( + "yyyy MM dd hh:mm:ss", calendar.getTime() + ), + new IsText("2017 12 13 02:15:16") + ).affirm(); + } + + @Test + void dateFormattedUsingCustomFormatDifferentLocale() { + final Calendar calendar = + Calendar.getInstance(TimeZone.getDefault()); + calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16); + new Assertion<>( + "Must format a java.util.Date with custom format.", + new TextOfDateTime( + "yyyy MMM dd hh:mm:ss", calendar.getTime(), Locale.ITALIAN + ), + new IsText("2017 dic 13 02:15:16") + ).affirm(); + } + + @Test + void offsetDateTimeFormattedAsIsoDateTime() { + final OffsetDateTime date = OffsetDateTime.of( + 2017, 12, 13, 14, 15, 16, 17, ZoneOffset.ofHours(1) + ); + new Assertion<>( + "Must format a OffsetDateTime with default/ISO format.", + new TextOfDateTime(date), + new IsText("2017-12-13T14:15:16.000000017+01:00") + ).affirm(); + } + + @Test + void offsetDateTimeFormattedWithFormatString() { + final OffsetDateTime date = OffsetDateTime.of( + 2017, 12, 13, 14, 15, 16, 17, ZoneOffset.ofHours(1) + ); + new Assertion<>( + "Must format a OffsetDateTime with format.", + new TextOfDateTime("yyyy-MM-dd HH:mm:ss", date), + new IsText("2017-12-13 14:15:16") + ).affirm(); + } + + @Test + void offsetDateTimeFormattedWithFormatStringWithLocale() { + final OffsetDateTime date = OffsetDateTime.of( + 2017, 12, 13, 14, 15, 16, 17, ZoneOffset.ofHours(1) + ); + new Assertion<>( + "Must format a OffsetDateTime with format using locale.", + new TextOfDateTime( + "yyyy MMM dd. HH.mm.ss", date, Locale.FRENCH + ), + new IsText("2017 déc. 13. 14.15.16") + ).affirm(); + } + + @Test + void currentOffsetDateTimeAsText() throws Exception { + new Assertion<>( + "Must format a OffsetDateTime with ISO format.", + new TextOfDateTime(OffsetDateTime.now()).asString(), + new IsNot<>(new IsNull<>()) + ).affirm(); + } + + @Test + void zonedDateTimeFormattedAsIsoDateTime() { + final ZonedDateTime date = ZonedDateTime.of( + 2017, 12, 13, 14, 15, 16, 17, ZoneId.of("Europe/Berlin") + ); + new Assertion<>( + "Must format a ZonedDateTime with default/ISO format.", + new TextOfDateTime(date), + new IsText("2017-12-13T14:15:16.000000017+01:00") + ).affirm(); + } + + @Test + void zonedDateTimeFormattedWithFormatString() { + final ZonedDateTime date = ZonedDateTime.of( + 2017, 12, 13, 14, 15, 16, 17, ZoneId.of("Europe/Berlin") + ); + new Assertion<>( + "Must format a ZonedDateTime with format.", + new TextOfDateTime("yyyy-MM-dd HH:mm:ss", date), + new IsText("2017-12-13 14:15:16") + ).affirm(); + } + + @Test + void zonedDateTimeFormattedWithFormatStringWithLocale() { + final ZonedDateTime date = ZonedDateTime.of( + 2017, 12, 13, 14, 15, 16, 17, ZoneId.of("Europe/Berlin") + ); + new Assertion<>( + "Must format a ZonedDateTime with format using locale.", + new TextOfDateTime( + "yyyy MMM dd. HH.mm.ss", date, Locale.FRENCH + ), + new IsText("2017 déc. 13. 14.15.16") + ).affirm(); + } + + @Test + void currentZonedDateTimeAsText() throws Exception { + new Assertion<>( + "Must format a ZonedDateTime with ISO format.", + new TextOfDateTime(ZonedDateTime.now()).asString(), + new IsNot<>(new IsNull<>()) + ).affirm(); + } +} diff --git a/src/test/java/org/cactoos/text/TextOfTest.java b/src/test/java/org/cactoos/text/TextOfTest.java index 009c30d282..68ac0294f0 100644 --- a/src/test/java/org/cactoos/text/TextOfTest.java +++ b/src/test/java/org/cactoos/text/TextOfTest.java @@ -28,28 +28,15 @@ import java.io.InputStream; import java.io.StringReader; import java.nio.charset.StandardCharsets; -import java.text.MessageFormat; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.OffsetDateTime; -import java.time.ZoneId; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.util.Calendar; -import java.util.Locale; -import java.util.TimeZone; import org.cactoos.bytes.BytesOf; import org.cactoos.io.InputOf; import org.cactoos.iterator.IteratorOfChars; import org.hamcrest.Matchers; import org.hamcrest.core.IsEqual; import org.hamcrest.core.IsNot; -import org.hamcrest.core.IsNull; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; import org.llorllale.cactoos.matchers.HasString; -import org.llorllale.cactoos.matchers.IsBlank; import org.llorllale.cactoos.matchers.IsText; /** @@ -331,242 +318,6 @@ void printsStackTraceFromArray() { ).affirm(); } - @Test - void readsLocalDateFormattedWithFormatString() { - final LocalDate date = LocalDate.of(2017, 12, 13); - new Assertion<>( - "Can't format a LocalDate with format.", - new TextOf(date, "yyyy-MM-dd HH:mm:ss"), - new IsText("2017-12-13 00:00:00") - ).affirm(); - } - - @Test - void readsLocalDateFormattedWithFormatStringWithLocale() { - final LocalDate date = LocalDate.of(2017, 12, 13); - new Assertion<>( - "Can't format a LocalDate with format using locale.", - new TextOf( - date, "yyyy MMM dd. HH.mm.ss", Locale.FRENCH - ), - new IsText("2017 déc. 13. 00.00.00") - ).affirm(); - } - - @Test - void readsLocalDateFormattedAsIsoDateTime() throws IOException { - final LocalDate date = LocalDate.of(2017, 12, 13); - new Assertion<>( - "Can't format a LocalDate with default/ISO format.", - new TextOf(date), - new IsText( - MessageFormat.format( - "2017-12-13T00:00:00{0}", - date.atTime(LocalTime.MIN).atZone(ZoneId.systemDefault()) - .getOffset().toString() - ) - ) - ).affirm(); - } - - @Test - void readsCurrentLocalDateAsText() throws Exception { - new Assertion<>( - "Can't format a LocalDate with ISO format.", - new TextOf(LocalDate.now()).asString(), - new IsNot<>(new IsBlank()) - ).affirm(); - } - - @Test - void localDateTimeFormattedAsIsoDateTime() { - final LocalDateTime date = LocalDateTime.of( - 2017, 12, 13, 14, 15, 16, 17 - ); - new Assertion<>( - "Can't format a LocalDateTime with default/ISO format.", - new TextOf(date), - new IsText( - MessageFormat.format( - "2017-12-13T14:15:16.000000017{0}", - date.atZone(ZoneId.systemDefault()).getOffset().toString() - ) - ) - ).affirm(); - } - - @Test - void localDateTimeFormattedWithFormatString() { - final LocalDateTime date = LocalDateTime.of( - 2017, 12, 13, 14, 15, 16, 17 - ); - new Assertion<>( - "Can't format a LocalDateTime with format.", - new TextOf(date, "yyyy-MM-dd HH:mm:ss"), - new IsText("2017-12-13 14:15:16") - ).affirm(); - } - - @Test - void localDateTimeFormattedWithFormatStringWithLocale() { - final LocalDateTime date = LocalDateTime.of( - 2017, 12, 13, 14, 15, 16, 17 - ); - new Assertion<>( - "Can't format a LocalDateTime with format using locale.", - new TextOf( - date, "yyyy MMM dd. HH.mm.ss", Locale.FRENCH - ), - new IsText("2017 déc. 13. 14.15.16") - ).affirm(); - } - - @Test - void currentLocalDateTimeAsText() throws Exception { - new Assertion<>( - "Can't format a LocalDateTime with ISO format.", - new TextOf(LocalDateTime.now()).asString(), - new IsNot<>(new IsNull<>()) - ).affirm(); - } - - @Test - void dateFormattedUsingIsoFormatter() { - final Calendar calendar = - Calendar.getInstance(TimeZone.getDefault()); - calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16); - calendar.set(Calendar.MILLISECOND, 17); - final ZoneOffset offset = calendar.getTimeZone().toZoneId() - .getRules().getOffset(calendar.toInstant()); - new Assertion<>( - "Can't format a java.util.Date with ISO format.", - new TextOf(calendar.getTime()), - new IsText("2017-12-13T14:15:16.017" + offset) - ).affirm(); - } - - @Test - void dateFormattedUsingCustomFormat() { - final Calendar calendar = - Calendar.getInstance(TimeZone.getDefault()); - calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16); - new Assertion<>( - "Can't format a java.util.Date with custom format.", - new TextOf( - calendar.getTime(), "yyyy MM dd hh:mm:ss" - ), - new IsText("2017 12 13 02:15:16") - ).affirm(); - } - - @Test - void dateFormattedUsingCustomFormatDifferentLocale() { - final Calendar calendar = - Calendar.getInstance(TimeZone.getDefault()); - calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16); - new Assertion<>( - "Can't format a java.util.Date with custom format.", - new TextOf( - calendar.getTime(), "yyyy MMM dd hh:mm:ss", Locale.ITALIAN - ), - new IsText("2017 dic 13 02:15:16") - ).affirm(); - } - - @Test - void offsetDateTimeFormattedAsIsoDateTime() { - final OffsetDateTime date = OffsetDateTime.of( - 2017, 12, 13, 14, 15, 16, 17, ZoneOffset.ofHours(1) - ); - new Assertion<>( - "Can't format a OffsetDateTime with default/ISO format.", - new TextOf(date), - new IsText("2017-12-13T14:15:16.000000017+01:00") - ).affirm(); - } - - @Test - void offsetDateTimeFormattedWithFormatString() { - final OffsetDateTime date = OffsetDateTime.of( - 2017, 12, 13, 14, 15, 16, 17, ZoneOffset.ofHours(1) - ); - new Assertion<>( - "Can't format a OffsetDateTime with format.", - new TextOf(date, "yyyy-MM-dd HH:mm:ss"), - new IsText("2017-12-13 14:15:16") - ).affirm(); - } - - @Test - void offsetDateTimeFormattedWithFormatStringWithLocale() { - final OffsetDateTime date = OffsetDateTime.of( - 2017, 12, 13, 14, 15, 16, 17, ZoneOffset.ofHours(1) - ); - new Assertion<>( - "Can't format a OffsetDateTime with format using locale.", - new TextOf( - date, "yyyy MMM dd. HH.mm.ss", Locale.FRENCH - ), - new IsText("2017 déc. 13. 14.15.16") - ).affirm(); - } - - @Test - void currentOffsetDateTimeAsText() throws Exception { - new Assertion<>( - "Can't format a OffsetDateTime with ISO format.", - new TextOf(OffsetDateTime.now()).asString(), - new IsNot<>(new IsNull<>()) - ).affirm(); - } - - @Test - void zonedDateTimeFormattedAsIsoDateTime() { - final ZonedDateTime date = ZonedDateTime.of( - 2017, 12, 13, 14, 15, 16, 17, ZoneId.of("Europe/Berlin") - ); - new Assertion<>( - "Can't format a ZonedDateTime with default/ISO format.", - new TextOf(date), - new IsText("2017-12-13T14:15:16.000000017+01:00") - ).affirm(); - } - - @Test - void zonedDateTimeFormattedWithFormatString() { - final ZonedDateTime date = ZonedDateTime.of( - 2017, 12, 13, 14, 15, 16, 17, ZoneId.of("Europe/Berlin") - ); - new Assertion<>( - "Can't format a ZonedDateTime with format.", - new TextOf(date, "yyyy-MM-dd HH:mm:ss"), - new IsText("2017-12-13 14:15:16") - ).affirm(); - } - - @Test - void zonedDateTimeFormattedWithFormatStringWithLocale() { - final ZonedDateTime date = ZonedDateTime.of( - 2017, 12, 13, 14, 15, 16, 17, ZoneId.of("Europe/Berlin") - ); - new Assertion<>( - "Can't format a ZonedDateTime with format using locale.", - new TextOf( - date, "yyyy MMM dd. HH.mm.ss", Locale.FRENCH - ), - new IsText("2017 déc. 13. 14.15.16") - ).affirm(); - } - - @Test - void currentZonedDateTimeAsText() throws Exception { - new Assertion<>( - "Can't format a ZonedDateTime with ISO format.", - new TextOf(ZonedDateTime.now()).asString(), - new IsNot<>(new IsNull<>()) - ).affirm(); - } - @Test void readsIteratorToText() throws Exception { new Assertion<>( From 457d35474fe668fd9d4521730a646f00ec9e8c85 Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Tue, 2 Mar 2021 15:29:01 -0300 Subject: [PATCH 02/29] (#1498) Added 'text.Newline' --- src/main/java/org/cactoos/text/Newline.java | 40 +++++++++++++++++ .../java/org/cactoos/text/NewlineTest.java | 44 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/main/java/org/cactoos/text/Newline.java create mode 100644 src/test/java/org/cactoos/text/NewlineTest.java diff --git a/src/main/java/org/cactoos/text/Newline.java b/src/main/java/org/cactoos/text/Newline.java new file mode 100644 index 0000000000..f18f820531 --- /dev/null +++ b/src/main/java/org/cactoos/text/Newline.java @@ -0,0 +1,40 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.text; + +import org.cactoos.Text; + +/** + * Returns a text that is the default system line separator. + * + *

There is no thread-safety guarantee. + * + * @since 0.50 + */ +public class Newline implements Text { + @Override + public String asString() throws Exception { + return System.lineSeparator(); + } +} diff --git a/src/test/java/org/cactoos/text/NewlineTest.java b/src/test/java/org/cactoos/text/NewlineTest.java new file mode 100644 index 0000000000..6b30e0e07d --- /dev/null +++ b/src/test/java/org/cactoos/text/NewlineTest.java @@ -0,0 +1,44 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.text; + +import org.hamcrest.core.IsEqual; +import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; + +/** + * Tests for {@link Newline}. + * + * @since 0.50 + */ +final class NewlineTest { + @Test + void testAsString() throws Exception { + new Assertion<>( + "must be equal to System.lineSeparator()", + new Newline().asString(), + new IsEqual<>(System.lineSeparator()) + ).affirm(); + } +} From fa605cd42cb88f637a37380fcc977e82e6dbd4bf Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Tue, 2 Mar 2021 15:53:40 -0300 Subject: [PATCH 03/29] (#1498) Changed the 'text.Newline' class to be a final class --- src/main/java/org/cactoos/text/Newline.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/text/Newline.java b/src/main/java/org/cactoos/text/Newline.java index f18f820531..58b3d9a6ae 100644 --- a/src/main/java/org/cactoos/text/Newline.java +++ b/src/main/java/org/cactoos/text/Newline.java @@ -32,7 +32,7 @@ * * @since 0.50 */ -public class Newline implements Text { +public final class Newline implements Text { @Override public String asString() throws Exception { return System.lineSeparator(); From 2672566cd12bfda60350c0b8b33c4f1a9bf13219 Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Tue, 2 Mar 2021 16:28:06 -0300 Subject: [PATCH 04/29] (#1498) Fixed checkstyle for 'text.Newline' and 'text.NewlineTest' --- src/main/java/org/cactoos/text/Newline.java | 1 - src/test/java/org/cactoos/text/NewlineTest.java | 7 +++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/cactoos/text/Newline.java b/src/main/java/org/cactoos/text/Newline.java index 58b3d9a6ae..532ca8aab0 100644 --- a/src/main/java/org/cactoos/text/Newline.java +++ b/src/main/java/org/cactoos/text/Newline.java @@ -29,7 +29,6 @@ * Returns a text that is the default system line separator. * *

There is no thread-safety guarantee. - * * @since 0.50 */ public final class Newline implements Text { diff --git a/src/test/java/org/cactoos/text/NewlineTest.java b/src/test/java/org/cactoos/text/NewlineTest.java index 6b30e0e07d..7b9ae6b92d 100644 --- a/src/test/java/org/cactoos/text/NewlineTest.java +++ b/src/test/java/org/cactoos/text/NewlineTest.java @@ -29,16 +29,15 @@ /** * Tests for {@link Newline}. - * * @since 0.50 */ final class NewlineTest { @Test void testAsString() throws Exception { new Assertion<>( - "must be equal to System.lineSeparator()", - new Newline().asString(), - new IsEqual<>(System.lineSeparator()) + "must be equal to System.lineSeparator()", + new Newline().asString(), + new IsEqual<>(System.lineSeparator()) ).affirm(); } } From 0602607984df6f6c2b19ae5de6f99a2ff45c2b90 Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Wed, 3 Mar 2021 20:32:00 -0300 Subject: [PATCH 05/29] Fixed the Newline implementation to be a subclass of TextEnvelope in order to have equals and hashCode consistent like all text implementations of the project. --- src/main/java/org/cactoos/text/Newline.java | 26 +++++++++++--- .../java/org/cactoos/text/NewlineTest.java | 35 ++++++++++++++++--- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/cactoos/text/Newline.java b/src/main/java/org/cactoos/text/Newline.java index 532ca8aab0..a40e3f28fd 100644 --- a/src/main/java/org/cactoos/text/Newline.java +++ b/src/main/java/org/cactoos/text/Newline.java @@ -31,9 +31,27 @@ *

There is no thread-safety guarantee. * @since 0.50 */ -public final class Newline implements Text { - @Override - public String asString() throws Exception { - return System.lineSeparator(); +public final class Newline extends TextEnvelope { + /** + * Ctor. + */ + public Newline() { + this(""); + } + + /** + * Ctor. + * @param text The text + */ + public Newline(final String text) { + this(new TextOf(text)); + } + + /** + * Ctor. + * @param text The text + */ + public Newline(final Text text) { + super(new Concatenated(new FormattedText("%n"), text)); } } diff --git a/src/test/java/org/cactoos/text/NewlineTest.java b/src/test/java/org/cactoos/text/NewlineTest.java index 7b9ae6b92d..79f062e9a8 100644 --- a/src/test/java/org/cactoos/text/NewlineTest.java +++ b/src/test/java/org/cactoos/text/NewlineTest.java @@ -23,9 +23,10 @@ */ package org.cactoos.text; -import org.hamcrest.core.IsEqual; +import java.util.UUID; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.IsText; /** * Tests for {@link Newline}. @@ -33,11 +34,35 @@ */ final class NewlineTest { @Test - void testAsString() throws Exception { + void testEmptyNewline() { new Assertion<>( - "must be equal to System.lineSeparator()", - new Newline().asString(), - new IsEqual<>(System.lineSeparator()) + "Must be equal to the System.lineSeparator()", + new Newline(), + new IsText(System.lineSeparator()) ).affirm(); } + + @Test + void testStringIntoNewline() { + final String text = UUID.randomUUID().toString(); + new Assertion<>( + "must be equal to System.lineSeparator() plus the provided plain String value", + new Newline(text), + new IsText(this.expectedValue(text)) + ).affirm(); + } + + @Test + void testTextIntoNewline() { + final String text = UUID.randomUUID().toString(); + new Assertion<>( + "must be equal to System.lineSeparator() plus the provided Text's value", + new Newline(new TextOf(text)), + new IsText(this.expectedValue(text)) + ).affirm(); + } + + private String expectedValue(final String text) { + return String.format("%n%1$s", text); + } } From c0af83f9b02b9ef4da5060c4530edcdea7631459 Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Thu, 4 Mar 2021 04:36:06 -0300 Subject: [PATCH 06/29] (#1507) Added new classes: - `iterable.MappedWithIndex` - `iterator.MappedWithIndex` - `scalar.MappedWithIndex` - `scalar.ScalarWithIndex` --- .../org/cactoos/iterable/MappedWithIndex.java | 70 ++++++++++++++++ .../org/cactoos/iterator/MappedWithIndex.java | 83 +++++++++++++++++++ .../org/cactoos/scalar/MappedWithIndex.java | 52 ++++++++++++ .../org/cactoos/scalar/ScalarWithIndex.java | 68 +++++++++++++++ 4 files changed, 273 insertions(+) create mode 100644 src/main/java/org/cactoos/iterable/MappedWithIndex.java create mode 100644 src/main/java/org/cactoos/iterator/MappedWithIndex.java create mode 100644 src/main/java/org/cactoos/scalar/MappedWithIndex.java create mode 100644 src/main/java/org/cactoos/scalar/ScalarWithIndex.java diff --git a/src/main/java/org/cactoos/iterable/MappedWithIndex.java b/src/main/java/org/cactoos/iterable/MappedWithIndex.java new file mode 100644 index 0000000000..019d5b17b4 --- /dev/null +++ b/src/main/java/org/cactoos/iterable/MappedWithIndex.java @@ -0,0 +1,70 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.iterable; + +import org.cactoos.BiFunc; + +/** + * Mapped with index iterable. + * + *

+ * There is no thread-safety guarantee. + * @param Type of target item + * @since 0.50 + */ +public final class MappedWithIndex extends IterableEnvelope { + /** + * Ctor. + * @param fnc Func + * @param src Source iterable + * @param Type of source item + */ + @SafeVarargs + public MappedWithIndex( + final BiFunc fnc, + final X... src + ) { + this(fnc, new IterableOf<>(src)); + } + + /** + * Ctor. + * @param fnc Func + * @param src Source iterable + * @param Type of source item + */ + public MappedWithIndex( + final BiFunc fnc, + final Iterable src + ) { + super( + new IterableOf<>( + () -> new org.cactoos.iterator.MappedWithIndex<>( + fnc, + src.iterator() + ) + ) + ); + } +} diff --git a/src/main/java/org/cactoos/iterator/MappedWithIndex.java b/src/main/java/org/cactoos/iterator/MappedWithIndex.java new file mode 100644 index 0000000000..4fe93884b1 --- /dev/null +++ b/src/main/java/org/cactoos/iterator/MappedWithIndex.java @@ -0,0 +1,83 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.iterator; + +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.concurrent.atomic.AtomicInteger; +import org.cactoos.BiFunc; +import org.cactoos.scalar.ScalarWithIndex; +import org.cactoos.scalar.Unchecked; + +/** + * Mapped with index iterator. + * + *

+ * There is no thread-safety guarantee. + * @param Type of target item + * @since 0.50 + */ +public final class MappedWithIndex extends IteratorEnvelope { + /** + * Ctor. + * @param func Func + * @param iterator Source iterator + * @param Type of item + * @checkstyle AnonInnerLengthCheck (60 lines) + */ + public MappedWithIndex( + final BiFunc func, + final Iterator iterator + ) { + super( + new Iterator() { + private final AtomicInteger indexcount = new AtomicInteger(-1); + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public Y next() { + if (this.hasNext()) { + final int index = this.indexcount.incrementAndGet(); + return new Unchecked<>( + new org.cactoos.scalar.MappedWithIndex<>( + func, new ScalarWithIndex<>(index, iterator::next) + ) + ).value(); + } + throw new NoSuchElementException(); + } + + @Override + public void remove() { + iterator.remove(); + this.indexcount.getAndDecrement(); + } + } + ); + } +} diff --git a/src/main/java/org/cactoos/scalar/MappedWithIndex.java b/src/main/java/org/cactoos/scalar/MappedWithIndex.java new file mode 100644 index 0000000000..033d36e0fd --- /dev/null +++ b/src/main/java/org/cactoos/scalar/MappedWithIndex.java @@ -0,0 +1,52 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.scalar; + +import org.cactoos.BiFunc; +import org.cactoos.Scalar; + +/** + * An indexed {@link Scalar} that apply a {@link BiFunc} to the result of + * another {@link Scalar}. + * + *

+ * There is no thread-safety guarantee. + *

+ * @param Type of result + * @since 0.50 + */ +public final class MappedWithIndex extends ScalarEnvelope { + /** + * Ctor. + * @param func Map BiFunction. + * @param scalar Given {@link ScalarWithIndex} scalar. + * @param Type of input. + */ + public MappedWithIndex( + final BiFunc func, + final ScalarWithIndex scalar + ) { + super(() -> func.apply(scalar.position(), scalar.value())); + } +} diff --git a/src/main/java/org/cactoos/scalar/ScalarWithIndex.java b/src/main/java/org/cactoos/scalar/ScalarWithIndex.java new file mode 100644 index 0000000000..ab1180911f --- /dev/null +++ b/src/main/java/org/cactoos/scalar/ScalarWithIndex.java @@ -0,0 +1,68 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.scalar; + +import org.cactoos.Scalar; + +/** + * Element with its index. + * + *

There is no thread-safety guarantee. + * @param Scalar type + * @since 0.50 + */ +public final class ScalarWithIndex implements Scalar { + /** + * Hold the position specified in the constructor. + */ + private final int pos; + + /** + * Hold the {@link Scalar} at the position specified in the constructor. + */ + private final Scalar scalar; + + /** + * Ctor. + * @param pos Position + * @param scalar Scalar + */ + public ScalarWithIndex(final Integer pos, final Scalar scalar) { + this.scalar = scalar; + this.pos = pos; + } + + @Override + public T value() throws Exception { + return this.scalar.value(); + } + + /** + * Returns the position specified in the constructor. + * @return The position + */ + public int position() { + return this.pos; + } +} From 0787769a87d7ff233b7e294431bc38202eebbb0a Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Thu, 4 Mar 2021 05:09:10 -0300 Subject: [PATCH 07/29] (#1507) Added new test classes: - `iterable.MappedWithIndexTest` - `iterator.MappedWithIndexTest` - `scalar.MappedWithIndexTest` - `scalar.ScalarWithIndexTest` --- .../cactoos/iterable/MappedWithIndexTest.java | 110 ++++++++++++++++++ .../cactoos/iterator/MappedWithIndexTest.java | 68 +++++++++++ .../cactoos/scalar/MappedWithIndexTest.java | 58 +++++++++ .../cactoos/scalar/ScalarWithIndexTest.java | 55 +++++++++ 4 files changed, 291 insertions(+) create mode 100644 src/test/java/org/cactoos/iterable/MappedWithIndexTest.java create mode 100644 src/test/java/org/cactoos/iterator/MappedWithIndexTest.java create mode 100644 src/test/java/org/cactoos/scalar/MappedWithIndexTest.java create mode 100644 src/test/java/org/cactoos/scalar/ScalarWithIndexTest.java diff --git a/src/test/java/org/cactoos/iterable/MappedWithIndexTest.java b/src/test/java/org/cactoos/iterable/MappedWithIndexTest.java new file mode 100644 index 0000000000..5050ff3b35 --- /dev/null +++ b/src/test/java/org/cactoos/iterable/MappedWithIndexTest.java @@ -0,0 +1,110 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.iterable; + +import java.util.Collections; +import org.cactoos.text.Joined; +import org.cactoos.text.TextOf; +import org.cactoos.text.Upper; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.hamcrest.core.IsEqual; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; + +/** + * Test case for {@link MappedWithIndex}. + * @since 0.50 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) + */ +final class MappedWithIndexTest { + @Test + void transformsList() throws Exception { + MatcherAssert.assertThat( + "Can't transform an iterable", + new MappedWithIndex<>( + (index, input) -> new Upper( + new Joined( + "-", + new TextOf(index.toString()), + new TextOf(input) + ) + ), + new IterableOf<>( + "hello", "world", "друг" + ) + ).iterator().next().asString(), + Matchers.equalTo("0-HELLO") + ); + } + + @Test + void transformsEmptyList() { + MatcherAssert.assertThat( + "Can't transform an empty iterable", + new MappedWithIndex<>( + (index, input) -> { + Assertions.fail("must do not be executed"); + return input; + }, + Collections.emptyList() + ), + Matchers.emptyIterable() + ); + } + + @Test + void string() { + MatcherAssert.assertThat( + "Can't convert to string", + new MappedWithIndex<>( + (index, x) -> x * index * 2, + new IterableOf<>(1, 2, 3) + ).toString(), + Matchers.equalTo("0, 4, 12") + ); + } + + @Test + void transformsArray() { + new Assertion<>( + "Transforms an array", + new MappedWithIndex<>( + (index, input) -> new Upper( + new TextOf( + String.format( + "%1$s-%2$s", + index, + input + ) + ) + ).asString(), + "a", "b", "c" + ), + new IsEqual<>(new IterableOf<>("0-A", "1-B", "2-C")) + ).affirm(); + } +} diff --git a/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java b/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java new file mode 100644 index 0000000000..45be596603 --- /dev/null +++ b/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java @@ -0,0 +1,68 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.iterator; + +import java.util.Iterator; +import java.util.NoSuchElementException; +import org.cactoos.iterable.IterableOf; +import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.HasValues; +import org.llorllale.cactoos.matchers.Throws; + +/** + * Tests for {@link MappedWithIndex}. + * @since 0.50 + */ +final class MappedWithIndexTest { + @Test + void iteratatesOver() { + new Assertion<>( + "must map values of iterator", + new IterableOf<>( + new MappedWithIndex<>( + (i, v) -> String + .format("%1$s - %2$s", i, v), + new IteratorOf(1L, 2, 0) + ) + ), + new HasValues<>("0 - 1", "1 - 2", "2 - 0") + ).affirm(); + } + + @Test + void failsIfIteratorExhausted() { + final Iterator iterator = new MappedWithIndex<>( + (i, v) -> String + .format("%1$s X %2$s", i, v.toString()), + new IteratorOf(1) + ); + iterator.next(); + new Assertion<>( + "must throw NSEE", + iterator::next, + new Throws<>(NoSuchElementException.class) + ).affirm(); + } +} diff --git a/src/test/java/org/cactoos/scalar/MappedWithIndexTest.java b/src/test/java/org/cactoos/scalar/MappedWithIndexTest.java new file mode 100644 index 0000000000..998b60759a --- /dev/null +++ b/src/test/java/org/cactoos/scalar/MappedWithIndexTest.java @@ -0,0 +1,58 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.scalar; + +import org.cactoos.text.TextOf; +import org.cactoos.text.Upper; +import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.HasValue; +import org.llorllale.cactoos.matchers.IsText; + +/** + * Test case for {@link MappedWithIndex}. + * @since 0.50 + * @checkstyle JavadocMethodCheck (500 lines) + */ +final class MappedWithIndexTest { + @Test + void transformsIndexedScalarToAnotherScalar() { + new Assertion<>( + "must transform an indexed scalar to another scalar", + new MappedWithIndex<>( + (index, input) -> new Upper( + new TextOf( + String.format( + "%1$s-%2$s", + index.toString(), + input + ) + ) + ), + new ScalarWithIndex<>(0, () -> "hello") + ), + new HasValue<>(new IsText("0-HELLO")) + ).affirm(); + } +} diff --git a/src/test/java/org/cactoos/scalar/ScalarWithIndexTest.java b/src/test/java/org/cactoos/scalar/ScalarWithIndexTest.java new file mode 100644 index 0000000000..5d7a7b6112 --- /dev/null +++ b/src/test/java/org/cactoos/scalar/ScalarWithIndexTest.java @@ -0,0 +1,55 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.scalar; + +import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.HasValue; +import org.llorllale.cactoos.matchers.IsNumber; + +/** + * Tests for {@link ScalarWithIndex}. + * @since 0.50 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +final class ScalarWithIndexTest { + @Test + void behaviorTest() { + final String value = "text"; + final ScalarWithIndex instance = new ScalarWithIndex<>( + 1, + () -> value + ); + new Assertion<>( + "must returns the scalar's value", + instance, + new HasValue<>(value) + ).affirm(); + new Assertion<>( + "must returns the scalar's position", + instance.position(), + new IsNumber(1) + ).affirm(); + } +} From 36676ba24db064103d688cd5d99bd25b8c71f762 Mon Sep 17 00:00:00 2001 From: "Olivier B. OURA" Date: Thu, 4 Mar 2021 08:04:03 +0000 Subject: [PATCH 08/29] Add a todo for Iterable and Iterator ctors of TextOf #1461 --- src/main/java/org/cactoos/text/TextOf.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index 660f645aef..42ed8209c1 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -317,6 +317,12 @@ public TextOf(final String input) { * Ctor. * * @param iterable The iterable to convert to string + * @todo #1461:30min We want constructors with {@code Iterable} and + * {@code Iterator} to have same behaviour (simply concatenate list of strings). + * To do that, we should change {@code Iterable} to {@code Iterable} + * and delegate the {@link Iterator} one to the {@link Iterable} one. After that, we need to + * modify other parts of cactoos that relied on this in order to preserve their behaviour + * by using {@link Joined} and {@link Concatenated}. */ public TextOf(final Iterable iterable) { super( From 4f574339e8273810264e604cb937a6d8a1d13c6b Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Thu, 4 Mar 2021 06:34:00 -0300 Subject: [PATCH 09/29] (#1507) Improved the test for `iterator.MappedWithIndex` class --- .../cactoos/iterator/MappedWithIndexTest.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java b/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java index 45be596603..e526e9a1e9 100644 --- a/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java +++ b/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java @@ -23,7 +23,9 @@ */ package org.cactoos.iterator; +import java.util.Arrays; import java.util.Iterator; +import java.util.LinkedList; import java.util.NoSuchElementException; import org.cactoos.iterable.IterableOf; import org.junit.jupiter.api.Test; @@ -34,6 +36,7 @@ /** * Tests for {@link MappedWithIndex}. * @since 0.50 + * @checkstyle MagicNumber (500 lines) */ final class MappedWithIndexTest { @Test @@ -65,4 +68,27 @@ void failsIfIteratorExhausted() { new Throws<>(NoSuchElementException.class) ).affirm(); } + + @Test + void removingElementsFromIterator() { + final Iterator iterator = new MappedWithIndex<>( + (i, v) -> String.format( + "%1$s : %2$s", + i, + v.toString() + ), + new LinkedList( + Arrays.asList(1, 2, 3) + ).iterator() + ); + iterator.next(); + iterator.remove(); + new Assertion<>( + "must map values of changed iterator", + new IterableOf<>( + iterator + ), + new HasValues<>("0 : 2", "1 : 3") + ).affirm(); + } } From 6ac79a1535d8d26384fa787cae1f6dfc7ba68c7a Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Fri, 5 Mar 2021 08:25:08 -0300 Subject: [PATCH 10/29] (#1507) Making MappedWithIndex a compositon of Mapped --- .../org/cactoos/iterable/MappedWithIndex.java | 13 +++- .../org/cactoos/iterator/MappedWithIndex.java | 38 +++-------- .../org/cactoos/scalar/MappedWithIndex.java | 52 -------------- .../org/cactoos/scalar/ScalarWithIndex.java | 68 ------------------- .../cactoos/iterator/MappedWithIndexTest.java | 2 +- .../cactoos/scalar/MappedWithIndexTest.java | 58 ---------------- .../cactoos/scalar/ScalarWithIndexTest.java | 55 --------------- 7 files changed, 22 insertions(+), 264 deletions(-) delete mode 100644 src/main/java/org/cactoos/scalar/MappedWithIndex.java delete mode 100644 src/main/java/org/cactoos/scalar/ScalarWithIndex.java delete mode 100644 src/test/java/org/cactoos/scalar/MappedWithIndexTest.java delete mode 100644 src/test/java/org/cactoos/scalar/ScalarWithIndexTest.java diff --git a/src/main/java/org/cactoos/iterable/MappedWithIndex.java b/src/main/java/org/cactoos/iterable/MappedWithIndex.java index 019d5b17b4..22ad0978f8 100644 --- a/src/main/java/org/cactoos/iterable/MappedWithIndex.java +++ b/src/main/java/org/cactoos/iterable/MappedWithIndex.java @@ -23,7 +23,9 @@ */ package org.cactoos.iterable; +import java.util.concurrent.atomic.AtomicInteger; import org.cactoos.BiFunc; +import org.cactoos.Func; /** * Mapped with index iterable. @@ -60,8 +62,15 @@ public MappedWithIndex( ) { super( new IterableOf<>( - () -> new org.cactoos.iterator.MappedWithIndex<>( - fnc, + () -> new org.cactoos.iterator.Mapped<>( + new Func() { + private final AtomicInteger indexcount = new AtomicInteger(-1); + + @Override + public Y apply(final X input) throws Exception { + return fnc.apply(this.indexcount.incrementAndGet(), input); + } + }, src.iterator() ) ) diff --git a/src/main/java/org/cactoos/iterator/MappedWithIndex.java b/src/main/java/org/cactoos/iterator/MappedWithIndex.java index 4fe93884b1..c33e68e1a6 100644 --- a/src/main/java/org/cactoos/iterator/MappedWithIndex.java +++ b/src/main/java/org/cactoos/iterator/MappedWithIndex.java @@ -24,11 +24,9 @@ package org.cactoos.iterator; import java.util.Iterator; -import java.util.NoSuchElementException; import java.util.concurrent.atomic.AtomicInteger; import org.cactoos.BiFunc; -import org.cactoos.scalar.ScalarWithIndex; -import org.cactoos.scalar.Unchecked; +import org.cactoos.Func; /** * Mapped with index iterator. @@ -51,33 +49,17 @@ public MappedWithIndex( final Iterator iterator ) { super( - new Iterator() { - private final AtomicInteger indexcount = new AtomicInteger(-1); + new Mapped<>( + new Func() { + private final AtomicInteger indexcount = new AtomicInteger(-1); - @Override - public boolean hasNext() { - return iterator.hasNext(); - } - - @Override - public Y next() { - if (this.hasNext()) { - final int index = this.indexcount.incrementAndGet(); - return new Unchecked<>( - new org.cactoos.scalar.MappedWithIndex<>( - func, new ScalarWithIndex<>(index, iterator::next) - ) - ).value(); + @Override + public Y apply(final X input) throws Exception { + return func.apply(this.indexcount.incrementAndGet(), input); } - throw new NoSuchElementException(); - } - - @Override - public void remove() { - iterator.remove(); - this.indexcount.getAndDecrement(); - } - } + }, + iterator + ) ); } } diff --git a/src/main/java/org/cactoos/scalar/MappedWithIndex.java b/src/main/java/org/cactoos/scalar/MappedWithIndex.java deleted file mode 100644 index 033d36e0fd..0000000000 --- a/src/main/java/org/cactoos/scalar/MappedWithIndex.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2017-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.cactoos.scalar; - -import org.cactoos.BiFunc; -import org.cactoos.Scalar; - -/** - * An indexed {@link Scalar} that apply a {@link BiFunc} to the result of - * another {@link Scalar}. - * - *

- * There is no thread-safety guarantee. - *

- * @param Type of result - * @since 0.50 - */ -public final class MappedWithIndex extends ScalarEnvelope { - /** - * Ctor. - * @param func Map BiFunction. - * @param scalar Given {@link ScalarWithIndex} scalar. - * @param Type of input. - */ - public MappedWithIndex( - final BiFunc func, - final ScalarWithIndex scalar - ) { - super(() -> func.apply(scalar.position(), scalar.value())); - } -} diff --git a/src/main/java/org/cactoos/scalar/ScalarWithIndex.java b/src/main/java/org/cactoos/scalar/ScalarWithIndex.java deleted file mode 100644 index ab1180911f..0000000000 --- a/src/main/java/org/cactoos/scalar/ScalarWithIndex.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2017-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.cactoos.scalar; - -import org.cactoos.Scalar; - -/** - * Element with its index. - * - *

There is no thread-safety guarantee. - * @param Scalar type - * @since 0.50 - */ -public final class ScalarWithIndex implements Scalar { - /** - * Hold the position specified in the constructor. - */ - private final int pos; - - /** - * Hold the {@link Scalar} at the position specified in the constructor. - */ - private final Scalar scalar; - - /** - * Ctor. - * @param pos Position - * @param scalar Scalar - */ - public ScalarWithIndex(final Integer pos, final Scalar scalar) { - this.scalar = scalar; - this.pos = pos; - } - - @Override - public T value() throws Exception { - return this.scalar.value(); - } - - /** - * Returns the position specified in the constructor. - * @return The position - */ - public int position() { - return this.pos; - } -} diff --git a/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java b/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java index e526e9a1e9..b51e2fa8ba 100644 --- a/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java +++ b/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java @@ -88,7 +88,7 @@ void removingElementsFromIterator() { new IterableOf<>( iterator ), - new HasValues<>("0 : 2", "1 : 3") + new HasValues<>("1 : 2", "2 : 3") ).affirm(); } } diff --git a/src/test/java/org/cactoos/scalar/MappedWithIndexTest.java b/src/test/java/org/cactoos/scalar/MappedWithIndexTest.java deleted file mode 100644 index 998b60759a..0000000000 --- a/src/test/java/org/cactoos/scalar/MappedWithIndexTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2017-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.cactoos.scalar; - -import org.cactoos.text.TextOf; -import org.cactoos.text.Upper; -import org.junit.jupiter.api.Test; -import org.llorllale.cactoos.matchers.Assertion; -import org.llorllale.cactoos.matchers.HasValue; -import org.llorllale.cactoos.matchers.IsText; - -/** - * Test case for {@link MappedWithIndex}. - * @since 0.50 - * @checkstyle JavadocMethodCheck (500 lines) - */ -final class MappedWithIndexTest { - @Test - void transformsIndexedScalarToAnotherScalar() { - new Assertion<>( - "must transform an indexed scalar to another scalar", - new MappedWithIndex<>( - (index, input) -> new Upper( - new TextOf( - String.format( - "%1$s-%2$s", - index.toString(), - input - ) - ) - ), - new ScalarWithIndex<>(0, () -> "hello") - ), - new HasValue<>(new IsText("0-HELLO")) - ).affirm(); - } -} diff --git a/src/test/java/org/cactoos/scalar/ScalarWithIndexTest.java b/src/test/java/org/cactoos/scalar/ScalarWithIndexTest.java deleted file mode 100644 index 5d7a7b6112..0000000000 --- a/src/test/java/org/cactoos/scalar/ScalarWithIndexTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2017-2020 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.cactoos.scalar; - -import org.junit.jupiter.api.Test; -import org.llorllale.cactoos.matchers.Assertion; -import org.llorllale.cactoos.matchers.HasValue; -import org.llorllale.cactoos.matchers.IsNumber; - -/** - * Tests for {@link ScalarWithIndex}. - * @since 0.50 - * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) - */ -final class ScalarWithIndexTest { - @Test - void behaviorTest() { - final String value = "text"; - final ScalarWithIndex instance = new ScalarWithIndex<>( - 1, - () -> value - ); - new Assertion<>( - "must returns the scalar's value", - instance, - new HasValue<>(value) - ).affirm(); - new Assertion<>( - "must returns the scalar's position", - instance.position(), - new IsNumber(1) - ).affirm(); - } -} From 9d796ef43c031dc88a966330b189011e6af54f56 Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Fri, 5 Mar 2021 18:23:51 -0300 Subject: [PATCH 11/29] (#1507) Removing code duplication and using matchers from the `cactoos-matchers` project --- .../org/cactoos/iterable/MappedWithIndex.java | 20 +++++++++---------- .../cactoos/iterable/MappedWithIndexTest.java | 15 +++++++------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/cactoos/iterable/MappedWithIndex.java b/src/main/java/org/cactoos/iterable/MappedWithIndex.java index 22ad0978f8..3d9d3b198c 100644 --- a/src/main/java/org/cactoos/iterable/MappedWithIndex.java +++ b/src/main/java/org/cactoos/iterable/MappedWithIndex.java @@ -61,18 +61,16 @@ public MappedWithIndex( final Iterable src ) { super( - new IterableOf<>( - () -> new org.cactoos.iterator.Mapped<>( - new Func() { - private final AtomicInteger indexcount = new AtomicInteger(-1); + new Mapped<>( + new Func() { + private final AtomicInteger indexcount = new AtomicInteger(-1); - @Override - public Y apply(final X input) throws Exception { - return fnc.apply(this.indexcount.incrementAndGet(), input); - } - }, - src.iterator() - ) + @Override + public Y apply(final X input) throws Exception { + return fnc.apply(this.indexcount.incrementAndGet(), input); + } + }, + src ) ); } diff --git a/src/test/java/org/cactoos/iterable/MappedWithIndexTest.java b/src/test/java/org/cactoos/iterable/MappedWithIndexTest.java index 5050ff3b35..39ebad1365 100644 --- a/src/test/java/org/cactoos/iterable/MappedWithIndexTest.java +++ b/src/test/java/org/cactoos/iterable/MappedWithIndexTest.java @@ -27,7 +27,6 @@ import org.cactoos.text.Joined; import org.cactoos.text.TextOf; import org.cactoos.text.Upper; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.hamcrest.core.IsEqual; import org.junit.jupiter.api.Assertions; @@ -43,7 +42,7 @@ final class MappedWithIndexTest { @Test void transformsList() throws Exception { - MatcherAssert.assertThat( + new Assertion<>( "Can't transform an iterable", new MappedWithIndex<>( (index, input) -> new Upper( @@ -57,13 +56,13 @@ void transformsList() throws Exception { "hello", "world", "друг" ) ).iterator().next().asString(), - Matchers.equalTo("0-HELLO") - ); + new IsEqual<>("0-HELLO") + ).affirm(); } @Test void transformsEmptyList() { - MatcherAssert.assertThat( + new Assertion<>( "Can't transform an empty iterable", new MappedWithIndex<>( (index, input) -> { @@ -73,19 +72,19 @@ void transformsEmptyList() { Collections.emptyList() ), Matchers.emptyIterable() - ); + ).affirm(); } @Test void string() { - MatcherAssert.assertThat( + new Assertion<>( "Can't convert to string", new MappedWithIndex<>( (index, x) -> x * index * 2, new IterableOf<>(1, 2, 3) ).toString(), Matchers.equalTo("0, 4, 12") - ); + ).affirm(); } @Test From 95bad1e5414d3257758541ada05d332f0db7cb3e Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Fri, 5 Mar 2021 19:51:23 -0300 Subject: [PATCH 12/29] (#1507) Fixed to use `iterator.MappedWithIndex` class --- .../java/org/cactoos/iterable/MappedWithIndex.java | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/cactoos/iterable/MappedWithIndex.java b/src/main/java/org/cactoos/iterable/MappedWithIndex.java index 3d9d3b198c..a014c22b71 100644 --- a/src/main/java/org/cactoos/iterable/MappedWithIndex.java +++ b/src/main/java/org/cactoos/iterable/MappedWithIndex.java @@ -23,9 +23,7 @@ */ package org.cactoos.iterable; -import java.util.concurrent.atomic.AtomicInteger; import org.cactoos.BiFunc; -import org.cactoos.Func; /** * Mapped with index iterable. @@ -61,16 +59,8 @@ public MappedWithIndex( final Iterable src ) { super( - new Mapped<>( - new Func() { - private final AtomicInteger indexcount = new AtomicInteger(-1); - - @Override - public Y apply(final X input) throws Exception { - return fnc.apply(this.indexcount.incrementAndGet(), input); - } - }, - src + new IterableOf<>( + () -> new org.cactoos.iterator.MappedWithIndex<>(fnc, src.iterator()) ) ); } From c5796bd545326e1d4c94957fab81204a34a403a4 Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Sat, 6 Mar 2021 15:16:53 -0300 Subject: [PATCH 13/29] (#1507) Removed the unnecessary complexity from `Newline` implementation --- src/main/java/org/cactoos/text/Newline.java | 22 ++------------ .../java/org/cactoos/text/NewlineTest.java | 29 ++----------------- 2 files changed, 4 insertions(+), 47 deletions(-) diff --git a/src/main/java/org/cactoos/text/Newline.java b/src/main/java/org/cactoos/text/Newline.java index a40e3f28fd..c1bfd9bd1b 100644 --- a/src/main/java/org/cactoos/text/Newline.java +++ b/src/main/java/org/cactoos/text/Newline.java @@ -23,35 +23,17 @@ */ package org.cactoos.text; -import org.cactoos.Text; - /** * Returns a text that is the default system line separator. * *

There is no thread-safety guarantee. - * @since 0.50 + * @since 1.0.0 */ public final class Newline extends TextEnvelope { /** * Ctor. */ public Newline() { - this(""); - } - - /** - * Ctor. - * @param text The text - */ - public Newline(final String text) { - this(new TextOf(text)); - } - - /** - * Ctor. - * @param text The text - */ - public Newline(final Text text) { - super(new Concatenated(new FormattedText("%n"), text)); + super(new FormattedText("%n")); } } diff --git a/src/test/java/org/cactoos/text/NewlineTest.java b/src/test/java/org/cactoos/text/NewlineTest.java index 79f062e9a8..66b007689a 100644 --- a/src/test/java/org/cactoos/text/NewlineTest.java +++ b/src/test/java/org/cactoos/text/NewlineTest.java @@ -23,46 +23,21 @@ */ package org.cactoos.text; -import java.util.UUID; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; import org.llorllale.cactoos.matchers.IsText; /** * Tests for {@link Newline}. - * @since 0.50 + * @since 1.0.0 */ final class NewlineTest { @Test - void testEmptyNewline() { + void test() { new Assertion<>( "Must be equal to the System.lineSeparator()", new Newline(), new IsText(System.lineSeparator()) ).affirm(); } - - @Test - void testStringIntoNewline() { - final String text = UUID.randomUUID().toString(); - new Assertion<>( - "must be equal to System.lineSeparator() plus the provided plain String value", - new Newline(text), - new IsText(this.expectedValue(text)) - ).affirm(); - } - - @Test - void testTextIntoNewline() { - final String text = UUID.randomUUID().toString(); - new Assertion<>( - "must be equal to System.lineSeparator() plus the provided Text's value", - new Newline(new TextOf(text)), - new IsText(this.expectedValue(text)) - ).affirm(); - } - - private String expectedValue(final String text) { - return String.format("%n%1$s", text); - } } From 7b2e04881eb2c63e056ca8c3f9baf0abb5c3e7e2 Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Sat, 6 Mar 2021 17:27:42 -0300 Subject: [PATCH 14/29] (#1507) Fixed the classes in order to follow the API niceness terms and it was removed unnecessary @checkstyle checks --- .../org/cactoos/iterable/MappedWithIndex.java | 4 +-- .../org/cactoos/iterator/MappedWithIndex.java | 34 ++++++++++++------- .../cactoos/iterable/MappedWithIndexTest.java | 19 +++++------ .../cactoos/iterator/MappedWithIndexTest.java | 16 ++++----- 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/cactoos/iterable/MappedWithIndex.java b/src/main/java/org/cactoos/iterable/MappedWithIndex.java index a014c22b71..2ee943388e 100644 --- a/src/main/java/org/cactoos/iterable/MappedWithIndex.java +++ b/src/main/java/org/cactoos/iterable/MappedWithIndex.java @@ -42,7 +42,7 @@ public final class MappedWithIndex extends IterableEnvelope { */ @SafeVarargs public MappedWithIndex( - final BiFunc fnc, + final BiFunc fnc, final X... src ) { this(fnc, new IterableOf<>(src)); @@ -55,7 +55,7 @@ public MappedWithIndex( * @param Type of source item */ public MappedWithIndex( - final BiFunc fnc, + final BiFunc fnc, final Iterable src ) { super( diff --git a/src/main/java/org/cactoos/iterator/MappedWithIndex.java b/src/main/java/org/cactoos/iterator/MappedWithIndex.java index c33e68e1a6..f10f83247e 100644 --- a/src/main/java/org/cactoos/iterator/MappedWithIndex.java +++ b/src/main/java/org/cactoos/iterator/MappedWithIndex.java @@ -26,7 +26,6 @@ import java.util.Iterator; import java.util.concurrent.atomic.AtomicInteger; import org.cactoos.BiFunc; -import org.cactoos.Func; /** * Mapped with index iterator. @@ -34,7 +33,7 @@ *

* There is no thread-safety guarantee. * @param Type of target item - * @since 0.50 + * @since 1.0.0 */ public final class MappedWithIndex extends IteratorEnvelope { /** @@ -42,22 +41,33 @@ public final class MappedWithIndex extends IteratorEnvelope { * @param func Func * @param iterator Source iterator * @param Type of item - * @checkstyle AnonInnerLengthCheck (60 lines) */ public MappedWithIndex( - final BiFunc func, + final BiFunc func, + final Iterator iterator + ) { + this( + new AtomicInteger(-1), + func, + iterator + ); + } + + /** + * Privated Ctor. + * @param indexcounter Index Counter + * @param func Func + * @param iterator Source iterator + * @param Type of item + */ + private MappedWithIndex( + final AtomicInteger indexcounter, + final BiFunc func, final Iterator iterator ) { super( new Mapped<>( - new Func() { - private final AtomicInteger indexcount = new AtomicInteger(-1); - - @Override - public Y apply(final X input) throws Exception { - return func.apply(this.indexcount.incrementAndGet(), input); - } - }, + item -> func.apply(item, indexcounter.incrementAndGet()), iterator ) ); diff --git a/src/test/java/org/cactoos/iterable/MappedWithIndexTest.java b/src/test/java/org/cactoos/iterable/MappedWithIndexTest.java index 39ebad1365..a1e05706d4 100644 --- a/src/test/java/org/cactoos/iterable/MappedWithIndexTest.java +++ b/src/test/java/org/cactoos/iterable/MappedWithIndexTest.java @@ -34,18 +34,17 @@ import org.llorllale.cactoos.matchers.Assertion; /** - * Test case for {@link MappedWithIndex}. - * @since 0.50 - * @checkstyle JavadocMethodCheck (500 lines) + * Tests for {@link MappedWithIndex}. + * @since 1.0.0 * @checkstyle MagicNumberCheck (500 lines) */ final class MappedWithIndexTest { @Test void transformsList() throws Exception { new Assertion<>( - "Can't transform an iterable", + "must transform an iterable", new MappedWithIndex<>( - (index, input) -> new Upper( + (input, index) -> new Upper( new Joined( "-", new TextOf(index.toString()), @@ -63,9 +62,9 @@ void transformsList() throws Exception { @Test void transformsEmptyList() { new Assertion<>( - "Can't transform an empty iterable", + "must transform an empty iterable", new MappedWithIndex<>( - (index, input) -> { + (input, index) -> { Assertions.fail("must do not be executed"); return input; }, @@ -78,9 +77,9 @@ void transformsEmptyList() { @Test void string() { new Assertion<>( - "Can't convert to string", + "must convert to string", new MappedWithIndex<>( - (index, x) -> x * index * 2, + (x, index) -> x * index * 2, new IterableOf<>(1, 2, 3) ).toString(), Matchers.equalTo("0, 4, 12") @@ -92,7 +91,7 @@ void transformsArray() { new Assertion<>( "Transforms an array", new MappedWithIndex<>( - (index, input) -> new Upper( + (input, index) -> new Upper( new TextOf( String.format( "%1$s-%2$s", diff --git a/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java b/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java index b51e2fa8ba..b34d733625 100644 --- a/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java +++ b/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java @@ -35,7 +35,7 @@ /** * Tests for {@link MappedWithIndex}. - * @since 0.50 + * @since 1.0.0 * @checkstyle MagicNumber (500 lines) */ final class MappedWithIndexTest { @@ -45,8 +45,8 @@ void iteratatesOver() { "must map values of iterator", new IterableOf<>( new MappedWithIndex<>( - (i, v) -> String - .format("%1$s - %2$s", i, v), + (item, index) -> String + .format("%1$s - %2$s", index, item), new IteratorOf(1L, 2, 0) ) ), @@ -57,8 +57,8 @@ void iteratatesOver() { @Test void failsIfIteratorExhausted() { final Iterator iterator = new MappedWithIndex<>( - (i, v) -> String - .format("%1$s X %2$s", i, v.toString()), + (item, index) -> String + .format("%1$s X %2$s", index, item), new IteratorOf(1) ); iterator.next(); @@ -72,10 +72,10 @@ void failsIfIteratorExhausted() { @Test void removingElementsFromIterator() { final Iterator iterator = new MappedWithIndex<>( - (i, v) -> String.format( + (item, index) -> String.format( "%1$s : %2$s", - i, - v.toString() + index, + item ), new LinkedList( Arrays.asList(1, 2, 3) From 8433f068701ee7980dfab24a91b4cf5493dcb4f8 Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Sat, 6 Mar 2021 17:30:43 -0300 Subject: [PATCH 15/29] (#1507) Fixed the annotation @since in javadoc --- src/main/java/org/cactoos/iterable/MappedWithIndex.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/iterable/MappedWithIndex.java b/src/main/java/org/cactoos/iterable/MappedWithIndex.java index 2ee943388e..4f4f17014c 100644 --- a/src/main/java/org/cactoos/iterable/MappedWithIndex.java +++ b/src/main/java/org/cactoos/iterable/MappedWithIndex.java @@ -31,7 +31,7 @@ *

* There is no thread-safety guarantee. * @param Type of target item - * @since 0.50 + * @since 1.0.0 */ public final class MappedWithIndex extends IterableEnvelope { /** From 20d8e2d953ce65780040530b4e49028f8e3d706b Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Sun, 7 Mar 2021 19:51:52 -0300 Subject: [PATCH 16/29] (#1507) Fixed tests --- .../cactoos/iterable/MappedWithIndexTest.java | 58 ++++++++++--------- .../cactoos/iterator/MappedWithIndexTest.java | 47 +++++++++------ 2 files changed, 60 insertions(+), 45 deletions(-) diff --git a/src/test/java/org/cactoos/iterable/MappedWithIndexTest.java b/src/test/java/org/cactoos/iterable/MappedWithIndexTest.java index a1e05706d4..b7fc75e0da 100644 --- a/src/test/java/org/cactoos/iterable/MappedWithIndexTest.java +++ b/src/test/java/org/cactoos/iterable/MappedWithIndexTest.java @@ -23,11 +23,8 @@ */ package org.cactoos.iterable; -import java.util.Collections; import org.cactoos.text.Joined; import org.cactoos.text.TextOf; -import org.cactoos.text.Upper; -import org.hamcrest.Matchers; import org.hamcrest.core.IsEqual; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -40,27 +37,30 @@ */ final class MappedWithIndexTest { @Test - void transformsList() throws Exception { + void transformsIterable() throws Exception { new Assertion<>( "must transform an iterable", new MappedWithIndex<>( - (input, index) -> new Upper( - new Joined( - "-", - new TextOf(index.toString()), - new TextOf(input) - ) + (input, index) -> new Joined( + "-", + new TextOf(index.toString()), + new TextOf(input) ), new IterableOf<>( - "hello", "world", "друг" + "hello", "world" ) - ).iterator().next().asString(), - new IsEqual<>("0-HELLO") + ), + new IsEqual<>( + new IterableOf<>( + new TextOf("0-hello"), + new TextOf("1-world") + ) + ) ).affirm(); } @Test - void transformsEmptyList() { + void transformsEmptyIterable() { new Assertion<>( "must transform an empty iterable", new MappedWithIndex<>( @@ -68,9 +68,11 @@ void transformsEmptyList() { Assertions.fail("must do not be executed"); return input; }, - Collections.emptyList() + new IterableOf<>() ), - Matchers.emptyIterable() + new IsEqual<>( + new IterableOf<>() + ) ).affirm(); } @@ -82,7 +84,7 @@ void string() { (x, index) -> x * index * 2, new IterableOf<>(1, 2, 3) ).toString(), - Matchers.equalTo("0, 4, 12") + new IsEqual<>("0, 4, 12") ).affirm(); } @@ -91,18 +93,20 @@ void transformsArray() { new Assertion<>( "Transforms an array", new MappedWithIndex<>( - (input, index) -> new Upper( - new TextOf( - String.format( - "%1$s-%2$s", - index, - input - ) - ) - ).asString(), + (input, index) -> new Joined( + "-", + index.toString(), + input + ), "a", "b", "c" ), - new IsEqual<>(new IterableOf<>("0-A", "1-B", "2-C")) + new IsEqual<>( + new IterableOf<>( + new TextOf("0-a"), + new TextOf("1-b"), + new TextOf("2-c") + ) + ) ).affirm(); } } diff --git a/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java b/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java index b34d733625..1bfa202fa9 100644 --- a/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java +++ b/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java @@ -23,11 +23,12 @@ */ package org.cactoos.iterator; -import java.util.Arrays; import java.util.Iterator; -import java.util.LinkedList; import java.util.NoSuchElementException; +import org.cactoos.Text; import org.cactoos.iterable.IterableOf; +import org.cactoos.text.FormattedText; +import org.cactoos.text.TextOf; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; import org.llorllale.cactoos.matchers.HasValues; @@ -36,30 +37,39 @@ /** * Tests for {@link MappedWithIndex}. * @since 1.0.0 - * @checkstyle MagicNumber (500 lines) */ final class MappedWithIndexTest { @Test - void iteratatesOver() { + void iteratesOver() { new Assertion<>( "must map values of iterator", new IterableOf<>( new MappedWithIndex<>( - (item, index) -> String - .format("%1$s - %2$s", index, item), - new IteratorOf(1L, 2, 0) + (item, index) -> new FormattedText( + "%1$s - %2$s", + index, + item + ), + new IterableOf<>("1", "2", "0").iterator() ) ), - new HasValues<>("0 - 1", "1 - 2", "2 - 0") + new HasValues<>( + new TextOf("0 - 1"), + new TextOf("1 - 2"), + new TextOf("2 - 0") + ) ).affirm(); } @Test void failsIfIteratorExhausted() { - final Iterator iterator = new MappedWithIndex<>( - (item, index) -> String - .format("%1$s X %2$s", index, item), - new IteratorOf(1) + final Iterator iterator = new MappedWithIndex<>( + (item, index) -> new FormattedText( + "%1$s X %2$s", + index, + item + ), + new IterableOf<>("1").iterator() ); iterator.next(); new Assertion<>( @@ -71,15 +81,13 @@ void failsIfIteratorExhausted() { @Test void removingElementsFromIterator() { - final Iterator iterator = new MappedWithIndex<>( - (item, index) -> String.format( + final Iterator iterator = new MappedWithIndex<>( + (item, index) -> new FormattedText( "%1$s : %2$s", index, item ), - new LinkedList( - Arrays.asList(1, 2, 3) - ).iterator() + new IterableOf<>("1", "2", "3").iterator() ); iterator.next(); iterator.remove(); @@ -88,7 +96,10 @@ void removingElementsFromIterator() { new IterableOf<>( iterator ), - new HasValues<>("1 : 2", "2 : 3") + new HasValues<>( + new TextOf("1 : 2"), + new TextOf("2 : 3") + ) ).affirm(); } } From 5356174986d13878f3c1019ee6ae5413f57bafeb Mon Sep 17 00:00:00 2001 From: "Olivier B. OURA" Date: Fri, 26 Feb 2021 12:29:42 +0000 Subject: [PATCH 17/29] Implements ListIteratorJoined #1254 --- src/main/java/org/cactoos/list/Joined.java | 3 + .../org/cactoos/list/JoinedListIterator.java | 195 ++++++++++++++++++ .../cactoos/list/JoinedListIteratorTest.java | 126 +++++++++++ 3 files changed, 324 insertions(+) create mode 100644 src/main/java/org/cactoos/list/JoinedListIterator.java create mode 100644 src/test/java/org/cactoos/list/JoinedListIteratorTest.java diff --git a/src/main/java/org/cactoos/list/Joined.java b/src/main/java/org/cactoos/list/Joined.java index 44338bcfda..18825c7d19 100644 --- a/src/main/java/org/cactoos/list/Joined.java +++ b/src/main/java/org/cactoos/list/Joined.java @@ -34,6 +34,9 @@ * * @param Type of source item * @since 0.20 + * @todo #1254:30min Make {@link Joined} implements directly {@link List} + * and delegate each operation to {@link JoinedListIterator} as if lists + * joined were one list. */ public final class Joined extends ListEnvelope { diff --git a/src/main/java/org/cactoos/list/JoinedListIterator.java b/src/main/java/org/cactoos/list/JoinedListIterator.java new file mode 100644 index 0000000000..a35e0e14d8 --- /dev/null +++ b/src/main/java/org/cactoos/list/JoinedListIterator.java @@ -0,0 +1,195 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.list; + +import java.util.Collections; +import java.util.List; +import java.util.ListIterator; +import java.util.NoSuchElementException; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * A few {@link ListIterator} joined together. + * + *

There is no thread-safety guarantee. + * + * @param Items type + * @since 1.0.0 + * @todo #1254:30min We should implement mutable operations (#add, #set and #remove) + * according to their javadoc. Also, the behaviour of adding/setting/removing an element + * when the cursor is in between two listerators should be well documented in this + * class (because there are multiple options in that case). + */ +@SuppressWarnings("PMD.TooManyMethods") +public final class JoinedListIterator implements ListIterator { + + /** + * {@link List} of {@link ListIterator}. + */ + private final List> listiters; + + /** + * Cursor of the {@link List} of {@link ListIterator}. + */ + private final AtomicInteger cursorlit; + + /** + * Cursor of {@link ListIterator}. + */ + private final AtomicInteger cursor; + + /** + * Ctor. + * @param items Items to concatenate + */ + @SafeVarargs + public JoinedListIterator(final ListIterator... items) { + this(new ListOf<>(items)); + } + + /** + * Ctor. + * @param item First item + * @param items ListIterator + */ + @SuppressWarnings("unchecked") + public JoinedListIterator(final T item, final ListIterator items) { + this(new ListOf<>(new ListOf<>(item).listIterator(), items)); + } + + /** + * Ctor. + * @param items ListIterators + * @param item End item + */ + @SuppressWarnings("unchecked") + public JoinedListIterator(final ListIterator items, final T item) { + this(new ListOf<>(items, new ListOf<>(item).listIterator())); + } + + /** + * Ctor. + * @param items Items to concatenate + */ + public JoinedListIterator(final List> items) { + this.listiters = items; + this.cursorlit = new AtomicInteger(-1); + this.cursor = new AtomicInteger(-1); + } + + @Override + public boolean hasNext() { + while (!this.currentListIterator().hasNext() && this.listHasNextElt()) { + this.cursorlit.getAndIncrement(); + } + return this.currentListIterator().hasNext(); + } + + @Override + public T next() { + if (!this.hasNext()) { + throw new NoSuchElementException(); + } + final T next = this.currentListIterator().next(); + this.cursor.getAndIncrement(); + return next; + } + + @Override + public boolean hasPrevious() { + while (!this.currentListIterator().hasPrevious() && this.listHasPreviousElt()) { + this.cursorlit.getAndDecrement(); + } + return this.currentListIterator().hasPrevious(); + } + + @Override + public T previous() { + if (!this.hasPrevious()) { + throw new NoSuchElementException(); + } + final T previous = this.currentListIterator().previous(); + this.cursor.getAndDecrement(); + return previous; + } + + @Override + public int nextIndex() { + return this.cursor.get() + 1; + } + + @Override + public int previousIndex() { + int previousidx = -1; + if (this.hasPrevious()) { + previousidx = this.cursor.get() - 1; + } + return previousidx; + } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } + + @Override + public void set(final T elt) { + throw new UnsupportedOperationException(); + } + + @Override + public void add(final T elt) { + throw new UnsupportedOperationException(); + } + + /** + * If {@link List} of {@link ListIterator} has next element. + * @return Has or no + */ + private boolean listHasNextElt() { + return this.cursorlit.get() + 1 <= this.listiters.size() - 1; + } + + /** + * If {@link List} of {@link ListIterator} has previous element. + * @return Has or no + */ + private boolean listHasPreviousElt() { + return this.cursorlit.get() - 1 >= 0; + } + + /** + * Get current {@link ListIterator}. + * @return Current element + */ + private ListIterator currentListIterator() { + final ListIterator current; + if (this.cursorlit.get() == -1) { + current = Collections.emptyListIterator(); + } else { + current = this.listiters.get(this.cursorlit.get()); + } + return current; + } +} diff --git a/src/test/java/org/cactoos/list/JoinedListIteratorTest.java b/src/test/java/org/cactoos/list/JoinedListIteratorTest.java new file mode 100644 index 0000000000..3f78135e9b --- /dev/null +++ b/src/test/java/org/cactoos/list/JoinedListIteratorTest.java @@ -0,0 +1,126 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.list; + +import java.util.ListIterator; +import java.util.NoSuchElementException; +import org.cactoos.iterable.IterableOf; +import org.hamcrest.core.IsEqual; +import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.Throws; + +/** + * Test case for {@link JoinedListIterator}. + * @since 1.0.0 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + * @checkstyle MagicNumber (500 lines) + */ +final class JoinedListIteratorTest { + + @Test + @SuppressWarnings("unchecked") + void joinsListIterators() { + new Assertion<>( + "Must concatenate iterable of listIterators together", + new IterableOf<>( + new JoinedListIterator<>( + new ListOf<>("x").listIterator(), + new ListOf<>("y").listIterator() + ) + ), + new IsEqual<>(new IterableOf<>("x", "y")) + ).affirm(); + } + + @Test + @SuppressWarnings("unchecked") + void navigatesInNonEmptyIterator() { + final ListIterator joined = new JoinedListIterator<>( + new ListOf<>(1).listIterator(), + new ListOf<>(2).listIterator() + ); + new Assertion<>( + "Must call next method directly on non-empty listIterator for the first time", + joined.next(), + new IsEqual<>(1) + ).affirm(); + new Assertion<>( + "Must call next method directly on non-empty listIterator for the second time", + joined.next(), + new IsEqual<>(2) + ).affirm(); + new Assertion<>( + "Must call previous method directly on non-empty listIterator for the first time", + joined.previous(), + new IsEqual<>(2) + ).affirm(); + new Assertion<>( + "Must call previous method directly on non-empty listIterator for the second time", + joined.previous(), + new IsEqual<>(1) + ).affirm(); + } + + @Test + @SuppressWarnings("unchecked") + void throwsExceptionWhenCallPreviousOnEmptyIterator() { + new Assertion<>( + "Must throw an exception", + () -> new JoinedListIterator(new ListOf<>()).previous(), + new Throws<>(NoSuchElementException.class) + ).affirm(); + } + + @Test + @SuppressWarnings("unchecked") + void joinItemAndIterable() { + new Assertion<>( + "Must join item and iterable", + new IterableOf<>( + new JoinedListIterator<>( + 0, + new ListOf<>(1, 2, 3).listIterator() + ) + ), + new IsEqual<>(new IterableOf<>(0, 1, 2, 3)) + ).affirm(); + } + + @Test + @SuppressWarnings("unchecked") + void joinIterableAndItem() { + new Assertion<>( + "Must join iterable and item", + new IterableOf<>( + new JoinedListIterator<>( + new ListOf<>(1, 2, 3).listIterator(), + 0 + ) + ), + new IsEqual<>(new IterableOf<>(1, 2, 3, 0)) + ).affirm(); + } +} From 58a54f303044583c9ced8c2529fec91ab2b87841 Mon Sep 17 00:00:00 2001 From: Maximillian Arruda Date: Sun, 7 Mar 2021 19:59:21 -0300 Subject: [PATCH 18/29] (#1507) Fixed tests --- src/test/java/org/cactoos/iterator/MappedWithIndexTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java b/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java index 1bfa202fa9..53b1359f69 100644 --- a/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java +++ b/src/test/java/org/cactoos/iterator/MappedWithIndexTest.java @@ -23,6 +23,8 @@ */ package org.cactoos.iterator; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; import java.util.NoSuchElementException; import org.cactoos.Text; @@ -87,7 +89,7 @@ void removingElementsFromIterator() { index, item ), - new IterableOf<>("1", "2", "3").iterator() + new ArrayList<>(Arrays.asList("1", "2", "3")).iterator() ); iterator.next(); iterator.remove(); From 55c45346da28f57d122fd3dcd0bebd7078eebae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Mon, 8 Mar 2021 16:11:23 +0100 Subject: [PATCH 19/29] only run codecov once --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index babecdade3..26c6c5d34f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -23,7 +23,7 @@ jobs: run: mvn clean test --errors --batch-mode - name: CodeCov uses: codecov/codecov-action@v1.0.13 - if: matrix.os == 'ubuntu-18.04' && github.repository == 'yegor256/cactoos' + if: matrix.os == 'ubuntu-18.04' && matrix.java == 8 && github.repository == 'yegor256/cactoos' timeout-minutes: 10 with: file: target/coverage/jacoco.xml From 96f12403d4e9cbfe79c4bca8bbb7f2caba9d11bb Mon Sep 17 00:00:00 2001 From: "Olivier B. OURA" Date: Mon, 8 Mar 2021 22:29:32 +0000 Subject: [PATCH 20/29] Exploit generic variance in package bytes #1533 --- src/main/java/org/cactoos/bytes/CheckedBytes.java | 4 ++-- src/main/java/org/cactoos/bytes/UncheckedBytes.java | 4 ++-- src/main/java/org/cactoos/collection/package-info.java | 3 +++ src/main/java/org/cactoos/scalar/Checked.java | 8 ++++---- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/cactoos/bytes/CheckedBytes.java b/src/main/java/org/cactoos/bytes/CheckedBytes.java index bd478c0071..a60553af60 100644 --- a/src/main/java/org/cactoos/bytes/CheckedBytes.java +++ b/src/main/java/org/cactoos/bytes/CheckedBytes.java @@ -43,14 +43,14 @@ public final class CheckedBytes implements Bytes { /** * Function that wraps exception of {@link #origin} to the required type. */ - private final Func func; + private final Func func; /** * Ctor. * @param orig Origin bytes. * @param fnc Function that wraps exceptions. */ - public CheckedBytes(final Bytes orig, final Func fnc) { + public CheckedBytes(final Bytes orig, final Func fnc) { this.origin = orig; this.func = fnc; } diff --git a/src/main/java/org/cactoos/bytes/UncheckedBytes.java b/src/main/java/org/cactoos/bytes/UncheckedBytes.java index e473d297ac..b7bc1879a1 100644 --- a/src/main/java/org/cactoos/bytes/UncheckedBytes.java +++ b/src/main/java/org/cactoos/bytes/UncheckedBytes.java @@ -44,7 +44,7 @@ public final class UncheckedBytes implements Bytes { /** * Fallback. */ - private final Func fallback; + private final Func fallback; /** * Ctor. @@ -67,7 +67,7 @@ public UncheckedBytes(final Bytes bts) { * @since 0.5 */ public UncheckedBytes(final Bytes bts, - final Func fbk) { + final Func fbk) { this.bytes = bts; this.fallback = fbk; } diff --git a/src/main/java/org/cactoos/collection/package-info.java b/src/main/java/org/cactoos/collection/package-info.java index 6f324687fa..f39a918e25 100644 --- a/src/main/java/org/cactoos/collection/package-info.java +++ b/src/main/java/org/cactoos/collection/package-info.java @@ -26,5 +26,8 @@ * Collections, tests. * * @since 0.14 + * @todo #1533:30min Continue to exploit variance for package + * org.cactoos.collection to ensure typing works as best as + * possible as it is explained in #1533 issue. */ package org.cactoos.collection; diff --git a/src/main/java/org/cactoos/scalar/Checked.java b/src/main/java/org/cactoos/scalar/Checked.java index ef3961a91c..a5e4432ea3 100644 --- a/src/main/java/org/cactoos/scalar/Checked.java +++ b/src/main/java/org/cactoos/scalar/Checked.java @@ -44,20 +44,20 @@ public final class Checked implements Scalar { /** * Function that wraps exception. */ - private final Func func; + private final Func func; /** * Original scalar. */ - private final Scalar origin; + private final Scalar origin; /** * Ctor. * @param scalar Encapsulated scalar * @param fnc Func that wraps exception */ - public Checked(final Scalar scalar, - final Func fnc) { + public Checked(final Scalar scalar, + final Func fnc) { this.func = fnc; this.origin = scalar; } From 894cc11e0f441ab6a30bafd9651a00a85acfa728 Mon Sep 17 00:00:00 2001 From: "Olivier B. OURA" Date: Mon, 8 Mar 2021 22:42:24 +0000 Subject: [PATCH 21/29] Puzzle compliance #1533 --- src/main/java/org/cactoos/collection/package-info.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/cactoos/collection/package-info.java b/src/main/java/org/cactoos/collection/package-info.java index f39a918e25..5c8c7e0569 100644 --- a/src/main/java/org/cactoos/collection/package-info.java +++ b/src/main/java/org/cactoos/collection/package-info.java @@ -26,8 +26,8 @@ * Collections, tests. * * @since 0.14 - * @todo #1533:30min Continue to exploit variance for package - * org.cactoos.collection to ensure typing works as best as - * possible as it is explained in #1533 issue. + * @todo #1533:30min Continue to exploit generic variance for package + * org.cactoos.collection to ensure typing works as best as + * possible as it is explained in #1533 issue. */ package org.cactoos.collection; From caaf0d9d86acb03fecae7e78160a588b764ecb6b Mon Sep 17 00:00:00 2001 From: "Olivier B. OURA" Date: Tue, 9 Mar 2021 08:49:24 +0000 Subject: [PATCH 22/29] Introduce tests for Checked class #1533 --- .../org/cactoos/bytes/UncheckedBytes.java | 4 +-- .../java/org/cactoos/scalar/CheckedTest.java | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/bytes/UncheckedBytes.java b/src/main/java/org/cactoos/bytes/UncheckedBytes.java index b7bc1879a1..24ef172c15 100644 --- a/src/main/java/org/cactoos/bytes/UncheckedBytes.java +++ b/src/main/java/org/cactoos/bytes/UncheckedBytes.java @@ -44,7 +44,7 @@ public final class UncheckedBytes implements Bytes { /** * Fallback. */ - private final Func fallback; + private final Func fallback; /** * Ctor. @@ -67,7 +67,7 @@ public UncheckedBytes(final Bytes bts) { * @since 0.5 */ public UncheckedBytes(final Bytes bts, - final Func fbk) { + final Func fbk) { this.bytes = bts; this.fallback = fbk; } diff --git a/src/test/java/org/cactoos/scalar/CheckedTest.java b/src/test/java/org/cactoos/scalar/CheckedTest.java index cba36c1caf..9594799bcf 100644 --- a/src/test/java/org/cactoos/scalar/CheckedTest.java +++ b/src/test/java/org/cactoos/scalar/CheckedTest.java @@ -25,9 +25,12 @@ import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.channels.AcceptPendingException; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.hamcrest.core.IsEqual; import org.junit.Test; +import org.llorllale.cactoos.matchers.Assertion; /** * Test case for {@link Checked}. @@ -46,6 +49,30 @@ public void runtimeExceptionGoesOut() throws Exception { ).value(); } + @Test(expected = IllegalStateException.class) + public void usesGenericVarianceOnExceptionTypes() throws Exception { + new Checked( + () -> { + throw new IllegalStateException(); + }, + (Throwable ex) -> { + return new AcceptPendingException(); + } + ).value(); + } + + @SuppressWarnings("PMD.AvoidDuplicateLiterals") + public void usesContravarianceOnResultType() throws Exception { + new Assertion<>( + "Must use contravariance on result", + new Checked( + () -> new String("contravariance"), + IOException::new + ).value(), + new IsEqual<>("contravariance") + ); + } + @Test(expected = IOException.class) public void throwsIoException() throws Exception { new Checked<>( From f042c2b3ee9b73027dd1e9aadd4689d5bf709b33 Mon Sep 17 00:00:00 2001 From: "Olivier B. OURA" Date: Thu, 11 Mar 2021 19:02:50 +0000 Subject: [PATCH 23/29] Replace MatcherAssert in a part of test package iterable #1425 --- .../java/org/cactoos/iterable/CycledTest.java | 9 ++--- .../org/cactoos/iterable/EndlessTest.java | 7 ++-- .../org/cactoos/iterable/FilteredTest.java | 25 ++++++------- .../java/org/cactoos/iterable/HeadOfTest.java | 6 +-- .../iterable/IterableOfBooleansTest.java | 11 +++--- .../iterable/IterableOfDoublesTest.java | 11 +++--- .../iterable/IterableOfFloatsTest.java | 11 +++--- .../cactoos/iterable/IterableOfIntsTest.java | 11 +++--- .../cactoos/iterable/IterableOfLongsTest.java | 11 +++--- .../iterable/IterableOfShortsTest.java | 11 +++--- .../org/cactoos/iterable/IterableOfTest.java | 37 +++++++++---------- .../java/org/cactoos/iterable/MappedTest.java | 28 +++++++------- .../org/cactoos/iterable/MatchedTest.java | 19 +++++++--- .../org/cactoos/iterable/PartitionedTest.java | 14 +++---- .../org/cactoos/iterable/RangeOfTest.java | 32 ++++++++-------- src/test/java/org/cactoos/package-info.java | 2 +- 16 files changed, 128 insertions(+), 117 deletions(-) diff --git a/src/test/java/org/cactoos/iterable/CycledTest.java b/src/test/java/org/cactoos/iterable/CycledTest.java index 8afdcff5b6..814e0f0d7a 100644 --- a/src/test/java/org/cactoos/iterable/CycledTest.java +++ b/src/test/java/org/cactoos/iterable/CycledTest.java @@ -26,7 +26,6 @@ import java.util.Collections; import org.cactoos.scalar.ItemAt; import org.cactoos.scalar.LengthOf; -import org.hamcrest.MatcherAssert; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; import org.llorllale.cactoos.matchers.HasValue; @@ -42,7 +41,7 @@ final class CycledTest { void repeatIterableTest() { final String expected = "two"; new Assertion<>( - "Can't repeat iterable", + "Must repeat iterable", new ItemAt<>( // @checkstyle MagicNumberCheck (1 line)< 7, new Cycled<>( @@ -59,14 +58,14 @@ void repeatIterableTest() { @Test void notCycledEmptyTest() throws Exception { - MatcherAssert.assertThat( - "Can't generate an empty iterable", + new Assertion<>( + "Must generate an empty iterable", new LengthOf( new Cycled<>( Collections::emptyIterator ) ), new HasValue<>(0L) - ); + ).affirm(); } } diff --git a/src/test/java/org/cactoos/iterable/EndlessTest.java b/src/test/java/org/cactoos/iterable/EndlessTest.java index 35ffe07374..37637c9b68 100644 --- a/src/test/java/org/cactoos/iterable/EndlessTest.java +++ b/src/test/java/org/cactoos/iterable/EndlessTest.java @@ -24,7 +24,6 @@ package org.cactoos.iterable; import java.util.NoSuchElementException; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; @@ -39,11 +38,11 @@ final class EndlessTest { @Test void endlessIterableTest() { - MatcherAssert.assertThat( - "Can't get unique endless iterable item", + new Assertion<>( + "Must get unique endless iterable item", new Endless<>(1), Matchers.hasItem(1) - ); + ).affirm(); } @Test diff --git a/src/test/java/org/cactoos/iterable/FilteredTest.java b/src/test/java/org/cactoos/iterable/FilteredTest.java index 1d96bce359..c69dd2b0c7 100644 --- a/src/test/java/org/cactoos/iterable/FilteredTest.java +++ b/src/test/java/org/cactoos/iterable/FilteredTest.java @@ -26,7 +26,6 @@ import org.cactoos.list.ListOf; import org.cactoos.scalar.LengthOf; import org.cactoos.text.StartsWith; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.hamcrest.collection.IsEmptyIterable; import org.hamcrest.core.IsNot; @@ -46,8 +45,8 @@ final class FilteredTest { @Test void filtersList() throws Exception { - MatcherAssert.assertThat( - "Can't calculate the length of an iterable", + new Assertion<>( + "Must calculate the length of an iterable", new LengthOf( new Filtered<>( // @checkstyle MagicNumber (1 line) @@ -58,13 +57,13 @@ void filtersList() throws Exception { ) ), new HasValue<>(2L) - ); + ).affirm(); } @Test void filtersEmptyList() throws Exception { - MatcherAssert.assertThat( - "Can't calculate the length of an empty iterable", + new Assertion<>( + "Must calculate the length of an empty iterable", new LengthOf( new Filtered<>( input -> input.length() > 1, @@ -72,7 +71,7 @@ void filtersEmptyList() throws Exception { ) ), new HasValue<>(0L) - ); + ).affirm(); } @Test @@ -81,14 +80,14 @@ void filtersIterablesWithSize() { i -> i > 0, new IterableOf<>(1, 2, -1, 0, 1) ); - MatcherAssert.assertThat( - "Can't filter the iterable twice", + new Assertion<>( + "Must filter the iterable twice", list, Matchers.iterableWithSize(3) - ); - MatcherAssert.assertThat( - "Can't filter the iterable twice, again", + ).affirm(); + new Assertion<>( + "Must filter the iterable twice, again", list, Matchers.iterableWithSize(3) - ); + ).affirm(); } @Test diff --git a/src/test/java/org/cactoos/iterable/HeadOfTest.java b/src/test/java/org/cactoos/iterable/HeadOfTest.java index 680b9957aa..30a1684472 100644 --- a/src/test/java/org/cactoos/iterable/HeadOfTest.java +++ b/src/test/java/org/cactoos/iterable/HeadOfTest.java @@ -24,8 +24,8 @@ package org.cactoos.iterable; import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.HasValues; /** * Test Case for {@link HeadOf}. @@ -38,13 +38,13 @@ final class HeadOfTest { @SuppressWarnings("PMD.AvoidDuplicateLiterals") void headIterable() throws Exception { MatcherAssert.assertThat( - "Can't skip elements in iterable", + "Must skip elements in iterable", new HeadOf<>( 2, new IterableOf<>( "one", "two", "three", "four" ) ), - Matchers.contains( + new HasValues<>( "one", "two" ) diff --git a/src/test/java/org/cactoos/iterable/IterableOfBooleansTest.java b/src/test/java/org/cactoos/iterable/IterableOfBooleansTest.java index 96389a881c..1e288cb33e 100644 --- a/src/test/java/org/cactoos/iterable/IterableOfBooleansTest.java +++ b/src/test/java/org/cactoos/iterable/IterableOfBooleansTest.java @@ -23,9 +23,9 @@ */ package org.cactoos.iterable; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.HasValues; /** * Test case for {@link IterableOfBooleans}. @@ -37,9 +37,10 @@ final class IterableOfBooleansTest { @Test void convertsBooleanValuesToIterable() { - MatcherAssert.assertThat( + new Assertion<>( + "Must convert boolean values to iterable", new IterableOfBooleans(true, false), - Matchers.contains(true, false) - ); + new HasValues<>(true, false) + ).affirm(); } } diff --git a/src/test/java/org/cactoos/iterable/IterableOfDoublesTest.java b/src/test/java/org/cactoos/iterable/IterableOfDoublesTest.java index c057a11ef3..a893225d8f 100644 --- a/src/test/java/org/cactoos/iterable/IterableOfDoublesTest.java +++ b/src/test/java/org/cactoos/iterable/IterableOfDoublesTest.java @@ -23,9 +23,9 @@ */ package org.cactoos.iterable; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.HasValues; /** * Test case for {@link IterableOfDoubles}. @@ -38,9 +38,10 @@ final class IterableOfDoublesTest { @Test void convertsDoubleValuesToIterable() { final double[] values = new double[]{1, 2, 3}; - MatcherAssert.assertThat( + new Assertion<>( + "Must convert double values to iterable", new IterableOfDoubles(values), - Matchers.contains(values[0], values[1], values[2]) - ); + new HasValues<>(values[0], values[1], values[2]) + ).affirm(); } } diff --git a/src/test/java/org/cactoos/iterable/IterableOfFloatsTest.java b/src/test/java/org/cactoos/iterable/IterableOfFloatsTest.java index d7131cb031..c3747c679e 100644 --- a/src/test/java/org/cactoos/iterable/IterableOfFloatsTest.java +++ b/src/test/java/org/cactoos/iterable/IterableOfFloatsTest.java @@ -23,9 +23,9 @@ */ package org.cactoos.iterable; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.HasValues; /** * Test case for {@link IterableOfFloats}. @@ -38,9 +38,10 @@ final class IterableOfFloatsTest { @Test void convertsFloatValuesToIterable() { final float[] values = {1, 2, 3}; - MatcherAssert.assertThat( + new Assertion<>( + "Must convert float values to iterable", new IterableOfFloats(values), - Matchers.contains(values[0], values[1], values[2]) - ); + new HasValues<>(values[0], values[1], values[2]) + ).affirm(); } } diff --git a/src/test/java/org/cactoos/iterable/IterableOfIntsTest.java b/src/test/java/org/cactoos/iterable/IterableOfIntsTest.java index 87db5c6a37..7d6180a0ca 100644 --- a/src/test/java/org/cactoos/iterable/IterableOfIntsTest.java +++ b/src/test/java/org/cactoos/iterable/IterableOfIntsTest.java @@ -23,9 +23,9 @@ */ package org.cactoos.iterable; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.HasValues; /** * Test case for {@link IterableOfInts}. @@ -38,9 +38,10 @@ final class IterableOfIntsTest { @Test void convertsIntegerValuesToIterable() { final int[] values = {1, 2, 3}; - MatcherAssert.assertThat( + new Assertion<>( + "Must convert integer values to iterable", new IterableOfInts(values), - Matchers.contains(values[0], values[1], values[2]) - ); + new HasValues<>(values[0], values[1], values[2]) + ).affirm(); } } diff --git a/src/test/java/org/cactoos/iterable/IterableOfLongsTest.java b/src/test/java/org/cactoos/iterable/IterableOfLongsTest.java index be6f32dc13..1d815f8bbc 100644 --- a/src/test/java/org/cactoos/iterable/IterableOfLongsTest.java +++ b/src/test/java/org/cactoos/iterable/IterableOfLongsTest.java @@ -23,9 +23,9 @@ */ package org.cactoos.iterable; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.HasValues; /** * Test case for {@link IterableOfLongs}. @@ -38,9 +38,10 @@ final class IterableOfLongsTest { @Test void convertsLongValuesToIterable() { final long[] values = new long[]{1, 2, 3}; - MatcherAssert.assertThat( + new Assertion<>( + "Must convert long values to iterable", new IterableOfLongs(values), - Matchers.contains(values[0], values[1], values[2]) - ); + new HasValues<>(values[0], values[1], values[2]) + ).affirm(); } } diff --git a/src/test/java/org/cactoos/iterable/IterableOfShortsTest.java b/src/test/java/org/cactoos/iterable/IterableOfShortsTest.java index 18dbe9876e..49a400769d 100644 --- a/src/test/java/org/cactoos/iterable/IterableOfShortsTest.java +++ b/src/test/java/org/cactoos/iterable/IterableOfShortsTest.java @@ -23,9 +23,9 @@ */ package org.cactoos.iterable; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.HasValues; /** * Test case for {@link IterableOfShorts}. @@ -38,9 +38,10 @@ final class IterableOfShortsTest { @SuppressWarnings("PMD.AvoidUsingShortType") void convertsShortValuesToIterable() { final short[] values = new short[]{1, 2, 3}; - MatcherAssert.assertThat( + new Assertion<>( + "Must convert short values to iterable", new IterableOfShorts(values), - Matchers.contains(values[0], values[1], values[2]) - ); + new HasValues<>(values[0], values[1], values[2]) + ).affirm(); } } diff --git a/src/test/java/org/cactoos/iterable/IterableOfTest.java b/src/test/java/org/cactoos/iterable/IterableOfTest.java index 9b2b144d0c..3f93b18f5e 100644 --- a/src/test/java/org/cactoos/iterable/IterableOfTest.java +++ b/src/test/java/org/cactoos/iterable/IterableOfTest.java @@ -25,13 +25,12 @@ import org.cactoos.scalar.LengthOf; import org.cactoos.text.TextOf; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; import org.hamcrest.core.IsEqual; import org.hamcrest.core.IsNot; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; import org.llorllale.cactoos.matchers.HasValue; +import org.llorllale.cactoos.matchers.HasValues; import org.llorllale.cactoos.matchers.IsTrue; /** @@ -44,8 +43,8 @@ final class IterableOfTest { @Test void convertsScalarsToIterable() throws Exception { - MatcherAssert.assertThat( - "must convert scalars to iterable", + new Assertion<>( + "Must convert scalars to iterable", new LengthOf( new IterableOf<>( "a", "b", "c" @@ -53,22 +52,22 @@ void convertsScalarsToIterable() throws Exception { ), // @checkstyle MagicNumber (1 line) new HasValue<>(3L) - ); + ).affirm(); } @Test void convertsArrayOfIntsToIterable() { - MatcherAssert.assertThat( - "must convert int scalars to iterable", + new Assertion<>( + "Must convert int scalars to iterable", new IterableOf<>(1, 2, 0, 2), - Matchers.hasItem(0) - ); + new HasValues<>(0) + ).affirm(); } @Test void convertsObjectsToIterable() throws Exception { - MatcherAssert.assertThat( - "must convert objects to iterable", + new Assertion<>( + "Must convert objects to iterable", new LengthOf( new IterableOf<>( new TextOf("a"), new TextOf("b"), new TextOf("c") @@ -76,7 +75,7 @@ void convertsObjectsToIterable() throws Exception { ), // @checkstyle MagicNumber (1 line) new HasValue<>(3L) - ); + ).affirm(); } @Test @@ -100,7 +99,7 @@ void isNotEqualsToIterableWithLessElements() { @Test void notEqualsToObjectOfAnotherType() { new Assertion<>( - "must not equal to object of another type", + "Must not equal to object of another type", new IterableOf<>(), new IsNot<>(new IsEqual<>(new Object())) ).affirm(); @@ -109,7 +108,7 @@ void notEqualsToObjectOfAnotherType() { @Test void notEqualsToIterableWithDifferentElements() { new Assertion<>( - "must not equal to Iterable with different elements", + "Must not equal to Iterable with different elements", new IterableOf<>(1, 2), new IsNot<>(new IsEqual<>(new IterableOf<>(1, 0))) ).affirm(); @@ -119,7 +118,7 @@ void notEqualsToIterableWithDifferentElements() { void isEqualToItself() { final IterableOf iterable = new IterableOf<>(1, 2); new Assertion<>( - "must be equal to itself", + "Must be equal to itself", iterable, new IsEqual<>(iterable) ).affirm(); @@ -128,7 +127,7 @@ void isEqualToItself() { @Test void isEqualToIterableWithTheSameElements() { new Assertion<>( - "must be equal to Iterable with the same elements", + "Must be equal to Iterable with the same elements", new IterableOf<>(1, 2), new IsEqual<>(new IterableOf<>(1, 2)) ).affirm(); @@ -137,7 +136,7 @@ void isEqualToIterableWithTheSameElements() { @Test void equalToEmptyIterable() { new Assertion<>( - "empty Iterable must be equal to empty Iterable", + "Empty Iterable must be equal to empty Iterable", new IterableOf<>(), new IsEqual<>(new IterableOf<>()) ).affirm(); @@ -146,7 +145,7 @@ void equalToEmptyIterable() { @Test void differentHashCode() { new Assertion<>( - "must have different hashCode for Iterables with different content", + "Must have different hashCode for Iterables with different content", new IterableOf<>(1, 2).hashCode(), new IsNot<>(new IsEqual<>(new IterableOf<>(2, 1).hashCode())) ).affirm(); @@ -155,7 +154,7 @@ void differentHashCode() { @Test void equalHashCode() { new Assertion<>( - "must have equal hashCode for Iterables with equal content", + "Must have equal hashCode for Iterables with equal content", new IterableOf<>(1, 2).hashCode(), new IsEqual<>(new IterableOf<>(1, 2).hashCode()) ).affirm(); diff --git a/src/test/java/org/cactoos/iterable/MappedTest.java b/src/test/java/org/cactoos/iterable/MappedTest.java index dcd71f0e61..1ec68dfe24 100644 --- a/src/test/java/org/cactoos/iterable/MappedTest.java +++ b/src/test/java/org/cactoos/iterable/MappedTest.java @@ -27,8 +27,7 @@ import org.cactoos.list.ListOf; import org.cactoos.text.TextOf; import org.cactoos.text.Upper; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; +import org.hamcrest.collection.IsEmptyIterable; import org.hamcrest.core.IsEqual; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; @@ -38,45 +37,46 @@ * @since 0.1 * @checkstyle JavadocMethodCheck (500 lines) * @checkstyle MagicNumberCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ final class MappedTest { @Test void transformsList() throws Exception { - MatcherAssert.assertThat( - "Can't transform an iterable", + new Assertion<>( + "Must transform an iterable", new Mapped<>( input -> new Upper(new TextOf(input)), new IterableOf<>( "hello", "world", "друг" ) ).iterator().next().asString(), - Matchers.equalTo("HELLO") - ); + new IsEqual<>("HELLO") + ).affirm(); } @Test void transformsEmptyList() { - MatcherAssert.assertThat( - "Can't transform an empty iterable", + new Assertion<>( + "Must transform an empty iterable", new Mapped<>( (String input) -> new Upper(new TextOf(input)), Collections.emptyList() ), - Matchers.emptyIterable() - ); + new IsEmptyIterable<>() + ).affirm(); } @Test void string() { - MatcherAssert.assertThat( - "Can't convert to string", + new Assertion<>( + "Must convert to string", new Mapped<>( x -> x * 2, new ListOf<>(1, 2, 3) ).toString(), - Matchers.equalTo("2, 4, 6") - ); + new IsEqual<>("2, 4, 6") + ).affirm(); } @Test diff --git a/src/test/java/org/cactoos/iterable/MatchedTest.java b/src/test/java/org/cactoos/iterable/MatchedTest.java index e204d87fa0..f1a6ba5a80 100644 --- a/src/test/java/org/cactoos/iterable/MatchedTest.java +++ b/src/test/java/org/cactoos/iterable/MatchedTest.java @@ -25,10 +25,12 @@ import org.cactoos.list.ListOf; import org.cactoos.scalar.LengthOf; -import org.hamcrest.Matchers; +import org.hamcrest.collection.IsIterableWithSize; +import org.hamcrest.core.IsEqual; import org.junit.Assert; import org.junit.Test; import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.HasValues; import org.llorllale.cactoos.matchers.Throws; /** @@ -37,6 +39,7 @@ * @since 0.39 * @checkstyle MagicNumberCheck (500 lines) * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") public final class MatchedTest { @@ -49,7 +52,7 @@ public void iterator() { new IterableOf<>(1, 2, 3), new IterableOf<>(1, 2, 3) ), - Matchers.hasItems(1, 2, 3) + new HasValues<>(1, 2, 3) ).affirm(); } @@ -92,7 +95,9 @@ public void endsWith() { new IterableOf<>("1st elem", "2nd elem", "3rd elem"), new IterableOf<>("`A` elem", "`B` elem", "'C' elem") ), - Matchers.iterableWithSize(3) + new IsIterableWithSize<>( + new IsEqual<>(3) + ) ).affirm(); } @@ -105,7 +110,9 @@ public void matchedAsNumbers() { new IterableOf<>(1d, 2d, 3d), new IterableOf<>(1L, 2L, 3L) ), - Matchers.iterableWithSize(3) + new IsIterableWithSize<>( + new IsEqual<>(3) + ) ).affirm(); } @@ -142,7 +149,9 @@ public void iterablesOfDifferentTypes() { new IterableOf<>(1, 2, 3), new IterableOf<>("1", "2", "3") ), - Matchers.iterableWithSize(3) + new IsIterableWithSize<>( + new IsEqual<>(3) + ) ).affirm(); } diff --git a/src/test/java/org/cactoos/iterable/PartitionedTest.java b/src/test/java/org/cactoos/iterable/PartitionedTest.java index 76a9eb200f..b6486ef7e0 100644 --- a/src/test/java/org/cactoos/iterable/PartitionedTest.java +++ b/src/test/java/org/cactoos/iterable/PartitionedTest.java @@ -24,8 +24,8 @@ package org.cactoos.iterable; import org.cactoos.scalar.LengthOf; -import org.hamcrest.MatcherAssert; import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; import org.llorllale.cactoos.matchers.HasValue; /** @@ -39,24 +39,24 @@ final class PartitionedTest { @Test void partitionedEmpty() throws Exception { - MatcherAssert.assertThat( - "Can't generate a Partitioned without values.", + new Assertion<>( + "Must generate a Partitioned without values.", new LengthOf( new Partitioned<>(2) ), new HasValue<>(0L) - ); + ).affirm(); } @Test void partitionedWithPartial() throws Exception { - MatcherAssert.assertThat( - "Can't generate a Partitioned with partition size.", + new Assertion<>( + "Must generate a Partitioned with partition size.", new LengthOf( new Partitioned<>(2, new IterableOf<>(1, 2, 3)) ), new HasValue<>(2L) - ); + ).affirm(); } } diff --git a/src/test/java/org/cactoos/iterable/RangeOfTest.java b/src/test/java/org/cactoos/iterable/RangeOfTest.java index 672986bcdb..f8b1876641 100644 --- a/src/test/java/org/cactoos/iterable/RangeOfTest.java +++ b/src/test/java/org/cactoos/iterable/RangeOfTest.java @@ -27,9 +27,9 @@ import java.time.temporal.ChronoUnit; import org.cactoos.Func; import org.cactoos.list.ListOf; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; /** * Test of range implementation. @@ -42,19 +42,19 @@ public class RangeOfTest { @Test public final void testIntegerRange() { - MatcherAssert.assertThat( - "Can't generate a range of integers", + new Assertion<>( + "Must generate a range of integers", new ListOf<>( new RangeOf<>(1, 5, value -> ++value) ), Matchers.contains(1, 2, 3, 4, 5) - ); + ).affirm(); } @Test public final void testIntegerFibonacciRange() { - MatcherAssert.assertThat( - "Can't generate a range of fibonacci integers", + new Assertion<>( + "Must generate a range of fibonacci integers", new ListOf<>( new RangeOf<>( 1, @@ -72,35 +72,35 @@ public Integer apply( ) ), Matchers.contains(1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89) - ); + ).affirm(); } @Test public final void testLongRange() { - MatcherAssert.assertThat( - "Can't generate a range of long", + new Assertion<>( + "Must generate a range of long", new ListOf<>( new RangeOf<>(1L, 5L, value -> ++value) ), Matchers.contains(1L, 2L, 3L, 4L, 5L) - ); + ).affirm(); } @Test public final void testCharacterRange() { - MatcherAssert.assertThat( - "Can't generate a range of characters", + new Assertion<>( + "Must generate a range of characters", new ListOf<>( new RangeOf<>('a', 'c', value -> ++value) ), Matchers.contains('a', 'b', 'c') - ); + ).affirm(); } @Test public final void testLocalDateRange() { - MatcherAssert.assertThat( - "Can't generate a range of local dates.", + new Assertion<>( + "Must generate a range of local dates.", new ListOf<>( new RangeOf<>( LocalDate.of(2017, 1, 1), @@ -113,7 +113,7 @@ public final void testLocalDateRange() { LocalDate.of(2017, 1, 2), LocalDate.of(2017, 1, 3) ) - ); + ).affirm(); } } diff --git a/src/test/java/org/cactoos/package-info.java b/src/test/java/org/cactoos/package-info.java index 7631359387..4b14612ef5 100644 --- a/src/test/java/org/cactoos/package-info.java +++ b/src/test/java/org/cactoos/package-info.java @@ -25,7 +25,7 @@ /** * Tests. * - * @todo #1420:30min Continue replacing usage of MatcherAssert.assertThat with + * @todo #1425:30min Continue replacing usage of MatcherAssert.assertThat with * Assertion from cactoos-matchers. Keep PR short and limit the changes to * single package. Update this puzzle for the next package. * After all packages are done, add MatcherAssert to forbidden-apis.txt From 9642cb0652ccb30ad6581443eb1b48258fea5607 Mon Sep 17 00:00:00 2001 From: "Olivier B. OURA" Date: Thu, 11 Mar 2021 22:37:55 +0000 Subject: [PATCH 24/29] Remove modifiers on RangeOfTest methods #1425 --- src/test/java/org/cactoos/iterable/RangeOfTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/cactoos/iterable/RangeOfTest.java b/src/test/java/org/cactoos/iterable/RangeOfTest.java index f8b1876641..131459b5d8 100644 --- a/src/test/java/org/cactoos/iterable/RangeOfTest.java +++ b/src/test/java/org/cactoos/iterable/RangeOfTest.java @@ -38,10 +38,10 @@ * @checkstyle JavadocMethodCheck (500 lines) * @checkstyle MagicNumber (500 lines) */ -public class RangeOfTest { +final class RangeOfTest { @Test - public final void testIntegerRange() { + void testIntegerRange() { new Assertion<>( "Must generate a range of integers", new ListOf<>( @@ -52,7 +52,7 @@ public final void testIntegerRange() { } @Test - public final void testIntegerFibonacciRange() { + void testIntegerFibonacciRange() { new Assertion<>( "Must generate a range of fibonacci integers", new ListOf<>( @@ -76,7 +76,7 @@ public Integer apply( } @Test - public final void testLongRange() { + void testLongRange() { new Assertion<>( "Must generate a range of long", new ListOf<>( @@ -87,7 +87,7 @@ public final void testLongRange() { } @Test - public final void testCharacterRange() { + void testCharacterRange() { new Assertion<>( "Must generate a range of characters", new ListOf<>( @@ -98,7 +98,7 @@ public final void testCharacterRange() { } @Test - public final void testLocalDateRange() { + void testLocalDateRange() { new Assertion<>( "Must generate a range of local dates.", new ListOf<>( From 62345fc1ba3db7fdfd562ba1a7ad32fab3cbb5e1 Mon Sep 17 00:00:00 2001 From: "Olivier B. OURA" Date: Thu, 11 Mar 2021 22:50:48 +0000 Subject: [PATCH 25/29] retrigger checks From fad022562442a30375387ee52cffe0085a1e8260 Mon Sep 17 00:00:00 2001 From: "Olivier B. OURA" Date: Sat, 13 Mar 2021 17:45:04 +0000 Subject: [PATCH 26/29] Add todo to each package #1533 --- src/main/java/org/cactoos/experimental/package-info.java | 2 ++ src/main/java/org/cactoos/func/package-info.java | 2 ++ src/main/java/org/cactoos/io/package-info.java | 2 ++ src/main/java/org/cactoos/iterable/package-info.java | 2 ++ src/main/java/org/cactoos/iterator/package-info.java | 2 ++ src/main/java/org/cactoos/list/package-info.java | 2 ++ src/main/java/org/cactoos/map/package-info.java | 2 ++ src/main/java/org/cactoos/proc/package-info.java | 2 ++ src/main/java/org/cactoos/scalar/package-info.java | 2 ++ src/main/java/org/cactoos/set/package-info.java | 2 ++ src/main/java/org/cactoos/text/package-info.java | 2 ++ src/main/java/org/cactoos/time/package-info.java | 2 ++ 12 files changed, 24 insertions(+) diff --git a/src/main/java/org/cactoos/experimental/package-info.java b/src/main/java/org/cactoos/experimental/package-info.java index 08b10d8559..4884d28f39 100644 --- a/src/main/java/org/cactoos/experimental/package-info.java +++ b/src/main/java/org/cactoos/experimental/package-info.java @@ -33,5 +33,7 @@ * whatsoever. * * @since 1.0.0 + * @todo #1533:30min Exploit generic variance for this package to ensure + * typing works as best as possible as it is explained in #1533 issue. */ package org.cactoos.experimental; diff --git a/src/main/java/org/cactoos/func/package-info.java b/src/main/java/org/cactoos/func/package-info.java index 74e16ae250..47bdc6f118 100644 --- a/src/main/java/org/cactoos/func/package-info.java +++ b/src/main/java/org/cactoos/func/package-info.java @@ -26,5 +26,7 @@ * Functions. * * @since 0.1 + * @todo #1533:30min Exploit generic variance for this package to ensure + * typing works as best as possible as it is explained in #1533 issue. */ package org.cactoos.func; diff --git a/src/main/java/org/cactoos/io/package-info.java b/src/main/java/org/cactoos/io/package-info.java index 78ff9bf66d..190e8e642d 100644 --- a/src/main/java/org/cactoos/io/package-info.java +++ b/src/main/java/org/cactoos/io/package-info.java @@ -26,5 +26,7 @@ * Input/Output. * * @since 0.1 + * @todo #1533:30min Exploit generic variance for this package to ensure + * typing works as best as possible as it is explained in #1533 issue. */ package org.cactoos.io; diff --git a/src/main/java/org/cactoos/iterable/package-info.java b/src/main/java/org/cactoos/iterable/package-info.java index b73ade1c57..0fd74ce891 100644 --- a/src/main/java/org/cactoos/iterable/package-info.java +++ b/src/main/java/org/cactoos/iterable/package-info.java @@ -26,5 +26,7 @@ * Iterables. * * @since 0.12 + * @todo #1533:30min Exploit generic variance for this package to ensure + * typing works as best as possible as it is explained in #1533 issue. */ package org.cactoos.iterable; diff --git a/src/main/java/org/cactoos/iterator/package-info.java b/src/main/java/org/cactoos/iterator/package-info.java index ea229f94da..e19a8968ae 100644 --- a/src/main/java/org/cactoos/iterator/package-info.java +++ b/src/main/java/org/cactoos/iterator/package-info.java @@ -26,5 +26,7 @@ * Iterators. * * @since 0.12 + * @todo #1533:30min Exploit generic variance for this package to ensure + * typing works as best as possible as it is explained in #1533 issue. */ package org.cactoos.iterator; diff --git a/src/main/java/org/cactoos/list/package-info.java b/src/main/java/org/cactoos/list/package-info.java index a80715ab6f..a47bcbf5a8 100644 --- a/src/main/java/org/cactoos/list/package-info.java +++ b/src/main/java/org/cactoos/list/package-info.java @@ -26,5 +26,7 @@ * Lists, tests. * * @since 0.14 + * @todo #1533:30min Exploit generic variance for this package to ensure + * typing works as best as possible as it is explained in #1533 issue. */ package org.cactoos.list; diff --git a/src/main/java/org/cactoos/map/package-info.java b/src/main/java/org/cactoos/map/package-info.java index 143718da45..ef5c12d369 100644 --- a/src/main/java/org/cactoos/map/package-info.java +++ b/src/main/java/org/cactoos/map/package-info.java @@ -26,5 +26,7 @@ * Maps. * * @since 0.14 + * @todo #1533:30min Exploit generic variance for this package to ensure + * typing works as best as possible as it is explained in #1533 issue. */ package org.cactoos.map; diff --git a/src/main/java/org/cactoos/proc/package-info.java b/src/main/java/org/cactoos/proc/package-info.java index b5b8b83a7a..dbe0483bd7 100644 --- a/src/main/java/org/cactoos/proc/package-info.java +++ b/src/main/java/org/cactoos/proc/package-info.java @@ -26,5 +26,7 @@ * Procedures. * * @since 0.47 + * @todo #1533:30min Exploit generic variance for this package to ensure + * typing works as best as possible as it is explained in #1533 issue. */ package org.cactoos.proc; diff --git a/src/main/java/org/cactoos/scalar/package-info.java b/src/main/java/org/cactoos/scalar/package-info.java index 6099baceb9..8b0f6b2baf 100644 --- a/src/main/java/org/cactoos/scalar/package-info.java +++ b/src/main/java/org/cactoos/scalar/package-info.java @@ -26,5 +26,7 @@ * Scalars. * * @since 0.12 + * @todo #1533:30min Exploit generic variance for this package to ensure + * typing works as best as possible as it is explained in #1533 issue. */ package org.cactoos.scalar; diff --git a/src/main/java/org/cactoos/set/package-info.java b/src/main/java/org/cactoos/set/package-info.java index fdc4dfbbcc..44f2c5482a 100644 --- a/src/main/java/org/cactoos/set/package-info.java +++ b/src/main/java/org/cactoos/set/package-info.java @@ -26,5 +26,7 @@ * Sets. * * @since 0.49.2 + * @todo #1533:30min Exploit generic variance for this package to ensure + * typing works as best as possible as it is explained in #1533 issue. */ package org.cactoos.set; diff --git a/src/main/java/org/cactoos/text/package-info.java b/src/main/java/org/cactoos/text/package-info.java index ad4793671b..e594647748 100644 --- a/src/main/java/org/cactoos/text/package-info.java +++ b/src/main/java/org/cactoos/text/package-info.java @@ -26,5 +26,7 @@ * Text. * * @since 0.1 + * @todo #1533:30min Exploit generic variance for this package to ensure + * typing works as best as possible as it is explained in #1533 issue. */ package org.cactoos.text; diff --git a/src/main/java/org/cactoos/time/package-info.java b/src/main/java/org/cactoos/time/package-info.java index 037f77c709..a4a7691793 100644 --- a/src/main/java/org/cactoos/time/package-info.java +++ b/src/main/java/org/cactoos/time/package-info.java @@ -26,5 +26,7 @@ * Time. * * @since 1.0 + * @todo #1533:30min Exploit generic variance for this package to ensure + * typing works as best as possible as it is explained in #1533 issue. */ package org.cactoos.time; From 1b6841aba4735743d82742d9a9737aec4a84ce97 Mon Sep 17 00:00:00 2001 From: "Olivier B. OURA" Date: Sat, 13 Mar 2021 18:10:04 +0000 Subject: [PATCH 27/29] Use Throws in CheckedTest #1533 --- .../java/org/cactoos/scalar/CheckedTest.java | 86 +++++++++++-------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/src/test/java/org/cactoos/scalar/CheckedTest.java b/src/test/java/org/cactoos/scalar/CheckedTest.java index 9594799bcf..3df5d9a7e6 100644 --- a/src/test/java/org/cactoos/scalar/CheckedTest.java +++ b/src/test/java/org/cactoos/scalar/CheckedTest.java @@ -26,39 +26,48 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.nio.channels.AcceptPendingException; -import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.hamcrest.core.IsEqual; import org.junit.Test; import org.llorllale.cactoos.matchers.Assertion; +import org.llorllale.cactoos.matchers.Throws; /** * Test case for {@link Checked}. * @since 0.30 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ public final class CheckedTest { - @Test(expected = IllegalStateException.class) + @Test public void runtimeExceptionGoesOut() throws Exception { - new Checked<>( - () -> { - throw new IllegalStateException("runtime"); - }, - IOException::new - ).value(); + new Assertion<>( + "Must throw runtime exception", + () -> new Checked<>( + () -> { + throw new IllegalStateException("runtime"); + }, + IOException::new + ).value(), + new Throws<>(IllegalStateException.class) + ).affirm(); } - @Test(expected = IllegalStateException.class) + @Test public void usesGenericVarianceOnExceptionTypes() throws Exception { - new Checked( - () -> { - throw new IllegalStateException(); - }, - (Throwable ex) -> { - return new AcceptPendingException(); - } - ).value(); + new Assertion<>( + "Must use generic variance on exception types", + () -> new Checked( + () -> { + throw new IllegalStateException(); + }, + (Throwable ex) -> { + return new AcceptPendingException(); + } + ).value(), + new Throws<>(IllegalStateException.class) + ).affirm(); } @SuppressWarnings("PMD.AvoidDuplicateLiterals") @@ -73,14 +82,18 @@ public void usesContravarianceOnResultType() throws Exception { ); } - @Test(expected = IOException.class) + @Test public void throwsIoException() throws Exception { - new Checked<>( - () -> { - throw new InterruptedException("interrupt"); - }, - IOException::new - ).value(); + new Assertion<>( + "Must throw io exception", + () -> new Checked<>( + () -> { + throw new InterruptedException("interrupt"); + }, + IOException::new + ).value(), + new Throws<>(IOException.class) + ).affirm(); } @Test @@ -93,10 +106,11 @@ public void ioExceptionGoesOut() throws Exception { IOException::new ).value(); } catch (final IOException exp) { - MatcherAssert.assertThat( + new Assertion<>( + "Must not have cause when throwing io exception", exp.getCause(), Matchers.nullValue() - ); + ).affirm(); } } @@ -110,28 +124,26 @@ public void fileNotFoundExceptionGoesOut() throws Exception { IOException::new ).value(); } catch (final FileNotFoundException exp) { - MatcherAssert.assertThat( + new Assertion<>( + "Must not have cause when throwing file not found exception", exp.getCause(), Matchers.nullValue() - ); + ).affirm(); } } @Test public void throwsIoExceptionWithModifiedMessage() throws Exception { final String message = "error msg"; - try { - new Checked<>( + new Assertion<>( + "Must throw io exception with modified message", + () -> new Checked<>( () -> { throw new IOException("io"); }, exp -> new IOException(message, exp) - ).value(); - } catch (final IOException exp) { - MatcherAssert.assertThat( - exp.getMessage(), - Matchers.containsString(message) - ); - } + ).value(), + new Throws<>(message, IOException.class) + ).affirm(); } } From a19013c90191f6cadadd61a107c4429f415d2685 Mon Sep 17 00:00:00 2001 From: "Olivier B. OURA" Date: Sat, 13 Mar 2021 18:23:14 +0000 Subject: [PATCH 28/29] Respect Puzzle compliance #1533 --- src/main/java/org/cactoos/experimental/package-info.java | 4 ++-- src/main/java/org/cactoos/func/package-info.java | 5 +++-- src/main/java/org/cactoos/io/package-info.java | 5 +++-- src/main/java/org/cactoos/iterable/package-info.java | 5 +++-- src/main/java/org/cactoos/iterator/package-info.java | 5 +++-- src/main/java/org/cactoos/list/package-info.java | 5 +++-- src/main/java/org/cactoos/map/package-info.java | 5 +++-- src/main/java/org/cactoos/proc/package-info.java | 5 +++-- src/main/java/org/cactoos/scalar/package-info.java | 5 +++-- src/main/java/org/cactoos/set/package-info.java | 5 +++-- src/main/java/org/cactoos/text/package-info.java | 5 +++-- src/main/java/org/cactoos/time/package-info.java | 5 +++-- 12 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/cactoos/experimental/package-info.java b/src/main/java/org/cactoos/experimental/package-info.java index 4884d28f39..178d0ee6ed 100644 --- a/src/main/java/org/cactoos/experimental/package-info.java +++ b/src/main/java/org/cactoos/experimental/package-info.java @@ -33,7 +33,7 @@ * whatsoever. * * @since 1.0.0 - * @todo #1533:30min Exploit generic variance for this package to ensure - * typing works as best as possible as it is explained in #1533 issue. + * @todo #1533:30min Exploit generic variance for package org.cactoos.experimental + * to ensure typing works as best as possible as it is explained in #1533 issue. */ package org.cactoos.experimental; diff --git a/src/main/java/org/cactoos/func/package-info.java b/src/main/java/org/cactoos/func/package-info.java index 47bdc6f118..b146ca2baa 100644 --- a/src/main/java/org/cactoos/func/package-info.java +++ b/src/main/java/org/cactoos/func/package-info.java @@ -26,7 +26,8 @@ * Functions. * * @since 0.1 - * @todo #1533:30min Exploit generic variance for this package to ensure - * typing works as best as possible as it is explained in #1533 issue. + * @todo #1533:30min Exploit generic variance for package org.cactoos.func + * to ensure typing works as best as possible as it is explained in + * #1533 issue. */ package org.cactoos.func; diff --git a/src/main/java/org/cactoos/io/package-info.java b/src/main/java/org/cactoos/io/package-info.java index 190e8e642d..839f999b56 100644 --- a/src/main/java/org/cactoos/io/package-info.java +++ b/src/main/java/org/cactoos/io/package-info.java @@ -26,7 +26,8 @@ * Input/Output. * * @since 0.1 - * @todo #1533:30min Exploit generic variance for this package to ensure - * typing works as best as possible as it is explained in #1533 issue. + * @todo #1533:30min Exploit generic variance for package org.cactoos.io + * to ensure typing works as best as possible as it is explained in + * #1533 issue. */ package org.cactoos.io; diff --git a/src/main/java/org/cactoos/iterable/package-info.java b/src/main/java/org/cactoos/iterable/package-info.java index 0fd74ce891..08886ae1a9 100644 --- a/src/main/java/org/cactoos/iterable/package-info.java +++ b/src/main/java/org/cactoos/iterable/package-info.java @@ -26,7 +26,8 @@ * Iterables. * * @since 0.12 - * @todo #1533:30min Exploit generic variance for this package to ensure - * typing works as best as possible as it is explained in #1533 issue. + * @todo #1533:30min Exploit generic variance for package org.cactoos.iterable + * to ensure typing works as best as possible as it is explained in + * #1533 issue. */ package org.cactoos.iterable; diff --git a/src/main/java/org/cactoos/iterator/package-info.java b/src/main/java/org/cactoos/iterator/package-info.java index e19a8968ae..6e8f3e4ebc 100644 --- a/src/main/java/org/cactoos/iterator/package-info.java +++ b/src/main/java/org/cactoos/iterator/package-info.java @@ -26,7 +26,8 @@ * Iterators. * * @since 0.12 - * @todo #1533:30min Exploit generic variance for this package to ensure - * typing works as best as possible as it is explained in #1533 issue. + * @todo #1533:30min Exploit generic variance for package org.cactoos.iterator + * to ensure typing works as best as possible as it is explained in + * #1533 issue. */ package org.cactoos.iterator; diff --git a/src/main/java/org/cactoos/list/package-info.java b/src/main/java/org/cactoos/list/package-info.java index a47bcbf5a8..cea27fff1e 100644 --- a/src/main/java/org/cactoos/list/package-info.java +++ b/src/main/java/org/cactoos/list/package-info.java @@ -26,7 +26,8 @@ * Lists, tests. * * @since 0.14 - * @todo #1533:30min Exploit generic variance for this package to ensure - * typing works as best as possible as it is explained in #1533 issue. + * @todo #1533:30min Exploit generic variance for package org.cactoos.list + * to ensure typing works as best as possible as it is explained in + * #1533 issue. */ package org.cactoos.list; diff --git a/src/main/java/org/cactoos/map/package-info.java b/src/main/java/org/cactoos/map/package-info.java index ef5c12d369..568e46fc76 100644 --- a/src/main/java/org/cactoos/map/package-info.java +++ b/src/main/java/org/cactoos/map/package-info.java @@ -26,7 +26,8 @@ * Maps. * * @since 0.14 - * @todo #1533:30min Exploit generic variance for this package to ensure - * typing works as best as possible as it is explained in #1533 issue. + * @todo #1533:30min Exploit generic variance for package org.cactoos.map + * to ensure typing works as best as possible as it is explained in + * #1533 issue. */ package org.cactoos.map; diff --git a/src/main/java/org/cactoos/proc/package-info.java b/src/main/java/org/cactoos/proc/package-info.java index dbe0483bd7..cf9ad79084 100644 --- a/src/main/java/org/cactoos/proc/package-info.java +++ b/src/main/java/org/cactoos/proc/package-info.java @@ -26,7 +26,8 @@ * Procedures. * * @since 0.47 - * @todo #1533:30min Exploit generic variance for this package to ensure - * typing works as best as possible as it is explained in #1533 issue. + * @todo #1533:30min Exploit generic variance for package org.cactoos.proc + * to ensure typing works as best as possible as it is explained in + * #1533 issue. */ package org.cactoos.proc; diff --git a/src/main/java/org/cactoos/scalar/package-info.java b/src/main/java/org/cactoos/scalar/package-info.java index 8b0f6b2baf..c1cc9e0c33 100644 --- a/src/main/java/org/cactoos/scalar/package-info.java +++ b/src/main/java/org/cactoos/scalar/package-info.java @@ -26,7 +26,8 @@ * Scalars. * * @since 0.12 - * @todo #1533:30min Exploit generic variance for this package to ensure - * typing works as best as possible as it is explained in #1533 issue. + * @todo #1533:30min Exploit generic variance for package org.cactoos.scalar + * to ensure typing works as best as possible as it is explained in + * #1533 issue. */ package org.cactoos.scalar; diff --git a/src/main/java/org/cactoos/set/package-info.java b/src/main/java/org/cactoos/set/package-info.java index 44f2c5482a..e78ef249ee 100644 --- a/src/main/java/org/cactoos/set/package-info.java +++ b/src/main/java/org/cactoos/set/package-info.java @@ -26,7 +26,8 @@ * Sets. * * @since 0.49.2 - * @todo #1533:30min Exploit generic variance for this package to ensure - * typing works as best as possible as it is explained in #1533 issue. + * @todo #1533:30min Exploit generic variance for package org.cactoos.set + * to ensure typing works as best as possible as it is explained in + * #1533 issue. */ package org.cactoos.set; diff --git a/src/main/java/org/cactoos/text/package-info.java b/src/main/java/org/cactoos/text/package-info.java index e594647748..85e90bdcbe 100644 --- a/src/main/java/org/cactoos/text/package-info.java +++ b/src/main/java/org/cactoos/text/package-info.java @@ -26,7 +26,8 @@ * Text. * * @since 0.1 - * @todo #1533:30min Exploit generic variance for this package to ensure - * typing works as best as possible as it is explained in #1533 issue. + * @todo #1533:30min Exploit generic variance for package org.cactoos.text + * to ensure typing works as best as possible as it is explained in + * #1533 issue. */ package org.cactoos.text; diff --git a/src/main/java/org/cactoos/time/package-info.java b/src/main/java/org/cactoos/time/package-info.java index a4a7691793..2c77a68afa 100644 --- a/src/main/java/org/cactoos/time/package-info.java +++ b/src/main/java/org/cactoos/time/package-info.java @@ -26,7 +26,8 @@ * Time. * * @since 1.0 - * @todo #1533:30min Exploit generic variance for this package to ensure - * typing works as best as possible as it is explained in #1533 issue. + * @todo #1533:30min Exploit generic variance for package org.cactoos.time + * to ensure typing works as best as possible as it is explained in + * #1533 issue. */ package org.cactoos.time; From 297f62637cae056e9dd9e68886c45c985d5fc295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20No=C3=ABl?= Date: Sun, 14 Mar 2021 10:53:53 +0100 Subject: [PATCH 29/29] use latest codecov-action --- .github/workflows/build.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 26c6c5d34f..b91bcd0e2d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -22,9 +22,8 @@ jobs: - name: Test run: mvn clean test --errors --batch-mode - name: CodeCov - uses: codecov/codecov-action@v1.0.13 + uses: codecov/codecov-action@v1 if: matrix.os == 'ubuntu-18.04' && matrix.java == 8 && github.repository == 'yegor256/cactoos' - timeout-minutes: 10 with: file: target/coverage/jacoco.xml