From 5d209f00a04513ac2d06fa45dd481029b3e5f058 Mon Sep 17 00:00:00 2001 From: Kirill Date: Thu, 28 Sep 2017 11:45:06 +0400 Subject: [PATCH 01/12] readme: questions --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 5049a816e9..17e9b6868a 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,10 @@ Cactoos | Guava | Apache Commons | JDK 8 `TrimmedText` | - | `StringUtils.stripAll()` | `String#trim()` `UpperText` | - | - | `String#toUpperCase()` +## Questions + +Ask your questions related to cactoos library on [Stackoverflow](https://stackoverflow.com/questions/ask) with [cactoos](https://stackoverflow.com/tags/cactoos/info) tag. + ## How to contribute? Just fork the repo and send us a pull request. From a1f59b5808fe0a2b8985400b897d6f3314c077e9 Mon Sep 17 00:00:00 2001 From: smallcreep Date: Sun, 22 Oct 2017 21:55:24 +0300 Subject: [PATCH 02/12] cactoos-451 Base64 Encode and Decode Added Base64 Encode for Bytes --- .../java/org/cactoos/bytes/BytesBase64.java | 57 ++++++++++++++++++ .../java/org/cactoos/bytes/package-info.java | 32 ++++++++++ .../org/cactoos/bytes/BytesBase64Test.java | 60 +++++++++++++++++++ .../java/org/cactoos/bytes/package-info.java | 32 ++++++++++ 4 files changed, 181 insertions(+) create mode 100644 src/main/java/org/cactoos/bytes/BytesBase64.java create mode 100644 src/main/java/org/cactoos/bytes/package-info.java create mode 100644 src/test/java/org/cactoos/bytes/BytesBase64Test.java create mode 100644 src/test/java/org/cactoos/bytes/package-info.java diff --git a/src/main/java/org/cactoos/bytes/BytesBase64.java b/src/main/java/org/cactoos/bytes/BytesBase64.java new file mode 100644 index 0000000000..7208ca1dc7 --- /dev/null +++ b/src/main/java/org/cactoos/bytes/BytesBase64.java @@ -0,0 +1,57 @@ +/** + * 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.bytes; + +import java.io.IOException; +import java.util.Base64; +import org.cactoos.Bytes; + +/** + * Encodes all origin bytes using the Base64 encoding scheme. + * + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.20.2 + */ +public final class BytesBase64 implements Bytes { + + /** + * Origin bytes. + */ + private final Bytes origin; + + /** + * Ctor. + * @param origin Origin bytes. + */ + public BytesBase64(final Bytes origin) { + this.origin = origin; + } + + @Override + public byte[] asBytes() throws IOException { + return Base64.getEncoder().encode(this.origin.asBytes()); + } +} diff --git a/src/main/java/org/cactoos/bytes/package-info.java b/src/main/java/org/cactoos/bytes/package-info.java new file mode 100644 index 0000000000..220878a550 --- /dev/null +++ b/src/main/java/org/cactoos/bytes/package-info.java @@ -0,0 +1,32 @@ +/** + * 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. + */ + +/** + * Bytes. + * + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.20.2 + */ +package org.cactoos.bytes; diff --git a/src/test/java/org/cactoos/bytes/BytesBase64Test.java b/src/test/java/org/cactoos/bytes/BytesBase64Test.java new file mode 100644 index 0000000000..97aee47bfc --- /dev/null +++ b/src/test/java/org/cactoos/bytes/BytesBase64Test.java @@ -0,0 +1,60 @@ +/** + * 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.bytes; + +import java.io.IOException; +import org.cactoos.io.BytesOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link org.cactoos.bytes.BytesBase64}. + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.20.2 + */ +public final class BytesBase64Test { + + /** + * Check bytes encodes using the Base64 encoding scheme. + * @throws IOException If fails. + */ + @Test + public void checkEncode() throws IOException { + MatcherAssert.assertThat( + "Can't encodes bytes using the Base64 encoding scheme", + new BytesBase64( + new BytesOf( + "Hello!" + ) + ).asBytes(), + Matchers.equalTo( + new BytesOf("SGVsbG8h").asBytes() + ) + ); + } + +} diff --git a/src/test/java/org/cactoos/bytes/package-info.java b/src/test/java/org/cactoos/bytes/package-info.java new file mode 100644 index 0000000000..45db840f10 --- /dev/null +++ b/src/test/java/org/cactoos/bytes/package-info.java @@ -0,0 +1,32 @@ +/** + * 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. + */ + +/** + * Bytes, tests. + * + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.20.2 + */ +package org.cactoos.bytes; From f1ff4da6c4e67ac33c3da1d77cb1bb7d867af005 Mon Sep 17 00:00:00 2001 From: smallcreep Date: Sun, 22 Oct 2017 22:07:26 +0300 Subject: [PATCH 03/12] cactoos-451 Base64 Encode and Decode Added Base64 Encode for Text --- .../java/org/cactoos/text/TextBase64.java | 77 +++++++++++++++++++ .../java/org/cactoos/text/TextBase64Test.java | 56 ++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/main/java/org/cactoos/text/TextBase64.java create mode 100644 src/test/java/org/cactoos/text/TextBase64Test.java diff --git a/src/main/java/org/cactoos/text/TextBase64.java b/src/main/java/org/cactoos/text/TextBase64.java new file mode 100644 index 0000000000..b153c37b6e --- /dev/null +++ b/src/main/java/org/cactoos/text/TextBase64.java @@ -0,0 +1,77 @@ +/** + * 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.Text; +import org.cactoos.bytes.BytesBase64; +import org.cactoos.io.BytesOf; + +/** + * Encodes the origin text using the Base64 encoding scheme. + * + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.20.2 + */ +public final class TextBase64 implements Text { + + /** + * Origin text. + */ + private final Text origin; + + /** + * Ctor. + * + * @param input The String + */ + public TextBase64(final String input) { + this(new TextOf(input)); + } + + /** + * Ctor. + * + * @param origin Origin text + */ + public TextBase64(final Text origin) { + this.origin = origin; + } + + @Override + public String asString() throws IOException { + return new TextOf( + new BytesBase64( + new BytesOf(this.origin) + ) + ).asString(); + } + + @Override + public int compareTo(final Text text) { + return new UncheckedText(this).compareTo(text); + } +} diff --git a/src/test/java/org/cactoos/text/TextBase64Test.java b/src/test/java/org/cactoos/text/TextBase64Test.java new file mode 100644 index 0000000000..0449728534 --- /dev/null +++ b/src/test/java/org/cactoos/text/TextBase64Test.java @@ -0,0 +1,56 @@ +/** + * 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.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test case for {@link org.cactoos.text.TextBase64}. + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.20.2 + */ +public final class TextBase64Test { + + /** + * Check text encodes using the Base64 encoding scheme. + * @throws IOException If fails. + */ + @Test + public void checkEncode() throws IOException { + MatcherAssert.assertThat( + "Can't encodes text using the Base64 encoding scheme", + new TextBase64( + "Hello!" + ), + new TextHasString( + "SGVsbG8h" + ) + ); + } +} From 974dbd2314194a66847dc61022e9c83a7c54ec4f Mon Sep 17 00:00:00 2001 From: smallcreep Date: Sun, 22 Oct 2017 22:15:39 +0300 Subject: [PATCH 04/12] cactoos-451 Base64 Encode and Decode Added Base64 Decode for Bytes --- .../java/org/cactoos/bytes/Base64Bytes.java | 58 ++++++++++++++++++ .../org/cactoos/bytes/Base64BytesTest.java | 61 +++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/main/java/org/cactoos/bytes/Base64Bytes.java create mode 100644 src/test/java/org/cactoos/bytes/Base64BytesTest.java diff --git a/src/main/java/org/cactoos/bytes/Base64Bytes.java b/src/main/java/org/cactoos/bytes/Base64Bytes.java new file mode 100644 index 0000000000..b8c27b7c3c --- /dev/null +++ b/src/main/java/org/cactoos/bytes/Base64Bytes.java @@ -0,0 +1,58 @@ +/** + * 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.bytes; + +import java.io.IOException; +import java.util.Base64; +import org.cactoos.Bytes; + +/** + * Decodes all origin bytes using the Base64 encoding scheme. + * + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.20.2 + */ +public final class Base64Bytes implements Bytes { + + /** + * Origin bytes. + */ + private final Bytes origin; + + /** + * Ctor. + * + * @param origin Origin bytes + */ + public Base64Bytes(final Bytes origin) { + this.origin = origin; + } + + @Override + public byte[] asBytes() throws IOException { + return Base64.getDecoder().decode(this.origin.asBytes()); + } +} diff --git a/src/test/java/org/cactoos/bytes/Base64BytesTest.java b/src/test/java/org/cactoos/bytes/Base64BytesTest.java new file mode 100644 index 0000000000..d6e07d71c8 --- /dev/null +++ b/src/test/java/org/cactoos/bytes/Base64BytesTest.java @@ -0,0 +1,61 @@ +/** + * 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.bytes; + +import java.io.IOException; +import org.cactoos.io.BytesOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link org.cactoos.bytes.Base64Bytes}. + * + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.20.2 + */ +public final class Base64BytesTest { + + /** + * Check bytes decodes using the Base64 encoding scheme. + * @throws IOException If fails. + */ + @Test + public void checkEncode() throws IOException { + MatcherAssert.assertThat( + "Can't decodes bytes using the Base64 encoding scheme", + new Base64Bytes( + new BytesOf( + "SGVsbG8h" + ) + ).asBytes(), + Matchers.equalTo( + new BytesOf("Hello!").asBytes() + ) + ); + } + +} From 517560cade6a88e39ebe9bdb1890235e08a34293 Mon Sep 17 00:00:00 2001 From: smallcreep Date: Sun, 22 Oct 2017 22:22:09 +0300 Subject: [PATCH 05/12] cactoos-451 Base64 Encode and Decode Added Base64 Decode for Text --- .../java/org/cactoos/text/Base64Text.java | 77 +++++++++++++++++++ .../org/cactoos/bytes/Base64BytesTest.java | 2 +- .../java/org/cactoos/text/Base64TextTest.java | 57 ++++++++++++++ 3 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/cactoos/text/Base64Text.java create mode 100644 src/test/java/org/cactoos/text/Base64TextTest.java diff --git a/src/main/java/org/cactoos/text/Base64Text.java b/src/main/java/org/cactoos/text/Base64Text.java new file mode 100644 index 0000000000..7d1bf0b9cb --- /dev/null +++ b/src/main/java/org/cactoos/text/Base64Text.java @@ -0,0 +1,77 @@ +/** + * 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.Text; +import org.cactoos.bytes.Base64Bytes; +import org.cactoos.io.BytesOf; + +/** + * Decodes the origin text using the Base64 encoding scheme. + * + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.20.2 + */ +public final class Base64Text implements Text { + + /** + * Origin text. + */ + private final Text origin; + + /** + * Ctor. + * + * @param input The String + */ + public Base64Text(final String input) { + this(new TextOf(input)); + } + + /** + * Ctor. + * + * @param origin Origin text + */ + public Base64Text(final Text origin) { + this.origin = origin; + } + + @Override + public String asString() throws IOException { + return new TextOf( + new Base64Bytes( + new BytesOf(this.origin) + ) + ).asString(); + } + + @Override + public int compareTo(final Text text) { + return new UncheckedText(this).compareTo(text); + } +} diff --git a/src/test/java/org/cactoos/bytes/Base64BytesTest.java b/src/test/java/org/cactoos/bytes/Base64BytesTest.java index d6e07d71c8..fd4664ba75 100644 --- a/src/test/java/org/cactoos/bytes/Base64BytesTest.java +++ b/src/test/java/org/cactoos/bytes/Base64BytesTest.java @@ -44,7 +44,7 @@ public final class Base64BytesTest { * @throws IOException If fails. */ @Test - public void checkEncode() throws IOException { + public void checkDecode() throws IOException { MatcherAssert.assertThat( "Can't decodes bytes using the Base64 encoding scheme", new Base64Bytes( diff --git a/src/test/java/org/cactoos/text/Base64TextTest.java b/src/test/java/org/cactoos/text/Base64TextTest.java new file mode 100644 index 0000000000..4c28be33b8 --- /dev/null +++ b/src/test/java/org/cactoos/text/Base64TextTest.java @@ -0,0 +1,57 @@ +/** + * 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.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test Case for {@link org.cactoos.text.Base64Text}. + * + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.20.2 + */ +public final class Base64TextTest { + + /** + * Check text decodes using the Base64 encoding scheme. + * @throws IOException If fails. + */ + @Test + public void checkDecode() throws IOException { + MatcherAssert.assertThat( + "Can't decodes text using the Base64 encoding scheme", + new Base64Text( + "SGVsbG8h" + ), + new TextHasString( + "Hello!" + ) + ); + } +} From 829e3964ec2a51dbcb25fb2600bbf6c4c0cb5ad8 Mon Sep 17 00:00:00 2001 From: GooKv Date: Sun, 5 Nov 2017 01:06:12 +0700 Subject: [PATCH 06/12] Added Folded scalar --- src/main/java/org/cactoos/func/MaxFunc.java | 45 +++++++++++ src/main/java/org/cactoos/func/MinFunc.java | 45 +++++++++++ src/main/java/org/cactoos/scalar/Folded.java | 78 ++++++++++++++++++++ src/main/java/org/cactoos/scalar/Max.java | 25 ++----- src/main/java/org/cactoos/scalar/Min.java | 25 ++----- 5 files changed, 182 insertions(+), 36 deletions(-) create mode 100644 src/main/java/org/cactoos/func/MaxFunc.java create mode 100644 src/main/java/org/cactoos/func/MinFunc.java create mode 100644 src/main/java/org/cactoos/scalar/Folded.java diff --git a/src/main/java/org/cactoos/func/MaxFunc.java b/src/main/java/org/cactoos/func/MaxFunc.java new file mode 100644 index 0000000000..52c2ea18b1 --- /dev/null +++ b/src/main/java/org/cactoos/func/MaxFunc.java @@ -0,0 +1,45 @@ +/** + * 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.func; + +import org.cactoos.BiFunc; + +/** + * Function, finding max among two items. + * + * @author Alexander Dyadyushenko (gookven@gmail.com) + * @version $Id$ + * @param comparable type + * @since 0.9 + */ +public final class MaxFunc> implements BiFunc { + @Override + public T apply(final T first, final T second) throws Exception { + T max = first; + if (second.compareTo(max) > 0) { + max = second; + } + return max; + } +} diff --git a/src/main/java/org/cactoos/func/MinFunc.java b/src/main/java/org/cactoos/func/MinFunc.java new file mode 100644 index 0000000000..b760d35278 --- /dev/null +++ b/src/main/java/org/cactoos/func/MinFunc.java @@ -0,0 +1,45 @@ +/** + * 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.func; + +import org.cactoos.BiFunc; + +/** + * Function, finding min among two items. + * + * @author Alexander Dyadyushenko (gookven@gmail.com) + * @version $Id$ + * @param comparable type + * @since 0.9 + */ +public final class MinFunc> implements BiFunc { + @Override + public T apply(final T first, final T second) throws Exception { + T min = first; + if (second.compareTo(min) < 0) { + min = second; + } + return min; + } +} diff --git a/src/main/java/org/cactoos/scalar/Folded.java b/src/main/java/org/cactoos/scalar/Folded.java new file mode 100644 index 0000000000..2c0d12093f --- /dev/null +++ b/src/main/java/org/cactoos/scalar/Folded.java @@ -0,0 +1,78 @@ +/** + * 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.scalar; + +import java.util.Iterator; +import java.util.NoSuchElementException; +import org.cactoos.BiFunc; +import org.cactoos.Scalar; + +/** + * Folds iterable via BiFunc + * + *

There is no thread-safety guarantee. + * + * @author Alexander Dyadyushenko (gookven@gmail.com) + * @version $Id$ + * @param Scalar type + * @since 0.9 + */ +public final class Folded implements Scalar { + + /** + * Items. + */ + private final Iterable> items; + + /** + * Folding function. + */ + private final BiFunc fold; + + /** + * Ctor. + * @param fold Folding function + * @param items The items + */ + public Folded(final BiFunc fold, final Iterable> items) { + this.items = items; + this.fold = fold; + } + + @Override + public T value() throws Exception { + final Iterator> iter = this.items.iterator(); + if (!iter.hasNext()) { + throw new NoSuchElementException( + "Can't find first element in an empty iterable" + ); + } + T acc = iter.next().value(); + while (iter.hasNext()) { + final T next = iter.next().value(); + acc = this.fold.apply(acc, next); + } + return acc; + } +} diff --git a/src/main/java/org/cactoos/scalar/Max.java b/src/main/java/org/cactoos/scalar/Max.java index 0962c8a305..c7a0b3d929 100644 --- a/src/main/java/org/cactoos/scalar/Max.java +++ b/src/main/java/org/cactoos/scalar/Max.java @@ -23,9 +23,8 @@ */ package org.cactoos.scalar; -import java.util.Iterator; -import java.util.NoSuchElementException; import org.cactoos.Scalar; +import org.cactoos.func.MaxFunc; import org.cactoos.iterable.IterableOf; /** @@ -43,7 +42,7 @@ public final class Max> implements Scalar { /** * Items. */ - private final Iterable> items; + private final Scalar result; /** * Ctor. @@ -59,25 +58,15 @@ public Max(final Scalar... scalars) { * @param iterable The items */ public Max(final Iterable> iterable) { - this.items = iterable; + this.result = new Folded<>( + new MaxFunc<>(), + iterable + ); } @Override public T value() throws Exception { - final Iterator> iter = this.items.iterator(); - if (!iter.hasNext()) { - throw new NoSuchElementException( - "Can't find greater element in an empty iterable" - ); - } - T max = iter.next().value(); - while (iter.hasNext()) { - final T next = iter.next().value(); - if (next.compareTo(max) > 0) { - max = next; - } - } - return max; + return this.result.value(); } } diff --git a/src/main/java/org/cactoos/scalar/Min.java b/src/main/java/org/cactoos/scalar/Min.java index ce845dcd0e..57fb364f7c 100644 --- a/src/main/java/org/cactoos/scalar/Min.java +++ b/src/main/java/org/cactoos/scalar/Min.java @@ -23,9 +23,8 @@ */ package org.cactoos.scalar; -import java.util.Iterator; -import java.util.NoSuchElementException; import org.cactoos.Scalar; +import org.cactoos.func.MinFunc; import org.cactoos.iterable.IterableOf; /** @@ -43,7 +42,7 @@ public final class Min> implements Scalar { /** * Items. */ - private final Iterable> items; + private final Scalar result; /** * Ctor. @@ -59,25 +58,15 @@ public Min(final Scalar... scalars) { * @param iterable The items */ public Min(final Iterable> iterable) { - this.items = iterable; + this.result = new Folded<>( + new MinFunc<>(), + iterable + ); } @Override public T value() throws Exception { - final Iterator> iter = this.items.iterator(); - if (!iter.hasNext()) { - throw new NoSuchElementException( - "Can't find smaller element in an empty iterable" - ); - } - T min = iter.next().value(); - while (iter.hasNext()) { - final T next = iter.next().value(); - if (next.compareTo(min) < 0) { - min = next; - } - } - return min; + return this.result.value(); } } From 6903bcc1ab6c53d8cefa99597990538197ac3484 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Sun, 5 Nov 2017 13:34:16 +0200 Subject: [PATCH 07/12] #458 Directory --- src/main/java/org/cactoos/io/Directory.java | 75 +++++++++++++++++++ src/main/java/org/cactoos/text/TextOf.java | 18 +++++ .../java/org/cactoos/io/DirectoryTest.java | 56 ++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 src/main/java/org/cactoos/io/Directory.java create mode 100644 src/test/java/org/cactoos/io/DirectoryTest.java diff --git a/src/main/java/org/cactoos/io/Directory.java b/src/main/java/org/cactoos/io/Directory.java new file mode 100644 index 0000000000..bee0096b2f --- /dev/null +++ b/src/main/java/org/cactoos/io/Directory.java @@ -0,0 +1,75 @@ +/** + * 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.io; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Iterator; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * Files in a directory. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.21 + */ +public final class Directory implements Iterable { + + /** + * Path of the directory. + */ + private final Path dir; + + /** + * Ctor. + * @param path Path of the dir + */ + public Directory(final File path) { + this(path.toPath()); + } + + /** + * Ctor. + * @param path Path of the dir + */ + public Directory(final Path path) { + this.dir = path; + } + + @Override + public Iterator iterator() { + try (final Stream files = Files.walk(this.dir)) { + return files.collect(Collectors.toList()).iterator(); + } catch (final IOException ex) { + throw new IllegalStateException(ex); + } + } + +} diff --git a/src/main/java/org/cactoos/text/TextOf.java b/src/main/java/org/cactoos/text/TextOf.java index db236ace42..6113716c2f 100644 --- a/src/main/java/org/cactoos/text/TextOf.java +++ b/src/main/java/org/cactoos/text/TextOf.java @@ -37,6 +37,7 @@ import org.cactoos.Text; import org.cactoos.io.BytesOf; import org.cactoos.io.InputOf; +import org.cactoos.iterable.Mapped; import org.cactoos.scalar.IoCheckedScalar; /** @@ -273,6 +274,23 @@ 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 JoinedText( + ", ", + new Mapped<>( + Object::toString, + iterable + ) + ).asString() + ); + } + /** * Ctor. * diff --git a/src/test/java/org/cactoos/io/DirectoryTest.java b/src/test/java/org/cactoos/io/DirectoryTest.java new file mode 100644 index 0000000000..14929bec9a --- /dev/null +++ b/src/test/java/org/cactoos/io/DirectoryTest.java @@ -0,0 +1,56 @@ +/** + * 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.io; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link Directory}. + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.12 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +public final class DirectoryTest { + + @Test + public void listsFilesInDirectory() throws IOException { + final Path dir = Files.createTempDirectory("x1"); + dir.resolve("x/y").toFile().mkdirs(); + Files.write(dir.resolve("x/y/test"), "".getBytes()); + MatcherAssert.assertThat( + "Can't list files in a directory", + new Directory(dir), + // @checkstyle MagicNumber (1 line) + Matchers.iterableWithSize(4) + ); + } + +} From 53051354883e649a211b5c42e158246de12e94cd Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Sun, 5 Nov 2017 14:03:58 +0200 Subject: [PATCH 08/12] javadoc.io new badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d5de1c4f9a..156096dff8 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ [![Build Status](https://travis-ci.org/yegor256/cactoos.svg?branch=master)](https://travis-ci.org/yegor256/cactoos) [![Build status](https://ci.appveyor.com/api/projects/status/8vs8huy61og6jwif?svg=true)](https://ci.appveyor.com/project/yegor256/cactoos) -[![Javadoc](https://javadoc-emblem.rhcloud.com/doc/org.cactoos/cactoos/badge.svg?color=blue&prefix=v)](http://www.javadoc.io/doc/org.cactoos/cactoos) +[![Javadoc](http://www.javadoc.io/badge/org.cactoos/cactoos.svg)](http://www.javadoc.io/doc/org.cactoos/cactoos) [![PDD status](http://www.0pdd.com/svg?name=yegor256/cactoos)](http://www.0pdd.com/p?name=yegor256/cactoos) [![Maven Central](https://img.shields.io/maven-central/v/org.cactoos/cactoos.svg)](https://maven-badges.herokuapp.com/maven-central/org.cactoos/cactoos) [![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/yegor256/cactoos/blob/master/LICENSE.txt) From fabd7288f5d318ca8fdb21510bd2904b72447be8 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Wed, 8 Nov 2017 10:48:22 +0200 Subject: [PATCH 09/12] #459 SumOf --- src/main/java/org/cactoos/BiFunc.java | 5 + src/main/java/org/cactoos/BiProc.java | 5 + src/main/java/org/cactoos/Input.java | 8 +- src/main/java/org/cactoos/Output.java | 1 - src/main/java/org/cactoos/Proc.java | 5 + src/main/java/org/cactoos/Text.java | 4 + .../IoCheckedBiProc.java} | 61 +++--- .../UncheckedBiProc.java} | 47 ++--- src/main/java/org/cactoos/scalar/SumOf.java | 197 ++++++++++++++++++ .../org/cactoos/iterable/IterableOfTest.java | 10 + .../org/cactoos/iterable/SumOfRealsTest.java | 67 ------ .../SumOfTest.java} | 35 +++- 12 files changed, 313 insertions(+), 132 deletions(-) rename src/main/java/org/cactoos/{iterable/SumOfReals.java => func/IoCheckedBiProc.java} (52%) rename src/main/java/org/cactoos/{iterable/SumOfInts.java => func/UncheckedBiProc.java} (61%) create mode 100644 src/main/java/org/cactoos/scalar/SumOf.java delete mode 100644 src/test/java/org/cactoos/iterable/SumOfRealsTest.java rename src/test/java/org/cactoos/{iterable/SumOfIntsTest.java => scalar/SumOfTest.java} (70%) diff --git a/src/main/java/org/cactoos/BiFunc.java b/src/main/java/org/cactoos/BiFunc.java index 7ce632db5a..9a5c7cab86 100644 --- a/src/main/java/org/cactoos/BiFunc.java +++ b/src/main/java/org/cactoos/BiFunc.java @@ -26,6 +26,11 @@ /** * Function that accepts two arguments. * + *

If you don't want to have any checked exceptions being thrown + * out of your {@link BiFunc}, you can use + * {@link org.cactoos.func.UncheckedBiFunc} decorator. Also + * you may try {@link org.cactoos.func.IoCheckedBiFunc}.

+ * *

There is no thread-safety guarantee. * * @author Yegor Bugayenko (yegor256@gmail.com) diff --git a/src/main/java/org/cactoos/BiProc.java b/src/main/java/org/cactoos/BiProc.java index ba891a6bc3..f7c4c82e33 100644 --- a/src/main/java/org/cactoos/BiProc.java +++ b/src/main/java/org/cactoos/BiProc.java @@ -26,6 +26,11 @@ /** * Proc that accepts two arguments. * + *

If you don't want to have any checked exceptions being thrown + * out of your {@link BiProc}, you can use + * {@link org.cactoos.func.UncheckedBiProc} decorator. Also + * you may try {@link org.cactoos.func.IoCheckedBiProc}.

+ * *

There is no thread-safety guarantee. * * @author Yegor Bugayenko (yegor256@gmail.com) diff --git a/src/main/java/org/cactoos/Input.java b/src/main/java/org/cactoos/Input.java index a058450b2e..da20af6fd1 100644 --- a/src/main/java/org/cactoos/Input.java +++ b/src/main/java/org/cactoos/Input.java @@ -36,15 +36,17 @@ * new InputOf(new File("/tmp/names.txt")) * ).asString(); * - *

Here {@link InputOf} implements {@link Input} and behaves like - * one, providing read-only access to the encapsulated {@link java.io.File}.

+ *

Here {@link org.cactoos.io.InputOf} implements + * {@link Input} and behaves like + * one, providing read-only access to + * the encapsulated {@link java.io.File}.

* *

There is no thread-safety guarantee. * * @author Yegor Bugayenko (yegor256@gmail.com) * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ - * @see InputOf + * @see org.cactoos.io.InputOf * @since 0.1 */ public interface Input { diff --git a/src/main/java/org/cactoos/Output.java b/src/main/java/org/cactoos/Output.java index 7412ba62f6..1fae173179 100644 --- a/src/main/java/org/cactoos/Output.java +++ b/src/main/java/org/cactoos/Output.java @@ -53,7 +53,6 @@ * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @see OutputTo - * @see org.cactoos.io.OutputTo * @since 0.1 */ public interface Output { diff --git a/src/main/java/org/cactoos/Proc.java b/src/main/java/org/cactoos/Proc.java index 8a652dd0e0..f8b88df209 100644 --- a/src/main/java/org/cactoos/Proc.java +++ b/src/main/java/org/cactoos/Proc.java @@ -26,6 +26,11 @@ /** * Procedure. * + *

If you don't want to have any checked exceptions being thrown + * out of your {@link Proc}, you can use + * {@link org.cactoos.func.UncheckedProc} decorator. Also + * you may try {@link org.cactoos.func.IoCheckedProc}.

+ * *

There is no thread-safety guarantee. * * @author Yegor Bugayenko (yegor256@gmail.com) diff --git a/src/main/java/org/cactoos/Text.java b/src/main/java/org/cactoos/Text.java index bedbf41346..f908f9e60a 100644 --- a/src/main/java/org/cactoos/Text.java +++ b/src/main/java/org/cactoos/Text.java @@ -29,6 +29,10 @@ /** * Text. * + *

If you don't want to have any checked exceptions being thrown + * out of your {@link Text}, you can use + * {@link UncheckedText} decorator.

+ * *

There is no thread-safety guarantee. * * @author Yegor Bugayenko (yegor256@gmail.com) diff --git a/src/main/java/org/cactoos/iterable/SumOfReals.java b/src/main/java/org/cactoos/func/IoCheckedBiProc.java similarity index 52% rename from src/main/java/org/cactoos/iterable/SumOfReals.java rename to src/main/java/org/cactoos/func/IoCheckedBiProc.java index b9bd56980e..a75d3b66ab 100644 --- a/src/main/java/org/cactoos/iterable/SumOfReals.java +++ b/src/main/java/org/cactoos/func/IoCheckedBiProc.java @@ -21,51 +21,58 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.cactoos.iterable; +package org.cactoos.func; -import java.util.Iterator; -import org.cactoos.Scalar; +import java.io.IOException; +import org.cactoos.BiProc; /** - * Real total of numbers. + * BiProc that doesn't throw checked {@link Exception}, but throws + * {@link IOException} instead. * *

There is no thread-safety guarantee. * - * @author Vseslav Sekorin (vssekorin@gmail.com) + * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ - * @since 0.9 + * @param Type of input + * @param Type of input + * @since 0.22 */ -public final class SumOfReals implements Scalar { +public final class IoCheckedBiProc implements BiProc { /** - * The iterable. + * Original proc. */ - private final Iterable> src; + private final BiProc proc; /** * Ctor. - * @param src Numbers + * @param prc Encapsulated func */ - @SafeVarargs - public SumOfReals(final Scalar... src) { - this(new IterableOf<>(src)); - } - - /** - * Ctor. - * @param src The iterable - */ - public SumOfReals(final Iterable> src) { - this.src = src; + public IoCheckedBiProc(final BiProc prc) { + this.proc = prc; } @Override - public Double value() throws Exception { - final Iterator> numbers = this.src.iterator(); - double result = 0.0d; - while (numbers.hasNext()) { - result += numbers.next().value().doubleValue(); + @SuppressWarnings + ( + { + "PMD.AvoidCatchingGenericException", + "PMD.AvoidRethrowingException" + } + ) + public void exec(final X first, final Y second) throws IOException { + try { + this.proc.exec(first, second); + } catch (final IOException ex) { + throw ex; + } catch (final InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new IOException(ex); + // @checkstyle IllegalCatchCheck (1 line) + } catch (final Exception ex) { + throw new IOException(ex); } - return result; } + } diff --git a/src/main/java/org/cactoos/iterable/SumOfInts.java b/src/main/java/org/cactoos/func/UncheckedBiProc.java similarity index 61% rename from src/main/java/org/cactoos/iterable/SumOfInts.java rename to src/main/java/org/cactoos/func/UncheckedBiProc.java index d9101937fa..9b8c40c5d3 100644 --- a/src/main/java/org/cactoos/iterable/SumOfInts.java +++ b/src/main/java/org/cactoos/func/UncheckedBiProc.java @@ -21,51 +21,44 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.cactoos.iterable; +package org.cactoos.func; -import java.util.Iterator; -import org.cactoos.Scalar; +import java.io.IOException; +import java.io.UncheckedIOException; +import org.cactoos.BiProc; /** - * Int total of numbers. + * BiProc that doesn't throw checked {@link Exception}. * *

There is no thread-safety guarantee. * - * @author Vseslav Sekorin (vssekorin@gmail.com) + * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ - * @since 0.9 + * @param Type of input + * @param Type of input + * @since 0.22 */ -public final class SumOfInts implements Scalar { +public final class UncheckedBiProc implements BiProc { /** - * The iterable. + * Original proc. */ - private final Iterable> src; + private final BiProc proc; /** * Ctor. - * @param src Numbers + * @param prc Encapsulated proc */ - @SafeVarargs - public SumOfInts(final Scalar... src) { - this(new IterableOf<>(src)); - } - - /** - * Ctor. - * @param src The iterable - */ - public SumOfInts(final Iterable> src) { - this.src = src; + public UncheckedBiProc(final BiProc prc) { + this.proc = prc; } @Override - public Long value() throws Exception { - final Iterator> numbers = this.src.iterator(); - Long result = 0L; - while (numbers.hasNext()) { - result += numbers.next().value().longValue(); + public void exec(final X first, final Y second) { + try { + new IoCheckedBiProc<>(this.proc).exec(first, second); + } catch (final IOException ex) { + throw new UncheckedIOException(ex); } - return result; } } diff --git a/src/main/java/org/cactoos/scalar/SumOf.java b/src/main/java/org/cactoos/scalar/SumOf.java new file mode 100644 index 0000000000..062cd7f553 --- /dev/null +++ b/src/main/java/org/cactoos/scalar/SumOf.java @@ -0,0 +1,197 @@ +/** + * 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.scalar; + +import java.util.Iterator; +import java.util.stream.Collectors; +import java.util.stream.DoubleStream; +import java.util.stream.IntStream; +import java.util.stream.LongStream; +import org.cactoos.Scalar; +import org.cactoos.iterable.IterableOf; +import org.cactoos.iterable.Mapped; + +/** + * Int total of numbers. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @since 0.9 + */ +public final class SumOf extends Number { + + /** + * Serialization marker. + */ + private static final long serialVersionUID = -1924406337256921883L; + + /** + * The LONG sum. + */ + private final Scalar lsum; + + /** + * The INT sum. + */ + private final Scalar isum; + + /** + * The FLOAT sum. + */ + private final Scalar fsum; + + /** + * The DOUBLE sum. + */ + private final Scalar dsum; + + /** + * Ctor. + * @param src Numbers + * @since 0.22 + */ + public SumOf(final int... src) { + this(new Mapped<>( + input -> () -> input, + IntStream.of(src).boxed().collect(Collectors.toList()) + )); + } + + /** + * Ctor. + * @param src Numbers + * @since 0.22 + */ + public SumOf(final long... src) { + this(new Mapped<>( + input -> () -> input, + LongStream.of(src).boxed().collect(Collectors.toList()) + )); + } + + /** + * Ctor. + * @param src Numbers + * @since 0.22 + */ + public SumOf(final double... src) { + this(new Mapped<>( + input -> () -> input, + DoubleStream.of(src).boxed().collect(Collectors.toList()) + )); + } + + /** + * Ctor. + * @param src Numbers + */ + @SafeVarargs + public SumOf(final Scalar... src) { + this(new IterableOf<>(src)); + } + + /** + * Ctor. + * @param src The iterable + * @checkstyle ExecutableStatementCountCheck (150 lines) + */ + public SumOf(final Iterable> src) { + super(); + this.lsum = new SyncScalar<>( + new StickyScalar<>( + () -> { + final Iterator> numbers = src.iterator(); + long sum = 0L; + while (numbers.hasNext()) { + final Number next = numbers.next().value(); + sum += next.longValue(); + } + return sum; + } + ) + ); + this.isum = new SyncScalar<>( + new StickyScalar<>( + () -> { + final Iterator> numbers = src.iterator(); + int sum = 0; + while (numbers.hasNext()) { + final Number next = numbers.next().value(); + sum += next.intValue(); + } + return sum; + } + ) + ); + this.fsum = new SyncScalar<>( + new StickyScalar<>( + () -> { + final Iterator> numbers = src.iterator(); + float sum = 0.0f; + while (numbers.hasNext()) { + final Number next = numbers.next().value(); + sum += next.floatValue(); + } + return sum; + } + ) + ); + this.dsum = new SyncScalar<>( + new StickyScalar<>( + () -> { + final Iterator> numbers = src.iterator(); + double sum = 0.0d; + while (numbers.hasNext()) { + final Number next = numbers.next().value(); + sum += next.doubleValue(); + } + return sum; + } + ) + ); + } + + @Override + public int intValue() { + return new UncheckedScalar<>(this.isum).value(); + } + + @Override + public long longValue() { + return new UncheckedScalar<>(this.lsum).value(); + } + + @Override + public float floatValue() { + return new UncheckedScalar<>(this.fsum).value(); + } + + @Override + public double doubleValue() { + return new UncheckedScalar<>(this.dsum).value(); + } +} diff --git a/src/test/java/org/cactoos/iterable/IterableOfTest.java b/src/test/java/org/cactoos/iterable/IterableOfTest.java index a6af97356b..5e67203019 100644 --- a/src/test/java/org/cactoos/iterable/IterableOfTest.java +++ b/src/test/java/org/cactoos/iterable/IterableOfTest.java @@ -26,6 +26,7 @@ import org.cactoos.ScalarHasValue; import org.cactoos.text.TextOf; import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; import org.junit.Test; /** @@ -52,6 +53,15 @@ public void convertsScalarsToIterable() { ); } + @Test + public void convertsArrayOfIntsToIterable() { + MatcherAssert.assertThat( + "Can't convert int scalars to iterable", + new IterableOf<>(1, 2, 0, 2), + Matchers.hasItem(0) + ); + } + @Test public void convertsObjectsToIterable() { MatcherAssert.assertThat( diff --git a/src/test/java/org/cactoos/iterable/SumOfRealsTest.java b/src/test/java/org/cactoos/iterable/SumOfRealsTest.java deleted file mode 100644 index 03b97f58df..0000000000 --- a/src/test/java/org/cactoos/iterable/SumOfRealsTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * 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.iterable; - -import org.cactoos.Scalar; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link SumOfReals}. - * - * @author Vseslav Sekorin (vssekorin@gmail.com) - * @version $Id$ - * @since 0.9 - * @checkstyle JavadocMethodCheck (500 lines) - * @checkstyle MagicNumberCheck (500 lines) - */ -public final class SumOfRealsTest { - - @Test - public void withVarargsCtor() throws Exception { - MatcherAssert.assertThat( - new SumOfReals( - () -> 1.2, - () -> 2.5, - () -> 3.3 - ).value(), - Matchers.closeTo(7.0, 0.0) - ); - } - - @Test - public void withIterCtor() throws Exception { - MatcherAssert.assertThat( - new SumOfReals( - new IterableOf>( - () -> 7.1, - () -> 8.1, - () -> 10.1 - ) - ).value(), - Matchers.closeTo(25.0, 0.3) - ); - } -} diff --git a/src/test/java/org/cactoos/iterable/SumOfIntsTest.java b/src/test/java/org/cactoos/scalar/SumOfTest.java similarity index 70% rename from src/test/java/org/cactoos/iterable/SumOfIntsTest.java rename to src/test/java/org/cactoos/scalar/SumOfTest.java index 8fcaa05f3a..24245b3b7a 100644 --- a/src/test/java/org/cactoos/iterable/SumOfIntsTest.java +++ b/src/test/java/org/cactoos/scalar/SumOfTest.java @@ -21,15 +21,16 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.cactoos.iterable; +package org.cactoos.scalar; import org.cactoos.Scalar; +import org.cactoos.iterable.IterableOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; /** - * Test case for {@link SumOfInts}. + * Test case for {@link SumOf}. * * @author Vseslav Sekorin (vssekorin@gmail.com) * @version $Id$ @@ -37,16 +38,36 @@ * @checkstyle JavadocMethodCheck (500 lines) * @checkstyle MagicNumberCheck (500 lines) */ -public final class SumOfIntsTest { +public final class SumOfTest { + + @Test + public void withListOfNumbers() throws Exception { + MatcherAssert.assertThat( + new SumOf(1, 2, 3).intValue(), + Matchers.equalTo(6) + ); + MatcherAssert.assertThat( + new SumOf(1.0d, 2.0d, 3.0d).doubleValue(), + Matchers.equalTo(6.0d) + ); + MatcherAssert.assertThat( + new SumOf(1.0f, 2.0f, 3.0f).floatValue(), + Matchers.equalTo(6.0f) + ); + MatcherAssert.assertThat( + new SumOf(1L, 2L, 3L).longValue(), + Matchers.equalTo(6L) + ); + } @Test public void withVarargsCtor() throws Exception { MatcherAssert.assertThat( - new SumOfInts( + new SumOf( () -> 1, () -> 2, () -> 3 - ).value(), + ).longValue(), Matchers.equalTo(6L) ); } @@ -54,13 +75,13 @@ public void withVarargsCtor() throws Exception { @Test public void withIterCtor() throws Exception { MatcherAssert.assertThat( - new SumOfInts( + new SumOf( new IterableOf>( () -> 7, () -> 8, () -> 10 ) - ).value(), + ).longValue(), Matchers.equalTo(25L) ); } From d324b97a36c7d3cc855ab4ce634ba0c8ba6e4faf Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Wed, 8 Nov 2017 10:55:55 +0200 Subject: [PATCH 10/12] #449 more javadoc --- src/main/java/org/cactoos/scalar/And.java | 7 +++++++ src/main/java/org/cactoos/scalar/AndWithIndex.java | 5 +++++ src/main/java/org/cactoos/scalar/BoolOf.java | 5 +++++ src/main/java/org/cactoos/scalar/DoubleOf.java | 5 +++++ src/main/java/org/cactoos/scalar/Equals.java | 5 +++++ src/main/java/org/cactoos/scalar/False.java | 2 +- src/main/java/org/cactoos/scalar/FloatOf.java | 5 +++++ src/main/java/org/cactoos/scalar/Folded.java | 5 +++++ src/main/java/org/cactoos/scalar/IoCheckedScalar.java | 5 +++++ src/main/java/org/cactoos/scalar/Max.java | 7 +++++++ src/main/java/org/cactoos/scalar/Min.java | 5 +++++ src/main/java/org/cactoos/scalar/Not.java | 5 +++++ src/main/java/org/cactoos/scalar/Or.java | 5 +++++ src/main/java/org/cactoos/scalar/RetryScalar.java | 5 +++++ src/main/java/org/cactoos/scalar/StickyScalar.java | 7 ++++++- src/main/java/org/cactoos/scalar/SumOf.java | 5 +++++ src/main/java/org/cactoos/scalar/SyncScalar.java | 5 +++++ src/main/java/org/cactoos/scalar/Ternary.java | 5 +++++ src/main/java/org/cactoos/scalar/True.java | 2 +- 19 files changed, 92 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/cactoos/scalar/And.java b/src/main/java/org/cactoos/scalar/And.java index a0fc2549da..9cb81dbd3b 100644 --- a/src/main/java/org/cactoos/scalar/And.java +++ b/src/main/java/org/cactoos/scalar/And.java @@ -43,10 +43,17 @@ * name -> System.out.printf("The name: %s\n", name) * ).value(); * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * *

There is no thread-safety guarantee. * * @author Vseslav Sekorin (vssekorin@gmail.com) * @version $Id$ + * @see UncheckedScalar + * @see IoCheckedScalar * @since 0.8 */ public final class And implements Scalar { diff --git a/src/main/java/org/cactoos/scalar/AndWithIndex.java b/src/main/java/org/cactoos/scalar/AndWithIndex.java index 45f1b459c2..234d27954c 100644 --- a/src/main/java/org/cactoos/scalar/AndWithIndex.java +++ b/src/main/java/org/cactoos/scalar/AndWithIndex.java @@ -48,6 +48,11 @@ * ) * ).value(); * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * *

There is no thread-safety guarantee. * * @author Yegor Bugayenko (yegor256@gmail.com) diff --git a/src/main/java/org/cactoos/scalar/BoolOf.java b/src/main/java/org/cactoos/scalar/BoolOf.java index c06ff108e7..5b129f1cef 100644 --- a/src/main/java/org/cactoos/scalar/BoolOf.java +++ b/src/main/java/org/cactoos/scalar/BoolOf.java @@ -33,6 +33,11 @@ * *

There is no thread-safety guarantee. * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * * @author Kirill (g4s8.public@gmail.com) * @version $Id$ * @since 0.2 diff --git a/src/main/java/org/cactoos/scalar/DoubleOf.java b/src/main/java/org/cactoos/scalar/DoubleOf.java index 9171d48141..a90fcd4325 100644 --- a/src/main/java/org/cactoos/scalar/DoubleOf.java +++ b/src/main/java/org/cactoos/scalar/DoubleOf.java @@ -33,6 +33,11 @@ * *

There is no thread-safety guarantee. * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * * @author Kirill (g4s8.public@gmail.com) * @version $Id$ * @since 0.2 diff --git a/src/main/java/org/cactoos/scalar/Equals.java b/src/main/java/org/cactoos/scalar/Equals.java index b2586bbcb8..9e3de6a95c 100644 --- a/src/main/java/org/cactoos/scalar/Equals.java +++ b/src/main/java/org/cactoos/scalar/Equals.java @@ -30,6 +30,11 @@ * *

There is no thread-safety guarantee. * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ * @param Type of object to compare diff --git a/src/main/java/org/cactoos/scalar/False.java b/src/main/java/org/cactoos/scalar/False.java index f18e421b63..35a381a059 100644 --- a/src/main/java/org/cactoos/scalar/False.java +++ b/src/main/java/org/cactoos/scalar/False.java @@ -37,7 +37,7 @@ public final class False implements Scalar { @Override - public Boolean value() throws Exception { + public Boolean value() { return false; } } diff --git a/src/main/java/org/cactoos/scalar/FloatOf.java b/src/main/java/org/cactoos/scalar/FloatOf.java index e3e396ada1..64b9685fbd 100644 --- a/src/main/java/org/cactoos/scalar/FloatOf.java +++ b/src/main/java/org/cactoos/scalar/FloatOf.java @@ -33,6 +33,11 @@ * *

There is no thread-safety guarantee. * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * * @author Kirill (g4s8.public@gmail.com) * @version $Id$ * @since 0.2 diff --git a/src/main/java/org/cactoos/scalar/Folded.java b/src/main/java/org/cactoos/scalar/Folded.java index 2c0d12093f..21b18f5830 100644 --- a/src/main/java/org/cactoos/scalar/Folded.java +++ b/src/main/java/org/cactoos/scalar/Folded.java @@ -33,6 +33,11 @@ * *

There is no thread-safety guarantee. * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * * @author Alexander Dyadyushenko (gookven@gmail.com) * @version $Id$ * @param Scalar type diff --git a/src/main/java/org/cactoos/scalar/IoCheckedScalar.java b/src/main/java/org/cactoos/scalar/IoCheckedScalar.java index f0ddc6dffd..2f4f134320 100644 --- a/src/main/java/org/cactoos/scalar/IoCheckedScalar.java +++ b/src/main/java/org/cactoos/scalar/IoCheckedScalar.java @@ -33,6 +33,11 @@ * *

There is no thread-safety guarantee. * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Type of result diff --git a/src/main/java/org/cactoos/scalar/Max.java b/src/main/java/org/cactoos/scalar/Max.java index c7a0b3d929..1785b42977 100644 --- a/src/main/java/org/cactoos/scalar/Max.java +++ b/src/main/java/org/cactoos/scalar/Max.java @@ -30,11 +30,18 @@ /** * Find the greater among items. * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * *

There is no thread-safety guarantee. * * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ * @param Scalar type + * @see UncheckedScalar + * @see IoCheckedScalar * @since 0.10 */ public final class Max> implements Scalar { diff --git a/src/main/java/org/cactoos/scalar/Min.java b/src/main/java/org/cactoos/scalar/Min.java index 57fb364f7c..1b5a672639 100644 --- a/src/main/java/org/cactoos/scalar/Min.java +++ b/src/main/java/org/cactoos/scalar/Min.java @@ -32,6 +32,11 @@ * *

There is no thread-safety guarantee. * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ * @param Scalar type diff --git a/src/main/java/org/cactoos/scalar/Not.java b/src/main/java/org/cactoos/scalar/Not.java index 5f66433c68..f2825b7f08 100644 --- a/src/main/java/org/cactoos/scalar/Not.java +++ b/src/main/java/org/cactoos/scalar/Not.java @@ -30,6 +30,11 @@ * *

There is no thread-safety guarantee. * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * * @author Vseslav Sekorin (vssekorin@gmail.com) * @version $Id$ * @since 0.7 diff --git a/src/main/java/org/cactoos/scalar/Or.java b/src/main/java/org/cactoos/scalar/Or.java index 98fbe14663..66bc2b0aad 100644 --- a/src/main/java/org/cactoos/scalar/Or.java +++ b/src/main/java/org/cactoos/scalar/Or.java @@ -35,6 +35,11 @@ * *

There is no thread-safety guarantee. * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * * @author Vseslav Sekorin (vssekorin@gmail.com) * @version $Id$ * @since 0.8 diff --git a/src/main/java/org/cactoos/scalar/RetryScalar.java b/src/main/java/org/cactoos/scalar/RetryScalar.java index 9e78a02aa1..ae9fba7a7a 100644 --- a/src/main/java/org/cactoos/scalar/RetryScalar.java +++ b/src/main/java/org/cactoos/scalar/RetryScalar.java @@ -32,6 +32,11 @@ * *

There is no thread-safety guarantee. * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Type of output diff --git a/src/main/java/org/cactoos/scalar/StickyScalar.java b/src/main/java/org/cactoos/scalar/StickyScalar.java index a9a791ef5b..708ff3ce73 100644 --- a/src/main/java/org/cactoos/scalar/StickyScalar.java +++ b/src/main/java/org/cactoos/scalar/StickyScalar.java @@ -35,7 +35,12 @@ * *

Pay attention that this class is not thread-safe. It is highly * recommended to always decorate it with {@link SyncScalar}.

- + * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * *

There is no thread-safety guarantee. * * @author Tim Hinkes (timmeey@timmeey.de) diff --git a/src/main/java/org/cactoos/scalar/SumOf.java b/src/main/java/org/cactoos/scalar/SumOf.java index 062cd7f553..0dca6993ab 100644 --- a/src/main/java/org/cactoos/scalar/SumOf.java +++ b/src/main/java/org/cactoos/scalar/SumOf.java @@ -35,6 +35,11 @@ /** * Int total of numbers. * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * *

There is no thread-safety guarantee. * * @author Yegor Bugayenko (yegor256@gmail.com) diff --git a/src/main/java/org/cactoos/scalar/SyncScalar.java b/src/main/java/org/cactoos/scalar/SyncScalar.java index dc06e7f4a0..dbd7492541 100644 --- a/src/main/java/org/cactoos/scalar/SyncScalar.java +++ b/src/main/java/org/cactoos/scalar/SyncScalar.java @@ -28,6 +28,11 @@ /** * Scalar that is thread-safe. * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * * @author Tim Hinkes (timmeey@timmeey.de) * @version $Id$ * @param Type of result diff --git a/src/main/java/org/cactoos/scalar/Ternary.java b/src/main/java/org/cactoos/scalar/Ternary.java index b8cd460026..933df1e291 100644 --- a/src/main/java/org/cactoos/scalar/Ternary.java +++ b/src/main/java/org/cactoos/scalar/Ternary.java @@ -31,6 +31,11 @@ * *

There is no thread-safety guarantee. * + *

This class implements {@link Scalar}, which throws a checked + * {@link Exception}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

+ * * @author Vseslav Sekorin (vssekorin@gmail.com) * @version $Id$ * @param Type of item. diff --git a/src/main/java/org/cactoos/scalar/True.java b/src/main/java/org/cactoos/scalar/True.java index a36fd50a5c..7c514a225b 100644 --- a/src/main/java/org/cactoos/scalar/True.java +++ b/src/main/java/org/cactoos/scalar/True.java @@ -37,7 +37,7 @@ public final class True implements Scalar { @Override - public Boolean value() throws Exception { + public Boolean value() { return true; } } From e263590f1712a22f7fc69fe726b23cffc59cb764 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Wed, 8 Nov 2017 11:05:43 +0200 Subject: [PATCH 11/12] #459 new method --- src/main/java/org/cactoos/scalar/BoolOf.java | 6 +++--- src/main/java/org/cactoos/scalar/SumOf.java | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/cactoos/scalar/BoolOf.java b/src/main/java/org/cactoos/scalar/BoolOf.java index 5b129f1cef..fee574cb21 100644 --- a/src/main/java/org/cactoos/scalar/BoolOf.java +++ b/src/main/java/org/cactoos/scalar/BoolOf.java @@ -52,10 +52,10 @@ public final class BoolOf implements Scalar { /** * Ctor. * - * @param string True or false string + * @param txt True or false string */ - public BoolOf(final String string) { - this(new TextOf(string)); + public BoolOf(final String txt) { + this(new TextOf(txt)); } /** diff --git a/src/main/java/org/cactoos/scalar/SumOf.java b/src/main/java/org/cactoos/scalar/SumOf.java index 0dca6993ab..d25e24c433 100644 --- a/src/main/java/org/cactoos/scalar/SumOf.java +++ b/src/main/java/org/cactoos/scalar/SumOf.java @@ -47,7 +47,7 @@ * @version $Id$ * @since 0.9 */ -public final class SumOf extends Number { +public final class SumOf extends Number implements Scalar { /** * Serialization marker. @@ -199,4 +199,10 @@ public float floatValue() { public double doubleValue() { return new UncheckedScalar<>(this.dsum).value(); } + + @Override + public Number value() { + return this; + } + } From f806833aefb978def4213063de7f714ff5c488b5 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Wed, 8 Nov 2017 11:16:38 +0200 Subject: [PATCH 12/12] #459 NumberOf --- .../java/org/cactoos/scalar/DoubleOf.java | 74 ----------------- src/main/java/org/cactoos/scalar/IntOf.java | 69 ---------------- src/main/java/org/cactoos/scalar/LongOf.java | 69 ---------------- .../scalar/{FloatOf.java => NumberOf.java} | 81 ++++++++++++++++--- .../java/org/cactoos/scalar/FloatOfTest.java | 55 ------------- .../java/org/cactoos/scalar/IntOfTest.java | 55 ------------- .../java/org/cactoos/scalar/LongOfTest.java | 55 ------------- .../{DoubleOfTest.java => NumberOfTest.java} | 57 +++++++++++-- 8 files changed, 121 insertions(+), 394 deletions(-) delete mode 100644 src/main/java/org/cactoos/scalar/DoubleOf.java delete mode 100644 src/main/java/org/cactoos/scalar/IntOf.java delete mode 100644 src/main/java/org/cactoos/scalar/LongOf.java rename src/main/java/org/cactoos/scalar/{FloatOf.java => NumberOf.java} (51%) delete mode 100644 src/test/java/org/cactoos/scalar/FloatOfTest.java delete mode 100644 src/test/java/org/cactoos/scalar/IntOfTest.java delete mode 100644 src/test/java/org/cactoos/scalar/LongOfTest.java rename src/test/java/org/cactoos/scalar/{DoubleOfTest.java => NumberOfTest.java} (51%) diff --git a/src/main/java/org/cactoos/scalar/DoubleOf.java b/src/main/java/org/cactoos/scalar/DoubleOf.java deleted file mode 100644 index a90fcd4325..0000000000 --- a/src/main/java/org/cactoos/scalar/DoubleOf.java +++ /dev/null @@ -1,74 +0,0 @@ -/** - * 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.scalar; - -import java.io.IOException; -import org.cactoos.Scalar; -import org.cactoos.Text; -import org.cactoos.text.TextOf; - -/** - * Text as {@link Double}. - * - *

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

This class implements {@link Scalar}, which throws a checked - * {@link Exception}. This may not be convenient in many cases. To make - * it more convenient and get rid of the checked exception you can - * use {@link UncheckedScalar} or {@link IoCheckedScalar} decorators.

- * - * @author Kirill (g4s8.public@gmail.com) - * @version $Id$ - * @since 0.2 - */ -public final class DoubleOf implements Scalar { - - /** - * Source text. - */ - private final Text origin; - - /** - * Ctor. - * - * @param string Number-string - */ - public DoubleOf(final String string) { - this(new TextOf(string)); - } - - /** - * Ctor. - * - * @param text Number-text - */ - public DoubleOf(final Text text) { - this.origin = text; - } - - @Override - public Double value() throws IOException { - return Double.valueOf(this.origin.asString()); - } -} diff --git a/src/main/java/org/cactoos/scalar/IntOf.java b/src/main/java/org/cactoos/scalar/IntOf.java deleted file mode 100644 index 0264159b88..0000000000 --- a/src/main/java/org/cactoos/scalar/IntOf.java +++ /dev/null @@ -1,69 +0,0 @@ -/** - * 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.scalar; - -import java.io.IOException; -import org.cactoos.Scalar; -import org.cactoos.Text; -import org.cactoos.text.TextOf; - -/** - * Text as {@link Integer}. - * - *

There is no thread-safety guarantee. - * - * @author Kirill (g4s8.public@gmail.com) - * @version $Id$ - * @since 0.2 - */ -public final class IntOf implements Scalar { - - /** - * Source text. - */ - private final Text origin; - - /** - * Ctor. - * - * @param string Number-string - */ - public IntOf(final String string) { - this(new TextOf(string)); - } - - /** - * Ctor. - * - * @param text Number-text - */ - public IntOf(final Text text) { - this.origin = text; - } - - @Override - public Integer value() throws IOException { - return Integer.valueOf(this.origin.asString()); - } -} diff --git a/src/main/java/org/cactoos/scalar/LongOf.java b/src/main/java/org/cactoos/scalar/LongOf.java deleted file mode 100644 index bd071743de..0000000000 --- a/src/main/java/org/cactoos/scalar/LongOf.java +++ /dev/null @@ -1,69 +0,0 @@ -/** - * 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.scalar; - -import java.io.IOException; -import org.cactoos.Scalar; -import org.cactoos.Text; -import org.cactoos.text.TextOf; - -/** - * Text as {@link Long}. - * - *

There is no thread-safety guarantee. - * - * @author Kirill (g4s8.public@gmail.com) - * @version $Id$ - * @since 0.2 - */ -public final class LongOf implements Scalar { - - /** - * Source text. - */ - private final Text origin; - - /** - * Ctor. - * - * @param string Number-string - */ - public LongOf(final String string) { - this(new TextOf(string)); - } - - /** - * Ctor. - * - * @param text Number-text - */ - public LongOf(final Text text) { - this.origin = text; - } - - @Override - public Long value() throws IOException { - return Long.valueOf(this.origin.asString()); - } -} diff --git a/src/main/java/org/cactoos/scalar/FloatOf.java b/src/main/java/org/cactoos/scalar/NumberOf.java similarity index 51% rename from src/main/java/org/cactoos/scalar/FloatOf.java rename to src/main/java/org/cactoos/scalar/NumberOf.java index 64b9685fbd..18866b0597 100644 --- a/src/main/java/org/cactoos/scalar/FloatOf.java +++ b/src/main/java/org/cactoos/scalar/NumberOf.java @@ -23,7 +23,6 @@ */ package org.cactoos.scalar; -import java.io.IOException; import org.cactoos.Scalar; import org.cactoos.Text; import org.cactoos.text.TextOf; @@ -42,20 +41,40 @@ * @version $Id$ * @since 0.2 */ -public final class FloatOf implements Scalar { +public final class NumberOf extends Number implements Scalar { /** - * Source text. + * Serialization marker. */ - private final Text origin; + private static final long serialVersionUID = -1924406337256921883L; + + /** + * The LONG number. + */ + private final Scalar lnum; + + /** + * The INT number. + */ + private final Scalar inum; + + /** + * The FLOAT number. + */ + private final Scalar fnum; + + /** + * The DOUBLE number. + */ + private final Scalar dnum; /** * Ctor. * - * @param string Number-string + * @param txt Number-string */ - public FloatOf(final String string) { - this(new TextOf(string)); + public NumberOf(final String txt) { + this(new TextOf(txt)); } /** @@ -63,12 +82,52 @@ public FloatOf(final String string) { * * @param text Number-text */ - public FloatOf(final Text text) { - this.origin = text; + public NumberOf(final Text text) { + super(); + this.lnum = new SyncScalar<>( + new StickyScalar<>( + () -> Long.parseLong(text.asString()) + ) + ); + this.inum = new SyncScalar<>( + new StickyScalar<>( + () -> Integer.parseInt(text.asString()) + ) + ); + this.fnum = new SyncScalar<>( + new StickyScalar<>( + () -> Float.parseFloat(text.asString()) + ) + ); + this.dnum = new SyncScalar<>( + new StickyScalar<>( + () -> Double.parseDouble(text.asString()) + ) + ); + } + + @Override + public Number value() { + return this; + } + + @Override + public int intValue() { + return new UncheckedScalar<>(this.inum).value(); + } + + @Override + public long longValue() { + return new UncheckedScalar<>(this.lnum).value(); + } + + @Override + public float floatValue() { + return new UncheckedScalar<>(this.fnum).value(); } @Override - public Float value() throws IOException { - return Float.valueOf(this.origin.asString()); + public double doubleValue() { + return new UncheckedScalar<>(this.dnum).value(); } } diff --git a/src/test/java/org/cactoos/scalar/FloatOfTest.java b/src/test/java/org/cactoos/scalar/FloatOfTest.java deleted file mode 100644 index a3f381bfe7..0000000000 --- a/src/test/java/org/cactoos/scalar/FloatOfTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * 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.scalar; - -import java.io.IOException; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link FloatOf}. - * - * @author Kirill (g4s8.public@gmail.com) - * @version $Id$ - * @since 0.2 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class FloatOfTest { - - @Test - public strictfp void numberTest() throws IOException { - MatcherAssert.assertThat( - "Can't parse float number", - new FloatOf("1656.894").value(), - // @checkstyle MagicNumber (1 line) - Matchers.equalTo(1656.894F) - ); - } - - @Test(expected = NumberFormatException.class) - public void failsIfTextDoesNotRepresentAFloat() throws IOException { - new FloatOf("abc").value(); - } -} diff --git a/src/test/java/org/cactoos/scalar/IntOfTest.java b/src/test/java/org/cactoos/scalar/IntOfTest.java deleted file mode 100644 index ebda11991a..0000000000 --- a/src/test/java/org/cactoos/scalar/IntOfTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * 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.scalar; - -import java.io.IOException; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link IntOf}. - * - * @author Kirill (g4s8.public@gmail.com) - * @version $Id$ - * @since 0.2 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class IntOfTest { - - @Test - public void numberTest() throws IOException { - MatcherAssert.assertThat( - "Can't parse integer number", - new IntOf("1867892354").value(), - // @checkstyle MagicNumber (1 line) - Matchers.equalTo(1867892354) - ); - } - - @Test(expected = NumberFormatException.class) - public void failsIfTextDoesNotRepresentAnInt() throws IOException { - new DoubleOf("abc").value(); - } -} diff --git a/src/test/java/org/cactoos/scalar/LongOfTest.java b/src/test/java/org/cactoos/scalar/LongOfTest.java deleted file mode 100644 index a6b2497fb8..0000000000 --- a/src/test/java/org/cactoos/scalar/LongOfTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * 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.scalar; - -import java.io.IOException; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link LongOf}. - * - * @author Kirill (g4s8.public@gmail.com) - * @version $Id$ - * @since 0.2 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class LongOfTest { - - @Test - public void numberTest() throws IOException { - MatcherAssert.assertThat( - "Can't parse long number", - new LongOf("186789235425346").value(), - // @checkstyle MagicNumber (1 line) - Matchers.equalTo(186789235425346L) - ); - } - - @Test(expected = NumberFormatException.class) - public void failsIfTextDoesNotRepresentALong() throws IOException { - new LongOf("abc").value(); - } -} diff --git a/src/test/java/org/cactoos/scalar/DoubleOfTest.java b/src/test/java/org/cactoos/scalar/NumberOfTest.java similarity index 51% rename from src/test/java/org/cactoos/scalar/DoubleOfTest.java rename to src/test/java/org/cactoos/scalar/NumberOfTest.java index 74351fa5e1..e7dc8cb5f9 100644 --- a/src/test/java/org/cactoos/scalar/DoubleOfTest.java +++ b/src/test/java/org/cactoos/scalar/NumberOfTest.java @@ -29,27 +29,72 @@ import org.junit.Test; /** - * Test case for {@link DoubleOf}. + * Test case for {@link NumberOf}. * * @author Kirill (g4s8.public@gmail.com) * @version $Id$ * @since 0.2 * @checkstyle JavadocMethodCheck (500 lines) */ -public final class DoubleOfTest { +public final class NumberOfTest { @Test - public strictfp void numberTest() throws IOException { + public void parsesFloat() throws IOException { + MatcherAssert.assertThat( + "Can't parse float number", + new NumberOf("1656.894").floatValue(), + // @checkstyle MagicNumber (1 line) + Matchers.equalTo(1656.894F) + ); + } + + @Test(expected = RuntimeException.class) + public void failsIfTextDoesNotRepresentAFloat() throws IOException { + new NumberOf("abcfds").floatValue(); + } + + @Test + public void parsesLong() throws IOException { + MatcherAssert.assertThat( + "Can't parse long number", + new NumberOf("186789235425346").longValue(), + // @checkstyle MagicNumber (1 line) + Matchers.equalTo(186789235425346L) + ); + } + + @Test(expected = RuntimeException.class) + public void failsIfTextDoesNotRepresentALong() throws IOException { + new NumberOf("abcddd").longValue(); + } + + @Test + public void parsesInteger() throws IOException { + MatcherAssert.assertThat( + "Can't parse integer number", + new NumberOf("1867892354").intValue(), + // @checkstyle MagicNumber (1 line) + Matchers.equalTo(1867892354) + ); + } + + @Test(expected = RuntimeException.class) + public void failsIfTextDoesNotRepresentAnInt() throws IOException { + new NumberOf("abc fdsf").intValue(); + } + + @Test + public void parsesDouble() throws IOException { MatcherAssert.assertThat( "Can't parse double number", - new DoubleOf("185.65156465123").value(), + new NumberOf("185.65156465123").doubleValue(), // @checkstyle MagicNumber (1 line) Matchers.equalTo(185.65156465123) ); } - @Test(expected = NumberFormatException.class) + @Test(expected = RuntimeException.class) public void failsIfTextDoesNotRepresentADouble() throws IOException { - new DoubleOf("abc").value(); + new NumberOf("abfdsc").doubleValue(); } }