diff --git a/src/main/java/org/cactoos/func/FuncWithFallback.java b/src/main/java/org/cactoos/func/FuncWithFallback.java index ad7e7b0bbc..9667a3d91b 100644 --- a/src/main/java/org/cactoos/func/FuncWithFallback.java +++ b/src/main/java/org/cactoos/func/FuncWithFallback.java @@ -23,9 +23,7 @@ */ package org.cactoos.func; -import java.util.concurrent.Callable; import org.cactoos.Func; -import org.cactoos.Proc; /** * Func with a fallback plan. @@ -65,219 +63,6 @@ public FuncWithFallback(final Func fnc, this(fnc, fbk, input -> input); } - /** - * Ctor. - * @param proc The proc - * @param fbk The fallback - * @since 0.12 - */ - public FuncWithFallback(final Proc proc, - final Func fbk) { - this(new FuncOf<>(proc), fbk); - } - - /** - * Ctor. - * @param proc The proc - * @param fbk The fallback - * @since 0.12 - */ - public FuncWithFallback(final Proc proc, - final Proc fbk) { - this(new FuncOf<>(proc), new FuncOf<>(fbk)); - } - - /** - * Ctor. - * @param proc The proc - * @param fbk The fallback - * @since 0.12 - */ - public FuncWithFallback(final Func proc, - final Proc fbk) { - this(proc, new FuncOf<>(fbk)); - } - - /** - * Ctor. - * @param fnc The func - * @param fbk The fallback - * @param flw The follow up func - * @since 0.12 - */ - public FuncWithFallback(final Func fnc, - final Proc fbk, final Func flw) { - this(fnc, new FuncOf<>(fbk), flw); - } - - /** - * Ctor. - * @param fnc The func - * @param fbk The fallback - * @param flw The follow up func - * @since 0.12 - */ - public FuncWithFallback(final Func fnc, - final Proc fbk, final Proc flw) { - this(fnc, new FuncOf<>(fbk), new FuncOf<>(flw)); - } - - /** - * Ctor. - * @param fnc The func - * @param fbk The fallback - * @param flw The follow up func - * @since 0.12 - */ - public FuncWithFallback(final Func fnc, - final Func fbk, final Proc flw) { - this(fnc, fbk, new FuncOf<>(flw)); - } - - /** - * Ctor. - * @param fnc The func - * @param fbk The fallback - * @param flw The follow up func - * @since 0.12 - */ - public FuncWithFallback(final Proc fnc, - final Func fbk, final Func flw) { - this(new FuncOf<>(fnc), fbk, flw); - } - - /** - * Ctor. - * @param fnc The func - * @param fbk The fallback - * @param flw The follow up func - * @since 0.12 - */ - public FuncWithFallback(final Proc fnc, - final Proc fbk, final Func flw) { - this(new FuncOf<>(fnc), fbk, flw); - } - - /** - * Ctor. - * @param fnc The func - * @param fbk The fallback - * @param flw The follow up func - * @since 0.12 - */ - public FuncWithFallback(final Proc fnc, - final Proc fbk, final Proc flw) { - this(new FuncOf<>(fnc), fbk, flw); - } - - /** - * Ctor. - * @param fnc The func - * @param fbk The fallback - * @param flw The follow up func - * @since 0.12 - */ - public FuncWithFallback(final Proc fnc, - final Func fbk, final Proc flw) { - this(new FuncOf<>(fnc), fbk, flw); - } - - /** - * Ctor. - * @param fnc The func - * @param fbk The fallback - * @param flw The follow up func - * @since 0.12 - */ - public FuncWithFallback(final Callable fnc, - final Func fbk, final Func flw) { - this(new FuncOf<>(fnc), fbk, flw); - } - - /** - * Ctor. - * @param fnc The func - * @param fbk The fallback - * @param flw The follow up func - * @since 0.12 - */ - public FuncWithFallback(final Callable fnc, - final Proc fbk, final Func flw) { - this(new FuncOf<>(fnc), fbk, flw); - } - - /** - * Ctor. - * @param fnc The func - * @param fbk The fallback - * @param flw The follow up func - * @since 0.12 - */ - public FuncWithFallback(final Callable fnc, - final Proc fbk, final Proc flw) { - this(new FuncOf<>(fnc), fbk, flw); - } - - /** - * Ctor. - * @param fnc The func - * @param fbk The fallback - * @param flw The follow up func - * @since 0.12 - */ - public FuncWithFallback(final Callable fnc, - final Func fbk, final Proc flw) { - this(new FuncOf<>(fnc), fbk, flw); - } - - /** - * Ctor. - * @param fnc The func - * @param fbk The fallback - * @param flw The follow up func - * @since 0.12 - */ - public FuncWithFallback(final Runnable fnc, - final Func fbk, final Proc flw) { - this(new FuncOf<>(fnc), fbk, flw); - } - - /** - * Ctor. - * @param fnc The func - * @param fbk The fallback - * @param flw The follow up func - * @since 0.12 - */ - public FuncWithFallback(final Runnable fnc, - final Func fbk, final Func flw) { - this(new FuncOf<>(fnc), fbk, flw); - } - - /** - * Ctor. - * @param fnc The func - * @param fbk The fallback - * @param flw The follow up func - * @since 0.12 - */ - public FuncWithFallback(final Runnable fnc, - final Proc fbk, final Func flw) { - this(new FuncOf<>(fnc), fbk, flw); - } - - /** - * Ctor. - * @param fnc The func - * @param fbk The fallback - * @param flw The follow up func - * @since 0.12 - */ - public FuncWithFallback(final Runnable fnc, - final Proc fbk, final Proc flw) { - this(new FuncOf<>(fnc), fbk, flw); - } - /** * Ctor. * @param fnc The func diff --git a/src/main/java/org/cactoos/io/WriterTo.java b/src/main/java/org/cactoos/io/WriterTo.java index 7a627e8b83..067081bacf 100644 --- a/src/main/java/org/cactoos/io/WriterTo.java +++ b/src/main/java/org/cactoos/io/WriterTo.java @@ -71,7 +71,7 @@ public WriterTo(final File file) { /** * Ctor. - * @param stream The outpup + * @param stream The output */ public WriterTo(final OutputStream stream) { this(new OutputTo(stream)); diff --git a/src/main/java/org/cactoos/text/HexOf.java b/src/main/java/org/cactoos/text/HexOf.java new file mode 100644 index 0000000000..d93221ec67 --- /dev/null +++ b/src/main/java/org/cactoos/text/HexOf.java @@ -0,0 +1,81 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.text; + +import java.io.IOException; +import org.cactoos.Bytes; +import org.cactoos.Text; + +/** + * Hexadecimal representation of Bytes. + * + *

There is no thread-safety guarantee. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.28 + */ +@SuppressWarnings("PMD.ConstructorShouldDoInitialization") +public final class HexOf implements Text { + + /** + * The hexadecimal chars. + */ + private static final char[] HEX_CHARS = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', + }; + + /** + * The Bytes. + */ + private final Bytes bytes; + + /** + * Ctor. + * @param source The bytes + */ + public HexOf(final Bytes source) { + this.bytes = source; + } + + @Override + public String asString() throws IOException { + final byte[] bts = this.bytes.asBytes(); + final char[] hex = new char[bts.length * 2]; + int chr = -1; + for (int idx = 0; idx < bts.length; ++idx) { + // @checkstyle MagicNumber (3 line) + final int value = 0xff & bts[idx]; + hex[++chr] = HexOf.HEX_CHARS[value >>> 4]; + hex[++chr] = HexOf.HEX_CHARS[value & 0x0f]; + } + return new String(hex); + } + + @Override + public int compareTo(final Text text) { + return new UncheckedText(this).compareTo(text); + } +} diff --git a/src/test/java/org/cactoos/collection/FilteredTest.java b/src/test/java/org/cactoos/collection/FilteredTest.java index 3959a56092..f9bb1b56fb 100644 --- a/src/test/java/org/cactoos/collection/FilteredTest.java +++ b/src/test/java/org/cactoos/collection/FilteredTest.java @@ -45,7 +45,7 @@ public final class FilteredTest { public void behavesAsCollection() throws Exception { MatcherAssert.assertThat( "Can't behave as a collection", - new Filtered<>(i -> i < 2, 1, 2, 0, -1), + new Filtered(i -> i < 2, 1, 2, 0, -1), new BehavesAsCollection<>(-1) ); } diff --git a/src/test/java/org/cactoos/func/FuncWithFallbackTest.java b/src/test/java/org/cactoos/func/FuncWithFallbackTest.java index a063cad446..fefb98dc9e 100644 --- a/src/test/java/org/cactoos/func/FuncWithFallbackTest.java +++ b/src/test/java/org/cactoos/func/FuncWithFallbackTest.java @@ -25,7 +25,6 @@ import java.io.IOException; import org.cactoos.FuncApplies; -import org.cactoos.Proc; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; @@ -53,11 +52,11 @@ public void usesMainFunc() throws Exception { } @Test - public void usesCallback() throws Exception { + public void usesFallback() throws Exception { MatcherAssert.assertThat( "Can't use the callback in case of exception", new FuncWithFallback<>( - (Proc) input -> { + input -> { throw new IOException("Failure"); }, ex -> "Never mind" diff --git a/src/test/java/org/cactoos/text/HexOfTest.java b/src/test/java/org/cactoos/text/HexOfTest.java new file mode 100644 index 0000000000..2dbf4c40c1 --- /dev/null +++ b/src/test/java/org/cactoos/text/HexOfTest.java @@ -0,0 +1,62 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.text; + +import java.io.IOException; +import org.cactoos.TextHasString; +import org.cactoos.io.BytesOf; +import org.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test case for {@link HexOf}. + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.28 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class HexOfTest { + + @Test + public void empytString() { + MatcherAssert.assertThat( + "Can't represent an empty string as hexadecimal", + new HexOf( + new BytesOf("") + ), + new TextHasString("") + ); + } + + @Test + public void notEmpytString() throws IOException { + MatcherAssert.assertThat( + "Can't represent a string as hexadecimal", + new HexOf( + new BytesOf("What's up, друг?") + ), + new TextHasString("5768617427732075702c20d0b4d180d183d0b33f") + ); + } +} diff --git a/src/test/java/org/cactoos/time/ZonedDateTimeOfTest.java b/src/test/java/org/cactoos/time/ZonedDateTimeOfTest.java index f26d6fa32c..18b77f73ef 100644 --- a/src/test/java/org/cactoos/time/ZonedDateTimeOfTest.java +++ b/src/test/java/org/cactoos/time/ZonedDateTimeOfTest.java @@ -25,11 +25,11 @@ import java.time.LocalDateTime; import java.time.ZoneId; +import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.Ignore; import org.junit.Test; /** @@ -43,18 +43,15 @@ @SuppressWarnings("PMD.AvoidDuplicateLiterals") public class ZonedDateTimeOfTest { - // @todo #504:30min This test fails for some reason and I can't - // understand what's wrong. Seems to be some issue with the Zone - // we are not detecting in the text. @Test - @Ignore public final void testParsingIsoFormattedStringToZonedDateTime() { MatcherAssert.assertThat( "Can't parse a ZonedDateTime with default/ISO format.", new ZonedDateTimeOf("2017-12-13T14:15:16.000000017+01:00").value(), Matchers.equalTo( ZonedDateTime.of( - 2017, 12, 13, 14, 15, 16, 17, ZoneId.of("Europe/Berlin") + 2017, 12, 13, 14, 15, 16, 17, + ZoneId.ofOffset("", ZoneOffset.ofHours(1)) ) ) );