From cd467c19f8f175afdf7957b37653711ae16798ab Mon Sep 17 00:00:00 2001 From: mehyil Date: Sat, 3 Jun 2017 14:33:19 +0300 Subject: [PATCH 001/169] Reverse The Text --- .../java/org/cactoos/text/ReversedText.java | 56 +++++++++++++++++++ .../org/cactoos/text/ReversedTextTest.java | 51 +++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/main/java/org/cactoos/text/ReversedText.java create mode 100644 src/test/java/org/cactoos/text/ReversedTextTest.java diff --git a/src/main/java/org/cactoos/text/ReversedText.java b/src/main/java/org/cactoos/text/ReversedText.java new file mode 100644 index 0000000000..e32e0be611 --- /dev/null +++ b/src/main/java/org/cactoos/text/ReversedText.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.Text; + +/** + * Reverse the Text. + * + * @author Mehmet Yildirim (memoyil@gmail.com) + * @version $Id$ + * @since 0.2 + */ +public final class ReversedText implements Text { + + /** + * The text. + */ + private final Text origin; + + /** + * Ctor. + * + * @param text The text + */ + public ReversedText(final Text text) { + this.origin = text; + } + + @Override + public String asString() throws IOException { + return new StringBuilder(this.origin.asString()).reverse().toString(); + } +} diff --git a/src/test/java/org/cactoos/text/ReversedTextTest.java b/src/test/java/org/cactoos/text/ReversedTextTest.java new file mode 100644 index 0000000000..3887d5bb50 --- /dev/null +++ b/src/test/java/org/cactoos/text/ReversedTextTest.java @@ -0,0 +1,51 @@ +/** + * 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 org.cactoos.TextHasString; +import org.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test case for {@link ReplacedText}. + * + * @author Mehmet Yildirim (memoyil@gmail.com) + * @version $Id$ + * @since 0.2 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class ReversedTextTest { + + @Test + public void reverseText() { + MatcherAssert.assertThat( + "Can't reverse a text", + new ReversedText( + new StringAsText("Hello!") + ), + new TextHasString("!olleH") + ); + } + +} From e0b69cb45cc3f1ff1f526273d19b2cc29d8f6673 Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Sat, 3 Jun 2017 09:12:49 -0300 Subject: [PATCH 002/169] #3: Not null text --- .../java/org/cactoos/text/NotNullText.java | 60 +++++++++++++++++++ .../org/cactoos/text/NotNullTextTest.java | 57 ++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/main/java/org/cactoos/text/NotNullText.java create mode 100644 src/test/java/org/cactoos/text/NotNullTextTest.java diff --git a/src/main/java/org/cactoos/text/NotNullText.java b/src/main/java/org/cactoos/text/NotNullText.java new file mode 100644 index 0000000000..691766a092 --- /dev/null +++ b/src/main/java/org/cactoos/text/NotNullText.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.text; + +import java.io.IOException; +import org.cactoos.Text; + +/** + * A safe Text. + * + *

There is no thread-safety guarantee. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.2 + */ +public final class NotNullText implements Text { + + /** + * The text. + */ + private final Text origin; + + /** + * Ctor. + * @param text The text + */ + public NotNullText(final Text text) { + this.origin = text; + } + + @Override + public String asString() throws IOException { + if (this.origin == null) { + throw new IOException("invalid text (null)"); + } + return this.origin.asString(); + } +} diff --git a/src/test/java/org/cactoos/text/NotNullTextTest.java b/src/test/java/org/cactoos/text/NotNullTextTest.java new file mode 100644 index 0000000000..b374b4b038 --- /dev/null +++ b/src/test/java/org/cactoos/text/NotNullTextTest.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 NotNullText}. + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.2 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class NotNullTextTest { + + @Test(expected = IOException.class) + public void failForNullText() throws IOException { + new NotNullText(null).asString(); + } + + @Test + public void checkForNullText() throws Exception { + final String message = "Hello"; + MatcherAssert.assertThat( + "Can't work with null text", + new NotNullText( + new StringAsText(message) + ), + new TextHasString(message) + ); + } + +} From b4da7692519de95820c6022813af930fd2d8380a Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Sat, 3 Jun 2017 09:13:47 -0300 Subject: [PATCH 003/169] #3: Not null input --- .../java/org/cactoos/io/NotNullInput.java | 62 +++++++++++++++++++ .../org/cactoos/text/NotNullInputTest.java | 44 +++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/main/java/org/cactoos/io/NotNullInput.java create mode 100644 src/test/java/org/cactoos/text/NotNullInputTest.java diff --git a/src/main/java/org/cactoos/io/NotNullInput.java b/src/main/java/org/cactoos/io/NotNullInput.java new file mode 100644 index 0000000000..fff531b250 --- /dev/null +++ b/src/main/java/org/cactoos/io/NotNullInput.java @@ -0,0 +1,62 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.io; + +import java.io.IOException; +import java.io.InputStream; +import org.cactoos.Input; + +/** + * A safe Input. + * + *

There is no thread-safety guarantee. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.2 + */ +public final class NotNullInput implements Input { + + /** + * The input. + */ + private final Input origin; + + /** + * Ctor. + * @param input The input + */ + public NotNullInput(final Input input) { + this.origin = input; + } + + @Override + public InputStream stream() throws IOException { + if (this.origin == null) { + throw new IOException("invalid input (null)"); + } + return this.origin.stream(); + } + +} diff --git a/src/test/java/org/cactoos/text/NotNullInputTest.java b/src/test/java/org/cactoos/text/NotNullInputTest.java new file mode 100644 index 0000000000..08abca4426 --- /dev/null +++ b/src/test/java/org/cactoos/text/NotNullInputTest.java @@ -0,0 +1,44 @@ +/** + * 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.io.NotNullInput; +import org.junit.Test; + +/** + * Test case for {@link NotNullInput}. + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.2 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class NotNullInputTest { + + @Test(expected = IOException.class) + public void failForNullInput() throws IOException { + new NotNullInput(null).stream(); + } + +} From d0fe41e9b9cee89883aa2ad3817a8b11653e83b9 Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Sat, 3 Jun 2017 09:14:09 -0300 Subject: [PATCH 004/169] #3: Not null output --- .../java/org/cactoos/io/NotNullOutput.java | 62 +++++++++++++++++++ .../org/cactoos/text/NotNullOutputTest.java | 45 ++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/main/java/org/cactoos/io/NotNullOutput.java create mode 100644 src/test/java/org/cactoos/text/NotNullOutputTest.java diff --git a/src/main/java/org/cactoos/io/NotNullOutput.java b/src/main/java/org/cactoos/io/NotNullOutput.java new file mode 100644 index 0000000000..c4324ecdd5 --- /dev/null +++ b/src/main/java/org/cactoos/io/NotNullOutput.java @@ -0,0 +1,62 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.io; + +import java.io.IOException; +import java.io.OutputStream; +import org.cactoos.Output; + +/** + * A safe Output. + * + *

There is no thread-safety guarantee. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.2 + */ +public final class NotNullOutput implements Output { + + /** + * The output. + */ + private final Output origin; + + /** + * Ctor. + * @param output The output + */ + public NotNullOutput(final Output output) { + this.origin = output; + } + + @Override + public OutputStream stream() throws IOException { + if (this.origin == null) { + throw new IOException("invalid output (null)"); + } + return this.origin.stream(); + } + +} diff --git a/src/test/java/org/cactoos/text/NotNullOutputTest.java b/src/test/java/org/cactoos/text/NotNullOutputTest.java new file mode 100644 index 0000000000..6b06aa2de2 --- /dev/null +++ b/src/test/java/org/cactoos/text/NotNullOutputTest.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.text; + +import java.io.IOException; +import org.cactoos.io.NotNullInput; +import org.cactoos.io.NotNullOutput; +import org.junit.Test; + +/** + * Test case for {@link NotNullInput}. + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.2 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class NotNullOutputTest { + + @Test(expected = IOException.class) + public void failForNullOutput() throws IOException { + new NotNullOutput(null).stream(); + } + +} From b36c24585974c27e3521bc07ab3b015511ab8619 Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Sat, 3 Jun 2017 16:33:50 -0300 Subject: [PATCH 005/169] #74: Unchecked scalar --- .../org/cactoos/func/UncheckedScalar.java | 63 +++++++++++++++++++ .../org/cactoos/func/UncheckedScalarTest.java | 48 ++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/main/java/org/cactoos/func/UncheckedScalar.java create mode 100644 src/test/java/org/cactoos/func/UncheckedScalarTest.java diff --git a/src/main/java/org/cactoos/func/UncheckedScalar.java b/src/main/java/org/cactoos/func/UncheckedScalar.java new file mode 100644 index 0000000000..88bc2971f0 --- /dev/null +++ b/src/main/java/org/cactoos/func/UncheckedScalar.java @@ -0,0 +1,63 @@ +/** + * 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 java.io.IOException; +import org.cactoos.Scalar; + +/** + * Scalar that doesn't throw checked {@link Exception}. + * + *

There is no thread-safety guarantee. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @param Type of result + * @since 0.2 + */ +public final class UncheckedScalar implements Scalar { + + /** + * Original scalar. + */ + private final Scalar scalar; + + /** + * Ctor. + * @param scalar Encapsulated scalar + */ + public UncheckedScalar(final Scalar scalar) { + this.scalar = scalar; + } + + @Override + public T asValue() { + try { + return this.scalar.asValue(); + } catch (final IOException ex) { + throw new IllegalStateException(ex); + } + } + +} diff --git a/src/test/java/org/cactoos/func/UncheckedScalarTest.java b/src/test/java/org/cactoos/func/UncheckedScalarTest.java new file mode 100644 index 0000000000..490939180b --- /dev/null +++ b/src/test/java/org/cactoos/func/UncheckedScalarTest.java @@ -0,0 +1,48 @@ +/** + * 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 java.io.IOException; +import org.junit.Test; + +/** + * Test case for {@link UncheckedScalar}. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.2 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class UncheckedScalarTest { + + @Test(expected = RuntimeException.class) + public void rethrowsCheckedToUncheckedException() { + new UncheckedScalar<>( + () -> { + throw new IOException("intended"); + } + ).asValue(); + } + +} From a0a10d231db45bdc49955c523005f908ed598f5a Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Sat, 3 Jun 2017 16:38:50 -0300 Subject: [PATCH 006/169] #74: Unchecked text --- .../java/org/cactoos/func/UncheckedText.java | 62 +++++++++++++++++++ .../org/cactoos/func/UncheckedTextTest.java | 48 ++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 src/main/java/org/cactoos/func/UncheckedText.java create mode 100644 src/test/java/org/cactoos/func/UncheckedTextTest.java diff --git a/src/main/java/org/cactoos/func/UncheckedText.java b/src/main/java/org/cactoos/func/UncheckedText.java new file mode 100644 index 0000000000..025a0f5d75 --- /dev/null +++ b/src/main/java/org/cactoos/func/UncheckedText.java @@ -0,0 +1,62 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.func; + +import java.io.IOException; +import org.cactoos.Text; + +/** + * Text that doesn't throw checked {@link Exception}. + * + *

There is no thread-safety guarantee. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.2 + */ +public final class UncheckedText implements Text { + + /** + * Original text. + */ + private final Text text; + + /** + * Ctor. + * @param text Encapsulated text + */ + public UncheckedText(final Text text) { + this.text = text; + } + + @Override + public String asString() { + try { + return this.text.asString(); + } catch (final IOException ex) { + throw new IllegalStateException(ex); + } + } + +} diff --git a/src/test/java/org/cactoos/func/UncheckedTextTest.java b/src/test/java/org/cactoos/func/UncheckedTextTest.java new file mode 100644 index 0000000000..c7a956890f --- /dev/null +++ b/src/test/java/org/cactoos/func/UncheckedTextTest.java @@ -0,0 +1,48 @@ +/** + * 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 java.io.IOException; +import org.junit.Test; + +/** + * Test case for {@link UncheckedText}. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.2 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class UncheckedTextTest { + + @Test(expected = RuntimeException.class) + public void rethrowsCheckedToUncheckedException() { + new UncheckedText( + () -> { + throw new IOException("intended"); + } + ).asString(); + } + +} From 6e4fca0b2e803b743f35c564ce34d0583292f62e Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Sat, 3 Jun 2017 16:43:36 -0300 Subject: [PATCH 007/169] #74: Unchecked bytes --- .../java/org/cactoos/func/UncheckedBytes.java | 62 +++++++++++++++++++ .../org/cactoos/func/UncheckedBytesTest.java | 48 ++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 src/main/java/org/cactoos/func/UncheckedBytes.java create mode 100644 src/test/java/org/cactoos/func/UncheckedBytesTest.java diff --git a/src/main/java/org/cactoos/func/UncheckedBytes.java b/src/main/java/org/cactoos/func/UncheckedBytes.java new file mode 100644 index 0000000000..75916a6858 --- /dev/null +++ b/src/main/java/org/cactoos/func/UncheckedBytes.java @@ -0,0 +1,62 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.func; + +import java.io.IOException; +import org.cactoos.Bytes; + +/** + * Bytes that doesn't throw checked {@link Exception}. + * + *

There is no thread-safety guarantee. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.2 + */ +public final class UncheckedBytes implements Bytes { + + /** + * Original bytes. + */ + private final Bytes bytes; + + /** + * Ctor. + * @param bytes Encapsulated bytes + */ + public UncheckedBytes(final Bytes bytes) { + this.bytes = bytes; + } + + @Override + public byte[] asBytes() { + try { + return this.bytes.asBytes(); + } catch (final IOException ex) { + throw new IllegalStateException(ex); + } + } + +} diff --git a/src/test/java/org/cactoos/func/UncheckedBytesTest.java b/src/test/java/org/cactoos/func/UncheckedBytesTest.java new file mode 100644 index 0000000000..dd6a153ed1 --- /dev/null +++ b/src/test/java/org/cactoos/func/UncheckedBytesTest.java @@ -0,0 +1,48 @@ +/** + * 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 java.io.IOException; +import org.junit.Test; + +/** + * Test case for {@link UncheckedBytes}. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.2 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class UncheckedBytesTest { + + @Test(expected = RuntimeException.class) + public void rethrowsCheckedToUncheckedException() { + new UncheckedBytes( + () -> { + throw new IOException("intended"); + } + ).asBytes(); + } + +} From 3643744214e955469938798cd265b187a3fa42be Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Sun, 4 Jun 2017 08:43:19 -0300 Subject: [PATCH 008/169] #3: Changed to correct version --- src/main/java/org/cactoos/io/NotNullInput.java | 2 +- src/main/java/org/cactoos/io/NotNullOutput.java | 2 +- src/main/java/org/cactoos/text/NotNullText.java | 2 +- src/test/java/org/cactoos/text/NotNullInputTest.java | 2 +- src/test/java/org/cactoos/text/NotNullOutputTest.java | 2 +- src/test/java/org/cactoos/text/NotNullTextTest.java | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/cactoos/io/NotNullInput.java b/src/main/java/org/cactoos/io/NotNullInput.java index fff531b250..4dbf4983b4 100644 --- a/src/main/java/org/cactoos/io/NotNullInput.java +++ b/src/main/java/org/cactoos/io/NotNullInput.java @@ -34,7 +34,7 @@ * * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ - * @since 0.2 + * @since 0.3 */ public final class NotNullInput implements Input { diff --git a/src/main/java/org/cactoos/io/NotNullOutput.java b/src/main/java/org/cactoos/io/NotNullOutput.java index c4324ecdd5..b79c46ed14 100644 --- a/src/main/java/org/cactoos/io/NotNullOutput.java +++ b/src/main/java/org/cactoos/io/NotNullOutput.java @@ -34,7 +34,7 @@ * * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ - * @since 0.2 + * @since 0.3 */ public final class NotNullOutput implements Output { diff --git a/src/main/java/org/cactoos/text/NotNullText.java b/src/main/java/org/cactoos/text/NotNullText.java index 691766a092..f47828fbcd 100644 --- a/src/main/java/org/cactoos/text/NotNullText.java +++ b/src/main/java/org/cactoos/text/NotNullText.java @@ -33,7 +33,7 @@ * * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ - * @since 0.2 + * @since 0.3 */ public final class NotNullText implements Text { diff --git a/src/test/java/org/cactoos/text/NotNullInputTest.java b/src/test/java/org/cactoos/text/NotNullInputTest.java index 08abca4426..b76b1bc508 100644 --- a/src/test/java/org/cactoos/text/NotNullInputTest.java +++ b/src/test/java/org/cactoos/text/NotNullInputTest.java @@ -31,7 +31,7 @@ * Test case for {@link NotNullInput}. * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ - * @since 0.2 + * @since 0.3 * @checkstyle JavadocMethodCheck (500 lines) */ public final class NotNullInputTest { diff --git a/src/test/java/org/cactoos/text/NotNullOutputTest.java b/src/test/java/org/cactoos/text/NotNullOutputTest.java index 6b06aa2de2..08f00fa4e5 100644 --- a/src/test/java/org/cactoos/text/NotNullOutputTest.java +++ b/src/test/java/org/cactoos/text/NotNullOutputTest.java @@ -32,7 +32,7 @@ * Test case for {@link NotNullInput}. * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ - * @since 0.2 + * @since 0.3 * @checkstyle JavadocMethodCheck (500 lines) */ public final class NotNullOutputTest { diff --git a/src/test/java/org/cactoos/text/NotNullTextTest.java b/src/test/java/org/cactoos/text/NotNullTextTest.java index b374b4b038..76fca6fa16 100644 --- a/src/test/java/org/cactoos/text/NotNullTextTest.java +++ b/src/test/java/org/cactoos/text/NotNullTextTest.java @@ -32,7 +32,7 @@ * Test case for {@link NotNullText}. * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ - * @since 0.2 + * @since 0.3 * @checkstyle JavadocMethodCheck (500 lines) */ public final class NotNullTextTest { From f149216312846428053fe47b6deaffde0661336c Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Sun, 4 Jun 2017 08:44:16 -0300 Subject: [PATCH 009/169] #3: Moved some io tests to correct the package --- src/test/java/org/cactoos/{text => io}/NotNullInputTest.java | 3 +-- src/test/java/org/cactoos/{text => io}/NotNullOutputTest.java | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) rename src/test/java/org/cactoos/{text => io}/NotNullInputTest.java (96%) rename src/test/java/org/cactoos/{text => io}/NotNullOutputTest.java (94%) diff --git a/src/test/java/org/cactoos/text/NotNullInputTest.java b/src/test/java/org/cactoos/io/NotNullInputTest.java similarity index 96% rename from src/test/java/org/cactoos/text/NotNullInputTest.java rename to src/test/java/org/cactoos/io/NotNullInputTest.java index b76b1bc508..fe64040b24 100644 --- a/src/test/java/org/cactoos/text/NotNullInputTest.java +++ b/src/test/java/org/cactoos/io/NotNullInputTest.java @@ -21,10 +21,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.cactoos.text; +package org.cactoos.io; import java.io.IOException; -import org.cactoos.io.NotNullInput; import org.junit.Test; /** diff --git a/src/test/java/org/cactoos/text/NotNullOutputTest.java b/src/test/java/org/cactoos/io/NotNullOutputTest.java similarity index 94% rename from src/test/java/org/cactoos/text/NotNullOutputTest.java rename to src/test/java/org/cactoos/io/NotNullOutputTest.java index 08f00fa4e5..4004e7e748 100644 --- a/src/test/java/org/cactoos/text/NotNullOutputTest.java +++ b/src/test/java/org/cactoos/io/NotNullOutputTest.java @@ -21,11 +21,9 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.cactoos.text; +package org.cactoos.io; import java.io.IOException; -import org.cactoos.io.NotNullInput; -import org.cactoos.io.NotNullOutput; import org.junit.Test; /** From 4204fd3f7e5d2b51a8e5998206396fc4881f0405 Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Sun, 4 Jun 2017 08:54:34 -0300 Subject: [PATCH 010/169] #74: Moved some io tests to correct the package --- src/main/java/org/cactoos/func/UncheckedScalar.java | 2 +- src/main/java/org/cactoos/{func => io}/UncheckedBytes.java | 4 ++-- src/main/java/org/cactoos/{func => text}/UncheckedText.java | 4 ++-- src/test/java/org/cactoos/func/UncheckedScalarTest.java | 2 +- .../java/org/cactoos/{func => io}/UncheckedBytesTest.java | 4 ++-- .../java/org/cactoos/{func => text}/UncheckedTextTest.java | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) rename src/main/java/org/cactoos/{func => io}/UncheckedBytes.java (97%) rename src/main/java/org/cactoos/{func => text}/UncheckedText.java (97%) rename src/test/java/org/cactoos/{func => io}/UncheckedBytesTest.java (97%) rename src/test/java/org/cactoos/{func => text}/UncheckedTextTest.java (97%) diff --git a/src/main/java/org/cactoos/func/UncheckedScalar.java b/src/main/java/org/cactoos/func/UncheckedScalar.java index 88bc2971f0..285e9d4a25 100644 --- a/src/main/java/org/cactoos/func/UncheckedScalar.java +++ b/src/main/java/org/cactoos/func/UncheckedScalar.java @@ -34,7 +34,7 @@ * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ * @param Type of result - * @since 0.2 + * @since 0.3 */ public final class UncheckedScalar implements Scalar { diff --git a/src/main/java/org/cactoos/func/UncheckedBytes.java b/src/main/java/org/cactoos/io/UncheckedBytes.java similarity index 97% rename from src/main/java/org/cactoos/func/UncheckedBytes.java rename to src/main/java/org/cactoos/io/UncheckedBytes.java index 75916a6858..d082933cc4 100644 --- a/src/main/java/org/cactoos/func/UncheckedBytes.java +++ b/src/main/java/org/cactoos/io/UncheckedBytes.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.cactoos.func; +package org.cactoos.io; import java.io.IOException; import org.cactoos.Bytes; @@ -33,7 +33,7 @@ * * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ - * @since 0.2 + * @since 0.3 */ public final class UncheckedBytes implements Bytes { diff --git a/src/main/java/org/cactoos/func/UncheckedText.java b/src/main/java/org/cactoos/text/UncheckedText.java similarity index 97% rename from src/main/java/org/cactoos/func/UncheckedText.java rename to src/main/java/org/cactoos/text/UncheckedText.java index 025a0f5d75..449e06b80d 100644 --- a/src/main/java/org/cactoos/func/UncheckedText.java +++ b/src/main/java/org/cactoos/text/UncheckedText.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.cactoos.func; +package org.cactoos.text; import java.io.IOException; import org.cactoos.Text; @@ -33,7 +33,7 @@ * * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ - * @since 0.2 + * @since 0.3 */ public final class UncheckedText implements Text { diff --git a/src/test/java/org/cactoos/func/UncheckedScalarTest.java b/src/test/java/org/cactoos/func/UncheckedScalarTest.java index 490939180b..22ea9e4c37 100644 --- a/src/test/java/org/cactoos/func/UncheckedScalarTest.java +++ b/src/test/java/org/cactoos/func/UncheckedScalarTest.java @@ -31,7 +31,7 @@ * * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ - * @since 0.2 + * @since 0.3 * @checkstyle JavadocMethodCheck (500 lines) */ public final class UncheckedScalarTest { diff --git a/src/test/java/org/cactoos/func/UncheckedBytesTest.java b/src/test/java/org/cactoos/io/UncheckedBytesTest.java similarity index 97% rename from src/test/java/org/cactoos/func/UncheckedBytesTest.java rename to src/test/java/org/cactoos/io/UncheckedBytesTest.java index dd6a153ed1..11d1641b65 100644 --- a/src/test/java/org/cactoos/func/UncheckedBytesTest.java +++ b/src/test/java/org/cactoos/io/UncheckedBytesTest.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.cactoos.func; +package org.cactoos.io; import java.io.IOException; import org.junit.Test; @@ -31,7 +31,7 @@ * * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ - * @since 0.2 + * @since 0.3 * @checkstyle JavadocMethodCheck (500 lines) */ public final class UncheckedBytesTest { diff --git a/src/test/java/org/cactoos/func/UncheckedTextTest.java b/src/test/java/org/cactoos/text/UncheckedTextTest.java similarity index 97% rename from src/test/java/org/cactoos/func/UncheckedTextTest.java rename to src/test/java/org/cactoos/text/UncheckedTextTest.java index c7a956890f..339dff67eb 100644 --- a/src/test/java/org/cactoos/func/UncheckedTextTest.java +++ b/src/test/java/org/cactoos/text/UncheckedTextTest.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.cactoos.func; +package org.cactoos.text; import java.io.IOException; import org.junit.Test; @@ -31,7 +31,7 @@ * * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ - * @since 0.2 + * @since 0.3 * @checkstyle JavadocMethodCheck (500 lines) */ public final class UncheckedTextTest { From 768fb1aacf1dcca929621d3f847d5e24bfe1105a Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Sun, 4 Jun 2017 12:11:27 -0300 Subject: [PATCH 011/169] #74: Changed IllegalArgumentException to UncheckedIOException --- src/main/java/org/cactoos/func/UncheckedScalar.java | 3 ++- src/main/java/org/cactoos/io/UncheckedBytes.java | 3 ++- src/main/java/org/cactoos/text/UncheckedText.java | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/cactoos/func/UncheckedScalar.java b/src/main/java/org/cactoos/func/UncheckedScalar.java index 285e9d4a25..ff9834e59d 100644 --- a/src/main/java/org/cactoos/func/UncheckedScalar.java +++ b/src/main/java/org/cactoos/func/UncheckedScalar.java @@ -24,6 +24,7 @@ package org.cactoos.func; import java.io.IOException; +import java.io.UncheckedIOException; import org.cactoos.Scalar; /** @@ -56,7 +57,7 @@ public T asValue() { try { return this.scalar.asValue(); } catch (final IOException ex) { - throw new IllegalStateException(ex); + throw new UncheckedIOException(ex); } } diff --git a/src/main/java/org/cactoos/io/UncheckedBytes.java b/src/main/java/org/cactoos/io/UncheckedBytes.java index d082933cc4..f70fb04b62 100644 --- a/src/main/java/org/cactoos/io/UncheckedBytes.java +++ b/src/main/java/org/cactoos/io/UncheckedBytes.java @@ -24,6 +24,7 @@ package org.cactoos.io; import java.io.IOException; +import java.io.UncheckedIOException; import org.cactoos.Bytes; /** @@ -55,7 +56,7 @@ public byte[] asBytes() { try { return this.bytes.asBytes(); } catch (final IOException ex) { - throw new IllegalStateException(ex); + throw new UncheckedIOException(ex); } } diff --git a/src/main/java/org/cactoos/text/UncheckedText.java b/src/main/java/org/cactoos/text/UncheckedText.java index 449e06b80d..e2085bbb5b 100644 --- a/src/main/java/org/cactoos/text/UncheckedText.java +++ b/src/main/java/org/cactoos/text/UncheckedText.java @@ -24,6 +24,7 @@ package org.cactoos.text; import java.io.IOException; +import java.io.UncheckedIOException; import org.cactoos.Text; /** @@ -55,7 +56,7 @@ public String asString() { try { return this.text.asString(); } catch (final IOException ex) { - throw new IllegalStateException(ex); + throw new UncheckedIOException(ex); } } From 485e6d1fdb470a3a02fa950f68aaf56999e6d6ef Mon Sep 17 00:00:00 2001 From: Tim Hinkes Date: Mon, 5 Jun 2017 16:31:45 +0200 Subject: [PATCH 012/169] Removed remove() operation from FilteredIterator FilteredIterator can not support remove() operation at the current time in a correct way since it needs to look ahead on the underlying iterator. --- src/main/java/org/cactoos/list/FilteredIterator.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/list/FilteredIterator.java b/src/main/java/org/cactoos/list/FilteredIterator.java index a4c7739eee..a25f6b445b 100644 --- a/src/main/java/org/cactoos/list/FilteredIterator.java +++ b/src/main/java/org/cactoos/list/FilteredIterator.java @@ -108,7 +108,9 @@ public X next() { @Override public void remove() { - this.iterator.remove(); + throw new UnsupportedOperationException( + "FilteredIterator does not support remove Operation" + ); } } From f1e99df06af69a2de49c3a80a3a00c70cb7ea228 Mon Sep 17 00:00:00 2001 From: Tim Hinkes Date: Mon, 5 Jun 2017 19:24:56 +0200 Subject: [PATCH 013/169] Added Cached Version of scalar --- .../java/org/cactoos/func/CachedScalar.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/main/java/org/cactoos/func/CachedScalar.java diff --git a/src/main/java/org/cactoos/func/CachedScalar.java b/src/main/java/org/cactoos/func/CachedScalar.java new file mode 100644 index 0000000000..47fc3a310f --- /dev/null +++ b/src/main/java/org/cactoos/func/CachedScalar.java @@ -0,0 +1,69 @@ +/** + * 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 java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.cactoos.Scalar; + +/** + * Cached version of a Scalar. + * + *

There is no thread-safety guarantee. + * + * @author Tim Hinkes (timmeey@timmeey.de) + * @version $Id$ + * @param Type of result + * @since 0.3 + */ +public final class CachedScalar implements Scalar { + + /** + * The scalar to cache. + */ + private final Scalar source; + + /** + * The list used as a optional value. + */ + private final List cache; + + /** + * Ctor. + * @param source The Scalar to cache + */ + public CachedScalar(final Scalar source) { + this.source = source; + this.cache = new ArrayList<>(1); + } + + @Override + public T asValue() throws IOException { + if (this.cache.isEmpty()) { + this.cache.add(this.source.asValue()); + } + return this.cache.get(0); + } +} From a5e39581705fd6e91cb6ac4c3ce977c3e17cb92b Mon Sep 17 00:00:00 2001 From: Tim Hinkes Date: Mon, 5 Jun 2017 19:25:57 +0200 Subject: [PATCH 014/169] Added usage of cachedScalar for size() --- src/main/java/org/cactoos/list/IterableAsList.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index 7e0d850c6d..05aaceae3c 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -26,6 +26,8 @@ import java.util.AbstractList; import java.util.ArrayList; import java.util.List; +import org.cactoos.func.CachedScalar; +import org.cactoos.func.UncheckedScalar; import org.cactoos.text.FormattedText; /** @@ -52,12 +54,8 @@ public final class IterableAsList extends AbstractList { /** * Iterable length. - * - * @todo #39:30m Needs cached `LengthOfIterable` version - * to improve `IterableAsList` performance. Now each call - * to `size()` goes through all iterable to calculate the size. */ - private final LengthOfIterable length; + private final UncheckedScalar length; /** * Ctor. @@ -68,7 +66,11 @@ public IterableAsList(final Iterable iterable) { super(); this.source = iterable; this.cache = new ArrayList<>(0); - this.length = new LengthOfIterable(iterable); + this.length = new UncheckedScalar<>( + new CachedScalar<>( + new LengthOfIterable(iterable) + ) + ); } @Override From 0d27ede39392e8a8f959cfb2f0e111bada08c0a8 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 6 Jun 2017 11:41:47 +0300 Subject: [PATCH 015/169] #86: doc --- src/main/java/org/cactoos/io/ResourceAsInput.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/io/ResourceAsInput.java b/src/main/java/org/cactoos/io/ResourceAsInput.java index ed8dbf9bb8..0773d0166c 100644 --- a/src/main/java/org/cactoos/io/ResourceAsInput.java +++ b/src/main/java/org/cactoos/io/ResourceAsInput.java @@ -29,7 +29,12 @@ import org.cactoos.text.FormattedText; /** - * Jar class resource. + * Classpath resource. + * + *

Pay attention that the name of resource must always be + * global, not starting with a leading slash. Thus, + * if you want to load a text file from {@code /com/example/Test.txt}, + * you must provide this name: {@code "com/example/Test.txt"}.

* * @author Kirill (g4s8.public@gmail.com) * @version $Id$ From 92cf9a216620127f90d73cf2534646a99fb46485 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 6 Jun 2017 11:45:48 +0300 Subject: [PATCH 016/169] #86: third arg for FuncWithCallback --- .../org/cactoos/func/FuncWithCallback.java | 21 +++++++++++++++++-- .../cactoos/func/FuncWithCallbackTest.java | 13 ++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/func/FuncWithCallback.java b/src/main/java/org/cactoos/func/FuncWithCallback.java index 2e0dc53e06..5522c75c5c 100644 --- a/src/main/java/org/cactoos/func/FuncWithCallback.java +++ b/src/main/java/org/cactoos/func/FuncWithCallback.java @@ -48,6 +48,11 @@ public final class FuncWithCallback implements Func { */ private final Func callback; + /** + * The follow up. + */ + private final Func follow; + /** * Ctor. * @param fnc The func @@ -55,8 +60,20 @@ public final class FuncWithCallback implements Func { */ public FuncWithCallback(final Func fnc, final Func cbk) { + this(fnc, cbk, input -> input); + } + + /** + * Ctor. + * @param fnc The func + * @param cbk The callback + * @param flw The follow up func + */ + public FuncWithCallback(final Func fnc, + final Func cbk, final Func flw) { this.func = fnc; this.callback = cbk; + this.follow = flw; } @Override @@ -64,7 +81,7 @@ public FuncWithCallback(final Func fnc, public Y apply(final X input) throws Exception { Y result; try { - result = this.func.apply(input); + result = this.func.apply(input); } catch (final InterruptedException ex) { Thread.currentThread().interrupt(); result = this.callback.apply(ex); @@ -72,7 +89,7 @@ public Y apply(final X input) throws Exception { } catch (final Throwable ex) { result = this.callback.apply(ex); } - return result; + return this.follow.apply(result); } } diff --git a/src/test/java/org/cactoos/func/FuncWithCallbackTest.java b/src/test/java/org/cactoos/func/FuncWithCallbackTest.java index 44f4c89ff9..74339669b0 100644 --- a/src/test/java/org/cactoos/func/FuncWithCallbackTest.java +++ b/src/test/java/org/cactoos/func/FuncWithCallbackTest.java @@ -65,4 +65,17 @@ public void usesCallback() throws Exception { ); } + @Test + public void usesFollowUp() throws Exception { + MatcherAssert.assertThat( + "Can't use the follow-up func", + new FuncWithCallback<>( + input -> "works fine", + ex -> "won't happen", + input -> "follow up" + ), + new FuncApplies<>(1, Matchers.containsString("follow")) + ); + } + } From cd367c88505e1c4c07efe67804c83b3692584fc0 Mon Sep 17 00:00:00 2001 From: mehyil Date: Wed, 7 Jun 2017 13:00:03 +0300 Subject: [PATCH 017/169] Correcting Typo --- src/test/java/org/cactoos/text/ReversedTextTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/cactoos/text/ReversedTextTest.java b/src/test/java/org/cactoos/text/ReversedTextTest.java index 3887d5bb50..665475cf65 100644 --- a/src/test/java/org/cactoos/text/ReversedTextTest.java +++ b/src/test/java/org/cactoos/text/ReversedTextTest.java @@ -28,7 +28,7 @@ import org.junit.Test; /** - * Test case for {@link ReplacedText}. + * Test case for {@link ReversedText}. * * @author Mehmet Yildirim (memoyil@gmail.com) * @version $Id$ From 1828ce91356c63a6675b798039c671a336737de7 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 7 Jun 2017 16:22:48 +0300 Subject: [PATCH 018/169] appveyor --- README.md | 1 + appveyor.yml | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 appveyor.yml diff --git a/README.md b/README.md index 0641777c16..9b39704b7c 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ [![DevOps By Rultor.com](http://www.rultor.com/b/yegor256/cactoos)](http://www.rultor.com/p/yegor256/cactoos) [![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) [![PDD status](http://www.0pdd.com/svg?name=yegor256/cactoos)](http://www.0pdd.com/p?name=yegor256/cactoos) [![Test Coverage](https://img.shields.io/codecov/c/github/yegor256/cactoos.svg)](https://codecov.io/github/yegor256/cactoos?branch=master) diff --git a/appveyor.yml b/appveyor.yml new file mode 100644 index 0000000000..0080cbb7b9 --- /dev/null +++ b/appveyor.yml @@ -0,0 +1,31 @@ +version: '{build}' +skip_tags: true +clone_depth: 10 +environment: + matrix: + - JAVA_HOME: C:\Program Files\Java\jdk1.8.0 +branches: + only: + - master + except: + - gh-pages +os: Windows Server 2012 +install: + - ps: | + Add-Type -AssemblyName System.IO.Compression.FileSystem + if (!(Test-Path -Path "C:\maven" )) { + (new-object System.Net.WebClient).DownloadFile('http://www.us.apache.org/dist/maven/maven-3/3.2.5/binaries/apache-maven-3.2.5-bin.zip', 'C:\maven-bin.zip') + [System.IO.Compression.ZipFile]::ExtractToDirectory("C:\maven-bin.zip", "C:\maven") + } + - cmd: SET PATH=C:\maven\apache-maven-3.2.5\bin;%JAVA_HOME%\bin;%PATH:C:\Ruby193\bin;=% + - cmd: SET MAVEN_OPTS=-XX:MaxPermSize=2g -Xmx4g + - cmd: SET JAVA_OPTS=-XX:MaxPermSize=2g -Xmx4g + - cmd: mvn --version + - cmd: java -version +build_script: + - mvn clean package -B -Dmaven.test.skip=true +test_script: + - mvn clean install --batch-mode -Pqulice +cache: + - C:\maven\ + - C:\Users\appveyor\.m2 From cdea7d1199259f154cd89e75399e8d42e93589e8 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 7 Jun 2017 16:27:53 +0300 Subject: [PATCH 019/169] M2_HOME --- appveyor.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/appveyor.yml b/appveyor.yml index 0080cbb7b9..31927637cc 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -18,6 +18,7 @@ install: [System.IO.Compression.ZipFile]::ExtractToDirectory("C:\maven-bin.zip", "C:\maven") } - cmd: SET PATH=C:\maven\apache-maven-3.2.5\bin;%JAVA_HOME%\bin;%PATH:C:\Ruby193\bin;=% + - cmd: SET M2_HOME=C:\maven\apache-maven-3.2.5 - cmd: SET MAVEN_OPTS=-XX:MaxPermSize=2g -Xmx4g - cmd: SET JAVA_OPTS=-XX:MaxPermSize=2g -Xmx4g - cmd: mvn --version From 515790c4a43e18720e4e4171a8ff84120038df1a Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 7 Jun 2017 16:44:24 +0300 Subject: [PATCH 020/169] doc --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9b39704b7c..ed9cd7b9be 100644 --- a/README.md +++ b/README.md @@ -70,8 +70,10 @@ To write a text into a file: ```java new LengthOfInput( new TeeInput( - new TextAsInput( - new StringAsText("Hello, world!") + new BytesAsInput( + new TextAsBytes( + new StringAsText("Hello, world!") + ) ), new FileAsOutput( new File("/code/a.txt") @@ -84,9 +86,7 @@ To read a binary file from classpath: ```java byte[] data = new InputAsBytes( - new UrlAsInput( - this.getClass().getResource("/foo/img.jpg") - ) + new ResourceAsInput("/foo/img.jpg") ).asBytes(); ``` From bc6b8e232f25e7d57715155fa29400615f8945b3 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 7 Jun 2017 16:44:44 +0300 Subject: [PATCH 021/169] contrib --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ed9cd7b9be..c49e89f981 100644 --- a/README.md +++ b/README.md @@ -203,7 +203,8 @@ Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static ## Contributors - - [Kirill Che.](https://github.com/g4s8) g4s8.public@gmail.com + - [Yegor Bugayenko](https://github.com/yegor256) + - [Kirill Che.](https://github.com/g4s8) g4s8.public@gmail.com ## License (MIT) From 76c339e7e71e735dc0668b6d93b1dbee0343d4b1 Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Wed, 7 Jun 2017 12:03:52 -0300 Subject: [PATCH 022/169] #88: added some contributors --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index c49e89f981..623ad8aa59 100644 --- a/README.md +++ b/README.md @@ -205,6 +205,10 @@ Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static - [Yegor Bugayenko](https://github.com/yegor256) - [Kirill Che.](https://github.com/g4s8) g4s8.public@gmail.com + - [Fabrício Cabral](https://github.com/fabriciofx) + - [Andriy Kryvtsun](https://github.com/englishman) + - [Vseslav Sekorin](https://github.com/VsSekorin) + - [Andrey Valyaev](https://github.com/DronMDF) ## License (MIT) From 5927cc5c395be82fa262c13f51c341a95d0cd46d Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 8 Jun 2017 12:52:29 +0300 Subject: [PATCH 023/169] #92: ResourceAsInput with extra argument --- src/main/java/org/cactoos/Scalar.java | 6 +- src/main/java/org/cactoos/ScalarHasValue.java | 10 ++- src/main/java/org/cactoos/TextHasString.java | 8 +-- .../java/org/cactoos/func/IoCheckedFunc.java | 70 ++++++++++++++++++ .../java/org/cactoos/func/IoCheckedProc.java | 66 +++++++++++++++++ .../org/cactoos/func/IoCheckedScalar.java | 62 ++++++++++++++++ .../{CachedScalar.java => StickyScalar.java} | 11 ++- .../java/org/cactoos/func/UncheckedFunc.java | 10 +-- .../org/cactoos/func/UncheckedScalar.java | 8 +-- .../java/org/cactoos/io/ResourceAsInput.java | 72 ++++++++++++++++--- .../java/org/cactoos/list/IterableAsList.java | 4 +- .../org/cactoos/io/ResourceAsInputTest.java | 17 +++++ 12 files changed, 298 insertions(+), 46 deletions(-) create mode 100644 src/main/java/org/cactoos/func/IoCheckedFunc.java create mode 100644 src/main/java/org/cactoos/func/IoCheckedProc.java create mode 100644 src/main/java/org/cactoos/func/IoCheckedScalar.java rename src/main/java/org/cactoos/func/{CachedScalar.java => StickyScalar.java} (88%) diff --git a/src/main/java/org/cactoos/Scalar.java b/src/main/java/org/cactoos/Scalar.java index c8bf2eeb73..171d7da80a 100644 --- a/src/main/java/org/cactoos/Scalar.java +++ b/src/main/java/org/cactoos/Scalar.java @@ -23,8 +23,6 @@ */ package org.cactoos; -import java.io.IOException; - /** * Scalar. * @@ -40,8 +38,8 @@ public interface Scalar { /** * Convert it to the value. * @return The value - * @throws IOException If fails + * @throws Exception If fails */ - T asValue() throws IOException; + T asValue() throws Exception; } diff --git a/src/main/java/org/cactoos/ScalarHasValue.java b/src/main/java/org/cactoos/ScalarHasValue.java index e0fedaaa2c..c6ac248aa5 100644 --- a/src/main/java/org/cactoos/ScalarHasValue.java +++ b/src/main/java/org/cactoos/ScalarHasValue.java @@ -23,7 +23,7 @@ */ package org.cactoos; -import java.io.IOException; +import org.cactoos.func.UncheckedScalar; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; @@ -63,11 +63,9 @@ public ScalarHasValue(final Matcher mtr) { @Override public boolean matchesSafely(final Scalar item) { - try { - return this.matcher.matches(item.asValue()); - } catch (final IOException ex) { - throw new IllegalStateException(ex); - } + return this.matcher.matches( + new UncheckedScalar<>(item).asValue() + ); } @Override diff --git a/src/main/java/org/cactoos/TextHasString.java b/src/main/java/org/cactoos/TextHasString.java index 4f6397598e..a63a083cb3 100644 --- a/src/main/java/org/cactoos/TextHasString.java +++ b/src/main/java/org/cactoos/TextHasString.java @@ -23,7 +23,7 @@ */ package org.cactoos; -import java.io.IOException; +import org.cactoos.text.UncheckedText; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; @@ -62,11 +62,7 @@ public TextHasString(final Matcher mtr) { @Override public boolean matchesSafely(final Text item) { - try { - return this.matcher.matches(item.asString()); - } catch (final IOException ex) { - throw new IllegalStateException(ex); - } + return this.matcher.matches(new UncheckedText(item).asString()); } @Override diff --git a/src/main/java/org/cactoos/func/IoCheckedFunc.java b/src/main/java/org/cactoos/func/IoCheckedFunc.java new file mode 100644 index 0000000000..e4859ed3c4 --- /dev/null +++ b/src/main/java/org/cactoos/func/IoCheckedFunc.java @@ -0,0 +1,70 @@ +/** + * 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 java.io.IOException; +import org.cactoos.Func; + +/** + * Func that doesn't throw checked {@link Exception}, but throws + * {@link java.io.IOException} instead. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of input + * @param Type of output + * @since 0.4 + */ +public final class IoCheckedFunc implements Func { + + /** + * Original func. + */ + private final Func func; + + /** + * Ctor. + * @param fnc Encapsulated func + */ + public IoCheckedFunc(final Func fnc) { + this.func = fnc; + } + + @Override + @SuppressWarnings("PMD.AvoidCatchingGenericException") + public Y apply(final X input) throws IOException { + try { + return this.func.apply(input); + } catch (final InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new IOException(ex); + // @checkstyle IllegalCatchCheck (1 line) + } catch (final Exception ex) { + throw new IOException(ex); + } + } + +} diff --git a/src/main/java/org/cactoos/func/IoCheckedProc.java b/src/main/java/org/cactoos/func/IoCheckedProc.java new file mode 100644 index 0000000000..ed17d8af7e --- /dev/null +++ b/src/main/java/org/cactoos/func/IoCheckedProc.java @@ -0,0 +1,66 @@ +/** + * 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 java.io.IOException; +import org.cactoos.Func; +import org.cactoos.Proc; + +/** + * Proc that doesn't throw checked {@link Exception}, but + * throws {@link java.io.IOException} instead. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of input + * @since 0.4 + */ +public final class IoCheckedProc implements Proc { + + /** + * Original proc. + */ + private final Proc proc; + + /** + * Ctor. + * @param prc Encapsulated func + */ + public IoCheckedProc(final Proc prc) { + this.proc = prc; + } + + @Override + public void exec(final X input) throws IOException { + new IoCheckedFunc<>( + (Func) arg -> { + this.proc.exec(arg); + return true; + } + ).apply(input); + } + +} diff --git a/src/main/java/org/cactoos/func/IoCheckedScalar.java b/src/main/java/org/cactoos/func/IoCheckedScalar.java new file mode 100644 index 0000000000..23356dbff3 --- /dev/null +++ b/src/main/java/org/cactoos/func/IoCheckedScalar.java @@ -0,0 +1,62 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.func; + +import java.io.IOException; +import org.cactoos.Scalar; + +/** + * Scalar that doesn't throw checked {@link Exception}, but throws + * {@link IOException} instead. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of result + * @since 0.4 + */ +public final class IoCheckedScalar implements Scalar { + + /** + * Original scalar. + */ + private final Scalar scalar; + + /** + * Ctor. + * @param scalar Encapsulated scalar + */ + public IoCheckedScalar(final Scalar scalar) { + this.scalar = scalar; + } + + @Override + public T asValue() throws IOException { + return new IoCheckedFunc, T>( + Scalar::asValue + ).apply(this.scalar); + } + +} diff --git a/src/main/java/org/cactoos/func/CachedScalar.java b/src/main/java/org/cactoos/func/StickyScalar.java similarity index 88% rename from src/main/java/org/cactoos/func/CachedScalar.java rename to src/main/java/org/cactoos/func/StickyScalar.java index 47fc3a310f..512d5aaf08 100644 --- a/src/main/java/org/cactoos/func/CachedScalar.java +++ b/src/main/java/org/cactoos/func/StickyScalar.java @@ -23,7 +23,6 @@ */ package org.cactoos.func; -import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.cactoos.Scalar; @@ -38,7 +37,7 @@ * @param Type of result * @since 0.3 */ -public final class CachedScalar implements Scalar { +public final class StickyScalar implements Scalar { /** * The scalar to cache. @@ -52,15 +51,15 @@ public final class CachedScalar implements Scalar { /** * Ctor. - * @param source The Scalar to cache + * @param src The Scalar to cache */ - public CachedScalar(final Scalar source) { - this.source = source; + public StickyScalar(final Scalar src) { + this.source = src; this.cache = new ArrayList<>(1); } @Override - public T asValue() throws IOException { + public T asValue() throws Exception { if (this.cache.isEmpty()) { this.cache.add(this.source.asValue()); } diff --git a/src/main/java/org/cactoos/func/UncheckedFunc.java b/src/main/java/org/cactoos/func/UncheckedFunc.java index b62582473c..49beb5d9d8 100644 --- a/src/main/java/org/cactoos/func/UncheckedFunc.java +++ b/src/main/java/org/cactoos/func/UncheckedFunc.java @@ -23,6 +23,7 @@ */ package org.cactoos.func; +import java.io.IOException; import org.cactoos.Func; /** @@ -52,15 +53,10 @@ public UncheckedFunc(final Func fnc) { } @Override - @SuppressWarnings("PMD.AvoidCatchingGenericException") public Y apply(final X input) { try { - return this.func.apply(input); - } catch (final InterruptedException ex) { - Thread.currentThread().interrupt(); - throw new IllegalStateException(ex); - // @checkstyle IllegalCatchCheck (1 line) - } catch (final Exception ex) { + return new IoCheckedFunc<>(this.func).apply(input); + } catch (final IOException ex) { throw new IllegalStateException(ex); } } diff --git a/src/main/java/org/cactoos/func/UncheckedScalar.java b/src/main/java/org/cactoos/func/UncheckedScalar.java index ff9834e59d..d421534687 100644 --- a/src/main/java/org/cactoos/func/UncheckedScalar.java +++ b/src/main/java/org/cactoos/func/UncheckedScalar.java @@ -46,16 +46,16 @@ public final class UncheckedScalar implements Scalar { /** * Ctor. - * @param scalar Encapsulated scalar + * @param scr Encapsulated scalar */ - public UncheckedScalar(final Scalar scalar) { - this.scalar = scalar; + public UncheckedScalar(final Scalar scr) { + this.scalar = scr; } @Override public T asValue() { try { - return this.scalar.asValue(); + return new IoCheckedScalar<>(this.scalar).asValue(); } catch (final IOException ex) { throw new UncheckedIOException(ex); } diff --git a/src/main/java/org/cactoos/io/ResourceAsInput.java b/src/main/java/org/cactoos/io/ResourceAsInput.java index 0773d0166c..21e17cd77f 100644 --- a/src/main/java/org/cactoos/io/ResourceAsInput.java +++ b/src/main/java/org/cactoos/io/ResourceAsInput.java @@ -25,8 +25,11 @@ import java.io.IOException; import java.io.InputStream; +import org.cactoos.Func; import org.cactoos.Input; +import org.cactoos.func.IoCheckedFunc; import org.cactoos.text.FormattedText; +import org.cactoos.text.TextAsBytes; /** * Classpath resource. @@ -48,6 +51,11 @@ public final class ResourceAsInput implements Input { */ private final String path; + /** + * Fallback. + */ + private final Func fallback; + /** * Resource class loader. */ @@ -55,37 +63,79 @@ public final class ResourceAsInput implements Input { /** * New resource input with current context {@link ClassLoader}. - * * @param res Resource name */ public ResourceAsInput(final String res) { + this(res, Thread.currentThread().getContextClassLoader()); + } + + /** + * New resource input with current context {@link ClassLoader}. + * @param res Resource name + * @param fbk Fallback + */ + public ResourceAsInput(final String res, final String fbk) { + this(res, input -> new BytesAsInput(new TextAsBytes(fbk))); + } + + /** + * New resource input with current context {@link ClassLoader}. + * @param res Resource name + * @param fbk Fallback + */ + public ResourceAsInput(final String res, final Input fbk) { + this(res, input -> fbk); + } + + /** + * New resource input with current context {@link ClassLoader}. + * @param res Resource name + * @param fbk Fallback + */ + public ResourceAsInput(final String res, final Func fbk) { + this(res, fbk, Thread.currentThread().getContextClassLoader()); + } + + /** + * New resource input with specified {@link ClassLoader}. + * @param res Resource name + * @param ldr Resource class loader + */ + public ResourceAsInput(final String res, final ClassLoader ldr) { this( res, - Thread.currentThread().getContextClassLoader() + input -> { + throw new IOException( + new FormattedText( + "Resource '%s' was not found", + input + ).asString() + ); + }, + ldr ); } /** * New resource input with specified {@link ClassLoader}. - * * @param res Resource name * @param ldr Resource class loader + * @param fbk Fallback */ - public ResourceAsInput(final String res, final ClassLoader ldr) { + public ResourceAsInput(final String res, final Func fbk, + final ClassLoader ldr) { this.path = res; this.loader = ldr; + this.fallback = fbk; } @Override public InputStream stream() throws IOException { - final InputStream input = this.loader.getResourceAsStream(this.path); + InputStream input = this.loader.getResourceAsStream(this.path); if (input == null) { - throw new IOException( - new FormattedText( - "Resource '%s' was not found", - this.path - ).asString() - ); + input = new IoCheckedFunc<>(this.fallback) + .apply(this.path) + .stream(); } return input; } diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index 05aaceae3c..e6abdc9db6 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -26,7 +26,7 @@ import java.util.AbstractList; import java.util.ArrayList; import java.util.List; -import org.cactoos.func.CachedScalar; +import org.cactoos.func.StickyScalar; import org.cactoos.func.UncheckedScalar; import org.cactoos.text.FormattedText; @@ -67,7 +67,7 @@ public IterableAsList(final Iterable iterable) { this.source = iterable; this.cache = new ArrayList<>(0); this.length = new UncheckedScalar<>( - new CachedScalar<>( + new StickyScalar<>( new LengthOfIterable(iterable) ) ); diff --git a/src/test/java/org/cactoos/io/ResourceAsInputTest.java b/src/test/java/org/cactoos/io/ResourceAsInputTest.java index 20e2e9474e..43dab3bc03 100644 --- a/src/test/java/org/cactoos/io/ResourceAsInputTest.java +++ b/src/test/java/org/cactoos/io/ResourceAsInputTest.java @@ -24,6 +24,7 @@ package org.cactoos.io; import java.util.Arrays; +import org.cactoos.text.BytesAsText; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; @@ -61,4 +62,20 @@ public void readResourceTest() throws Exception { ); } + @Test + public void readAbsentResourceTest() throws Exception { + MatcherAssert.assertThat( + "Can't replace an absent resource with a text", + new BytesAsText( + new InputAsBytes( + new ResourceAsInput( + "foo/this-resource-is-definitely-absent.txt", + "the replacement" + ) + ) + ).asString(), + Matchers.endsWith("replacement") + ); + } + } From 09464b3a329a49afc5a240e72e0639f718303cfd Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 8 Jun 2017 13:54:24 +0300 Subject: [PATCH 024/169] BytesAsInput --- src/main/java/org/cactoos/io/BytesAsInput.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/org/cactoos/io/BytesAsInput.java b/src/main/java/org/cactoos/io/BytesAsInput.java index 1eabae559f..15f7dad450 100644 --- a/src/main/java/org/cactoos/io/BytesAsInput.java +++ b/src/main/java/org/cactoos/io/BytesAsInput.java @@ -28,6 +28,7 @@ import java.io.InputStream; import org.cactoos.Bytes; import org.cactoos.Input; +import org.cactoos.text.TextAsBytes; /** * Bytes as Input. @@ -45,6 +46,15 @@ public final class BytesAsInput implements Input { */ private final Bytes source; + /** + * Ctor. + * @param text The text + * @since 0.4 + */ + public BytesAsInput(final String text) { + this(new TextAsBytes(text)); + } + /** * Ctor. * @param bytes The bytes From baef1d00a36b636abd8a36a049ccb2b01fba7117 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 8 Jun 2017 15:34:51 +0300 Subject: [PATCH 025/169] #94: Sync primitives --- src/main/java/org/cactoos/func/SyncFunc.java | 59 +++++++++++++++++++ src/main/java/org/cactoos/func/SyncProc.java | 58 ++++++++++++++++++ .../java/org/cactoos/func/SyncScalar.java | 57 ++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 src/main/java/org/cactoos/func/SyncFunc.java create mode 100644 src/main/java/org/cactoos/func/SyncProc.java create mode 100644 src/main/java/org/cactoos/func/SyncScalar.java diff --git a/src/main/java/org/cactoos/func/SyncFunc.java b/src/main/java/org/cactoos/func/SyncFunc.java new file mode 100644 index 0000000000..ccc8d80f41 --- /dev/null +++ b/src/main/java/org/cactoos/func/SyncFunc.java @@ -0,0 +1,59 @@ +/** + * 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.Func; + +/** + * Func that is thread-safe. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of input + * @param Type of output + * @since 0.4 + */ +public final class SyncFunc implements Func { + + /** + * Original func. + */ + private final Func func; + + /** + * Ctor. + * @param fnc Func original + */ + public SyncFunc(final Func fnc) { + this.func = fnc; + } + + @Override + public Y apply(final X input) throws Exception { + synchronized (this.func) { + return this.func.apply(input); + } + } + +} diff --git a/src/main/java/org/cactoos/func/SyncProc.java b/src/main/java/org/cactoos/func/SyncProc.java new file mode 100644 index 0000000000..ed9de046ef --- /dev/null +++ b/src/main/java/org/cactoos/func/SyncProc.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.func; + +import org.cactoos.Proc; + +/** + * Proc that is thread-safe. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of input + * @since 0.4 + */ +public final class SyncProc implements Proc { + + /** + * Original proc. + */ + private final Proc proc; + + /** + * Ctor. + * @param prc Func original + */ + public SyncProc(final Proc prc) { + this.proc = prc; + } + + @Override + public void exec(final X input) throws Exception { + synchronized (this.proc) { + this.proc.exec(input); + } + } + +} diff --git a/src/main/java/org/cactoos/func/SyncScalar.java b/src/main/java/org/cactoos/func/SyncScalar.java new file mode 100644 index 0000000000..26c3112941 --- /dev/null +++ b/src/main/java/org/cactoos/func/SyncScalar.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.func; + +import org.cactoos.Scalar; + +/** + * Scalar that is thread-safe. + * + * @author Tim Hinkes (timmeey@timmeey.de) + * @version $Id$ + * @param Type of result + * @since 0.3 + */ +public final class SyncScalar implements Scalar { + + /** + * The scalar to cache. + */ + private final Scalar source; + + /** + * Ctor. + * @param src The Scalar to cache + */ + public SyncScalar(final Scalar src) { + this.source = src; + } + + @Override + public T asValue() throws Exception { + synchronized (this.source) { + return this.source.asValue(); + } + } +} From 8735346d1e712c183d1041d0cbfda48880f9b5ce Mon Sep 17 00:00:00 2001 From: Ilia Date: Fri, 9 Jun 2017 12:41:26 +0300 Subject: [PATCH 026/169] Change pattern to Text in FormattedText. --- .../java/org/cactoos/list/IterableAsList.java | 21 ++++--- .../java/org/cactoos/text/FormattedText.java | 58 ++++++++++++++++++- 2 files changed, 68 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index e6abdc9db6..b8e8792131 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -23,6 +23,7 @@ */ package org.cactoos.list; +import java.io.IOException; import java.util.AbstractList; import java.util.ArrayList; import java.util.List; @@ -76,14 +77,18 @@ public IterableAsList(final Iterable iterable) { @Override public T get(final int index) { if (index < 0 || index >= this.size()) { - throw new IndexOutOfBoundsException( - new FormattedText( - "index=%d, bounds=[%d; %d]", - index, - 0, - this.size() - ).asString() - ); + try { + throw new IndexOutOfBoundsException( + new FormattedText( + "index=%d, bounds=[%d; %d]", + index, + 0, + this.size() + ).asString() + ); + } catch (final IOException exception) { + throw new IllegalStateException(exception); + } } return this.cachedItem(index); } diff --git a/src/main/java/org/cactoos/text/FormattedText.java b/src/main/java/org/cactoos/text/FormattedText.java index 15bf90840c..80162ef321 100644 --- a/src/main/java/org/cactoos/text/FormattedText.java +++ b/src/main/java/org/cactoos/text/FormattedText.java @@ -23,6 +23,7 @@ */ package org.cactoos.text; +import java.io.IOException; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -44,7 +45,7 @@ public final class FormattedText implements Text { /** * Pattern. */ - private final String pattern; + private final Text pattern; /** * Arguments. @@ -66,6 +67,16 @@ public FormattedText(final String ptn, final Object... arguments) { this(ptn, Arrays.asList(arguments)); } + /** + * New formatted string with default locale. + * + * @param ptn Pattern + * @param arguments Arguments + */ + public FormattedText(final Text ptn, final Object... arguments) { + this(ptn, Arrays.asList(arguments)); + } + /** * New formatted string with specified locale. * @@ -81,6 +92,21 @@ public FormattedText( this(ptn, locale, Arrays.asList(arguments)); } + /** + * New formatted string with specified locale. + * + * @param ptn Pattern + * @param locale Format locale + * @param arguments Arguments + */ + public FormattedText( + final Text ptn, + final Locale locale, + final Object... arguments + ) { + this(ptn, locale, Arrays.asList(arguments)); + } + /** * New formatted string with default locale. * @@ -91,6 +117,16 @@ public FormattedText(final String ptn, final Collection arguments) { this(ptn, Locale.getDefault(Locale.Category.FORMAT), arguments); } + /** + * New formatted string with default locale. + * + * @param ptn Pattern + * @param arguments Arguments + */ + public FormattedText(final Text ptn, final Collection arguments) { + this(ptn, Locale.getDefault(Locale.Category.FORMAT), arguments); + } + /** * New formatted string with specified locale. * @@ -102,6 +138,21 @@ public FormattedText( final String ptn, final Locale locale, final Collection arguments + ) { + this(new StringAsText(ptn), locale, arguments); + } + + /** + * New formatted string with specified locale. + * + * @param ptn Pattern + * @param locale Format locale + * @param arguments Arguments + */ + public FormattedText( + final Text ptn, + final Locale locale, + final Collection arguments ) { this.pattern = ptn; this.locale = locale; @@ -109,11 +160,12 @@ public FormattedText( } @Override - public String asString() { + public String asString() throws IOException { final StringBuilder out = new StringBuilder(0); try (final Formatter fmt = new Formatter(out, this.locale)) { fmt.format( - this.pattern, this.args.toArray(new Object[this.args.size()]) + this.pattern.asString(), + this.args.toArray(new Object[this.args.size()]) ); } return out.toString(); From a6bf96ec35aefe6dd9779f4926338bdd8349a73d Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 9 Jun 2017 13:16:58 +0300 Subject: [PATCH 027/169] #98: EndlessIterable --- .../org/cactoos/list/EndlessIterable.java | 59 +++++++++++++++ .../org/cactoos/list/EndlessIterator.java | 63 ++++++++++++++++ .../list/{Repeat.java => RepeatIterable.java} | 38 +++------- .../java/org/cactoos/list/RepeatIterator.java | 71 +++++++++++++++++++ ...epeatTest.java => RepeatIterableTest.java} | 8 +-- .../org/cactoos/list/RepeatIteratorTest.java | 65 +++++++++++++++++ 6 files changed, 272 insertions(+), 32 deletions(-) create mode 100644 src/main/java/org/cactoos/list/EndlessIterable.java create mode 100644 src/main/java/org/cactoos/list/EndlessIterator.java rename src/main/java/org/cactoos/list/{Repeat.java => RepeatIterable.java} (69%) create mode 100644 src/main/java/org/cactoos/list/RepeatIterator.java rename src/test/java/org/cactoos/list/{RepeatTest.java => RepeatIterableTest.java} (92%) create mode 100644 src/test/java/org/cactoos/list/RepeatIteratorTest.java diff --git a/src/main/java/org/cactoos/list/EndlessIterable.java b/src/main/java/org/cactoos/list/EndlessIterable.java new file mode 100644 index 0000000000..bc1a1172db --- /dev/null +++ b/src/main/java/org/cactoos/list/EndlessIterable.java @@ -0,0 +1,59 @@ +/** + * 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.list; + +import java.util.Iterator; + +/** + * Endless iterable. + * + *

If you need to repeat certain amount of time, + * use {@link RepeatIterable}.

+ * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Element type + * @since 0.4 + */ +public final class EndlessIterable implements Iterable { + + /** + * Element to repeat. + */ + private final T element; + + /** + * Ctor. + * @param elm To repeat + */ + public EndlessIterable(final T elm) { + this.element = elm; + } + + @Override + public Iterator iterator() { + return new EndlessIterator<>(this.element); + } + +} diff --git a/src/main/java/org/cactoos/list/EndlessIterator.java b/src/main/java/org/cactoos/list/EndlessIterator.java new file mode 100644 index 0000000000..ada8996ffb --- /dev/null +++ b/src/main/java/org/cactoos/list/EndlessIterator.java @@ -0,0 +1,63 @@ +/** + * 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.list; + +import java.util.Iterator; + +/** + * Iterator that never ends. + * + *

If you need to repeat certain amount of time, + * use {@link RepeatIterator}.

+ * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Element type + * @since 0.4 + */ +public final class EndlessIterator implements Iterator { + + /** + * The element to repeat. + */ + private final T element; + + /** + * Ctor. + * @param elm Element to repeat + */ + public EndlessIterator(final T elm) { + this.element = elm; + } + + @Override + public boolean hasNext() { + return true; + } + + @Override + public T next() { + return this.element; + } +} diff --git a/src/main/java/org/cactoos/list/Repeat.java b/src/main/java/org/cactoos/list/RepeatIterable.java similarity index 69% rename from src/main/java/org/cactoos/list/Repeat.java rename to src/main/java/org/cactoos/list/RepeatIterable.java index 4249660c92..f03a57cc8d 100644 --- a/src/main/java/org/cactoos/list/Repeat.java +++ b/src/main/java/org/cactoos/list/RepeatIterable.java @@ -28,12 +28,15 @@ /** * Repeat an element. * + *

If you need to repeat endlessly, use {@link EndlessIterable}.

+ * * @author Kirill (g4s8.public@gmail.com) + * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Element type * @since 0.1 */ -public final class Repeat implements Iterable { +public final class RepeatIterable implements Iterable { /** * Element to repeat. @@ -48,38 +51,17 @@ public final class Repeat implements Iterable { /** * Ctor. * - * @param element To repeat - * @param count Count + * @param elm To repeat + * @param cnt Count */ - public Repeat(final T element, final int count) { - this.element = element; - this.count = count; + public RepeatIterable(final T elm, final int cnt) { + this.element = elm; + this.count = cnt; } @Override public Iterator iterator() { - return new RepeatIterator(); + return new RepeatIterator<>(this.element, this.count); } - /** - * An iterator. - */ - private final class RepeatIterator implements Iterator { - - /** - * Current position. - */ - private int cursor; - - @Override - public boolean hasNext() { - return this.cursor < Repeat.this.count; - } - - @Override - public T next() { - ++this.cursor; - return Repeat.this.element; - } - } } diff --git a/src/main/java/org/cactoos/list/RepeatIterator.java b/src/main/java/org/cactoos/list/RepeatIterator.java new file mode 100644 index 0000000000..1e8a369217 --- /dev/null +++ b/src/main/java/org/cactoos/list/RepeatIterator.java @@ -0,0 +1,71 @@ +/** + * 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.list; + +import java.util.Iterator; + +/** + * Repeat an element. + * + *

If you need to repeat endlessly, use {@link EndlessIterable}.

+ * + * @author Kirill (g4s8.public@gmail.com) + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Element type + * @since 0.4 + */ +public final class RepeatIterator implements Iterator { + + /** + * The element to repeat. + */ + private final T element; + + /** + * How many more repeats will happen. + */ + private int left; + + /** + * Ctor. + * @param elm Element to repeat + * @param max How many times to repeat + */ + public RepeatIterator(final T elm, final int max) { + this.element = elm; + this.left = max; + } + + @Override + public boolean hasNext() { + return this.left > 0; + } + + @Override + public T next() { + --this.left; + return this.element; + } +} diff --git a/src/test/java/org/cactoos/list/RepeatTest.java b/src/test/java/org/cactoos/list/RepeatIterableTest.java similarity index 92% rename from src/test/java/org/cactoos/list/RepeatTest.java rename to src/test/java/org/cactoos/list/RepeatIterableTest.java index b459d49cd9..93ad2fe43a 100644 --- a/src/test/java/org/cactoos/list/RepeatTest.java +++ b/src/test/java/org/cactoos/list/RepeatIterableTest.java @@ -28,14 +28,14 @@ import org.junit.Test; /** - * Test case for {@link Repeat}. + * Test case for {@link RepeatIterable}. * * @author Kirill (g4s8.public@gmail.com) * @version $Id$ * @since 0.1 * @checkstyle JavadocMethodCheck (500 lines) */ -public final class RepeatTest { +public final class RepeatIterableTest { @Test public void allSameTest() throws Exception { @@ -45,7 +45,7 @@ public void allSameTest() throws Exception { "Can't generate an iterable with fixed size", new LengthOfIterable( new FilteredIterable<>( - new Repeat<>( + new RepeatIterable<>( element, size ), @@ -61,7 +61,7 @@ public void emptyTest() throws Exception { MatcherAssert.assertThat( "Can't generate an empty iterable", new LengthOfIterable( - new Repeat<>(0, 0) + new RepeatIterable<>(0, 0) ), new ScalarHasValue<>(0) ); diff --git a/src/test/java/org/cactoos/list/RepeatIteratorTest.java b/src/test/java/org/cactoos/list/RepeatIteratorTest.java new file mode 100644 index 0000000000..0ade5e6dfe --- /dev/null +++ b/src/test/java/org/cactoos/list/RepeatIteratorTest.java @@ -0,0 +1,65 @@ +/** + * 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.list; + +import org.cactoos.ScalarHasValue; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link RepeatIterator}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class RepeatIteratorTest { + + @Test + public void allSameTest() throws Exception { + final int size = 42; + final int element = 11; + MatcherAssert.assertThat( + "Can't generate an iterable with fixed size", + new LengthOfIterator( + new RepeatIterator<>( + element, + size + ) + ), + new ScalarHasValue<>(size) + ); + } + + @Test + public void emptyTest() throws Exception { + MatcherAssert.assertThat( + "Can't generate an empty iterator", + (Iterable) () -> new RepeatIterator<>(0, 0), + Matchers.iterableWithSize(0) + ); + } +} From c3fecb29fffac9814c12827480bec35c8c19dbfd Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 9 Jun 2017 13:29:09 +0300 Subject: [PATCH 028/169] #98: doc --- README.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/README.md b/README.md index 623ad8aa59..9111c559a7 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,49 @@ int total = new LengthOfIterable( ).asValue(); ``` +## Funcs and Procs + +This is a traditional `foreach` loop: + +```java +for (String name : names) { + System.out.printf("Hello, %s!\n", name); +} +``` + +This is its object-oriented alternative (no streams!): + +```java +new IterableAsBoolean<>( + names, + new ProcAsFunc<>( + n -> { + System.out.printf("Hello, %s!\n", n); + } + ) +).asValue(); +``` + +This is an endless `while/do` loop: + +```java +while (!ready) { + System.out.prinln("Still waiting..."); +} +``` + +Here is its object-oriented alternative: + +```java +new IterableAsBoolean<>( + new EndlessIterable<>(ready), + r -> { + System.out.prinln("Still waiting..."); + return !ready; + } +).asValue(); +``` + ## How to contribute? Just fork the repo and send us a pull request. From dff6e53eefa797e2970209dcd517838b0f352aff Mon Sep 17 00:00:00 2001 From: Ilia Date: Fri, 9 Jun 2017 13:39:20 +0300 Subject: [PATCH 029/169] Change pattern to Text in FormattedText. Replace to UncheckedText. --- src/main/java/org/cactoos/list/IterableAsList.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index b8e8792131..a76c176eb9 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -23,13 +23,13 @@ */ package org.cactoos.list; -import java.io.IOException; import java.util.AbstractList; import java.util.ArrayList; import java.util.List; import org.cactoos.func.StickyScalar; import org.cactoos.func.UncheckedScalar; import org.cactoos.text.FormattedText; +import org.cactoos.text.UncheckedText; /** * Iterable as {@link List}. @@ -77,18 +77,16 @@ public IterableAsList(final Iterable iterable) { @Override public T get(final int index) { if (index < 0 || index >= this.size()) { - try { - throw new IndexOutOfBoundsException( + throw new IndexOutOfBoundsException( + new UncheckedText( new FormattedText( "index=%d, bounds=[%d; %d]", index, 0, this.size() - ).asString() - ); - } catch (final IOException exception) { - throw new IllegalStateException(exception); - } + ) + ).asString() + ); } return this.cachedItem(index); } From b13b1489e8001eaadfe05c42b5eeba79b8988811 Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Fri, 9 Jun 2017 07:43:01 -0300 Subject: [PATCH 030/169] #90: StringAsUrl and UrlAsString --- .../java/org/cactoos/text/StringAsUrl.java | 93 +++++++++++++++++++ .../java/org/cactoos/text/UrlAsString.java | 93 +++++++++++++++++++ .../org/cactoos/text/StringAsUrlTest.java | 48 ++++++++++ .../org/cactoos/text/UrlAsStringTest.java | 48 ++++++++++ 4 files changed, 282 insertions(+) create mode 100644 src/main/java/org/cactoos/text/StringAsUrl.java create mode 100644 src/main/java/org/cactoos/text/UrlAsString.java create mode 100644 src/test/java/org/cactoos/text/StringAsUrlTest.java create mode 100644 src/test/java/org/cactoos/text/UrlAsStringTest.java diff --git a/src/main/java/org/cactoos/text/StringAsUrl.java b/src/main/java/org/cactoos/text/StringAsUrl.java new file mode 100644 index 0000000000..f5fbf990f4 --- /dev/null +++ b/src/main/java/org/cactoos/text/StringAsUrl.java @@ -0,0 +1,93 @@ +/** + * 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.net.URLEncoder; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import org.cactoos.Scalar; +import org.cactoos.Text; + +/** + * String as URL. + * + *

There is no thread-safety guarantee. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.4 + */ +public final class StringAsUrl implements Scalar { + + /** + * The source. + */ + private final Text source; + + /** + * The encoding. + */ + private final Charset encoding; + + /** + * Ctor. + * @param url The URL as String + */ + public StringAsUrl(final String url) { + this(url, StandardCharsets.UTF_8); + } + + /** + * Ctor. + * @param url The URL as Text + */ + public StringAsUrl(final Text url) { + this(url, StandardCharsets.UTF_8); + } + + /** + * Ctor. + * @param url The URL as String + * @param encoding The encoding + */ + public StringAsUrl(final String url, final Charset encoding) { + this(new StringAsText(url), encoding); + } + + /** + * Ctor. + * @param url The URL as Text + * @param encoding The encoding + */ + public StringAsUrl(final Text url, final Charset encoding) { + this.source = url; + this.encoding = encoding; + } + + @Override + public String asValue() throws Exception { + return URLEncoder.encode(this.source.asString(), this.encoding.name()); + } + +} diff --git a/src/main/java/org/cactoos/text/UrlAsString.java b/src/main/java/org/cactoos/text/UrlAsString.java new file mode 100644 index 0000000000..b1a10a131e --- /dev/null +++ b/src/main/java/org/cactoos/text/UrlAsString.java @@ -0,0 +1,93 @@ +/** + * 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.net.URLDecoder; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import org.cactoos.Scalar; +import org.cactoos.Text; + +/** + * URL as String. + * + *

There is no thread-safety guarantee. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.4 + */ +public final class UrlAsString implements Scalar { + + /** + * The source. + */ + private final Text source; + + /** + * The encoding. + */ + private final Charset encoding; + + /** + * Ctor. + * @param url The URL as String + */ + public UrlAsString(final String url) { + this(url, StandardCharsets.UTF_8); + } + + /** + * Ctor. + * @param url The URL as Text + */ + public UrlAsString(final Text url) { + this(url, StandardCharsets.UTF_8); + } + + /** + * Ctor. + * @param url The URL as String + * @param encoding The encoding + */ + public UrlAsString(final String url, final Charset encoding) { + this(new StringAsText(url), encoding); + } + + /** + * Ctor. + * @param url The URL as Text + * @param encoding The encoding + */ + public UrlAsString(final Text url, final Charset encoding) { + this.source = url; + this.encoding = encoding; + } + + @Override + public String asValue() throws Exception { + return URLDecoder.decode(this.source.asString(), this.encoding.name()); + } + +} diff --git a/src/test/java/org/cactoos/text/StringAsUrlTest.java b/src/test/java/org/cactoos/text/StringAsUrlTest.java new file mode 100644 index 0000000000..6f2f04266f --- /dev/null +++ b/src/test/java/org/cactoos/text/StringAsUrlTest.java @@ -0,0 +1,48 @@ +/** + * 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 org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link StringAsUrl}. + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class StringAsUrlTest { + + @Test + public void encodeStringToUrl() throws Exception { + MatcherAssert.assertThat( + "Can't encode a string as URL", + new StringAsUrl("друг").asValue(), + Matchers.equalTo("%D0%B4%D1%80%D1%83%D0%B3") + ); + } + +} diff --git a/src/test/java/org/cactoos/text/UrlAsStringTest.java b/src/test/java/org/cactoos/text/UrlAsStringTest.java new file mode 100644 index 0000000000..f90b23a106 --- /dev/null +++ b/src/test/java/org/cactoos/text/UrlAsStringTest.java @@ -0,0 +1,48 @@ +/** + * 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 org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link UrlAsString}. + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class UrlAsStringTest { + + @Test + public void decodeUrlToString() throws Exception { + MatcherAssert.assertThat( + "Can't convert a string to URL", + new UrlAsString("%D0%B0%20%D1%8F").asValue(), + Matchers.equalTo("а я") + ); + } + +} From c9423fd1bb7de8ab49c50e2b32a4d5a92c66afc6 Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Fri, 9 Jun 2017 07:57:45 -0300 Subject: [PATCH 031/169] #90: removed some whitespaces --- src/main/java/org/cactoos/text/StringAsUrl.java | 4 ++-- src/main/java/org/cactoos/text/UrlAsString.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/cactoos/text/StringAsUrl.java b/src/main/java/org/cactoos/text/StringAsUrl.java index f5fbf990f4..dcaaaf39c9 100644 --- a/src/main/java/org/cactoos/text/StringAsUrl.java +++ b/src/main/java/org/cactoos/text/StringAsUrl.java @@ -57,7 +57,7 @@ public final class StringAsUrl implements Scalar { public StringAsUrl(final String url) { this(url, StandardCharsets.UTF_8); } - + /** * Ctor. * @param url The URL as Text @@ -74,7 +74,7 @@ public StringAsUrl(final Text url) { public StringAsUrl(final String url, final Charset encoding) { this(new StringAsText(url), encoding); } - + /** * Ctor. * @param url The URL as Text diff --git a/src/main/java/org/cactoos/text/UrlAsString.java b/src/main/java/org/cactoos/text/UrlAsString.java index b1a10a131e..d0ce2946a0 100644 --- a/src/main/java/org/cactoos/text/UrlAsString.java +++ b/src/main/java/org/cactoos/text/UrlAsString.java @@ -74,7 +74,7 @@ public UrlAsString(final Text url) { public UrlAsString(final String url, final Charset encoding) { this(new StringAsText(url), encoding); } - + /** * Ctor. * @param url The URL as Text From 4c3075758db4eaf364e050625be0908eeec507e6 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 9 Jun 2017 17:34:38 +0300 Subject: [PATCH 032/169] #102: bug --- .../org/cactoos/func/IoCheckedFuncTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/test/java/org/cactoos/func/IoCheckedFuncTest.java diff --git a/src/test/java/org/cactoos/func/IoCheckedFuncTest.java b/src/test/java/org/cactoos/func/IoCheckedFuncTest.java new file mode 100644 index 0000000000..a2b1c0233c --- /dev/null +++ b/src/test/java/org/cactoos/func/IoCheckedFuncTest.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.func; + +import java.io.IOException; +import org.cactoos.Func; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link IoCheckedFunc}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class IoCheckedFuncTest { + + @Test + public void rethrowsCheckedToUncheckedException() { + final IOException exception = new IOException("intended"); + try { + new IoCheckedFunc<>( + (Func) i -> { + throw exception; + } + ).apply(1); + } catch (final IOException ex) { + MatcherAssert.assertThat( + ex, Matchers.is(exception) + ); + } + } + +} From 80b2f1452c6c23349c0f1d78f239c91173b101da Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 9 Jun 2017 17:36:48 +0300 Subject: [PATCH 033/169] #102: more bugs --- .../org/cactoos/func/IoCheckedProcTest.java | 58 +++++++++++++++++++ .../org/cactoos/func/IoCheckedScalarTest.java | 58 +++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/test/java/org/cactoos/func/IoCheckedProcTest.java create mode 100644 src/test/java/org/cactoos/func/IoCheckedScalarTest.java diff --git a/src/test/java/org/cactoos/func/IoCheckedProcTest.java b/src/test/java/org/cactoos/func/IoCheckedProcTest.java new file mode 100644 index 0000000000..37185f4fb4 --- /dev/null +++ b/src/test/java/org/cactoos/func/IoCheckedProcTest.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.func; + +import java.io.IOException; +import org.cactoos.Proc; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link IoCheckedProc}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class IoCheckedProcTest { + + @Test + public void rethrowsCheckedToUncheckedException() { + final IOException exception = new IOException("intended"); + try { + new IoCheckedProc<>( + (Proc) i -> { + throw exception; + } + ).exec(1); + } catch (final IOException ex) { + MatcherAssert.assertThat( + ex, Matchers.is(exception) + ); + } + } + +} diff --git a/src/test/java/org/cactoos/func/IoCheckedScalarTest.java b/src/test/java/org/cactoos/func/IoCheckedScalarTest.java new file mode 100644 index 0000000000..4c0b5e904f --- /dev/null +++ b/src/test/java/org/cactoos/func/IoCheckedScalarTest.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.func; + +import java.io.IOException; +import org.cactoos.Scalar; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link IoCheckedScalar}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class IoCheckedScalarTest { + + @Test + public void rethrowsCheckedToUncheckedException() { + final IOException exception = new IOException("intended"); + try { + new IoCheckedScalar<>( + (Scalar) () -> { + throw exception; + } + ).asValue(); + } catch (final IOException ex) { + MatcherAssert.assertThat( + ex, Matchers.is(exception) + ); + } + } + +} From 83597677eb02fdffce1670cdd86d8869c778a1e7 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 9 Jun 2017 17:38:43 +0300 Subject: [PATCH 034/169] #102: fixed --- src/main/java/org/cactoos/func/IoCheckedFunc.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/func/IoCheckedFunc.java b/src/main/java/org/cactoos/func/IoCheckedFunc.java index e4859ed3c4..7bc90637aa 100644 --- a/src/main/java/org/cactoos/func/IoCheckedFunc.java +++ b/src/main/java/org/cactoos/func/IoCheckedFunc.java @@ -54,10 +54,18 @@ public IoCheckedFunc(final Func fnc) { } @Override - @SuppressWarnings("PMD.AvoidCatchingGenericException") + @SuppressWarnings + ( + { + "PMD.AvoidCatchingGenericException", + "PMD.AvoidRethrowingException" + } + ) public Y apply(final X input) throws IOException { try { return this.func.apply(input); + } catch (final IOException ex) { + throw ex; } catch (final InterruptedException ex) { Thread.currentThread().interrupt(); throw new IOException(ex); From 98f0a7717a0c3f3333ff643ab798e9d089814ff0 Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Fri, 9 Jun 2017 14:30:50 -0300 Subject: [PATCH 035/169] #90: removed throws Exception --- src/main/java/org/cactoos/text/StringAsUrl.java | 13 +++++++++++-- src/main/java/org/cactoos/text/UrlAsString.java | 13 +++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/cactoos/text/StringAsUrl.java b/src/main/java/org/cactoos/text/StringAsUrl.java index dcaaaf39c9..92d5d95969 100644 --- a/src/main/java/org/cactoos/text/StringAsUrl.java +++ b/src/main/java/org/cactoos/text/StringAsUrl.java @@ -23,6 +23,8 @@ */ package org.cactoos.text; +import java.io.IOException; +import java.io.UncheckedIOException; import java.net.URLEncoder; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -86,8 +88,15 @@ public StringAsUrl(final Text url, final Charset encoding) { } @Override - public String asValue() throws Exception { - return URLEncoder.encode(this.source.asString(), this.encoding.name()); + public String asValue() { + try { + return URLEncoder.encode( + this.source.asString(), + this.encoding.name() + ); + } catch (final IOException ex) { + throw new UncheckedIOException(ex); + } } } diff --git a/src/main/java/org/cactoos/text/UrlAsString.java b/src/main/java/org/cactoos/text/UrlAsString.java index d0ce2946a0..81b6199f4f 100644 --- a/src/main/java/org/cactoos/text/UrlAsString.java +++ b/src/main/java/org/cactoos/text/UrlAsString.java @@ -23,6 +23,8 @@ */ package org.cactoos.text; +import java.io.IOException; +import java.io.UncheckedIOException; import java.net.URLDecoder; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -86,8 +88,15 @@ public UrlAsString(final Text url, final Charset encoding) { } @Override - public String asValue() throws Exception { - return URLDecoder.decode(this.source.asString(), this.encoding.name()); + public String asValue() { + try { + return URLDecoder.decode( + this.source.asString(), + this.encoding.name() + ); + } catch (final IOException ex) { + throw new UncheckedIOException(ex); + } } } From b99858db61a2d49b741cd8d1dd6ab7d6349b8d5a Mon Sep 17 00:00:00 2001 From: Tim Hinkes Date: Sat, 10 Jun 2017 18:37:49 +0200 Subject: [PATCH 036/169] Refactored IterableAsList to remove unnecessary call to size() and remove not working cache no more calls to size() if not needed. Modifications in the underlying Iterable would lead to inconsistent cache. Removed cache completely. Caches do not work for modifyable Iterables. --- README.md | 1 + .../java/org/cactoos/list/IterableAsList.java | 49 ++++++------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 9111c559a7..ec628cdba4 100644 --- a/README.md +++ b/README.md @@ -252,6 +252,7 @@ Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static - [Andriy Kryvtsun](https://github.com/englishman) - [Vseslav Sekorin](https://github.com/VsSekorin) - [Andrey Valyaev](https://github.com/DronMDF) + - [Tim Hinkes](https://github.com/timmeey) [Blog](https://blog.timmeey.de) ## License (MIT) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index a76c176eb9..1c5d6426af 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -24,7 +24,6 @@ package org.cactoos.list; import java.util.AbstractList; -import java.util.ArrayList; import java.util.List; import org.cactoos.func.StickyScalar; import org.cactoos.func.UncheckedScalar; @@ -48,11 +47,6 @@ public final class IterableAsList extends AbstractList { */ private final Iterable source; - /** - * Cache for source. - */ - private final List cache; - /** * Iterable length. */ @@ -66,7 +60,6 @@ public final class IterableAsList extends AbstractList { public IterableAsList(final Iterable iterable) { super(); this.source = iterable; - this.cache = new ArrayList<>(0); this.length = new UncheckedScalar<>( new StickyScalar<>( new LengthOfIterable(iterable) @@ -76,19 +69,23 @@ public IterableAsList(final Iterable iterable) { @Override public T get(final int index) { - if (index < 0 || index >= this.size()) { - throw new IndexOutOfBoundsException( - new UncheckedText( - new FormattedText( - "index=%d, bounds=[%d; %d]", - index, - 0, - this.size() - ) - ).asString() - ); + int position = 0; + for (final T elem : this.source) { + if (position == index) { + return elem; + } + position += 1; } - return this.cachedItem(index); + throw new IndexOutOfBoundsException( + new UncheckedText( + new FormattedText( + "index=%d, bounds=[%d; %d]", + index, + 0, + this.size() + ) + ).asString() + ); } @Override @@ -96,18 +93,4 @@ public int size() { return this.length.asValue(); } - /** - * Find item in cache by index. - * - * @param index Item index - * @return Cached item - */ - private T cachedItem(final int index) { - if (this.cache.size() != this.size()) { - for (final T item : this.source) { - this.cache.add(item); - } - } - return this.cache.get(index); - } } From da64c705b53f3e92c378d5ad2a204692cc52a243 Mon Sep 17 00:00:00 2001 From: Tim Hinkes Date: Sat, 10 Jun 2017 18:54:20 +0200 Subject: [PATCH 037/169] Switched to non Cached size Since size() is not called all the time, and the premise is a mutable iterable, the size() can not be cached. --- src/main/java/org/cactoos/list/IterableAsList.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index 1c5d6426af..8dd9c642d1 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -25,7 +25,6 @@ import java.util.AbstractList; import java.util.List; -import org.cactoos.func.StickyScalar; import org.cactoos.func.UncheckedScalar; import org.cactoos.text.FormattedText; import org.cactoos.text.UncheckedText; @@ -61,9 +60,7 @@ public IterableAsList(final Iterable iterable) { super(); this.source = iterable; this.length = new UncheckedScalar<>( - new StickyScalar<>( - new LengthOfIterable(iterable) - ) + new LengthOfIterable(iterable) ); } From aeb61efe2f82b63bcffc37beda105c04e5636db1 Mon Sep 17 00:00:00 2001 From: Dusan Rychnovsky Date: Sat, 10 Jun 2017 20:34:41 +0200 Subject: [PATCH 038/169] #106 Improve unit tests. --- .../java/org/cactoos/text/ReplacedTextTest.java | 13 +++++++++++++ .../java/org/cactoos/text/ReversedTextTest.java | 10 ++++++++++ src/test/java/org/cactoos/text/TextAsBoolTest.java | 9 +++++++++ .../java/org/cactoos/text/TextAsDoubleTest.java | 5 +++++ src/test/java/org/cactoos/text/TextAsFloatTest.java | 5 +++++ src/test/java/org/cactoos/text/TextAsIntTest.java | 5 +++++ src/test/java/org/cactoos/text/TextAsLongTest.java | 5 +++++ src/test/java/org/cactoos/text/TrimmedTextTest.java | 8 ++++++++ 8 files changed, 60 insertions(+) diff --git a/src/test/java/org/cactoos/text/ReplacedTextTest.java b/src/test/java/org/cactoos/text/ReplacedTextTest.java index 345360294c..5bb29c4469 100644 --- a/src/test/java/org/cactoos/text/ReplacedTextTest.java +++ b/src/test/java/org/cactoos/text/ReplacedTextTest.java @@ -61,4 +61,17 @@ public void notReplaceTextWhenSubstringNotFound() { new TextHasString(text) ); } + + @Test + public void replacesAllOccurrences() { + MatcherAssert.assertThat( + "Can't replace a text with multiple needle occurrences", + new ReplacedText( + new StringAsText("one cat, two cats, three cats"), + "cat", + "dog" + ), + new TextHasString("one dog, two dogs, three dogs") + ); + } } diff --git a/src/test/java/org/cactoos/text/ReversedTextTest.java b/src/test/java/org/cactoos/text/ReversedTextTest.java index 665475cf65..3f688656d6 100644 --- a/src/test/java/org/cactoos/text/ReversedTextTest.java +++ b/src/test/java/org/cactoos/text/ReversedTextTest.java @@ -48,4 +48,14 @@ public void reverseText() { ); } + @Test + public void reversedEmptyTextIsEmptyText() { + MatcherAssert.assertThat( + "Can't reverse empty text", + new ReversedText( + new StringAsText("") + ), + new TextHasString("") + ); + } } diff --git a/src/test/java/org/cactoos/text/TextAsBoolTest.java b/src/test/java/org/cactoos/text/TextAsBoolTest.java index 0d8281a303..7260a74207 100644 --- a/src/test/java/org/cactoos/text/TextAsBoolTest.java +++ b/src/test/java/org/cactoos/text/TextAsBoolTest.java @@ -55,4 +55,13 @@ public void falseTest() throws IOException { Matchers.equalTo(false) ); } + + @Test + public void isFalseIfTextDoesNotRepresentABoolean() throws IOException { + MatcherAssert.assertThat( + "Can't parse a non-boolean string", + new TextAsBool("abc").asValue(), + Matchers.equalTo(false) + ); + } } diff --git a/src/test/java/org/cactoos/text/TextAsDoubleTest.java b/src/test/java/org/cactoos/text/TextAsDoubleTest.java index 367a6b5e1a..646805bffa 100644 --- a/src/test/java/org/cactoos/text/TextAsDoubleTest.java +++ b/src/test/java/org/cactoos/text/TextAsDoubleTest.java @@ -47,4 +47,9 @@ public strictfp void numberTest() throws IOException { Matchers.equalTo(185.65156465123) ); } + + @Test(expected = NumberFormatException.class) + public void failsIfTextDoesNotRepresentADouble() throws IOException { + new TextAsDouble("abc").asValue(); + } } diff --git a/src/test/java/org/cactoos/text/TextAsFloatTest.java b/src/test/java/org/cactoos/text/TextAsFloatTest.java index af0a634578..69cba90a27 100644 --- a/src/test/java/org/cactoos/text/TextAsFloatTest.java +++ b/src/test/java/org/cactoos/text/TextAsFloatTest.java @@ -47,4 +47,9 @@ public strictfp void numberTest() throws IOException { Matchers.equalTo(1656.894F) ); } + + @Test(expected = NumberFormatException.class) + public void failsIfTextDoesNotRepresentAFloat() throws IOException { + new TextAsFloat("abc").asValue(); + } } diff --git a/src/test/java/org/cactoos/text/TextAsIntTest.java b/src/test/java/org/cactoos/text/TextAsIntTest.java index cf4b162afb..c765e00011 100644 --- a/src/test/java/org/cactoos/text/TextAsIntTest.java +++ b/src/test/java/org/cactoos/text/TextAsIntTest.java @@ -47,4 +47,9 @@ public void numberTest() throws IOException { Matchers.equalTo(1867892354) ); } + + @Test(expected = NumberFormatException.class) + public void failsIfTextDoesNotRepresentAnInt() throws IOException { + new TextAsDouble("abc").asValue(); + } } diff --git a/src/test/java/org/cactoos/text/TextAsLongTest.java b/src/test/java/org/cactoos/text/TextAsLongTest.java index b3a2749a9f..cfa489c504 100644 --- a/src/test/java/org/cactoos/text/TextAsLongTest.java +++ b/src/test/java/org/cactoos/text/TextAsLongTest.java @@ -47,4 +47,9 @@ public void numberTest() throws IOException { Matchers.equalTo(186789235425346L) ); } + + @Test(expected = NumberFormatException.class) + public void failsIfTextDoesNotRepresentALong() throws IOException { + new TextAsLong("abc").asValue(); + } } diff --git a/src/test/java/org/cactoos/text/TrimmedTextTest.java b/src/test/java/org/cactoos/text/TrimmedTextTest.java index 2225c1c454..c97ed4096e 100644 --- a/src/test/java/org/cactoos/text/TrimmedTextTest.java +++ b/src/test/java/org/cactoos/text/TrimmedTextTest.java @@ -45,4 +45,12 @@ public void convertsText() { ); } + @Test + public void trimmedBlankTextIsEmptyText() { + MatcherAssert.assertThat( + "Can't trim a blank text", + new TrimmedText(new StringAsText(" \t ")), + new TextHasString("") + ); + } } From ac2736eaf38d63e967c266cf8ca456c17cdbdc71 Mon Sep 17 00:00:00 2001 From: Dusan Rychnovsky Date: Sat, 10 Jun 2017 20:49:32 +0200 Subject: [PATCH 039/169] #106 Add contributor. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 9111c559a7..df273711c2 100644 --- a/README.md +++ b/README.md @@ -252,6 +252,7 @@ Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static - [Andriy Kryvtsun](https://github.com/englishman) - [Vseslav Sekorin](https://github.com/VsSekorin) - [Andrey Valyaev](https://github.com/DronMDF) + - [Dušan Rychnovský](https://github.com/dusan-rychnovsky) [Blog](http://blog.dusanrychnovsky.cz/) ## License (MIT) From 13dd4ef53da41c8d726b7902c2e8a88a7a7306b2 Mon Sep 17 00:00:00 2001 From: Dusan Rychnovsky Date: Sat, 10 Jun 2017 21:38:17 +0200 Subject: [PATCH 040/169] #106 Fix code-style issues. --- src/test/java/org/cactoos/text/ReplacedTextTest.java | 2 +- src/test/java/org/cactoos/text/TextAsBoolTest.java | 2 +- src/test/java/org/cactoos/text/TextAsDoubleTest.java | 2 +- src/test/java/org/cactoos/text/TextAsFloatTest.java | 2 +- src/test/java/org/cactoos/text/TextAsIntTest.java | 2 +- src/test/java/org/cactoos/text/TextAsLongTest.java | 4 ++-- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/cactoos/text/ReplacedTextTest.java b/src/test/java/org/cactoos/text/ReplacedTextTest.java index 5bb29c4469..211f5f742c 100644 --- a/src/test/java/org/cactoos/text/ReplacedTextTest.java +++ b/src/test/java/org/cactoos/text/ReplacedTextTest.java @@ -61,7 +61,7 @@ public void notReplaceTextWhenSubstringNotFound() { new TextHasString(text) ); } - + @Test public void replacesAllOccurrences() { MatcherAssert.assertThat( diff --git a/src/test/java/org/cactoos/text/TextAsBoolTest.java b/src/test/java/org/cactoos/text/TextAsBoolTest.java index 7260a74207..5ddb3265a8 100644 --- a/src/test/java/org/cactoos/text/TextAsBoolTest.java +++ b/src/test/java/org/cactoos/text/TextAsBoolTest.java @@ -55,7 +55,7 @@ public void falseTest() throws IOException { Matchers.equalTo(false) ); } - + @Test public void isFalseIfTextDoesNotRepresentABoolean() throws IOException { MatcherAssert.assertThat( diff --git a/src/test/java/org/cactoos/text/TextAsDoubleTest.java b/src/test/java/org/cactoos/text/TextAsDoubleTest.java index 646805bffa..bd8f5d1cf0 100644 --- a/src/test/java/org/cactoos/text/TextAsDoubleTest.java +++ b/src/test/java/org/cactoos/text/TextAsDoubleTest.java @@ -47,7 +47,7 @@ public strictfp void numberTest() throws IOException { Matchers.equalTo(185.65156465123) ); } - + @Test(expected = NumberFormatException.class) public void failsIfTextDoesNotRepresentADouble() throws IOException { new TextAsDouble("abc").asValue(); diff --git a/src/test/java/org/cactoos/text/TextAsFloatTest.java b/src/test/java/org/cactoos/text/TextAsFloatTest.java index 69cba90a27..c6fbebed01 100644 --- a/src/test/java/org/cactoos/text/TextAsFloatTest.java +++ b/src/test/java/org/cactoos/text/TextAsFloatTest.java @@ -47,7 +47,7 @@ public strictfp void numberTest() throws IOException { Matchers.equalTo(1656.894F) ); } - + @Test(expected = NumberFormatException.class) public void failsIfTextDoesNotRepresentAFloat() throws IOException { new TextAsFloat("abc").asValue(); diff --git a/src/test/java/org/cactoos/text/TextAsIntTest.java b/src/test/java/org/cactoos/text/TextAsIntTest.java index c765e00011..d999ff2da2 100644 --- a/src/test/java/org/cactoos/text/TextAsIntTest.java +++ b/src/test/java/org/cactoos/text/TextAsIntTest.java @@ -47,7 +47,7 @@ public void numberTest() throws IOException { Matchers.equalTo(1867892354) ); } - + @Test(expected = NumberFormatException.class) public void failsIfTextDoesNotRepresentAnInt() throws IOException { new TextAsDouble("abc").asValue(); diff --git a/src/test/java/org/cactoos/text/TextAsLongTest.java b/src/test/java/org/cactoos/text/TextAsLongTest.java index cfa489c504..1a8ae4334f 100644 --- a/src/test/java/org/cactoos/text/TextAsLongTest.java +++ b/src/test/java/org/cactoos/text/TextAsLongTest.java @@ -47,9 +47,9 @@ public void numberTest() throws IOException { Matchers.equalTo(186789235425346L) ); } - + @Test(expected = NumberFormatException.class) public void failsIfTextDoesNotRepresentALong() throws IOException { - new TextAsLong("abc").asValue(); + new TextAsLong("abc").asValue(); } } From 2c3282c3a63b71cc2506d5305acdda77485ef310 Mon Sep 17 00:00:00 2001 From: g4s8 Date: Sun, 11 Jun 2017 13:12:03 +0400 Subject: [PATCH 041/169] first or default --- src/main/java/org/cactoos/ConstScalar.java | 56 ++++++++++ .../java/org/cactoos/list/FirstOrDefault.java | 103 ++++++++++++++++++ .../org/cactoos/list/FirstOrDefaultTest.java | 64 +++++++++++ 3 files changed, 223 insertions(+) create mode 100644 src/main/java/org/cactoos/ConstScalar.java create mode 100644 src/main/java/org/cactoos/list/FirstOrDefault.java create mode 100644 src/test/java/org/cactoos/list/FirstOrDefaultTest.java diff --git a/src/main/java/org/cactoos/ConstScalar.java b/src/main/java/org/cactoos/ConstScalar.java new file mode 100644 index 0000000000..dcbcc0e830 --- /dev/null +++ b/src/main/java/org/cactoos/ConstScalar.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; + +/** + * Constant scalar. + * + *

This class is immutable and thread-safe. + * + * @author Kirill (g4s8.public@gmail.com) + * @version $Id$ + * @param Type of result + * @since 0.4 + */ +public final class ConstScalar implements Scalar { + + /** + * Scalar value. + */ + private final T value; + + /** + * Ctor. + * + * @param value Scalar value + */ + public ConstScalar(final T value) { + this.value = value; + } + + @Override + public T asValue() throws Exception { + return this.value; + } +} diff --git a/src/main/java/org/cactoos/list/FirstOrDefault.java b/src/main/java/org/cactoos/list/FirstOrDefault.java new file mode 100644 index 0000000000..db1e89b48a --- /dev/null +++ b/src/main/java/org/cactoos/list/FirstOrDefault.java @@ -0,0 +1,103 @@ +/** + * 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.list; + +import java.io.IOException; +import org.cactoos.ConstScalar; +import org.cactoos.Scalar; + +/** + * First element in {@link Iterable} or default value if iterable is empty. + * + *

There is no thread-safety guarantee. + * + * @author Kirill (g4s8.public@gmail.com) + * @version $Id$ + * @param Scalar type + * @since 0.4 + */ +public final class FirstOrDefault implements Scalar { + + /** + * First element. + */ + private final FirstOf first; + + /** + * Fallback value. + */ + private final Scalar fallback; + + /** + * Ctor. + * + * @param source Iterable + * @param fallback Default value + */ + public FirstOrDefault(final Iterable source, final T fallback) { + this(source, new ConstScalar<>(fallback)); + } + + /** + * Ctor. + * + * @param source Iterable + * @param fallback Default value + */ + public FirstOrDefault(final Iterable source, final Scalar fallback) { + this(new FirstOf<>(source), fallback); + } + + /** + * Ctor. + * + * @param first First element + * @param fallback Default value + */ + public FirstOrDefault(final FirstOf first, final T fallback) { + this(first, new ConstScalar<>(fallback)); + } + + /** + * Ctor. + * + * @param first First element + * @param fallback Default value + */ + public FirstOrDefault(final FirstOf first, final Scalar fallback) { + this.first = first; + this.fallback = fallback; + } + + @Override + public T asValue() throws Exception { + T value; + try { + value = this.first.asValue(); + } catch (final IOException ex) { + value = this.fallback.asValue(); + } + return value; + } +} diff --git a/src/test/java/org/cactoos/list/FirstOrDefaultTest.java b/src/test/java/org/cactoos/list/FirstOrDefaultTest.java new file mode 100644 index 0000000000..8e22f5bb4e --- /dev/null +++ b/src/test/java/org/cactoos/list/FirstOrDefaultTest.java @@ -0,0 +1,64 @@ +/** + * 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.list; + +import java.util.Collections; +import org.cactoos.ScalarHasValue; +import org.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test case for {@link FirstOrDefault}. + * + * @author Kirill (g4s8.public@gmail.com) + * @version $Id$ + * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class FirstOrDefaultTest { + + @Test + public void firstTest() { + MatcherAssert.assertThat( + "Can't find first element", + new FirstOrDefault<>( + new ArrayAsIterable<>(2, 1, 0), + -1 + ), + new ScalarHasValue<>(2) + ); + } + + @Test + public void defaultTest() { + MatcherAssert.assertThat( + "Can't fallback to default element", + new FirstOrDefault<>( + Collections.emptyList(), + 1 + ), + new ScalarHasValue<>(1) + ); + } +} From d20f319bc13b3ccf3682fb2704ed83e695226ae0 Mon Sep 17 00:00:00 2001 From: g4s8 Date: Sun, 11 Jun 2017 17:08:01 +0400 Subject: [PATCH 042/169] fallback value as FirstOf param --- src/main/java/org/cactoos/ConstScalar.java | 56 ---------- src/main/java/org/cactoos/list/FirstOf.java | 56 ++++++++-- .../java/org/cactoos/list/FirstOrDefault.java | 103 ------------------ .../java/org/cactoos/list/FirstOfTest.java | 12 ++ .../org/cactoos/list/FirstOrDefaultTest.java | 64 ----------- 5 files changed, 58 insertions(+), 233 deletions(-) delete mode 100644 src/main/java/org/cactoos/ConstScalar.java delete mode 100644 src/main/java/org/cactoos/list/FirstOrDefault.java delete mode 100644 src/test/java/org/cactoos/list/FirstOrDefaultTest.java diff --git a/src/main/java/org/cactoos/ConstScalar.java b/src/main/java/org/cactoos/ConstScalar.java deleted file mode 100644 index dcbcc0e830..0000000000 --- a/src/main/java/org/cactoos/ConstScalar.java +++ /dev/null @@ -1,56 +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; - -/** - * Constant scalar. - * - *

This class is immutable and thread-safe. - * - * @author Kirill (g4s8.public@gmail.com) - * @version $Id$ - * @param Type of result - * @since 0.4 - */ -public final class ConstScalar implements Scalar { - - /** - * Scalar value. - */ - private final T value; - - /** - * Ctor. - * - * @param value Scalar value - */ - public ConstScalar(final T value) { - this.value = value; - } - - @Override - public T asValue() throws Exception { - return this.value; - } -} diff --git a/src/main/java/org/cactoos/list/FirstOf.java b/src/main/java/org/cactoos/list/FirstOf.java index 597a9048f8..b567fa2c63 100644 --- a/src/main/java/org/cactoos/list/FirstOf.java +++ b/src/main/java/org/cactoos/list/FirstOf.java @@ -28,7 +28,9 @@ import org.cactoos.Scalar; /** - * First element in {@link Iterable}. + * First element in {@link Iterable} or fallback value if iterable is empty. + * + *

There is no thread-safety guarantee. * * @author Kirill (g4s8.public@gmail.com) * @version $Id$ @@ -40,23 +42,57 @@ public final class FirstOf implements Scalar { /** * Source iterable. */ - private final Iterable source; + private final Iterable src; + + /** + * Fallback value. + */ + private final Scalar fbk; + + /** + * Ctor. + * + * @param src Iterable + */ + public FirstOf(final Iterable src) { + this( + src, + () -> { + throw new IOException("Iterable is empty"); + } + ); + } + + /** + * Ctor. + * + * @param src Iterable + * @param fbk Fallback value + */ + public FirstOf(final Iterable src, final T fbk) { + this(src, () -> fbk); + } /** * Ctor. * - * @param source Iterable + * @param src Iterable + * @param fbk Fallback value */ - public FirstOf(final Iterable source) { - this.source = source; + public FirstOf(final Iterable src, final Scalar fbk) { + this.src = src; + this.fbk = fbk; } @Override - public T asValue() throws IOException { - final Iterator iterator = this.source.iterator(); - if (!iterator.hasNext()) { - throw new IOException("Iterable is empty"); + public T asValue() throws Exception { + final Iterator itr = this.src.iterator(); + final T ret; + if (itr.hasNext()) { + ret = itr.next(); + } else { + ret = this.fbk.asValue(); } - return iterator.next(); + return ret; } } diff --git a/src/main/java/org/cactoos/list/FirstOrDefault.java b/src/main/java/org/cactoos/list/FirstOrDefault.java deleted file mode 100644 index db1e89b48a..0000000000 --- a/src/main/java/org/cactoos/list/FirstOrDefault.java +++ /dev/null @@ -1,103 +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.list; - -import java.io.IOException; -import org.cactoos.ConstScalar; -import org.cactoos.Scalar; - -/** - * First element in {@link Iterable} or default value if iterable is empty. - * - *

There is no thread-safety guarantee. - * - * @author Kirill (g4s8.public@gmail.com) - * @version $Id$ - * @param Scalar type - * @since 0.4 - */ -public final class FirstOrDefault implements Scalar { - - /** - * First element. - */ - private final FirstOf first; - - /** - * Fallback value. - */ - private final Scalar fallback; - - /** - * Ctor. - * - * @param source Iterable - * @param fallback Default value - */ - public FirstOrDefault(final Iterable source, final T fallback) { - this(source, new ConstScalar<>(fallback)); - } - - /** - * Ctor. - * - * @param source Iterable - * @param fallback Default value - */ - public FirstOrDefault(final Iterable source, final Scalar fallback) { - this(new FirstOf<>(source), fallback); - } - - /** - * Ctor. - * - * @param first First element - * @param fallback Default value - */ - public FirstOrDefault(final FirstOf first, final T fallback) { - this(first, new ConstScalar<>(fallback)); - } - - /** - * Ctor. - * - * @param first First element - * @param fallback Default value - */ - public FirstOrDefault(final FirstOf first, final Scalar fallback) { - this.first = first; - this.fallback = fallback; - } - - @Override - public T asValue() throws Exception { - T value; - try { - value = this.first.asValue(); - } catch (final IOException ex) { - value = this.fallback.asValue(); - } - return value; - } -} diff --git a/src/test/java/org/cactoos/list/FirstOfTest.java b/src/test/java/org/cactoos/list/FirstOfTest.java index 0ee7e79a22..3adf6f3c4f 100644 --- a/src/test/java/org/cactoos/list/FirstOfTest.java +++ b/src/test/java/org/cactoos/list/FirstOfTest.java @@ -56,4 +56,16 @@ public void firstElementTest() throws Exception { public void failForEmptyCollectionTest() throws Exception { new FirstOf<>(Collections.emptyList()).asValue(); } + + @Test + public void fallbackTest() throws Exception { + MatcherAssert.assertThat( + "Can't fallback to default value", + new FirstOf<>( + Collections.emptyList(), + 1 + ), + new ScalarHasValue<>(1) + ); + } } diff --git a/src/test/java/org/cactoos/list/FirstOrDefaultTest.java b/src/test/java/org/cactoos/list/FirstOrDefaultTest.java deleted file mode 100644 index 8e22f5bb4e..0000000000 --- a/src/test/java/org/cactoos/list/FirstOrDefaultTest.java +++ /dev/null @@ -1,64 +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.list; - -import java.util.Collections; -import org.cactoos.ScalarHasValue; -import org.hamcrest.MatcherAssert; -import org.junit.Test; - -/** - * Test case for {@link FirstOrDefault}. - * - * @author Kirill (g4s8.public@gmail.com) - * @version $Id$ - * @since 0.4 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class FirstOrDefaultTest { - - @Test - public void firstTest() { - MatcherAssert.assertThat( - "Can't find first element", - new FirstOrDefault<>( - new ArrayAsIterable<>(2, 1, 0), - -1 - ), - new ScalarHasValue<>(2) - ); - } - - @Test - public void defaultTest() { - MatcherAssert.assertThat( - "Can't fallback to default element", - new FirstOrDefault<>( - Collections.emptyList(), - 1 - ), - new ScalarHasValue<>(1) - ); - } -} From 3d71e3f150862f8ca08ae81dcfc399c1317a928d Mon Sep 17 00:00:00 2001 From: Dusan Rychnovsky Date: Mon, 12 Jun 2017 23:08:08 +0200 Subject: [PATCH 043/169] #109 LimitedIterable --- .../org/cactoos/list/LimitedIterable.java | 40 ++++++++++++ .../org/cactoos/list/LimitedIterator.java | 49 +++++++++++++++ .../org/cactoos/list/LimitedIterableTest.java | 62 +++++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 src/main/java/org/cactoos/list/LimitedIterable.java create mode 100644 src/main/java/org/cactoos/list/LimitedIterator.java create mode 100644 src/test/java/org/cactoos/list/LimitedIterableTest.java diff --git a/src/main/java/org/cactoos/list/LimitedIterable.java b/src/main/java/org/cactoos/list/LimitedIterable.java new file mode 100644 index 0000000000..60de3859ec --- /dev/null +++ b/src/main/java/org/cactoos/list/LimitedIterable.java @@ -0,0 +1,40 @@ +package org.cactoos.list; + +import java.util.Iterator; + +/** + * Limited iterable. + * + *

This is a view of an existing iterable containing the given number of its + * first elements.

+ * + * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) + * @param Element type + * @since 0.4 + */ +public class LimitedIterable implements Iterable { + + private final Iterable iterable; + private final int limitSize; + + /** + * Ctor. + * + * @param iterable the underlying iterable + * @param limitSize the requested number of elements + */ + public LimitedIterable(Iterable iterable, int limitSize) { + if (limitSize < 0) { + throw new IllegalArgumentException( + "Expected limitSize >= 0, got [" + limitSize + "]." + ); + } + this.iterable = iterable; + this.limitSize = limitSize; + } + + @Override + public Iterator iterator() { + return new LimitedIterator<>(iterable.iterator(), limitSize); + } +} diff --git a/src/main/java/org/cactoos/list/LimitedIterator.java b/src/main/java/org/cactoos/list/LimitedIterator.java new file mode 100644 index 0000000000..8f1945d216 --- /dev/null +++ b/src/main/java/org/cactoos/list/LimitedIterator.java @@ -0,0 +1,49 @@ +package org.cactoos.list; + +import java.util.Iterator; + +/** + * Limited iterator. + * + *

This is a decorator over an existing iterator. Returns elements of the + * original iterator, until either the requested number of items have been + * returned or the underlying iterator has been exhausted.

+ * + * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) + * @param Element type + * @since 0.4 + */ +public class LimitedIterator implements Iterator { + + private final Iterator iterator; + private final int limitSize; + private int consumedSize; + + /** + * Ctor. + * + * @param iterator the underlying iterator + * @param limitSize the requested number of elements + */ + public LimitedIterator(Iterator iterator, int limitSize) { + if (limitSize < 0) { + throw new IllegalArgumentException( + "Expected limitSize >= 0, got [" + limitSize + "]." + ); + } + this.iterator = iterator; + this.limitSize = limitSize; + this.consumedSize = 0; + } + + @Override + public boolean hasNext() { + return (consumedSize < limitSize) && iterator.hasNext(); + } + + @Override + public T next() { + ++consumedSize; + return iterator.next(); + } +} diff --git a/src/test/java/org/cactoos/list/LimitedIterableTest.java b/src/test/java/org/cactoos/list/LimitedIterableTest.java new file mode 100644 index 0000000000..ea61a3cbf4 --- /dev/null +++ b/src/test/java/org/cactoos/list/LimitedIterableTest.java @@ -0,0 +1,62 @@ +package org.cactoos.list; + +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link LimitedIterable} + * + * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) + * @since 0.4 + */ +public class LimitedIterableTest { + + @Test + public void iteratesOverPrefixOfGivenLength() { + MatcherAssert.assertThat( + "Can't limit an iterable with more items", + new LimitedIterable<>( + new ArrayAsIterable<>(0, 1, 2, 3, 4), + 3 + ), + Matchers.contains(0, 1, 2) + ); + } + + @Test + public void iteratesOverWholeIterableIfThereAreNotEnoughItems() { + MatcherAssert.assertThat( + "Can't limit an iterable with less items", + new LimitedIterable<>( + new ArrayAsIterable<>(0, 1, 2, 3, 4), + 10 + ), + Matchers.contains(0, 1, 2, 3, 4) + ); + } + + @Test + public void limitOfZeroProducesEmptyIterable() { + MatcherAssert.assertThat( + "Can't limit an iterable to zero items", + new LimitedIterable<>( + new ArrayAsIterable<>(0, 1, 2, 3, 4), + 0 + ), + Matchers.iterableWithSize(0) + ); + } + + @Test + public void emptyIterableProducesEmptyIterable() { + MatcherAssert.assertThat( + "Can't limit an empty iterable", + new LimitedIterable<>( + new ArrayAsIterable<>(), + 10 + ), + Matchers.iterableWithSize(0) + ); + } +} From b8672d4fe89310dd9c4b034de0519e75bc4278d0 Mon Sep 17 00:00:00 2001 From: Dusan Rychnovsky Date: Mon, 12 Jun 2017 23:12:38 +0200 Subject: [PATCH 044/169] #109 Additional test case --- src/test/java/org/cactoos/list/LimitedIterableTest.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/test/java/org/cactoos/list/LimitedIterableTest.java b/src/test/java/org/cactoos/list/LimitedIterableTest.java index ea61a3cbf4..fd0ca9dc77 100644 --- a/src/test/java/org/cactoos/list/LimitedIterableTest.java +++ b/src/test/java/org/cactoos/list/LimitedIterableTest.java @@ -12,6 +12,11 @@ */ public class LimitedIterableTest { + @Test(expected = IllegalArgumentException.class) + public void failsIfGivenLimitIsLessThanZero() { + new LimitedIterable<>(new ArrayAsIterable<>(), -1); + } + @Test public void iteratesOverPrefixOfGivenLength() { MatcherAssert.assertThat( From f9593621ba750800a00b62e219cdd1e07cd65817 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 09:34:01 +0300 Subject: [PATCH 045/169] #116: IterableAsMap --- .../java/org/cactoos/map/IterableAsMap.java | 148 ++++++++++++++++++ .../java/org/cactoos/map/package-info.java | 32 ++++ .../org/cactoos/map/IterableAsMapTest.java | 56 +++++++ .../java/org/cactoos/map/package-info.java | 32 ++++ 4 files changed, 268 insertions(+) create mode 100644 src/main/java/org/cactoos/map/IterableAsMap.java create mode 100644 src/main/java/org/cactoos/map/package-info.java create mode 100644 src/test/java/org/cactoos/map/IterableAsMapTest.java create mode 100644 src/test/java/org/cactoos/map/package-info.java diff --git a/src/main/java/org/cactoos/map/IterableAsMap.java b/src/main/java/org/cactoos/map/IterableAsMap.java new file mode 100644 index 0000000000..17e48e418d --- /dev/null +++ b/src/main/java/org/cactoos/map/IterableAsMap.java @@ -0,0 +1,148 @@ +/** + * 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.map; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import org.cactoos.func.StickyScalar; +import org.cactoos.func.UncheckedScalar; +import org.cactoos.list.ArrayAsIterable; + +/** + * Iterable as {@link Map}. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of key + * @param Type of value + * @since 0.4 + */ +public final class IterableAsMap implements Map { + + /** + * The map. + */ + private final UncheckedScalar> map; + + /** + * Ctor. + * @param entries Entries for the map + */ + @SafeVarargs + @SuppressWarnings("varargs") + public IterableAsMap(final Map.Entry... entries) { + this(new ArrayAsIterable<>(entries)); + } + + /** + * Ctor. + * @param entries Entries for the map + */ + public IterableAsMap(final Iterable> entries) { + this.map = new UncheckedScalar<>( + new StickyScalar<>( + () -> { + final Map temp = new HashMap<>(0); + for (final Map.Entry entry : entries) { + temp.put(entry.getKey(), entry.getValue()); + } + return temp; + } + ) + ); + } + + @Override + public int size() { + return this.map.asValue().size(); + } + + @Override + public boolean isEmpty() { + return this.map.asValue().isEmpty(); + } + + @Override + public boolean containsKey(final Object key) { + return this.map.asValue().containsKey(key); + } + + @Override + public boolean containsValue(final Object value) { + return this.map.asValue().containsValue(value); + } + + @Override + public Y get(final Object key) { + return this.map.asValue().get(key); + } + + @Override + public Y put(final X key, final Y value) { + throw new UnsupportedOperationException( + "#put() is not supported" + ); + } + + @Override + public Y remove(final Object key) { + throw new UnsupportedOperationException( + "#remove() is not supported" + ); + } + + @Override + public void putAll(final Map entries) { + throw new UnsupportedOperationException( + "#putAll() is not supported" + ); + } + + @Override + public void clear() { + throw new UnsupportedOperationException( + "#clear() is not supported" + ); + } + + @Override + public Set keySet() { + return this.map.asValue().keySet(); + } + + @Override + public Collection values() { + return this.map.asValue().values(); + } + + @Override + public Set> entrySet() { + return this.map.asValue().entrySet(); + } + +} diff --git a/src/main/java/org/cactoos/map/package-info.java b/src/main/java/org/cactoos/map/package-info.java new file mode 100644 index 0000000000..cd9f22d554 --- /dev/null +++ b/src/main/java/org/cactoos/map/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. + */ + +/** + * Maps. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.4 + */ +package org.cactoos.map; diff --git a/src/test/java/org/cactoos/map/IterableAsMapTest.java b/src/test/java/org/cactoos/map/IterableAsMapTest.java new file mode 100644 index 0000000000..cdcad047ea --- /dev/null +++ b/src/test/java/org/cactoos/map/IterableAsMapTest.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.map; + +import java.util.AbstractMap; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link IterableAsMap}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class IterableAsMapTest { + + @Test + @SuppressWarnings("unchecked") + public void convertsIterableToMap() { + MatcherAssert.assertThat( + "Can't convert iterable to map", + new IterableAsMap<>( + new AbstractMap.SimpleEntry<>(0, "hello, "), + new AbstractMap.SimpleEntry<>(1, "world!") + ), + Matchers.hasEntry( + Matchers.equalTo(0), + Matchers.startsWith("hello") + ) + ); + } +} diff --git a/src/test/java/org/cactoos/map/package-info.java b/src/test/java/org/cactoos/map/package-info.java new file mode 100644 index 0000000000..6a01588604 --- /dev/null +++ b/src/test/java/org/cactoos/map/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. + */ + +/** + * Maps, tests. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.4 + */ +package org.cactoos.map; From 769e354b44eefbe2dbb6be5af2c23d728be97c5d Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 10:32:53 +0300 Subject: [PATCH 046/169] #119: StickyScalar fixed --- .../java/org/cactoos/func/StickyScalar.java | 23 +++----- .../java/org/cactoos/func/StickyFuncTest.java | 53 +++++++++++++++++++ .../org/cactoos/func/StickyScalarTest.java | 53 +++++++++++++++++++ 3 files changed, 114 insertions(+), 15 deletions(-) create mode 100644 src/test/java/org/cactoos/func/StickyFuncTest.java create mode 100644 src/test/java/org/cactoos/func/StickyScalarTest.java diff --git a/src/main/java/org/cactoos/func/StickyScalar.java b/src/main/java/org/cactoos/func/StickyScalar.java index 512d5aaf08..981a569883 100644 --- a/src/main/java/org/cactoos/func/StickyScalar.java +++ b/src/main/java/org/cactoos/func/StickyScalar.java @@ -23,8 +23,7 @@ */ package org.cactoos.func; -import java.util.ArrayList; -import java.util.List; +import org.cactoos.Func; import org.cactoos.Scalar; /** @@ -33,6 +32,7 @@ *

There is no thread-safety guarantee. * * @author Tim Hinkes (timmeey@timmeey.de) + * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Type of result * @since 0.3 @@ -40,29 +40,22 @@ public final class StickyScalar implements Scalar { /** - * The scalar to cache. + * Func. */ - private final Scalar source; - - /** - * The list used as a optional value. - */ - private final List cache; + private final Func func; /** * Ctor. * @param src The Scalar to cache */ public StickyScalar(final Scalar src) { - this.source = src; - this.cache = new ArrayList<>(1); + this.func = new StickyFunc<>( + input -> src.asValue() + ); } @Override public T asValue() throws Exception { - if (this.cache.isEmpty()) { - this.cache.add(this.source.asValue()); - } - return this.cache.get(0); + return this.func.apply(true); } } diff --git a/src/test/java/org/cactoos/func/StickyFuncTest.java b/src/test/java/org/cactoos/func/StickyFuncTest.java new file mode 100644 index 0000000000..94da814cbd --- /dev/null +++ b/src/test/java/org/cactoos/func/StickyFuncTest.java @@ -0,0 +1,53 @@ +/** + * 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 java.security.SecureRandom; +import org.cactoos.Func; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link StickyFunc}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class StickyFuncTest { + + @Test + public void cachesFuncResults() throws Exception { + final Func func = new StickyFunc<>( + input -> new SecureRandom().nextInt() + ); + MatcherAssert.assertThat( + func.apply(true) + func.apply(true), + Matchers.equalTo(func.apply(true) + func.apply(true)) + ); + } + +} diff --git a/src/test/java/org/cactoos/func/StickyScalarTest.java b/src/test/java/org/cactoos/func/StickyScalarTest.java new file mode 100644 index 0000000000..0e16476e24 --- /dev/null +++ b/src/test/java/org/cactoos/func/StickyScalarTest.java @@ -0,0 +1,53 @@ +/** + * 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 java.security.SecureRandom; +import org.cactoos.Scalar; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link StickyScalar}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class StickyScalarTest { + + @Test + public void cachesScalarResults() throws Exception { + final Scalar scalar = new StickyScalar<>( + () -> new SecureRandom().nextInt() + ); + MatcherAssert.assertThat( + scalar.asValue() + scalar.asValue(), + Matchers.equalTo(scalar.asValue() + scalar.asValue()) + ); + } + +} From 0a1894824c8e8e9f0d66833e9718f9ad8ad1bae2 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 15:44:20 +0300 Subject: [PATCH 047/169] #116 package removed --- .../cactoos/{map => list}/IterableAsMap.java | 3 +- .../java/org/cactoos/map/package-info.java | 32 ------------------- .../{map => list}/IterableAsMapTest.java | 2 +- .../java/org/cactoos/map/package-info.java | 32 ------------------- 4 files changed, 2 insertions(+), 67 deletions(-) rename src/main/java/org/cactoos/{map => list}/IterableAsMap.java (98%) delete mode 100644 src/main/java/org/cactoos/map/package-info.java rename src/test/java/org/cactoos/{map => list}/IterableAsMapTest.java (98%) delete mode 100644 src/test/java/org/cactoos/map/package-info.java diff --git a/src/main/java/org/cactoos/map/IterableAsMap.java b/src/main/java/org/cactoos/list/IterableAsMap.java similarity index 98% rename from src/main/java/org/cactoos/map/IterableAsMap.java rename to src/main/java/org/cactoos/list/IterableAsMap.java index 17e48e418d..b1f316434c 100644 --- a/src/main/java/org/cactoos/map/IterableAsMap.java +++ b/src/main/java/org/cactoos/list/IterableAsMap.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.cactoos.map; +package org.cactoos.list; import java.util.Collection; import java.util.HashMap; @@ -29,7 +29,6 @@ import java.util.Set; import org.cactoos.func.StickyScalar; import org.cactoos.func.UncheckedScalar; -import org.cactoos.list.ArrayAsIterable; /** * Iterable as {@link Map}. diff --git a/src/main/java/org/cactoos/map/package-info.java b/src/main/java/org/cactoos/map/package-info.java deleted file mode 100644 index cd9f22d554..0000000000 --- a/src/main/java/org/cactoos/map/package-info.java +++ /dev/null @@ -1,32 +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. - */ - -/** - * Maps. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @since 0.4 - */ -package org.cactoos.map; diff --git a/src/test/java/org/cactoos/map/IterableAsMapTest.java b/src/test/java/org/cactoos/list/IterableAsMapTest.java similarity index 98% rename from src/test/java/org/cactoos/map/IterableAsMapTest.java rename to src/test/java/org/cactoos/list/IterableAsMapTest.java index cdcad047ea..57bb9a4c49 100644 --- a/src/test/java/org/cactoos/map/IterableAsMapTest.java +++ b/src/test/java/org/cactoos/list/IterableAsMapTest.java @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.cactoos.map; +package org.cactoos.list; import java.util.AbstractMap; import org.hamcrest.MatcherAssert; diff --git a/src/test/java/org/cactoos/map/package-info.java b/src/test/java/org/cactoos/map/package-info.java deleted file mode 100644 index 6a01588604..0000000000 --- a/src/test/java/org/cactoos/map/package-info.java +++ /dev/null @@ -1,32 +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. - */ - -/** - * Maps, tests. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @since 0.4 - */ -package org.cactoos.map; From 9d39e23a8fb57ab5b694dbd727fca9cab9802509 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 15:57:03 +0300 Subject: [PATCH 048/169] names --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3347205fe1..fc992280df 100644 --- a/README.md +++ b/README.md @@ -246,14 +246,14 @@ Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static ## Contributors - - [Yegor Bugayenko](https://github.com/yegor256) - - [Kirill Che.](https://github.com/g4s8) g4s8.public@gmail.com - - [Fabrício Cabral](https://github.com/fabriciofx) - - [Andriy Kryvtsun](https://github.com/englishman) - - [Vseslav Sekorin](https://github.com/VsSekorin) - - [Andrey Valyaev](https://github.com/DronMDF) - - [Dušan Rychnovský](https://github.com/dusan-rychnovsky) [Blog](http://blog.dusanrychnovsky.cz/) - - [Tim Hinkes](https://github.com/timmeey) [Blog](https://blog.timmeey.de) + - [@yegor256](https://github.com/yegor256) as Yegor Bugayenko (yegor256@gmail.com) + - [@g4s8](https://github.com/g4s8) as Kirill Che. (g4s8.public@gmail.com) + - [@fabriciofx](https://github.com/fabriciofx) as Fabrício Cabral + - [@englishman](https://github.com/englishman) as Andriy Kryvtsun + - [@VsSekorin](https://github.com/VsSekorin) as Vseslav Sekorin + - [@DronMDF](https://github.com/DronMDF) as Andrey Valyaev + - [@dusan-rychnovsky](https://github.com/dusan-rychnovsky) as Dušan Rychnovský ([Blog](http://blog.dusanrychnovsky.cz/)) + - [@timmeey](https://github.com/timmeey) as Tim Hinkes ([Blog](https://blog.timmeey.de)) ## License (MIT) From 2a49072a89e8507279e731fe46297af874847bc8 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 15:57:38 +0300 Subject: [PATCH 049/169] blog --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fc992280df..1d9987c2b7 100644 --- a/README.md +++ b/README.md @@ -246,7 +246,7 @@ Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static ## Contributors - - [@yegor256](https://github.com/yegor256) as Yegor Bugayenko (yegor256@gmail.com) + - [@yegor256](https://github.com/yegor256) as Yegor Bugayenko ([Blog](http://www.yegor256.com)) - [@g4s8](https://github.com/g4s8) as Kirill Che. (g4s8.public@gmail.com) - [@fabriciofx](https://github.com/fabriciofx) as Fabrício Cabral - [@englishman](https://github.com/englishman) as Andriy Kryvtsun From 4ad21e22e11df69c862619a2263f7fa03807d8ed Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 18:32:26 +0300 Subject: [PATCH 050/169] doc --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1d9987c2b7..8d9e1f3025 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Java version required: 1.8+. ## Input/Output -To read a file: +To read a text file in UTF-8: ```java String text = new BytesAsText( @@ -86,7 +86,7 @@ To read a binary file from classpath: ```java byte[] data = new InputAsBytes( - new ResourceAsInput("/foo/img.jpg") + new ResourceAsInput("foo/img.jpg") ).asBytes(); ``` From 424ced1e0f1c8e2e79818f154ec8f9310dba6879 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 18:34:26 +0300 Subject: [PATCH 051/169] doc --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8d9e1f3025..9b8a21a6ad 100644 --- a/README.md +++ b/README.md @@ -145,12 +145,11 @@ To iterate a collection: new AllOf( new TransformedIterable<>( new ArrayAsIterable<>("how", "are", "you"), - new Func.Quiet() { - @Override - public void exec(final String input) throws Exception { + new ProcAsFunc<>( + input -> { System.out.printf("Item: %s\n", input); } - } + ) ) ).asValue(); ``` @@ -160,7 +159,9 @@ Or even more compact: ```java new IterableAsBoolean( new ArrayAsIterable<>("how", "are", "you"), - (Func.Quiet) i -> System.out.printf("Item: %s\n", i) + new ProcAsFunc<>( + input -> System.out.printf("Item: %s\n", i) + ) ).asValue(); ``` From 54d07df187134c2776729eb10770107b2795eab5 Mon Sep 17 00:00:00 2001 From: Dusan Rychnovsky Date: Tue, 13 Jun 2017 20:56:52 +0200 Subject: [PATCH 052/169] #109 Code-review suggestions --- .../org/cactoos/list/LimitedIterable.java | 19 +++++------- .../org/cactoos/list/LimitedIterator.java | 29 ++++++++++--------- .../org/cactoos/list/LimitedIterableTest.java | 19 ++++++++---- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/cactoos/list/LimitedIterable.java b/src/main/java/org/cactoos/list/LimitedIterable.java index 60de3859ec..5073675959 100644 --- a/src/main/java/org/cactoos/list/LimitedIterable.java +++ b/src/main/java/org/cactoos/list/LimitedIterable.java @@ -8,33 +8,30 @@ *

This is a view of an existing iterable containing the given number of its * first elements.

* + *

There is no thread-safety guarantee.

+ * * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) * @param Element type * @since 0.4 */ -public class LimitedIterable implements Iterable { +public final class LimitedIterable implements Iterable { private final Iterable iterable; - private final int limitSize; + private final int limit; /** * Ctor. * * @param iterable the underlying iterable - * @param limitSize the requested number of elements + * @param limit the requested number of elements */ - public LimitedIterable(Iterable iterable, int limitSize) { - if (limitSize < 0) { - throw new IllegalArgumentException( - "Expected limitSize >= 0, got [" + limitSize + "]." - ); - } + public LimitedIterable(Iterable iterable, int limit) { this.iterable = iterable; - this.limitSize = limitSize; + this.limit = limit; } @Override public Iterator iterator() { - return new LimitedIterator<>(iterable.iterator(), limitSize); + return new LimitedIterator<>(iterable.iterator(), limit); } } diff --git a/src/main/java/org/cactoos/list/LimitedIterator.java b/src/main/java/org/cactoos/list/LimitedIterator.java index 8f1945d216..85d2206185 100644 --- a/src/main/java/org/cactoos/list/LimitedIterator.java +++ b/src/main/java/org/cactoos/list/LimitedIterator.java @@ -1,6 +1,7 @@ package org.cactoos.list; import java.util.Iterator; +import java.util.NoSuchElementException; /** * Limited iterator. @@ -9,41 +10,41 @@ * original iterator, until either the requested number of items have been * returned or the underlying iterator has been exhausted.

* + *

There is no thread-safety guarantee.

+ * * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) * @param Element type * @since 0.4 */ -public class LimitedIterator implements Iterator { +public final class LimitedIterator implements Iterator { private final Iterator iterator; - private final int limitSize; - private int consumedSize; + private final int limit; + private int consumed; /** * Ctor. * * @param iterator the underlying iterator - * @param limitSize the requested number of elements + * @param limit the requested number of elements */ - public LimitedIterator(Iterator iterator, int limitSize) { - if (limitSize < 0) { - throw new IllegalArgumentException( - "Expected limitSize >= 0, got [" + limitSize + "]." - ); - } + public LimitedIterator(Iterator iterator, int limit) { this.iterator = iterator; - this.limitSize = limitSize; - this.consumedSize = 0; + this.limit = limit; + this.consumed = 0; } @Override public boolean hasNext() { - return (consumedSize < limitSize) && iterator.hasNext(); + return (consumed < limit) && iterator.hasNext(); } @Override public T next() { - ++consumedSize; + if (!hasNext()) { + throw new NoSuchElementException("No more elements."); + } + ++consumed; return iterator.next(); } } diff --git a/src/test/java/org/cactoos/list/LimitedIterableTest.java b/src/test/java/org/cactoos/list/LimitedIterableTest.java index fd0ca9dc77..e160a0a577 100644 --- a/src/test/java/org/cactoos/list/LimitedIterableTest.java +++ b/src/test/java/org/cactoos/list/LimitedIterableTest.java @@ -10,12 +10,7 @@ * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) * @since 0.4 */ -public class LimitedIterableTest { - - @Test(expected = IllegalArgumentException.class) - public void failsIfGivenLimitIsLessThanZero() { - new LimitedIterable<>(new ArrayAsIterable<>(), -1); - } +public final class LimitedIterableTest { @Test public void iteratesOverPrefixOfGivenLength() { @@ -53,6 +48,18 @@ public void limitOfZeroProducesEmptyIterable() { ); } + @Test + public void negativeLimitProducesEmptyIterable() { + MatcherAssert.assertThat( + "Can't limit an iterable to negative number of items", + new LimitedIterable<>( + new ArrayAsIterable<>(0, 1, 2, 3, 4), + -1 + ), + Matchers.iterableWithSize(0) + ); + } + @Test public void emptyIterableProducesEmptyIterable() { MatcherAssert.assertThat( From f65f993de89c73a61036cbbf6e523fdbbbb3bf5e Mon Sep 17 00:00:00 2001 From: Dusan Rychnovsky Date: Tue, 13 Jun 2017 21:10:42 +0200 Subject: [PATCH 053/169] #109 Fix code-style issues --- .../org/cactoos/list/LimitedIterable.java | 36 +++++++++++++++-- .../org/cactoos/list/LimitedIterator.java | 40 +++++++++++++++++-- .../org/cactoos/list/LimitedIterableTest.java | 32 ++++++++++++++- 3 files changed, 100 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/cactoos/list/LimitedIterable.java b/src/main/java/org/cactoos/list/LimitedIterable.java index 5073675959..a15c8258a0 100644 --- a/src/main/java/org/cactoos/list/LimitedIterable.java +++ b/src/main/java/org/cactoos/list/LimitedIterable.java @@ -1,3 +1,26 @@ +/** + * 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.list; import java.util.Iterator; @@ -15,8 +38,15 @@ * @since 0.4 */ public final class LimitedIterable implements Iterable { - + + /** + * Decorated iterable. + */ private final Iterable iterable; + + /** + * Number of elements to return. + */ private final int limit; /** @@ -25,13 +55,13 @@ public final class LimitedIterable implements Iterable { * @param iterable the underlying iterable * @param limit the requested number of elements */ - public LimitedIterable(Iterable iterable, int limit) { + public LimitedIterable(final Iterable iterable, final int limit) { this.iterable = iterable; this.limit = limit; } @Override public Iterator iterator() { - return new LimitedIterator<>(iterable.iterator(), limit); + return new LimitedIterator<>(this.iterable.iterator(), this.limit); } } diff --git a/src/main/java/org/cactoos/list/LimitedIterator.java b/src/main/java/org/cactoos/list/LimitedIterator.java index 85d2206185..f45e023cfa 100644 --- a/src/main/java/org/cactoos/list/LimitedIterator.java +++ b/src/main/java/org/cactoos/list/LimitedIterator.java @@ -1,3 +1,26 @@ +/** + * 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.list; import java.util.Iterator; @@ -18,8 +41,19 @@ */ public final class LimitedIterator implements Iterator { + /** + * Decorated iterator. + */ private final Iterator iterator; + + /** + * Number of elements to return. + */ private final int limit; + + /** + * Number of elements returned so far. + */ private int consumed; /** @@ -36,7 +70,7 @@ public LimitedIterator(Iterator iterator, int limit) { @Override public boolean hasNext() { - return (consumed < limit) && iterator.hasNext(); + return (this.consumed < this.limit) && this.iterator.hasNext(); } @Override @@ -44,7 +78,7 @@ public T next() { if (!hasNext()) { throw new NoSuchElementException("No more elements."); } - ++consumed; - return iterator.next(); + ++this.consumed; + return this.iterator.next(); } } diff --git a/src/test/java/org/cactoos/list/LimitedIterableTest.java b/src/test/java/org/cactoos/list/LimitedIterableTest.java index e160a0a577..4a8e8aa015 100644 --- a/src/test/java/org/cactoos/list/LimitedIterableTest.java +++ b/src/test/java/org/cactoos/list/LimitedIterableTest.java @@ -1,3 +1,26 @@ +/** + * 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.list; import org.hamcrest.MatcherAssert; @@ -5,7 +28,7 @@ import org.junit.Test; /** - * Test case for {@link LimitedIterable} + * Test case for {@link LimitedIterable}. * * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) * @since 0.4 @@ -14,6 +37,7 @@ public final class LimitedIterableTest { @Test public void iteratesOverPrefixOfGivenLength() { + // @checkstyle MagicNumber (7 lines) MatcherAssert.assertThat( "Can't limit an iterable with more items", new LimitedIterable<>( @@ -26,6 +50,7 @@ public void iteratesOverPrefixOfGivenLength() { @Test public void iteratesOverWholeIterableIfThereAreNotEnoughItems() { + // @checkstyle MagicNumber (7 lines) MatcherAssert.assertThat( "Can't limit an iterable with less items", new LimitedIterable<>( @@ -35,9 +60,10 @@ public void iteratesOverWholeIterableIfThereAreNotEnoughItems() { Matchers.contains(0, 1, 2, 3, 4) ); } - + @Test public void limitOfZeroProducesEmptyIterable() { + // @checkstyle MagicNumber (7 lines) MatcherAssert.assertThat( "Can't limit an iterable to zero items", new LimitedIterable<>( @@ -50,6 +76,7 @@ public void limitOfZeroProducesEmptyIterable() { @Test public void negativeLimitProducesEmptyIterable() { + // @checkstyle MagicNumber (7 lines) MatcherAssert.assertThat( "Can't limit an iterable to negative number of items", new LimitedIterable<>( @@ -62,6 +89,7 @@ public void negativeLimitProducesEmptyIterable() { @Test public void emptyIterableProducesEmptyIterable() { + // @checkstyle MagicNumber (7 lines) MatcherAssert.assertThat( "Can't limit an empty iterable", new LimitedIterable<>( From 6e2a9762e636227cc31dbc9753397c8cc89d1aa9 Mon Sep 17 00:00:00 2001 From: Dusan Rychnovsky Date: Tue, 13 Jun 2017 21:18:08 +0200 Subject: [PATCH 054/169] #109 Fix code-style issues --- .../java/org/cactoos/list/LimitedIterable.java | 4 ++-- .../java/org/cactoos/list/LimitedIterator.java | 16 ++++++++-------- .../org/cactoos/list/LimitedIterableTest.java | 1 + 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/cactoos/list/LimitedIterable.java b/src/main/java/org/cactoos/list/LimitedIterable.java index a15c8258a0..958ed4739e 100644 --- a/src/main/java/org/cactoos/list/LimitedIterable.java +++ b/src/main/java/org/cactoos/list/LimitedIterable.java @@ -52,8 +52,8 @@ public final class LimitedIterable implements Iterable { /** * Ctor. * - * @param iterable the underlying iterable - * @param limit the requested number of elements + * @param iterable The underlying iterable + * @param limit The requested number of elements */ public LimitedIterable(final Iterable iterable, final int limit) { this.iterable = iterable; diff --git a/src/main/java/org/cactoos/list/LimitedIterator.java b/src/main/java/org/cactoos/list/LimitedIterator.java index f45e023cfa..4e0bf0704a 100644 --- a/src/main/java/org/cactoos/list/LimitedIterator.java +++ b/src/main/java/org/cactoos/list/LimitedIterator.java @@ -40,17 +40,17 @@ * @since 0.4 */ public final class LimitedIterator implements Iterator { - + /** * Decorated iterator. */ private final Iterator iterator; - + /** * Number of elements to return. - */ + */ private final int limit; - + /** * Number of elements returned so far. */ @@ -59,10 +59,10 @@ public final class LimitedIterator implements Iterator { /** * Ctor. * - * @param iterator the underlying iterator - * @param limit the requested number of elements + * @param iterator The underlying iterator + * @param limit The requested number of elements */ - public LimitedIterator(Iterator iterator, int limit) { + public LimitedIterator(final Iterator iterator, final int limit) { this.iterator = iterator; this.limit = limit; this.consumed = 0; @@ -75,7 +75,7 @@ public boolean hasNext() { @Override public T next() { - if (!hasNext()) { + if (!this.hasNext()) { throw new NoSuchElementException("No more elements."); } ++this.consumed; diff --git a/src/test/java/org/cactoos/list/LimitedIterableTest.java b/src/test/java/org/cactoos/list/LimitedIterableTest.java index 4a8e8aa015..5a1e182ebe 100644 --- a/src/test/java/org/cactoos/list/LimitedIterableTest.java +++ b/src/test/java/org/cactoos/list/LimitedIterableTest.java @@ -32,6 +32,7 @@ * * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) */ public final class LimitedIterableTest { From 307843fe8c082eb30bee61c9e708b858e401b4eb Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 23:12:49 +0300 Subject: [PATCH 055/169] #121: bug --- .../org/cactoos/io/TeeInputStreamTest.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/test/java/org/cactoos/io/TeeInputStreamTest.java diff --git a/src/test/java/org/cactoos/io/TeeInputStreamTest.java b/src/test/java/org/cactoos/io/TeeInputStreamTest.java new file mode 100644 index 0000000000..11f12c4376 --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputStreamTest.java @@ -0,0 +1,81 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.io; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link TeeInputStream}. + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.1 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +public final class TeeInputStreamTest { + + @Test + public void copiesContentByteByByte() throws IOException { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final String content = "Hello, товарищ!"; + MatcherAssert.assertThat( + "Can't copy InputStream to OutputStream byte by byte", + TeeInputStreamTest.asString( + new TeeInputStream( + new ByteArrayInputStream( + content.getBytes(StandardCharsets.UTF_8) + ), + baos + ) + ), + Matchers.allOf( + Matchers.equalTo(content), + Matchers.equalTo( + new String(baos.toByteArray(), StandardCharsets.UTF_8) + ) + ) + ); + } + + private static String asString(final InputStream input) throws IOException { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + while (true) { + final int data = input.read(); + if (data < 0) { + break; + } + baos.write(data); + } + input.close(); + return new String(baos.toByteArray(), StandardCharsets.UTF_8); + } + +} From e40eeb80203baa5b48da63629a54d0de19b70760 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 23:13:34 +0300 Subject: [PATCH 056/169] #121: fixed --- src/main/java/org/cactoos/io/TeeInputStream.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/io/TeeInputStream.java b/src/main/java/org/cactoos/io/TeeInputStream.java index bd31b29de9..c882476354 100644 --- a/src/main/java/org/cactoos/io/TeeInputStream.java +++ b/src/main/java/org/cactoos/io/TeeInputStream.java @@ -59,7 +59,9 @@ public TeeInputStream(final InputStream src, final OutputStream tgt) { @Override public int read() throws IOException { final int data = this.input.read(); - this.output.write(data); + if (data >= 0) { + this.output.write(data); + } return data; } From b1f65f228ced616ef9f9b04000c5a48e9c58b3de Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 23:35:55 +0300 Subject: [PATCH 057/169] #123: bug --- .../java/org/cactoos/io/TeeInputStream.java | 3 ++ .../java/org/cactoos/io/InputAsBytesTest.java | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/main/java/org/cactoos/io/TeeInputStream.java b/src/main/java/org/cactoos/io/TeeInputStream.java index c882476354..9ea6a68425 100644 --- a/src/main/java/org/cactoos/io/TeeInputStream.java +++ b/src/main/java/org/cactoos/io/TeeInputStream.java @@ -37,14 +37,17 @@ * @since 0.1 */ public final class TeeInputStream extends InputStream { + /** * Input. */ private final InputStream input; + /** * Output. */ private final OutputStream output; + /** * Ctor. * @param src Source of data diff --git a/src/test/java/org/cactoos/io/InputAsBytesTest.java b/src/test/java/org/cactoos/io/InputAsBytesTest.java index 9268b1740f..3b009de1f1 100644 --- a/src/test/java/org/cactoos/io/InputAsBytesTest.java +++ b/src/test/java/org/cactoos/io/InputAsBytesTest.java @@ -23,8 +23,13 @@ */ package org.cactoos.io; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.util.concurrent.atomic.AtomicBoolean; +import org.cactoos.func.FuncAsMatcher; +import org.cactoos.text.BytesAsText; import org.cactoos.text.StringAsText; import org.cactoos.text.TextAsBytes; import org.hamcrest.MatcherAssert; @@ -84,4 +89,36 @@ public void readsInputIntoBytesWithSmallBuffer() throws IOException { ); } + @Test + public void closesInputStream() throws IOException { + final AtomicBoolean closed = new AtomicBoolean(); + final InputStream input = new ByteArrayInputStream( + "how are you?".getBytes() + ); + MatcherAssert.assertThat( + "Can't close InputStream correctly", + new BytesAsText( + new InputAsBytes( + new InputStreamAsInput( + new InputStream() { + @Override + public int read() throws IOException { + return input.read(); + } + @Override + public void close() throws IOException { + input.close(); + closed.set(true); + } + } + ) + ).asBytes(), + StandardCharsets.UTF_8 + ).asString(), + new FuncAsMatcher<>( + text -> closed.get() + ) + ); + } + } From 2e988e9d546cde3a50c683a3bcc895d2fb3e1a48 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 13 Jun 2017 23:42:43 +0300 Subject: [PATCH 058/169] #123: fixed --- src/main/java/org/cactoos/io/InputAsBytes.java | 4 ++-- src/test/java/org/cactoos/io/InputAsBytesTest.java | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/io/InputAsBytes.java b/src/main/java/org/cactoos/io/InputAsBytes.java index 667edddc5f..0ea6e9b340 100644 --- a/src/main/java/org/cactoos/io/InputAsBytes.java +++ b/src/main/java/org/cactoos/io/InputAsBytes.java @@ -71,11 +71,11 @@ public InputAsBytes(final Input input, final int max) { @Override public byte[] asBytes() throws IOException { - try (final ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + try (final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final InputStream stream = new TeeInput( this.source, new OutputStreamAsOutput(baos) - ).stream(); + ).stream()) { final byte[] buf = new byte[this.size]; while (true) { if (stream.read(buf) != buf.length) { diff --git a/src/test/java/org/cactoos/io/InputAsBytesTest.java b/src/test/java/org/cactoos/io/InputAsBytesTest.java index 3b009de1f1..202807517d 100644 --- a/src/test/java/org/cactoos/io/InputAsBytesTest.java +++ b/src/test/java/org/cactoos/io/InputAsBytesTest.java @@ -43,6 +43,7 @@ * @version $Id$ * @since 0.1 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ public final class InputAsBytesTest { From 2f19da6e7e22b74dd80832a88d957839c9f88414 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 14 Jun 2017 08:14:08 +0300 Subject: [PATCH 059/169] #126: UncheckedBytes with fallback --- .../java/org/cactoos/io/UncheckedBytes.java | 36 ++++++++++++++++--- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/cactoos/io/UncheckedBytes.java b/src/main/java/org/cactoos/io/UncheckedBytes.java index f70fb04b62..ba5cf8645f 100644 --- a/src/main/java/org/cactoos/io/UncheckedBytes.java +++ b/src/main/java/org/cactoos/io/UncheckedBytes.java @@ -26,6 +26,8 @@ import java.io.IOException; import java.io.UncheckedIOException; import org.cactoos.Bytes; +import org.cactoos.Func; +import org.cactoos.func.UncheckedFunc; /** * Bytes that doesn't throw checked {@link Exception}. @@ -43,21 +45,45 @@ public final class UncheckedBytes implements Bytes { */ private final Bytes bytes; + /** + * Fallback. + */ + private final Func fallback; + + /** + * Ctor. + * @param bts Encapsulated bytes + */ + public UncheckedBytes(final Bytes bts) { + this( + bts, + error -> { + throw new UncheckedIOException(error); + } + ); + } + /** * Ctor. - * @param bytes Encapsulated bytes + * @param bts Encapsulated bytes + * @param fbk Fallback + * @since 0.5 */ - public UncheckedBytes(final Bytes bytes) { - this.bytes = bytes; + public UncheckedBytes(final Bytes bts, + final Func fbk) { + this.bytes = bts; + this.fallback = fbk; } @Override public byte[] asBytes() { + byte[] data; try { - return this.bytes.asBytes(); + data = this.bytes.asBytes(); } catch (final IOException ex) { - throw new UncheckedIOException(ex); + data = new UncheckedFunc<>(this.fallback).apply(ex); } + return data; } } From f9c381f41f14d0bc97388961ea65eb7534e0205b Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 14 Jun 2017 08:14:50 +0300 Subject: [PATCH 060/169] #126: optimized --- src/main/java/org/cactoos/func/UncheckedProc.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/org/cactoos/func/UncheckedProc.java b/src/main/java/org/cactoos/func/UncheckedProc.java index 66df43f6c9..5999d2d3e2 100644 --- a/src/main/java/org/cactoos/func/UncheckedProc.java +++ b/src/main/java/org/cactoos/func/UncheckedProc.java @@ -23,7 +23,6 @@ */ package org.cactoos.func; -import org.cactoos.Func; import org.cactoos.Proc; /** @@ -53,12 +52,7 @@ public UncheckedProc(final Proc prc) { @Override public void exec(final X input) { - new UncheckedFunc<>( - (Func) arg -> { - this.proc.exec(arg); - return true; - } - ).apply(input); + new UncheckedFunc<>(new ProcAsFunc<>(this.proc)).apply(input); } } From cf795707957580365212a3d7812770dbce2c6111 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 14 Jun 2017 08:37:45 +0300 Subject: [PATCH 061/169] #126: UncheckedIOException --- src/main/java/org/cactoos/func/IoCheckedFunc.java | 2 +- src/main/java/org/cactoos/func/UncheckedFunc.java | 3 ++- src/test/java/org/cactoos/func/UncheckedFuncTest.java | 3 ++- src/test/java/org/cactoos/func/UncheckedProcTest.java | 3 ++- src/test/java/org/cactoos/func/UncheckedScalarTest.java | 3 ++- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/cactoos/func/IoCheckedFunc.java b/src/main/java/org/cactoos/func/IoCheckedFunc.java index 7bc90637aa..bc083ceb86 100644 --- a/src/main/java/org/cactoos/func/IoCheckedFunc.java +++ b/src/main/java/org/cactoos/func/IoCheckedFunc.java @@ -28,7 +28,7 @@ /** * Func that doesn't throw checked {@link Exception}, but throws - * {@link java.io.IOException} instead. + * {@link IOException} instead. * *

There is no thread-safety guarantee. * diff --git a/src/main/java/org/cactoos/func/UncheckedFunc.java b/src/main/java/org/cactoos/func/UncheckedFunc.java index 49beb5d9d8..e4ee5c7c26 100644 --- a/src/main/java/org/cactoos/func/UncheckedFunc.java +++ b/src/main/java/org/cactoos/func/UncheckedFunc.java @@ -24,6 +24,7 @@ package org.cactoos.func; import java.io.IOException; +import java.io.UncheckedIOException; import org.cactoos.Func; /** @@ -57,7 +58,7 @@ public Y apply(final X input) { try { return new IoCheckedFunc<>(this.func).apply(input); } catch (final IOException ex) { - throw new IllegalStateException(ex); + throw new UncheckedIOException(ex); } } diff --git a/src/test/java/org/cactoos/func/UncheckedFuncTest.java b/src/test/java/org/cactoos/func/UncheckedFuncTest.java index 1ec4ee5fa8..c6da6ab170 100644 --- a/src/test/java/org/cactoos/func/UncheckedFuncTest.java +++ b/src/test/java/org/cactoos/func/UncheckedFuncTest.java @@ -24,6 +24,7 @@ package org.cactoos.func; import java.io.IOException; +import java.io.UncheckedIOException; import org.cactoos.Func; import org.junit.Test; @@ -37,7 +38,7 @@ */ public final class UncheckedFuncTest { - @Test(expected = RuntimeException.class) + @Test(expected = UncheckedIOException.class) public void rethrowsCheckedToUncheckedException() { new UncheckedFunc<>( (Func) i -> { diff --git a/src/test/java/org/cactoos/func/UncheckedProcTest.java b/src/test/java/org/cactoos/func/UncheckedProcTest.java index 5e0c7d1699..59361df323 100644 --- a/src/test/java/org/cactoos/func/UncheckedProcTest.java +++ b/src/test/java/org/cactoos/func/UncheckedProcTest.java @@ -24,6 +24,7 @@ package org.cactoos.func; import java.io.IOException; +import java.io.UncheckedIOException; import org.junit.Test; /** @@ -36,7 +37,7 @@ */ public final class UncheckedProcTest { - @Test(expected = RuntimeException.class) + @Test(expected = UncheckedIOException.class) public void rethrowsCheckedToUncheckedException() { new UncheckedProc<>( input -> { diff --git a/src/test/java/org/cactoos/func/UncheckedScalarTest.java b/src/test/java/org/cactoos/func/UncheckedScalarTest.java index 22ea9e4c37..55d5766236 100644 --- a/src/test/java/org/cactoos/func/UncheckedScalarTest.java +++ b/src/test/java/org/cactoos/func/UncheckedScalarTest.java @@ -24,6 +24,7 @@ package org.cactoos.func; import java.io.IOException; +import java.io.UncheckedIOException; import org.junit.Test; /** @@ -36,7 +37,7 @@ */ public final class UncheckedScalarTest { - @Test(expected = RuntimeException.class) + @Test(expected = UncheckedIOException.class) public void rethrowsCheckedToUncheckedException() { new UncheckedScalar<>( () -> { From 6a21178a3cb31a89a85298db23f4548214275b04 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 14 Jun 2017 08:44:11 +0300 Subject: [PATCH 062/169] #126: UncheckedText with callback --- .../java/org/cactoos/text/UncheckedText.java | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/cactoos/text/UncheckedText.java b/src/main/java/org/cactoos/text/UncheckedText.java index e2085bbb5b..aeb359f68f 100644 --- a/src/main/java/org/cactoos/text/UncheckedText.java +++ b/src/main/java/org/cactoos/text/UncheckedText.java @@ -25,7 +25,9 @@ import java.io.IOException; import java.io.UncheckedIOException; +import org.cactoos.Func; import org.cactoos.Text; +import org.cactoos.func.UncheckedFunc; /** * Text that doesn't throw checked {@link Exception}. @@ -43,21 +45,44 @@ public final class UncheckedText implements Text { */ private final Text text; + /** + * Fallback. + */ + private final Func fallback; + + /** + * Ctor. + * @param txt Encapsulated text + */ + public UncheckedText(final Text txt) { + this( + txt, + error -> { + throw new UncheckedIOException(error); + } + ); + } + /** * Ctor. - * @param text Encapsulated text + * @param txt Encapsulated text + * @param fbk Fallback func if {@link IOException} happens + * @since 0.5 */ - public UncheckedText(final Text text) { - this.text = text; + public UncheckedText(final Text txt, final Func fbk) { + this.text = txt; + this.fallback = fbk; } @Override public String asString() { + String txt; try { - return this.text.asString(); + txt = this.text.asString(); } catch (final IOException ex) { - throw new UncheckedIOException(ex); + txt = new UncheckedFunc<>(this.fallback).apply(ex); } + return txt; } } From c9ba740c834240bcc78dbb6c0416d61469bb4ade Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 14 Jun 2017 09:26:07 +0300 Subject: [PATCH 063/169] #128: more ctors --- .../java/org/cactoos/io/BytesAsInput.java | 9 ++ src/main/java/org/cactoos/io/TeeInput.java | 82 +++++++++++++++++++ .../java/org/cactoos/io/TeeInputTest.java | 10 +-- 3 files changed, 92 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/cactoos/io/BytesAsInput.java b/src/main/java/org/cactoos/io/BytesAsInput.java index 15f7dad450..0b13b0f5c5 100644 --- a/src/main/java/org/cactoos/io/BytesAsInput.java +++ b/src/main/java/org/cactoos/io/BytesAsInput.java @@ -28,6 +28,7 @@ import java.io.InputStream; import org.cactoos.Bytes; import org.cactoos.Input; +import org.cactoos.text.ArrayAsBytes; import org.cactoos.text.TextAsBytes; /** @@ -55,6 +56,14 @@ public BytesAsInput(final String text) { this(new TextAsBytes(text)); } + /** + * Ctor. + * @param bytes The bytes + */ + public BytesAsInput(final byte[] bytes) { + this(new ArrayAsBytes(bytes)); + } + /** * Ctor. * @param bytes The bytes diff --git a/src/main/java/org/cactoos/io/TeeInput.java b/src/main/java/org/cactoos/io/TeeInput.java index 246c6d81f2..b21e2dd181 100644 --- a/src/main/java/org/cactoos/io/TeeInput.java +++ b/src/main/java/org/cactoos/io/TeeInput.java @@ -23,8 +23,10 @@ */ package org.cactoos.io; +import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Path; import org.cactoos.Input; import org.cactoos.Output; @@ -49,6 +51,86 @@ public final class TeeInput implements Input { */ private final Output target; + /** + * Ctor. + * @param input The source + * @param output The output file + * @since 0.5 + */ + public TeeInput(final Path input, final Path output) { + this(new PathAsInput(input), new PathAsOutput(output)); + } + + /** + * Ctor. + * @param input The source + * @param output The output file + * @since 0.5 + */ + public TeeInput(final Path input, final File output) { + this(new PathAsInput(input), new FileAsOutput(output)); + } + + /** + * Ctor. + * @param input The source + * @param output The output file + * @since 0.5 + */ + public TeeInput(final File input, final File output) { + this(new FileAsInput(input), new FileAsOutput(output)); + } + + /** + * Ctor. + * @param input The source + * @param output The output file + * @since 0.5 + */ + public TeeInput(final File input, final Path output) { + this(new FileAsInput(input), new PathAsOutput(output)); + } + + /** + * Ctor. + * @param input The source + * @param file The output file + * @since 0.5 + */ + public TeeInput(final String input, final File file) { + this(new BytesAsInput(input), new FileAsOutput(file)); + } + + /** + * Ctor. + * @param input The source + * @param file The output file + * @since 0.5 + */ + public TeeInput(final String input, final Path file) { + this(new BytesAsInput(input), new PathAsOutput(file)); + } + + /** + * Ctor. + * @param input The source + * @param file The output file + * @since 0.5 + */ + public TeeInput(final byte[] input, final Path file) { + this(new BytesAsInput(input), new PathAsOutput(file)); + } + + /** + * Ctor. + * @param input The source + * @param output The target + * @since 0.5 + */ + public TeeInput(final String input, final Output output) { + this(new BytesAsInput(input), output); + } + /** * Ctor. * @param input The source diff --git a/src/test/java/org/cactoos/io/TeeInputTest.java b/src/test/java/org/cactoos/io/TeeInputTest.java index ca14a5550c..93914a8e30 100644 --- a/src/test/java/org/cactoos/io/TeeInputTest.java +++ b/src/test/java/org/cactoos/io/TeeInputTest.java @@ -31,7 +31,6 @@ import org.cactoos.TextHasString; import org.cactoos.func.FuncAsMatcher; import org.cactoos.text.BytesAsText; -import org.cactoos.text.StringAsText; import org.cactoos.text.TextAsBytes; import org.hamcrest.MatcherAssert; import org.junit.Test; @@ -79,14 +78,7 @@ public void copiesToFile() throws IOException { "Can't copy Input to File and return content", new BytesAsText( new InputAsBytes( - new TeeInput( - new BytesAsInput( - new TextAsBytes( - new StringAsText("Hello, друг!") - ) - ), - new PathAsOutput(temp) - ) + new TeeInput("Hello, друг!", temp) ) ), new TextHasString( From 08095409c43b5779af90e3561bda91a0eb481105 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 14 Jun 2017 12:29:59 +0300 Subject: [PATCH 064/169] #130: reproduced --- .../org/cactoos/io/ResourceAsInputTest.java | 19 +- src/test/resources/org/cactoos/large-text.txt | 1147 +++++++++++++++++ 2 files changed, 1164 insertions(+), 2 deletions(-) create mode 100644 src/test/resources/org/cactoos/large-text.txt diff --git a/src/test/java/org/cactoos/io/ResourceAsInputTest.java b/src/test/java/org/cactoos/io/ResourceAsInputTest.java index 43dab3bc03..2b80eeeeaa 100644 --- a/src/test/java/org/cactoos/io/ResourceAsInputTest.java +++ b/src/test/java/org/cactoos/io/ResourceAsInputTest.java @@ -40,13 +40,13 @@ public final class ResourceAsInputTest { @Test - public void readResourceTest() throws Exception { + public void readsBinaryResource() throws Exception { MatcherAssert.assertThat( "Can't read bytes from a classpath resource", Arrays.copyOfRange( new InputAsBytes( new ResourceAsInput( - "org/cactoos/io/ResourceAsInputTest.class" + "/org/cactoos/io/ResourceAsInputTest.class" ) ).asBytes(), // @checkstyle MagicNumber (2 lines) @@ -62,6 +62,21 @@ public void readResourceTest() throws Exception { ); } + @Test + public void readsTextResource() throws Exception { + MatcherAssert.assertThat( + "Can't read a text resource from classpath", + new BytesAsText( + new InputAsBytes( + new ResourceAsInput( + "org/cactoos/large-text.txt" + ) + ) + ).asString(), + Matchers.endsWith("est laborum.\n") + ); + } + @Test public void readAbsentResourceTest() throws Exception { MatcherAssert.assertThat( diff --git a/src/test/resources/org/cactoos/large-text.txt b/src/test/resources/org/cactoos/large-text.txt new file mode 100644 index 0000000000..35e7dc4868 --- /dev/null +++ b/src/test/resources/org/cactoos/large-text.txt @@ -0,0 +1,1147 @@ +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. + +Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod +tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, +quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo +consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse +cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non +proident, sunt in culpa qui officia deserunt mollit anim id est laborum. From 178d4fa430178619ee929ecb229f716f7f1e5abd Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 14 Jun 2017 16:27:57 +0300 Subject: [PATCH 065/169] #130: fix --- src/main/java/org/cactoos/io/ResourceAsInput.java | 2 +- src/test/java/org/cactoos/io/ResourceAsInputTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/io/ResourceAsInput.java b/src/main/java/org/cactoos/io/ResourceAsInput.java index 21e17cd77f..da7ff7c0cb 100644 --- a/src/main/java/org/cactoos/io/ResourceAsInput.java +++ b/src/main/java/org/cactoos/io/ResourceAsInput.java @@ -107,7 +107,7 @@ public ResourceAsInput(final String res, final ClassLoader ldr) { input -> { throw new IOException( new FormattedText( - "Resource '%s' was not found", + "Resource \"%s\" was not found", input ).asString() ); diff --git a/src/test/java/org/cactoos/io/ResourceAsInputTest.java b/src/test/java/org/cactoos/io/ResourceAsInputTest.java index 2b80eeeeaa..6ead5ef855 100644 --- a/src/test/java/org/cactoos/io/ResourceAsInputTest.java +++ b/src/test/java/org/cactoos/io/ResourceAsInputTest.java @@ -46,7 +46,7 @@ public void readsBinaryResource() throws Exception { Arrays.copyOfRange( new InputAsBytes( new ResourceAsInput( - "/org/cactoos/io/ResourceAsInputTest.class" + "org/cactoos/io/ResourceAsInputTest.class" ) ).asBytes(), // @checkstyle MagicNumber (2 lines) From 0359abd91353bf7f36ea78e3e0b5859be6e6d317 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 14 Jun 2017 18:07:42 +0300 Subject: [PATCH 066/169] #132: InputAsLSInput --- .../java/org/cactoos/io/InputAsLSInput.java | 148 ++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 src/main/java/org/cactoos/io/InputAsLSInput.java diff --git a/src/main/java/org/cactoos/io/InputAsLSInput.java b/src/main/java/org/cactoos/io/InputAsLSInput.java new file mode 100644 index 0000000000..80a0fefe48 --- /dev/null +++ b/src/main/java/org/cactoos/io/InputAsLSInput.java @@ -0,0 +1,148 @@ +/** + * 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.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.StandardCharsets; +import org.cactoos.Input; +import org.cactoos.text.BytesAsText; +import org.w3c.dom.ls.LSInput; + +/** + * Input as LSInput. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.6 + * @checkstyle AbbreviationAsWordInNameCheck (10 lines) + */ +public final class InputAsLSInput implements LSInput { + + /** + * The input. + */ + private final Input input; + + /** + * Ctor. + * @param inpt Input + */ + public InputAsLSInput(final Input inpt) { + this.input = inpt; + } + + @Override + public Reader getCharacterStream() { + return new InputStreamReader(this.getByteStream()); + } + + @Override + public void setCharacterStream(final Reader stream) { + // nothing to do + } + + @Override + public InputStream getByteStream() { + try { + return this.input.stream(); + } catch (final IOException ex) { + throw new IllegalStateException(ex); + } + } + + @Override + public void setByteStream(final InputStream stream) { + // nothing to do + } + + @Override + public String getStringData() { + try { + return new BytesAsText(new InputAsBytes(this.input)).asString(); + } catch (final IOException ex) { + throw new IllegalStateException(ex); + } + } + + @Override + public void setStringData(final String data) { + // nothing to do + } + + @Override + public String getSystemId() { + return "#system"; + } + + @Override + public void setSystemId(final String sid) { + // nothing to do + } + + @Override + public String getPublicId() { + return "#public"; + } + + @Override + public void setPublicId(final String pid) { + // nothing to do + } + + @Override + public String getBaseURI() { + return "#base"; + } + + @Override + public void setBaseURI(final String uri) { + // nothing to do + } + + @Override + public String getEncoding() { + return StandardCharsets.UTF_8.displayName(); + } + + @Override + public void setEncoding(final String encoding) { + // nothing to do + } + + @Override + @SuppressWarnings("PMD.BooleanGetMethodName") + public boolean getCertifiedText() { + return true; + } + + @Override + public void setCertifiedText(final boolean text) { + // nothing to do + } +} From 0fc720a9c4f2131b543feecf7f0745aa437955b6 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 14 Jun 2017 18:30:48 +0300 Subject: [PATCH 067/169] #134: UrlAsInput accepts URI and String --- src/main/java/org/cactoos/io/UrlAsInput.java | 35 +++++++++++++++++-- .../java/org/cactoos/io/UrlAsInputTest.java | 4 +-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/cactoos/io/UrlAsInput.java b/src/main/java/org/cactoos/io/UrlAsInput.java index 6fc8588aeb..02238ea027 100644 --- a/src/main/java/org/cactoos/io/UrlAsInput.java +++ b/src/main/java/org/cactoos/io/UrlAsInput.java @@ -25,8 +25,11 @@ import java.io.IOException; import java.io.InputStream; +import java.net.URI; import java.net.URL; import org.cactoos.Input; +import org.cactoos.Scalar; +import org.cactoos.func.IoCheckedScalar; /** * URL as Input. @@ -42,19 +45,45 @@ public final class UrlAsInput implements Input { /** * The URL. */ - private final URL source; + private final Scalar source; + + /** + * Ctor. + * @param url The URL + * @since 0.6 + */ + public UrlAsInput(final String url) { + this(() -> new URL(url)); + } + + /** + * Ctor. + * @param url The URL + * @since 0.6 + */ + public UrlAsInput(final URI url) { + this(url::toURL); + } /** * Ctor. * @param url The URL */ public UrlAsInput(final URL url) { - this.source = url; + this(() -> url); + } + + /** + * Ctor. + * @param src Source + */ + public UrlAsInput(final Scalar src) { + this.source = src; } @Override public InputStream stream() throws IOException { - return this.source.openStream(); + return new IoCheckedScalar<>(this.source).asValue().openStream(); } } diff --git a/src/test/java/org/cactoos/io/UrlAsInputTest.java b/src/test/java/org/cactoos/io/UrlAsInputTest.java index 3a011ccfa4..17802fc98c 100644 --- a/src/test/java/org/cactoos/io/UrlAsInputTest.java +++ b/src/test/java/org/cactoos/io/UrlAsInputTest.java @@ -63,9 +63,7 @@ public void readsRealUrl() throws IOException { "Can't fetch bytes from the URL", new BytesAsText( new InputAsBytes( - new UrlAsInput( - home.toURL() - ) + new UrlAsInput(home) ) ), new TextHasString( From 488c57c233ca2f31ccbd11dc0f0b8afe70c58c55 Mon Sep 17 00:00:00 2001 From: Dusan Rychnovsky Date: Wed, 14 Jun 2017 18:46:59 +0200 Subject: [PATCH 068/169] #109 Fix code-style issues --- src/main/java/org/cactoos/list/LimitedIterable.java | 1 + src/main/java/org/cactoos/list/LimitedIterator.java | 1 + src/test/java/org/cactoos/list/LimitedIterableTest.java | 1 + 3 files changed, 3 insertions(+) diff --git a/src/main/java/org/cactoos/list/LimitedIterable.java b/src/main/java/org/cactoos/list/LimitedIterable.java index 958ed4739e..5d9881c982 100644 --- a/src/main/java/org/cactoos/list/LimitedIterable.java +++ b/src/main/java/org/cactoos/list/LimitedIterable.java @@ -35,6 +35,7 @@ * * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) * @param Element type + * @version $Id$ * @since 0.4 */ public final class LimitedIterable implements Iterable { diff --git a/src/main/java/org/cactoos/list/LimitedIterator.java b/src/main/java/org/cactoos/list/LimitedIterator.java index 4e0bf0704a..7a08350433 100644 --- a/src/main/java/org/cactoos/list/LimitedIterator.java +++ b/src/main/java/org/cactoos/list/LimitedIterator.java @@ -37,6 +37,7 @@ * * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) * @param Element type + * @version $Id$ * @since 0.4 */ public final class LimitedIterator implements Iterator { diff --git a/src/test/java/org/cactoos/list/LimitedIterableTest.java b/src/test/java/org/cactoos/list/LimitedIterableTest.java index 5a1e182ebe..77b6dd7399 100644 --- a/src/test/java/org/cactoos/list/LimitedIterableTest.java +++ b/src/test/java/org/cactoos/list/LimitedIterableTest.java @@ -31,6 +31,7 @@ * Test case for {@link LimitedIterable}. * * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) + * @version $Id$ * @since 0.4 * @checkstyle JavadocMethodCheck (500 lines) */ From a2105adadc29e4ed5ee656df141e32763d815875 Mon Sep 17 00:00:00 2001 From: Dusan Rychnovsky Date: Wed, 14 Jun 2017 18:47:27 +0200 Subject: [PATCH 069/169] #109 Update @since --- src/main/java/org/cactoos/list/LimitedIterable.java | 2 +- src/main/java/org/cactoos/list/LimitedIterator.java | 2 +- src/test/java/org/cactoos/list/LimitedIterableTest.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/cactoos/list/LimitedIterable.java b/src/main/java/org/cactoos/list/LimitedIterable.java index 5d9881c982..194aa8a79a 100644 --- a/src/main/java/org/cactoos/list/LimitedIterable.java +++ b/src/main/java/org/cactoos/list/LimitedIterable.java @@ -36,7 +36,7 @@ * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) * @param Element type * @version $Id$ - * @since 0.4 + * @since 0.6 */ public final class LimitedIterable implements Iterable { diff --git a/src/main/java/org/cactoos/list/LimitedIterator.java b/src/main/java/org/cactoos/list/LimitedIterator.java index 7a08350433..e157e94e24 100644 --- a/src/main/java/org/cactoos/list/LimitedIterator.java +++ b/src/main/java/org/cactoos/list/LimitedIterator.java @@ -38,7 +38,7 @@ * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) * @param Element type * @version $Id$ - * @since 0.4 + * @since 0.6 */ public final class LimitedIterator implements Iterator { diff --git a/src/test/java/org/cactoos/list/LimitedIterableTest.java b/src/test/java/org/cactoos/list/LimitedIterableTest.java index 77b6dd7399..0032496bf3 100644 --- a/src/test/java/org/cactoos/list/LimitedIterableTest.java +++ b/src/test/java/org/cactoos/list/LimitedIterableTest.java @@ -32,7 +32,7 @@ * * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) * @version $Id$ - * @since 0.4 + * @since 0.6 * @checkstyle JavadocMethodCheck (500 lines) */ public final class LimitedIterableTest { From c72bbd7d54f79afd33624438512471dfac02eebf Mon Sep 17 00:00:00 2001 From: Dusan Rychnovsky Date: Wed, 14 Jun 2017 20:15:37 +0200 Subject: [PATCH 070/169] #109 Fix code-style issues --- src/main/java/org/cactoos/list/LimitedIterable.java | 2 +- src/main/java/org/cactoos/list/LimitedIterator.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/list/LimitedIterable.java b/src/main/java/org/cactoos/list/LimitedIterable.java index 194aa8a79a..364936929d 100644 --- a/src/main/java/org/cactoos/list/LimitedIterable.java +++ b/src/main/java/org/cactoos/list/LimitedIterable.java @@ -34,8 +34,8 @@ *

There is no thread-safety guarantee.

* * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) - * @param Element type * @version $Id$ + * @param Element type * @since 0.6 */ public final class LimitedIterable implements Iterable { diff --git a/src/main/java/org/cactoos/list/LimitedIterator.java b/src/main/java/org/cactoos/list/LimitedIterator.java index e157e94e24..0be8b39af1 100644 --- a/src/main/java/org/cactoos/list/LimitedIterator.java +++ b/src/main/java/org/cactoos/list/LimitedIterator.java @@ -36,8 +36,8 @@ *

There is no thread-safety guarantee.

* * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) - * @param Element type * @version $Id$ + * @param Element type * @since 0.6 */ public final class LimitedIterator implements Iterator { From fa809b9c93ac14932768ee1316bfa4979b9a5a8b Mon Sep 17 00:00:00 2001 From: Dusan Rychnovsky Date: Wed, 14 Jun 2017 20:20:54 +0200 Subject: [PATCH 071/169] #109 Fix code-style issues --- src/main/java/org/cactoos/list/LimitedIterator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/list/LimitedIterator.java b/src/main/java/org/cactoos/list/LimitedIterator.java index 0be8b39af1..45c96ef11b 100644 --- a/src/main/java/org/cactoos/list/LimitedIterator.java +++ b/src/main/java/org/cactoos/list/LimitedIterator.java @@ -71,7 +71,7 @@ public LimitedIterator(final Iterator iterator, final int limit) { @Override public boolean hasNext() { - return (this.consumed < this.limit) && this.iterator.hasNext(); + return this.consumed < this.limit && this.iterator.hasNext(); } @Override From 99373f59a91180bce39309073434ac1214339816 Mon Sep 17 00:00:00 2001 From: Ilia Date: Wed, 14 Jun 2017 22:52:08 +0300 Subject: [PATCH 072/169] Introduced CycledIterable --- .../java/org/cactoos/list/CycledIterable.java | 56 ++++++++++++++ .../java/org/cactoos/list/CycledIterator.java | 74 +++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 src/main/java/org/cactoos/list/CycledIterable.java create mode 100644 src/main/java/org/cactoos/list/CycledIterator.java diff --git a/src/main/java/org/cactoos/list/CycledIterable.java b/src/main/java/org/cactoos/list/CycledIterable.java new file mode 100644 index 0000000000..df06bd3720 --- /dev/null +++ b/src/main/java/org/cactoos/list/CycledIterable.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.list; + +import java.util.Iterator; + +/** + * Cycled Iterable. + * + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @param Type of item + * @since 0.6 + */ +public final class CycledIterable implements Iterable { + + /** + * Iterable. + */ + private final Iterable iterable; + + /** + * Ctor. + * @param iterable Iterable + */ + public CycledIterable(final Iterable iterable) { + this.iterable = iterable; + } + + @Override + public Iterator iterator() { + return new CycledIterator<>(this.iterable); + } +} diff --git a/src/main/java/org/cactoos/list/CycledIterator.java b/src/main/java/org/cactoos/list/CycledIterator.java new file mode 100644 index 0000000000..f1bdea700e --- /dev/null +++ b/src/main/java/org/cactoos/list/CycledIterator.java @@ -0,0 +1,74 @@ +/** + * 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.list; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +/** + * Cycled Iterator. + * + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @param Type of item + * @since 0.6 + */ +public final class CycledIterator implements Iterator { + + /** + * Iterable. + */ + private final Iterable iterable; + + /** + * Iterator. + */ + private Iterator iterator; + + /** + * Ctor. + * @param iterable Iterable. + */ + public CycledIterator(final Iterable iterable) { + this.iterable = iterable; + this.iterator = this.iterable.iterator(); + } + + @Override + public boolean hasNext() { + return this.iterator.hasNext() || this.iterable.iterator().hasNext(); + } + + @Override + public T next() { + if (!this.iterator.hasNext()) { + this.iterator = this.iterable.iterator(); + if (!this.iterator.hasNext()) { + throw new NoSuchElementException(); + } + } + return this.iterator.next(); + } +} From c2e8e9cdafe4446d27bd142d9a2743b5517cc347 Mon Sep 17 00:00:00 2001 From: g4s8 Date: Thu, 15 Jun 2017 00:15:31 +0400 Subject: [PATCH 073/169] use func instead of scalar --- src/main/java/org/cactoos/list/FirstOf.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/cactoos/list/FirstOf.java b/src/main/java/org/cactoos/list/FirstOf.java index b567fa2c63..6d90e464ed 100644 --- a/src/main/java/org/cactoos/list/FirstOf.java +++ b/src/main/java/org/cactoos/list/FirstOf.java @@ -25,7 +25,9 @@ import java.io.IOException; import java.util.Iterator; +import org.cactoos.Func; import org.cactoos.Scalar; +import org.cactoos.text.FormattedText; /** * First element in {@link Iterable} or fallback value if iterable is empty. @@ -47,7 +49,7 @@ public final class FirstOf implements Scalar { /** * Fallback value. */ - private final Scalar fbk; + private final Func, T> fbk; /** * Ctor. @@ -57,8 +59,11 @@ public final class FirstOf implements Scalar { public FirstOf(final Iterable src) { this( src, - () -> { - throw new IOException("Iterable is empty"); + itr -> { + throw new IOException( + new FormattedText("Iterable %s is empty", itr) + .asString() + ); } ); } @@ -70,7 +75,7 @@ public FirstOf(final Iterable src) { * @param fbk Fallback value */ public FirstOf(final Iterable src, final T fbk) { - this(src, () -> fbk); + this(src, itr -> fbk); } /** @@ -79,7 +84,7 @@ public FirstOf(final Iterable src, final T fbk) { * @param src Iterable * @param fbk Fallback value */ - public FirstOf(final Iterable src, final Scalar fbk) { + public FirstOf(final Iterable src, final Func, T> fbk) { this.src = src; this.fbk = fbk; } @@ -91,7 +96,7 @@ public T asValue() throws Exception { if (itr.hasNext()) { ret = itr.next(); } else { - ret = this.fbk.asValue(); + ret = this.fbk.apply(this.src); } return ret; } From 072a9c9c8d0d9b4a9637104718ef87236852e0dd Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 15 Jun 2017 09:12:31 +0300 Subject: [PATCH 074/169] extra test --- src/test/java/org/cactoos/io/UrlAsInputTest.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/test/java/org/cactoos/io/UrlAsInputTest.java b/src/test/java/org/cactoos/io/UrlAsInputTest.java index 17802fc98c..a4157084be 100644 --- a/src/test/java/org/cactoos/io/UrlAsInputTest.java +++ b/src/test/java/org/cactoos/io/UrlAsInputTest.java @@ -76,4 +76,20 @@ public void readsRealUrl() throws IOException { ); } + @Test + public void readsHttpsUrl() { + MatcherAssert.assertThat( + "Can't fetch bytes from the HTTPS URL", + new BytesAsText( + new InputAsBytes( + new UrlAsInput( + // @checkstyle LineLength (1 line) + "https://raw.githubusercontent.com/yegor256/cactoos/0.5/pom.xml" + ) + ) + ), + new TextHasString(Matchers.containsString(" Date: Thu, 15 Jun 2017 09:49:46 +0300 Subject: [PATCH 075/169] #139: more args --- .../java/org/cactoos/io/InputAsLSInput.java | 73 +++++++++++++++---- 1 file changed, 60 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/cactoos/io/InputAsLSInput.java b/src/main/java/org/cactoos/io/InputAsLSInput.java index 80a0fefe48..74718d8813 100644 --- a/src/main/java/org/cactoos/io/InputAsLSInput.java +++ b/src/main/java/org/cactoos/io/InputAsLSInput.java @@ -49,12 +49,43 @@ public final class InputAsLSInput implements LSInput { */ private final Input input; + /** + * PublicID. + */ + private final String pid; + + /** + * SystemID. + */ + private final String sid; + + /** + * Base. + */ + private final String base; + /** * Ctor. * @param inpt Input */ public InputAsLSInput(final Input inpt) { + this(inpt, "#public", "#system", "#base"); + } + + /** + * Ctor. + * @param inpt Input + * @param pubid PublicID + * @param sysid SystemID + * @param bse Base + * @checkstyle ParameterNumberCheck (5 lines) + */ + public InputAsLSInput(final Input inpt, final String pubid, + final String sysid, final String bse) { this.input = inpt; + this.pid = pubid; + this.sid = sysid; + this.base = bse; } @Override @@ -64,7 +95,9 @@ public Reader getCharacterStream() { @Override public void setCharacterStream(final Reader stream) { - // nothing to do + throw new UnsupportedOperationException( + "#setCharacterStream() is not supported" + ); } @Override @@ -78,7 +111,9 @@ public InputStream getByteStream() { @Override public void setByteStream(final InputStream stream) { - // nothing to do + throw new UnsupportedOperationException( + "#setByteStream() is not supported" + ); } @Override @@ -92,37 +127,45 @@ public String getStringData() { @Override public void setStringData(final String data) { - // nothing to do + throw new UnsupportedOperationException( + "#setStringData() is not supported" + ); } @Override public String getSystemId() { - return "#system"; + return this.sid; } @Override - public void setSystemId(final String sid) { - // nothing to do + public void setSystemId(final String sysid) { + throw new UnsupportedOperationException( + "#setSystemId() is not supported" + ); } @Override public String getPublicId() { - return "#public"; + return this.pid; } @Override - public void setPublicId(final String pid) { - // nothing to do + public void setPublicId(final String pubid) { + throw new UnsupportedOperationException( + "#setPublicId() is not supported" + ); } @Override public String getBaseURI() { - return "#base"; + return this.base; } @Override public void setBaseURI(final String uri) { - // nothing to do + throw new UnsupportedOperationException( + "#setBaseURI() is not supported" + ); } @Override @@ -132,7 +175,9 @@ public String getEncoding() { @Override public void setEncoding(final String encoding) { - // nothing to do + throw new UnsupportedOperationException( + "#setEncoding() is not supported" + ); } @Override @@ -143,6 +188,8 @@ public boolean getCertifiedText() { @Override public void setCertifiedText(final boolean text) { - // nothing to do + throw new UnsupportedOperationException( + "#setCertifiedText() is not supported" + ); } } From 9642efa0cd3ee47b47809ad97b5b46892301175d Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 15 Jun 2017 10:09:58 +0300 Subject: [PATCH 076/169] more javadoc --- src/main/java/org/cactoos/Func.java | 12 ++++++++++++ src/main/java/org/cactoos/Scalar.java | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/main/java/org/cactoos/Func.java b/src/main/java/org/cactoos/Func.java index 1e5bf41078..ee5ff8cb1a 100644 --- a/src/main/java/org/cactoos/Func.java +++ b/src/main/java/org/cactoos/Func.java @@ -26,12 +26,24 @@ /** * Function. * + *

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

+ * + *

If you want to cache the result of the {@link Func} and + * make sure it doesn't calculate anything twice, you can use + * {@link org.cactoos.func.StickyFunc} decorator.

+ * *

There is no thread-safety guarantee. * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Type of input * @param Type of output + * @see org.cactoos.func.StickyFunc + * @see org.cactoos.func.UncheckedFunc + * @see org.cactoos.func.IoCheckedFunc * @since 0.1 */ public interface Func { diff --git a/src/main/java/org/cactoos/Scalar.java b/src/main/java/org/cactoos/Scalar.java index 171d7da80a..9bbeac0ffc 100644 --- a/src/main/java/org/cactoos/Scalar.java +++ b/src/main/java/org/cactoos/Scalar.java @@ -26,11 +26,23 @@ /** * Scalar. * + *

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

+ * + *

If you want to cache the result of the {@link Scalar} and + * make sure it doesn't calculate anything twice, you can use + * {@link org.cactoos.func.StickyScalar} decorator.

+ * *

There is no thread-safety guarantee. * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Type of result + * @see org.cactoos.func.StickyScalar + * @see org.cactoos.func.UncheckedScalar + * @see org.cactoos.func.IoCheckedScalar * @since 0.1 */ public interface Scalar { From 10ff375afa7995da7e25baf54f15212218827192 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 15 Jun 2017 11:24:56 +0300 Subject: [PATCH 077/169] #141: FuncWithFallback --- src/main/java/org/cactoos/Bytes.java | 1 + src/main/java/org/cactoos/Input.java | 3 ++ src/main/java/org/cactoos/Output.java | 2 ++ src/main/java/org/cactoos/Proc.java | 1 + src/main/java/org/cactoos/Text.java | 1 + ...ithCallback.java => FuncWithFallback.java} | 28 +++++++++---------- ...ackTest.java => FuncWithFallbackTest.java} | 10 +++---- 7 files changed, 27 insertions(+), 19 deletions(-) rename src/main/java/org/cactoos/func/{FuncWithCallback.java => FuncWithFallback.java} (79%) rename src/test/java/org/cactoos/func/{FuncWithCallbackTest.java => FuncWithFallbackTest.java} (92%) diff --git a/src/main/java/org/cactoos/Bytes.java b/src/main/java/org/cactoos/Bytes.java index 27e0a9aa98..ec7c67c811 100644 --- a/src/main/java/org/cactoos/Bytes.java +++ b/src/main/java/org/cactoos/Bytes.java @@ -32,6 +32,7 @@ * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ + * @see org.cactoos.text.TextAsBytes * @since 0.1 */ public interface Bytes { diff --git a/src/main/java/org/cactoos/Input.java b/src/main/java/org/cactoos/Input.java index 2e51152c5a..7b3f037c3a 100644 --- a/src/main/java/org/cactoos/Input.java +++ b/src/main/java/org/cactoos/Input.java @@ -46,6 +46,9 @@ * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ + * @see org.cactoos.io.BytesAsInput + * @see FileAsInput + * @see org.cactoos.io.PathAsInput * @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 7d5c9ff937..e753024651 100644 --- a/src/main/java/org/cactoos/Output.java +++ b/src/main/java/org/cactoos/Output.java @@ -52,6 +52,8 @@ * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ + * @see FileAsOutput + * @see org.cactoos.io.PathAsOutput * @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 bdaf7a4f3e..e237cebf79 100644 --- a/src/main/java/org/cactoos/Proc.java +++ b/src/main/java/org/cactoos/Proc.java @@ -31,6 +31,7 @@ * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Type of input + * @see org.cactoos.func.ProcAsFunc * @since 0.1 */ public interface Proc { diff --git a/src/main/java/org/cactoos/Text.java b/src/main/java/org/cactoos/Text.java index 2511977f64..577917bb85 100644 --- a/src/main/java/org/cactoos/Text.java +++ b/src/main/java/org/cactoos/Text.java @@ -32,6 +32,7 @@ * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ + * @see org.cactoos.text.StringAsText * @since 0.1 */ public interface Text { diff --git a/src/main/java/org/cactoos/func/FuncWithCallback.java b/src/main/java/org/cactoos/func/FuncWithFallback.java similarity index 79% rename from src/main/java/org/cactoos/func/FuncWithCallback.java rename to src/main/java/org/cactoos/func/FuncWithFallback.java index 5522c75c5c..9667a3d91b 100644 --- a/src/main/java/org/cactoos/func/FuncWithCallback.java +++ b/src/main/java/org/cactoos/func/FuncWithFallback.java @@ -26,7 +26,7 @@ import org.cactoos.Func; /** - * Func with Callback. + * Func with a fallback plan. * *

There is no thread-safety guarantee. * @@ -36,7 +36,7 @@ * @param Type of output * @since 0.2 */ -public final class FuncWithCallback implements Func { +public final class FuncWithFallback implements Func { /** * The func. @@ -44,9 +44,9 @@ public final class FuncWithCallback implements Func { private final Func func; /** - * The callback. + * The fallback. */ - private final Func callback; + private final Func fallback; /** * The follow up. @@ -56,23 +56,23 @@ public final class FuncWithCallback implements Func { /** * Ctor. * @param fnc The func - * @param cbk The callback + * @param fbk The fallback */ - public FuncWithCallback(final Func fnc, - final Func cbk) { - this(fnc, cbk, input -> input); + public FuncWithFallback(final Func fnc, + final Func fbk) { + this(fnc, fbk, input -> input); } /** * Ctor. * @param fnc The func - * @param cbk The callback + * @param fbk The fallback * @param flw The follow up func */ - public FuncWithCallback(final Func fnc, - final Func cbk, final Func flw) { + public FuncWithFallback(final Func fnc, + final Func fbk, final Func flw) { this.func = fnc; - this.callback = cbk; + this.fallback = fbk; this.follow = flw; } @@ -84,10 +84,10 @@ public Y apply(final X input) throws Exception { result = this.func.apply(input); } catch (final InterruptedException ex) { Thread.currentThread().interrupt(); - result = this.callback.apply(ex); + result = this.fallback.apply(ex); // @checkstyle IllegalCatchCheck (1 line) } catch (final Throwable ex) { - result = this.callback.apply(ex); + result = this.fallback.apply(ex); } return this.follow.apply(result); } diff --git a/src/test/java/org/cactoos/func/FuncWithCallbackTest.java b/src/test/java/org/cactoos/func/FuncWithFallbackTest.java similarity index 92% rename from src/test/java/org/cactoos/func/FuncWithCallbackTest.java rename to src/test/java/org/cactoos/func/FuncWithFallbackTest.java index 74339669b0..a734b4bc11 100644 --- a/src/test/java/org/cactoos/func/FuncWithCallbackTest.java +++ b/src/test/java/org/cactoos/func/FuncWithFallbackTest.java @@ -30,20 +30,20 @@ import org.junit.Test; /** - * Test case for {@link FuncWithCallback}. + * Test case for {@link FuncWithFallback}. * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @since 0.2 * @checkstyle JavadocMethodCheck (500 lines) */ -public final class FuncWithCallbackTest { +public final class FuncWithFallbackTest { @Test public void usesMainFunc() throws Exception { MatcherAssert.assertThat( "Can't use the main function if no exception", - new FuncWithCallback<>( + new FuncWithFallback<>( input -> "It's success", ex -> "In case of failure..." ), @@ -55,7 +55,7 @@ public void usesMainFunc() throws Exception { public void usesCallback() throws Exception { MatcherAssert.assertThat( "Can't use the callback in case of exception", - new FuncWithCallback<>( + new FuncWithFallback<>( input -> { throw new IOException("Failure"); }, @@ -69,7 +69,7 @@ public void usesCallback() throws Exception { public void usesFollowUp() throws Exception { MatcherAssert.assertThat( "Can't use the follow-up func", - new FuncWithCallback<>( + new FuncWithFallback<>( input -> "works fine", ex -> "won't happen", input -> "follow up" From 3d387228050ffca0bd42c113a3f3175779cb0a23 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 15 Jun 2017 11:41:24 +0300 Subject: [PATCH 078/169] #141: RunnableWithFallback --- .../cactoos/func/RunnableWithFallback.java | 70 +++++++++++++++++ .../func/RunnableWithFallbackTest.java | 76 +++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 src/main/java/org/cactoos/func/RunnableWithFallback.java create mode 100644 src/test/java/org/cactoos/func/RunnableWithFallbackTest.java diff --git a/src/main/java/org/cactoos/func/RunnableWithFallback.java b/src/main/java/org/cactoos/func/RunnableWithFallback.java new file mode 100644 index 0000000000..c616b0e047 --- /dev/null +++ b/src/main/java/org/cactoos/func/RunnableWithFallback.java @@ -0,0 +1,70 @@ +/** + * 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.Proc; + +/** + * Runnable with a fallback plan. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.6 + */ +public final class RunnableWithFallback implements Runnable { + + /** + * The runnable. + */ + private final Runnable runnable; + + /** + * The fallback. + */ + private final Proc fallback; + + /** + * Ctor. + * @param rnb Runnable + * @param fbk The fallback + */ + public RunnableWithFallback(final Runnable rnb, + final Proc fbk) { + this.runnable = rnb; + this.fallback = fbk; + } + + @Override + public void run() { + new UncheckedFunc<>( + new FuncWithFallback( + new RunnableAsFunc<>(this.runnable), + new ProcAsFunc<>(this.fallback) + ) + ).apply(true); + } + +} diff --git a/src/test/java/org/cactoos/func/RunnableWithFallbackTest.java b/src/test/java/org/cactoos/func/RunnableWithFallbackTest.java new file mode 100644 index 0000000000..0d3b4ecb6f --- /dev/null +++ b/src/test/java/org/cactoos/func/RunnableWithFallbackTest.java @@ -0,0 +1,76 @@ +/** + * 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 java.io.IOException; +import org.cactoos.FuncApplies; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link RunnableWithFallback}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.6 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class RunnableWithFallbackTest { + + @Test + public void usesMainFunc() throws Exception { + MatcherAssert.assertThat( + "Can't use the main function if no exception", + new RunnableAsFunc<>( + new RunnableWithFallback( + () -> { + }, + input -> { + throw new IOException(input); + } + ) + ), + new FuncApplies<>(true, Matchers.nullValue()) + ); + } + + @Test + public void usesFallback() throws Exception { + MatcherAssert.assertThat( + "Can't use the fallback function if there is exception", + new RunnableAsFunc<>( + new RunnableWithFallback( + () -> { + throw new IllegalStateException("intended"); + }, + input -> { + } + ) + ), + new FuncApplies<>(true, Matchers.nullValue()) + ); + } + +} From f19ea2363f2cff5948ead2dd9ca99448a8a066cf Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 15 Jun 2017 11:46:23 +0300 Subject: [PATCH 079/169] #141: FuncAsProc --- .../java/org/cactoos/func/FuncAsProc.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/main/java/org/cactoos/func/FuncAsProc.java diff --git a/src/main/java/org/cactoos/func/FuncAsProc.java b/src/main/java/org/cactoos/func/FuncAsProc.java new file mode 100644 index 0000000000..13e50948bd --- /dev/null +++ b/src/main/java/org/cactoos/func/FuncAsProc.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.func; + +import org.cactoos.Func; +import org.cactoos.Proc; + +/** + * Func as Proc. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of input + * @since 0.6 + */ +public final class FuncAsProc implements Proc { + + /** + * The func. + */ + private final Func func; + + /** + * Ctor. + * @param fnc The proc + */ + public FuncAsProc(final Func fnc) { + this.func = fnc; + } + + @Override + public void exec(final X input) throws Exception { + this.func.apply(input); + } +} From f395bccfb71d204bddf5836cf840e25b00983848 Mon Sep 17 00:00:00 2001 From: VseslavSekorin Date: Thu, 15 Jun 2017 12:49:44 +0300 Subject: [PATCH 080/169] #144 interface Pred --- src/main/java/org/cactoos/Pred.java | 46 +++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/main/java/org/cactoos/Pred.java diff --git a/src/main/java/org/cactoos/Pred.java b/src/main/java/org/cactoos/Pred.java new file mode 100644 index 0000000000..4b530751e3 --- /dev/null +++ b/src/main/java/org/cactoos/Pred.java @@ -0,0 +1,46 @@ +/** + * 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; + +/** + * Predicate. + * + *

There is no thread-safety guarantee. + * + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @param Type of input + * @since 0.5 + */ +public interface Pred { + + /** + * Evaluates predicate on the given argument. + * + * @param input The argument + * @return The result + * @throws Exception If fails + */ + Boolean test(X input) throws Exception; +} From 9f406bc5ad870390ac416043818eb8a7907a5b08 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 15 Jun 2017 13:27:18 +0300 Subject: [PATCH 081/169] #146: StickyInput --- src/main/java/org/cactoos/io/StickyInput.java | 75 +++++++++++++++++++ .../java/org/cactoos/io/StickyInputTest.java | 57 ++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 src/main/java/org/cactoos/io/StickyInput.java create mode 100644 src/test/java/org/cactoos/io/StickyInputTest.java diff --git a/src/main/java/org/cactoos/io/StickyInput.java b/src/main/java/org/cactoos/io/StickyInput.java new file mode 100644 index 0000000000..cc2f35b40f --- /dev/null +++ b/src/main/java/org/cactoos/io/StickyInput.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.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import org.cactoos.Input; +import org.cactoos.Scalar; +import org.cactoos.func.IoCheckedScalar; +import org.cactoos.func.StickyScalar; + +/** + * Input that reads only once. + * + *

There is no thread-safety guarantee. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.6 + */ +public final class StickyInput implements Input { + + /** + * The cache. + */ + private final Scalar cache; + + /** + * Ctor. + * @param input The input + */ + public StickyInput(final Input input) { + this.cache = new StickyScalar<>( + () -> { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + new LengthOfInput( + new TeeInput( + input, + new OutputStreamAsOutput(baos) + ) + ).asValue(); + return new ByteArrayInputStream(baos.toByteArray()); + } + ); + } + + @Override + public InputStream stream() throws IOException { + return new IoCheckedScalar<>(this.cache).asValue(); + } + +} diff --git a/src/test/java/org/cactoos/io/StickyInputTest.java b/src/test/java/org/cactoos/io/StickyInputTest.java new file mode 100644 index 0000000000..53adb4021f --- /dev/null +++ b/src/test/java/org/cactoos/io/StickyInputTest.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.io; + +import java.io.IOException; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link StickyInput}. + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.6 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class StickyInputTest { + + @Test + public void readsFileContent() throws IOException { + MatcherAssert.assertThat( + "Can't read bytes from a file", + new InputAsBytes( + new StickyInput( + new UrlAsInput( + this.getClass().getResource( + "/org/cactoos/io/UrlAsInput.class" + ) + ) + ) + ).asBytes().length, + Matchers.greaterThan(0) + ); + } + +} From a258e3875e1c8f76e549b5d47ab3bdfd9e71deb3 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 15 Jun 2017 13:33:49 +0300 Subject: [PATCH 082/169] #146: more tests --- .../java/org/cactoos/io/StickyInputTest.java | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/test/java/org/cactoos/io/StickyInputTest.java b/src/test/java/org/cactoos/io/StickyInputTest.java index 53adb4021f..5e55a7f02e 100644 --- a/src/test/java/org/cactoos/io/StickyInputTest.java +++ b/src/test/java/org/cactoos/io/StickyInputTest.java @@ -23,7 +23,8 @@ */ package org.cactoos.io; -import java.io.IOException; +import org.cactoos.func.FuncAsMatcher; +import org.cactoos.func.ProcAsFunc; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; @@ -38,19 +39,34 @@ public final class StickyInputTest { @Test - public void readsFileContent() throws IOException { + public void readsFileContent() { MatcherAssert.assertThat( "Can't read bytes from a file", - new InputAsBytes( - new StickyInput( - new UrlAsInput( - this.getClass().getResource( - "/org/cactoos/io/UrlAsInput.class" - ) + new StickyInput( + new UrlAsInput( + this.getClass().getResource( + "/org/cactoos/io/UrlAsInput.class" ) ) - ).asBytes().length, - Matchers.greaterThan(0) + ), + new FuncAsMatcher<>( + new ProcAsFunc<>( + input -> { + MatcherAssert.assertThat( + new InputAsBytes( + new TeeInput(input, new DeadOutput()) + ).asBytes().length, + Matchers.greaterThan(0) + ); + MatcherAssert.assertThat( + new InputAsBytes( + new TeeInput(input, new DeadOutput()) + ).asBytes().length, + Matchers.greaterThan(0) + ); + } + ) + ) ); } From 3bf1975463592dc00ab8090fc610a3a0ad827e66 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 15 Jun 2017 13:50:53 +0300 Subject: [PATCH 083/169] #146: RepeatedFunc --- .../java/org/cactoos/func/RepeatedFunc.java | 73 +++++++++++++++++++ .../java/org/cactoos/func/StickyFunc.java | 4 + .../java/org/cactoos/func/StickyScalar.java | 4 + src/main/java/org/cactoos/io/StickyInput.java | 8 +- .../org/cactoos/func/RepeatedFuncTest.java | 54 ++++++++++++++ .../java/org/cactoos/io/StickyInputTest.java | 30 +++----- 6 files changed, 149 insertions(+), 24 deletions(-) create mode 100644 src/main/java/org/cactoos/func/RepeatedFunc.java create mode 100644 src/test/java/org/cactoos/func/RepeatedFuncTest.java diff --git a/src/main/java/org/cactoos/func/RepeatedFunc.java b/src/main/java/org/cactoos/func/RepeatedFunc.java new file mode 100644 index 0000000000..37f6adca59 --- /dev/null +++ b/src/main/java/org/cactoos/func/RepeatedFunc.java @@ -0,0 +1,73 @@ +/** + * 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.Func; + +/** + * Func that repeats its calculation a few times before + * returning the last result. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of input + * @param Type of output + * @since 0.6 + */ +public final class RepeatedFunc implements Func { + + /** + * Original func. + */ + private final Func func; + + /** + * How many times to run. + */ + private final int times; + + /** + * Ctor. + * + *

If {@code max} is equal or less than zero {@link #apply(Object)} + * will return {@code null}.

+ * + * @param fnc Func original + * @param max How many times + */ + public RepeatedFunc(final Func fnc, final int max) { + this.func = fnc; + this.times = max; + } + + @Override + public Y apply(final X input) throws Exception { + Y result = null; + for (int idx = 0; idx < this.times; ++idx) { + result = this.func.apply(input); + } + return result; + } + +} diff --git a/src/main/java/org/cactoos/func/StickyFunc.java b/src/main/java/org/cactoos/func/StickyFunc.java index 5a4645ec81..d53aa1a140 100644 --- a/src/main/java/org/cactoos/func/StickyFunc.java +++ b/src/main/java/org/cactoos/func/StickyFunc.java @@ -31,12 +31,16 @@ * Func that caches previously calculated values and doesn't * recalculate again. * + *

This {@link Func} decorator technically is an in-memory + * cache.

+ * *

There is no thread-safety guarantee. * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Type of input * @param Type of output + * @see StickyScalar * @since 0.1 */ public final class StickyFunc implements Func { diff --git a/src/main/java/org/cactoos/func/StickyScalar.java b/src/main/java/org/cactoos/func/StickyScalar.java index 981a569883..dba757846d 100644 --- a/src/main/java/org/cactoos/func/StickyScalar.java +++ b/src/main/java/org/cactoos/func/StickyScalar.java @@ -29,12 +29,16 @@ /** * Cached version of a Scalar. * + *

This {@link Scalar} decorator technically is an in-memory + * cache.

+ * *

There is no thread-safety guarantee. * * @author Tim Hinkes (timmeey@timmeey.de) * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Type of result + * @see StickyFunc * @since 0.3 */ public final class StickyScalar implements Scalar { diff --git a/src/main/java/org/cactoos/io/StickyInput.java b/src/main/java/org/cactoos/io/StickyInput.java index cc2f35b40f..f8be244f3b 100644 --- a/src/main/java/org/cactoos/io/StickyInput.java +++ b/src/main/java/org/cactoos/io/StickyInput.java @@ -46,7 +46,7 @@ public final class StickyInput implements Input { /** * The cache. */ - private final Scalar cache; + private final Scalar cache; /** * Ctor. @@ -62,14 +62,16 @@ public StickyInput(final Input input) { new OutputStreamAsOutput(baos) ) ).asValue(); - return new ByteArrayInputStream(baos.toByteArray()); + return baos.toByteArray(); } ); } @Override public InputStream stream() throws IOException { - return new IoCheckedScalar<>(this.cache).asValue(); + return new ByteArrayInputStream( + new IoCheckedScalar<>(this.cache).asValue() + ); } } diff --git a/src/test/java/org/cactoos/func/RepeatedFuncTest.java b/src/test/java/org/cactoos/func/RepeatedFuncTest.java new file mode 100644 index 0000000000..6542189169 --- /dev/null +++ b/src/test/java/org/cactoos/func/RepeatedFuncTest.java @@ -0,0 +1,54 @@ +/** + * 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 java.security.SecureRandom; +import org.cactoos.Func; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link RepeatedFunc}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.6 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class RepeatedFuncTest { + + @Test + public void runsFuncMultipleTimes() throws Exception { + final Func func = new RepeatedFunc<>( + input -> new SecureRandom().nextInt(), + 2 + ); + MatcherAssert.assertThat( + func.apply(true), + Matchers.not(Matchers.equalTo(func.apply(true))) + ); + } + +} diff --git a/src/test/java/org/cactoos/io/StickyInputTest.java b/src/test/java/org/cactoos/io/StickyInputTest.java index 5e55a7f02e..9dc5df44ff 100644 --- a/src/test/java/org/cactoos/io/StickyInputTest.java +++ b/src/test/java/org/cactoos/io/StickyInputTest.java @@ -24,9 +24,8 @@ package org.cactoos.io; import org.cactoos.func.FuncAsMatcher; -import org.cactoos.func.ProcAsFunc; +import org.cactoos.func.RepeatedFunc; import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; import org.junit.Test; /** @@ -43,28 +42,17 @@ public void readsFileContent() { MatcherAssert.assertThat( "Can't read bytes from a file", new StickyInput( - new UrlAsInput( - this.getClass().getResource( - "/org/cactoos/io/UrlAsInput.class" - ) + new ResourceAsInput( + "org/cactoos/large-text.txt" ) ), new FuncAsMatcher<>( - new ProcAsFunc<>( - input -> { - MatcherAssert.assertThat( - new InputAsBytes( - new TeeInput(input, new DeadOutput()) - ).asBytes().length, - Matchers.greaterThan(0) - ); - MatcherAssert.assertThat( - new InputAsBytes( - new TeeInput(input, new DeadOutput()) - ).asBytes().length, - Matchers.greaterThan(0) - ); - } + new RepeatedFunc<>( + input -> new InputAsBytes( + new TeeInput(input, new DeadOutput()) + // @checkstyle MagicNumber (1 line) + ).asBytes().length == 73471, + 2 ) ) ); From c952ae6e16c223281e2eb0b64ede918090918ad5 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 15 Jun 2017 14:17:13 +0300 Subject: [PATCH 084/169] #146: ten times --- src/main/java/org/cactoos/io/StickyInput.java | 5 +---- src/test/java/org/cactoos/io/StickyInputTest.java | 4 ++-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/cactoos/io/StickyInput.java b/src/main/java/org/cactoos/io/StickyInput.java index f8be244f3b..2e77f35910 100644 --- a/src/main/java/org/cactoos/io/StickyInput.java +++ b/src/main/java/org/cactoos/io/StickyInput.java @@ -57,10 +57,7 @@ public StickyInput(final Input input) { () -> { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); new LengthOfInput( - new TeeInput( - input, - new OutputStreamAsOutput(baos) - ) + new TeeInput(input, new OutputStreamAsOutput(baos)) ).asValue(); return baos.toByteArray(); } diff --git a/src/test/java/org/cactoos/io/StickyInputTest.java b/src/test/java/org/cactoos/io/StickyInputTest.java index 9dc5df44ff..ddfb1cbe0f 100644 --- a/src/test/java/org/cactoos/io/StickyInputTest.java +++ b/src/test/java/org/cactoos/io/StickyInputTest.java @@ -50,9 +50,9 @@ public void readsFileContent() { new RepeatedFunc<>( input -> new InputAsBytes( new TeeInput(input, new DeadOutput()) - // @checkstyle MagicNumber (1 line) + // @checkstyle MagicNumber (2 lines) ).asBytes().length == 73471, - 2 + 10 ) ) ); From da2b7e30558eb0669127274e4b9de22123ed3eac Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 15 Jun 2017 14:20:12 +0300 Subject: [PATCH 085/169] #146: LengthOfInput closes Input --- .../java/org/cactoos/io/LengthOfInput.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/cactoos/io/LengthOfInput.java b/src/main/java/org/cactoos/io/LengthOfInput.java index 2fa55ce1ba..46b5260640 100644 --- a/src/main/java/org/cactoos/io/LengthOfInput.java +++ b/src/main/java/org/cactoos/io/LengthOfInput.java @@ -70,19 +70,20 @@ public LengthOfInput(final Input input, final int max) { @Override public Long asValue() throws IOException { - final InputStream stream = this.source.stream(); - final byte[] buf = new byte[this.size]; - long length = 0L; - while (true) { - final int len = stream.read(buf); - if (len > 0) { - length += (long) len; - } - if (len != buf.length) { - break; + try (final InputStream stream = this.source.stream()) { + final byte[] buf = new byte[this.size]; + long length = 0L; + while (true) { + final int len = stream.read(buf); + if (len > 0) { + length += (long) len; + } + if (len != buf.length) { + break; + } } + return length; } - return length; } } From 2f25023126a37637b67d862abb87501c4441edf7 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 15 Jun 2017 14:45:55 +0300 Subject: [PATCH 086/169] #146: StdoutOutput, StderrOutput, StdinInput --- .../java/org/cactoos/io/StderrOutput.java | 45 +++++++++++++++++++ src/main/java/org/cactoos/io/StdinInput.java | 45 +++++++++++++++++++ .../java/org/cactoos/io/StdoutOutput.java | 45 +++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 src/main/java/org/cactoos/io/StderrOutput.java create mode 100644 src/main/java/org/cactoos/io/StdinInput.java create mode 100644 src/main/java/org/cactoos/io/StdoutOutput.java diff --git a/src/main/java/org/cactoos/io/StderrOutput.java b/src/main/java/org/cactoos/io/StderrOutput.java new file mode 100644 index 0000000000..72bbfac1d8 --- /dev/null +++ b/src/main/java/org/cactoos/io/StderrOutput.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.io; + +import java.io.OutputStream; +import org.cactoos.Output; + +/** + * Output that writes to {@code stderr}. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.6 + */ +public final class StderrOutput implements Output { + + @Override + public OutputStream stream() { + return System.err; + } + +} diff --git a/src/main/java/org/cactoos/io/StdinInput.java b/src/main/java/org/cactoos/io/StdinInput.java new file mode 100644 index 0000000000..25b5d904e3 --- /dev/null +++ b/src/main/java/org/cactoos/io/StdinInput.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.io; + +import java.io.InputStream; +import org.cactoos.Input; + +/** + * Input that reads from {@code stdin}. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.6 + */ +public final class StdinInput implements Input { + + @Override + public InputStream stream() { + return System.in; + } + +} diff --git a/src/main/java/org/cactoos/io/StdoutOutput.java b/src/main/java/org/cactoos/io/StdoutOutput.java new file mode 100644 index 0000000000..9187487700 --- /dev/null +++ b/src/main/java/org/cactoos/io/StdoutOutput.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.io; + +import java.io.OutputStream; +import org.cactoos.Output; + +/** + * Output that writes to {@code stdout}. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.6 + */ +public final class StdoutOutput implements Output { + + @Override + public OutputStream stream() { + return System.out; + } + +} From 2ff33c0c82dcf3bef45e20d7ca66c38d83cf57c3 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 15 Jun 2017 15:25:44 +0300 Subject: [PATCH 087/169] #146: bug in LengthOfInput --- .../java/org/cactoos/io/LengthOfInput.java | 2 +- .../org/cactoos/io/LengthOfInputTest.java | 17 +++++++++++++ .../java/org/cactoos/io/StickyInputTest.java | 24 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/io/LengthOfInput.java b/src/main/java/org/cactoos/io/LengthOfInput.java index 46b5260640..33dc108df4 100644 --- a/src/main/java/org/cactoos/io/LengthOfInput.java +++ b/src/main/java/org/cactoos/io/LengthOfInput.java @@ -78,7 +78,7 @@ public Long asValue() throws IOException { if (len > 0) { length += (long) len; } - if (len != buf.length) { + if (len < 0) { break; } } diff --git a/src/test/java/org/cactoos/io/LengthOfInputTest.java b/src/test/java/org/cactoos/io/LengthOfInputTest.java index 1e34a08b31..21e4df09f3 100644 --- a/src/test/java/org/cactoos/io/LengthOfInputTest.java +++ b/src/test/java/org/cactoos/io/LengthOfInputTest.java @@ -23,11 +23,13 @@ */ package org.cactoos.io; +import java.io.IOException; import java.nio.charset.StandardCharsets; import org.cactoos.ScalarHasValue; import org.cactoos.text.StringAsText; import org.cactoos.text.TextAsBytes; import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; import org.junit.Test; /** @@ -66,4 +68,19 @@ public void calculatesZeroLength() { ); } + @Test + public void readsRealUrl() throws IOException { + MatcherAssert.assertThat( + "Can't calculate length of a real page at the URL", + new LengthOfInput( + new UrlAsInput( + // @checkstyle LineLength (1 line) + "https://raw.githubusercontent.com/yegor256/cactoos/0.5/pom.xml" + ) + ).asValue(), + // @checkstyle MagicNumber (1 line) + Matchers.equalTo(5960L) + ); + } + } diff --git a/src/test/java/org/cactoos/io/StickyInputTest.java b/src/test/java/org/cactoos/io/StickyInputTest.java index ddfb1cbe0f..29db28038c 100644 --- a/src/test/java/org/cactoos/io/StickyInputTest.java +++ b/src/test/java/org/cactoos/io/StickyInputTest.java @@ -23,9 +23,12 @@ */ package org.cactoos.io; +import org.cactoos.TextHasString; import org.cactoos.func.FuncAsMatcher; import org.cactoos.func.RepeatedFunc; +import org.cactoos.text.BytesAsText; import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; import org.junit.Test; /** @@ -34,6 +37,7 @@ * @version $Id$ * @since 0.6 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ public final class StickyInputTest { @@ -58,4 +62,24 @@ public void readsFileContent() { ); } + @Test + public void readsRealUrl() { + MatcherAssert.assertThat( + "Can't fetch text page from the URL", + new BytesAsText( + new InputAsBytes( + new StickyInput( + new UrlAsInput( + // @checkstyle LineLength (1 line) + "https://raw.githubusercontent.com/yegor256/cactoos/0.5/pom.xml" + ) + ) + ) + ), + new TextHasString( + Matchers.endsWith("\n") + ) + ); + } + } From f53b6e6eecfe275d70e0334f68e6a75f9496e451 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 15 Jun 2017 18:21:27 +0300 Subject: [PATCH 088/169] #146 javadoc fix --- src/main/java/org/cactoos/io/InputAsLSInput.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/io/InputAsLSInput.java b/src/main/java/org/cactoos/io/InputAsLSInput.java index 74718d8813..ba74890c05 100644 --- a/src/main/java/org/cactoos/io/InputAsLSInput.java +++ b/src/main/java/org/cactoos/io/InputAsLSInput.java @@ -32,6 +32,7 @@ import org.cactoos.text.BytesAsText; import org.w3c.dom.ls.LSInput; +// @checkstyle AbbreviationAsWordInNameCheck (10 lines) /** * Input as LSInput. * @@ -40,7 +41,6 @@ * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @since 0.6 - * @checkstyle AbbreviationAsWordInNameCheck (10 lines) */ public final class InputAsLSInput implements LSInput { @@ -72,13 +72,13 @@ public InputAsLSInput(final Input inpt) { this(inpt, "#public", "#system", "#base"); } + // @checkstyle ParameterNumberCheck (10 lines) /** * Ctor. * @param inpt Input * @param pubid PublicID * @param sysid SystemID * @param bse Base - * @checkstyle ParameterNumberCheck (5 lines) */ public InputAsLSInput(final Input inpt, final String pubid, final String sysid, final String bse) { From aef94cf7333cb346e8a0846068a42028cfc81203 Mon Sep 17 00:00:00 2001 From: Vseslav Sekorin Date: Thu, 15 Jun 2017 20:37:52 +0300 Subject: [PATCH 089/169] Add True --- .../org/cactoos/{Pred.java => Logical.java} | 12 +++--- src/main/java/org/cactoos/func/True.java | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) rename src/main/java/org/cactoos/{Pred.java => Logical.java} (86%) create mode 100644 src/main/java/org/cactoos/func/True.java diff --git a/src/main/java/org/cactoos/Pred.java b/src/main/java/org/cactoos/Logical.java similarity index 86% rename from src/main/java/org/cactoos/Pred.java rename to src/main/java/org/cactoos/Logical.java index 4b530751e3..a8d276d090 100644 --- a/src/main/java/org/cactoos/Pred.java +++ b/src/main/java/org/cactoos/Logical.java @@ -24,23 +24,21 @@ package org.cactoos; /** - * Predicate. + * Logical value. * *

There is no thread-safety guarantee. * * @author Vseslav Sekorin (vssekorin@gmail.com) * @version $Id$ - * @param Type of input * @since 0.5 */ -public interface Pred { +public interface Logical { /** - * Evaluates predicate on the given argument. + * Convert to boolean value. * - * @param input The argument - * @return The result + * @return The value * @throws Exception If fails */ - Boolean test(X input) throws Exception; + Boolean asValue() throws Exception; } diff --git a/src/main/java/org/cactoos/func/True.java b/src/main/java/org/cactoos/func/True.java new file mode 100644 index 0000000000..4648f7c1ac --- /dev/null +++ b/src/main/java/org/cactoos/func/True.java @@ -0,0 +1,43 @@ +/** + * 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.Logical; + +/** + * Logical truth. + * + *

There is no thread-safety guarantee. + * + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @since 0.5 + */ +public final class True implements Logical { + + @Override + public Boolean asValue() throws Exception { + return true; + } +} From 403761eb40cb44d0b26a885c4dbc19178333bccf Mon Sep 17 00:00:00 2001 From: Vseslav Sekorin Date: Thu, 15 Jun 2017 20:42:59 +0300 Subject: [PATCH 090/169] Add False --- src/main/java/org/cactoos/func/False.java | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/main/java/org/cactoos/func/False.java diff --git a/src/main/java/org/cactoos/func/False.java b/src/main/java/org/cactoos/func/False.java new file mode 100644 index 0000000000..c57fd9fd7f --- /dev/null +++ b/src/main/java/org/cactoos/func/False.java @@ -0,0 +1,43 @@ +/** + * 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.Logical; + +/** + * Logical false. + * + *

There is no thread-safety guarantee. + * + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @since 0.5 + */ +public final class False implements Logical { + + @Override + public Boolean asValue() throws Exception { + return false; + } +} From 9c389d27ee2e61f035de8e942e812152d2c18898 Mon Sep 17 00:00:00 2001 From: Vseslav Sekorin Date: Thu, 15 Jun 2017 21:03:26 +0300 Subject: [PATCH 091/169] Add test case for True --- src/test/java/org/cactoos/func/TrueTest.java | 47 ++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/test/java/org/cactoos/func/TrueTest.java diff --git a/src/test/java/org/cactoos/func/TrueTest.java b/src/test/java/org/cactoos/func/TrueTest.java new file mode 100644 index 0000000000..9d49c9890a --- /dev/null +++ b/src/test/java/org/cactoos/func/TrueTest.java @@ -0,0 +1,47 @@ +/** + * 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.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link True}. + * + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @since 0.5 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class TrueTest { + + @Test + public void asValue() throws Exception { + MatcherAssert.assertThat( + new True().asValue(), + Matchers.equalTo(true) + ); + } +} From 5c598770c240213458ba7ff79aaa36406adcb2fa Mon Sep 17 00:00:00 2001 From: Vseslav Sekorin Date: Thu, 15 Jun 2017 21:17:56 +0300 Subject: [PATCH 092/169] Add test case for False --- src/test/java/org/cactoos/func/FalseTest.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/test/java/org/cactoos/func/FalseTest.java diff --git a/src/test/java/org/cactoos/func/FalseTest.java b/src/test/java/org/cactoos/func/FalseTest.java new file mode 100644 index 0000000000..ccc6c2bbd7 --- /dev/null +++ b/src/test/java/org/cactoos/func/FalseTest.java @@ -0,0 +1,48 @@ +/** + * 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.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link False}. + * + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @since 0.5 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class FalseTest { + + @Test + public void asValue() throws Exception { + MatcherAssert.assertThat( + new False().asValue(), + Matchers.equalTo(false) + ); + } + +} From 0eeb6b6616b7bb504e9aace736221b6f3f694e81 Mon Sep 17 00:00:00 2001 From: englishman Date: Thu, 15 Jun 2017 20:06:32 -0400 Subject: [PATCH 093/169] add a new ctor from array --- src/main/java/org/cactoos/list/IterableAsList.java | 9 +++++++++ src/test/java/org/cactoos/list/IterableAsListTest.java | 3 +-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index 8dd9c642d1..ce0974675f 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -51,6 +51,15 @@ public final class IterableAsList extends AbstractList { */ private final UncheckedScalar length; + /** + * Ctor. + * + * @param array An array of some elements + */ + public IterableAsList(T... array) { + this(new ArrayAsIterable<>(array)); + } + /** * Ctor. * diff --git a/src/test/java/org/cactoos/list/IterableAsListTest.java b/src/test/java/org/cactoos/list/IterableAsListTest.java index 53ccd5a1ce..effdb94b8d 100644 --- a/src/test/java/org/cactoos/list/IterableAsListTest.java +++ b/src/test/java/org/cactoos/list/IterableAsListTest.java @@ -44,8 +44,7 @@ public void elementAtIndexTest() throws Exception { MatcherAssert.assertThat( "Can't convert an iterable to a list", new IterableAsList<>( - // @checkstyle MagicNumber (2 lines) - new ArrayAsIterable<>(0, 1, 2, num, 3, 4) + -1, 0, 1, num, 1 ).get(3), Matchers.equalTo(num) ); From 7b56e50c14103ae50284e1e05c01a01b2ee97454 Mon Sep 17 00:00:00 2001 From: englishman Date: Thu, 15 Jun 2017 20:21:48 -0400 Subject: [PATCH 094/169] use IterableAsList instead of Arrays.asList --- src/main/java/org/cactoos/list/AllOf.java | 6 +++--- .../java/org/cactoos/list/ConcatenatedIterable.java | 3 +-- .../java/org/cactoos/list/ConcatenatedIterator.java | 3 +-- src/main/java/org/cactoos/list/IterableAsBoolean.java | 2 +- src/main/java/org/cactoos/list/IterableAsBooleans.java | 2 +- src/main/java/org/cactoos/text/FormattedText.java | 10 +++++----- 6 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/cactoos/list/AllOf.java b/src/main/java/org/cactoos/list/AllOf.java index 7819887b8d..b5cf9e184c 100644 --- a/src/main/java/org/cactoos/list/AllOf.java +++ b/src/main/java/org/cactoos/list/AllOf.java @@ -35,7 +35,7 @@ * *

 new AllOf(
  *   new TransformedIterable<String>(
- *     Arrays.asList("hello", "world"),
+ *     new IdentityArrayList<>("hello", "world"),
  *     new ProcAsFunc<>(i -> System.out.println(i))
  *   )
  * ).asValue();
@@ -44,7 +44,7 @@ * *
 new AllOf(
  *   new IterableAsBooleans<String>(
- *     Arrays.asList("hello", "world"),
+ *     new IterableAsList<String>("hello", "world"),
  *     i -> System.out.println(i)
  *   )
  * ).asValue();
@@ -52,7 +52,7 @@ *

Or you can even use {@link IterableAsBoolean}:

* *
 new IterableAsBoolean<String>(
- *   Arrays.asList("hello", "world"),
+ *   new IterableAsList<String>("hello", "world"),
  *   i -> System.out.println(i)
  * ).asValue();
* diff --git a/src/main/java/org/cactoos/list/ConcatenatedIterable.java b/src/main/java/org/cactoos/list/ConcatenatedIterable.java index b2b2c89879..f178053900 100644 --- a/src/main/java/org/cactoos/list/ConcatenatedIterable.java +++ b/src/main/java/org/cactoos/list/ConcatenatedIterable.java @@ -23,7 +23,6 @@ */ package org.cactoos.list; -import java.util.Arrays; import java.util.Iterator; import org.cactoos.func.StickyFunc; @@ -51,7 +50,7 @@ public final class ConcatenatedIterable implements Iterable { @SafeVarargs @SuppressWarnings("varargs") public ConcatenatedIterable(final Iterable... items) { - this(Arrays.asList(items)); + this(new IterableAsList<>(items)); } /** diff --git a/src/main/java/org/cactoos/list/ConcatenatedIterator.java b/src/main/java/org/cactoos/list/ConcatenatedIterator.java index a9c138b9d6..4daa30adf7 100644 --- a/src/main/java/org/cactoos/list/ConcatenatedIterator.java +++ b/src/main/java/org/cactoos/list/ConcatenatedIterator.java @@ -23,7 +23,6 @@ */ package org.cactoos.list; -import java.util.Arrays; import java.util.Iterator; /** @@ -50,7 +49,7 @@ public final class ConcatenatedIterator implements Iterator { @SafeVarargs @SuppressWarnings("varargs") public ConcatenatedIterator(final Iterator... items) { - this(Arrays.asList(items)); + this(new IterableAsList<>(items)); } /** diff --git a/src/main/java/org/cactoos/list/IterableAsBoolean.java b/src/main/java/org/cactoos/list/IterableAsBoolean.java index 6352b4b85b..6613937b83 100644 --- a/src/main/java/org/cactoos/list/IterableAsBoolean.java +++ b/src/main/java/org/cactoos/list/IterableAsBoolean.java @@ -33,7 +33,7 @@ * a collection of items:

* *
 new IterableAsBoolean<>(
- *   Arrays.asList("hello", "world"),
+ *   new IterableAsList<String>("hello", "world"),
  *   i -> System.out.println(i)
  * ).asValue();
* diff --git a/src/main/java/org/cactoos/list/IterableAsBooleans.java b/src/main/java/org/cactoos/list/IterableAsBooleans.java index 98c50a1c0f..f903bfd3ec 100644 --- a/src/main/java/org/cactoos/list/IterableAsBooleans.java +++ b/src/main/java/org/cactoos/list/IterableAsBooleans.java @@ -34,7 +34,7 @@ * *
 new AllOf(
  *   new IterableAsBooleans<String>(
- *     Arrays.asList("hello", "world"),
+ *     new IterableAsList<String>("hello", "world"),
  *     i -> System.out.println(i)
  *   )
  * ).asValue();
diff --git a/src/main/java/org/cactoos/text/FormattedText.java b/src/main/java/org/cactoos/text/FormattedText.java index 80162ef321..9508203602 100644 --- a/src/main/java/org/cactoos/text/FormattedText.java +++ b/src/main/java/org/cactoos/text/FormattedText.java @@ -24,12 +24,12 @@ package org.cactoos.text; import java.io.IOException; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Formatter; import java.util.Locale; import org.cactoos.Text; +import org.cactoos.list.IterableAsList; /** * Text in Sprinf format. @@ -64,7 +64,7 @@ public final class FormattedText implements Text { * @param arguments Arguments */ public FormattedText(final String ptn, final Object... arguments) { - this(ptn, Arrays.asList(arguments)); + this(ptn, new IterableAsList<>(arguments)); } /** @@ -74,7 +74,7 @@ public FormattedText(final String ptn, final Object... arguments) { * @param arguments Arguments */ public FormattedText(final Text ptn, final Object... arguments) { - this(ptn, Arrays.asList(arguments)); + this(ptn, new IterableAsList<>(arguments)); } /** @@ -89,7 +89,7 @@ public FormattedText( final Locale locale, final Object... arguments ) { - this(ptn, locale, Arrays.asList(arguments)); + this(ptn, locale, new IterableAsList<>(arguments)); } /** @@ -104,7 +104,7 @@ public FormattedText( final Locale locale, final Object... arguments ) { - this(ptn, locale, Arrays.asList(arguments)); + this(ptn, locale, new IterableAsList<>(arguments)); } /** From 5c951a3c938951aa474926fc61b0084affdc5d82 Mon Sep 17 00:00:00 2001 From: englishman Date: Thu, 15 Jun 2017 20:28:10 -0400 Subject: [PATCH 095/169] suppress warnings --- src/main/java/org/cactoos/list/IterableAsList.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index ce0974675f..bce66e6777 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -56,6 +56,8 @@ public final class IterableAsList extends AbstractList { * * @param array An array of some elements */ + @SafeVarargs + @SuppressWarnings("varargs") public IterableAsList(T... array) { this(new ArrayAsIterable<>(array)); } From b6c347d331f83049f8138cf5b1da60fa017fc5ab Mon Sep 17 00:00:00 2001 From: englishman Date: Thu, 15 Jun 2017 20:35:07 -0400 Subject: [PATCH 096/169] fix code style --- src/main/java/org/cactoos/list/IterableAsList.java | 2 +- src/test/java/org/cactoos/list/IterableAsListTest.java | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index bce66e6777..12e73c26a2 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -58,7 +58,7 @@ public final class IterableAsList extends AbstractList { */ @SafeVarargs @SuppressWarnings("varargs") - public IterableAsList(T... array) { + public IterableAsList(final T... array) { this(new ArrayAsIterable<>(array)); } diff --git a/src/test/java/org/cactoos/list/IterableAsListTest.java b/src/test/java/org/cactoos/list/IterableAsListTest.java index effdb94b8d..a0f418d9f8 100644 --- a/src/test/java/org/cactoos/list/IterableAsListTest.java +++ b/src/test/java/org/cactoos/list/IterableAsListTest.java @@ -43,9 +43,7 @@ public void elementAtIndexTest() throws Exception { final int num = 345; MatcherAssert.assertThat( "Can't convert an iterable to a list", - new IterableAsList<>( - -1, 0, 1, num, 1 - ).get(3), + new IterableAsList<>(-1, num, 0, 1).get(1), Matchers.equalTo(num) ); } From abafbe67e7b17b52d2b3fb0863367d8373f95d77 Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Fri, 16 Jun 2017 00:26:26 -0300 Subject: [PATCH 097/169] #152: Eclipse cannot infer type arguments for IterableAsMap error --- src/test/java/org/cactoos/list/IterableAsMapTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/org/cactoos/list/IterableAsMapTest.java b/src/test/java/org/cactoos/list/IterableAsMapTest.java index 57bb9a4c49..5c8ee2b1c0 100644 --- a/src/test/java/org/cactoos/list/IterableAsMapTest.java +++ b/src/test/java/org/cactoos/list/IterableAsMapTest.java @@ -39,11 +39,10 @@ public final class IterableAsMapTest { @Test - @SuppressWarnings("unchecked") public void convertsIterableToMap() { MatcherAssert.assertThat( "Can't convert iterable to map", - new IterableAsMap<>( + new IterableAsMap( new AbstractMap.SimpleEntry<>(0, "hello, "), new AbstractMap.SimpleEntry<>(1, "world!") ), From 3a28677b3cf7f76e1e2960c05c9ad9b3cb40354f Mon Sep 17 00:00:00 2001 From: Ilia Rogozhin Date: Fri, 16 Jun 2017 12:05:19 +0300 Subject: [PATCH 098/169] modify FirstOf to ItemOfIterable --- .../{FirstOf.java => ItemOfIterable.java} | 73 ++++++-- .../java/org/cactoos/list/ItemOfIterator.java | 161 ++++++++++++++++++ ...rstOfTest.java => ItemOfIterableTest.java} | 33 ++-- .../org/cactoos/list/ItemOfIteratorTest.java | 101 +++++++++++ 4 files changed, 343 insertions(+), 25 deletions(-) rename src/main/java/org/cactoos/list/{FirstOf.java => ItemOfIterable.java} (61%) create mode 100644 src/main/java/org/cactoos/list/ItemOfIterator.java rename src/test/java/org/cactoos/list/{FirstOfTest.java => ItemOfIterableTest.java} (73%) create mode 100644 src/test/java/org/cactoos/list/ItemOfIteratorTest.java diff --git a/src/main/java/org/cactoos/list/FirstOf.java b/src/main/java/org/cactoos/list/ItemOfIterable.java similarity index 61% rename from src/main/java/org/cactoos/list/FirstOf.java rename to src/main/java/org/cactoos/list/ItemOfIterable.java index 6d90e464ed..3cc971be36 100644 --- a/src/main/java/org/cactoos/list/FirstOf.java +++ b/src/main/java/org/cactoos/list/ItemOfIterable.java @@ -24,22 +24,22 @@ package org.cactoos.list; import java.io.IOException; -import java.util.Iterator; import org.cactoos.Func; import org.cactoos.Scalar; import org.cactoos.text.FormattedText; /** - * First element in {@link Iterable} or fallback value if iterable is empty. + * Element from position in {@link Iterable} + * or fallback value if iterable hasn't this position. * *

There is no thread-safety guarantee. * * @author Kirill (g4s8.public@gmail.com) * @version $Id$ * @param Scalar type - * @since 0.1 + * @since 0.7 */ -public final class FirstOf implements Scalar { +public final class ItemOfIterable implements Scalar { /** * Source iterable. @@ -51,12 +51,17 @@ public final class FirstOf implements Scalar { */ private final Func, T> fbk; + /** + * Position. + */ + private final int pos; + /** * Ctor. * * @param src Iterable */ - public FirstOf(final Iterable src) { + public ItemOfIterable(final Iterable src) { this( src, itr -> { @@ -74,7 +79,7 @@ public FirstOf(final Iterable src) { * @param src Iterable * @param fbk Fallback value */ - public FirstOf(final Iterable src, final T fbk) { + public ItemOfIterable(final Iterable src, final T fbk) { this(src, itr -> fbk); } @@ -84,20 +89,58 @@ public FirstOf(final Iterable src, final T fbk) { * @param src Iterable * @param fbk Fallback value */ - public FirstOf(final Iterable src, final Func, T> fbk) { + public ItemOfIterable( + final Iterable src, + final Func, T> fbk + ) { + this(src, 0, fbk); + } + + /** + * Ctor. + * + * @param src Iterable + * @param pos Position + */ + public ItemOfIterable(final Iterable src, final int pos) { + this( + src, + pos, + itr -> { + throw new IOException( + new FormattedText( + "Iterable %s hasn't element from position %d", + itr, + pos + ).asString() + ); + } + ); + } + + /** + * Ctor. + * + * @param src Iterable + * @param pos Position + * @param fbk Fallback value + */ + public ItemOfIterable( + final Iterable src, + final int pos, + final Func, T> fbk + ) { + this.pos = pos; this.src = src; this.fbk = fbk; } @Override public T asValue() throws Exception { - final Iterator itr = this.src.iterator(); - final T ret; - if (itr.hasNext()) { - ret = itr.next(); - } else { - ret = this.fbk.apply(this.src); - } - return ret; + return new ItemOfIterator<>( + this.src.iterator(), + this.pos, + this.fbk + ).asValue(); } } diff --git a/src/main/java/org/cactoos/list/ItemOfIterator.java b/src/main/java/org/cactoos/list/ItemOfIterator.java new file mode 100644 index 0000000000..f5bae33408 --- /dev/null +++ b/src/main/java/org/cactoos/list/ItemOfIterator.java @@ -0,0 +1,161 @@ +/** + * 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.list; + +import java.io.IOException; +import java.util.Iterator; +import org.cactoos.Func; +import org.cactoos.Scalar; +import org.cactoos.text.FormattedText; + +/** + * Element from position in {@link Iterator} + * or fallback value if iterator hasn't this position. + * + *

There is no thread-safety guarantee. + * + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @param Scalar type + * @since 0.7 + */ +public final class ItemOfIterator implements Scalar { + + /** + * Source iterator. + */ + private final Iterator src; + + /** + * Fallback value. + */ + private final Func, T> fbk; + + /** + * Position. + */ + private final int pos; + + /** + * Ctor. + * + * @param src Iterator + */ + public ItemOfIterator(final Iterator src) { + this( + src, + itr -> { + throw new IOException( + new FormattedText("Iterator %s is empty", itr.iterator()) + .asString() + ); + } + ); + } + + /** + * Ctor. + * + * @param src Iterator + * @param fbk Fallback value + */ + public ItemOfIterator(final Iterator src, final T fbk) { + this(src, itr -> fbk); + } + + /** + * Ctor. + * + * @param src Iterator + * @param fbk Fallback value + */ + public ItemOfIterator( + final Iterator src, + final Func, T> fbk + ) { + this(src, 0, fbk); + } + + /** + * Ctor. + * + * @param src Iterator + * @param pos Position + */ + public ItemOfIterator(final Iterator src, final int pos) { + this( + src, + pos, + itr -> { + throw new IOException( + new FormattedText( + "Iterator %s hasn't element from position %d", + itr.iterator(), + pos + ).asString() + ); + } + ); + } + + /** + * Ctor. + * + * @param src Iterator + * @param pos Position + * @param fbk Fallback value + */ + public ItemOfIterator( + final Iterator src, + final int pos, + final Func, T> fbk + ) { + this.pos = pos; + this.src = src; + this.fbk = fbk; + } + + @Override + public T asValue() throws Exception { + if (this.pos < 0) { + throw new IOException( + new FormattedText( + "Position must be nonnegative! But position = %d", + this.pos + ).asString() + ); + } + final T ret; + int cur; + for (cur = 0; cur < this.pos && this.src.hasNext(); ++cur) { + this.src.next(); + } + if (cur == this.pos && this.src.hasNext()) { + ret = this.src.next(); + } else { + ret = this.fbk.apply(() -> this.src); + } + return ret; + } +} diff --git a/src/test/java/org/cactoos/list/FirstOfTest.java b/src/test/java/org/cactoos/list/ItemOfIterableTest.java similarity index 73% rename from src/test/java/org/cactoos/list/FirstOfTest.java rename to src/test/java/org/cactoos/list/ItemOfIterableTest.java index 3adf6f3c4f..d2222a61dd 100644 --- a/src/test/java/org/cactoos/list/FirstOfTest.java +++ b/src/test/java/org/cactoos/list/ItemOfIterableTest.java @@ -24,48 +24,61 @@ package org.cactoos.list; import java.io.IOException; -import java.util.Arrays; import java.util.Collections; import org.cactoos.ScalarHasValue; import org.hamcrest.MatcherAssert; import org.junit.Test; /** - * Test case for {@link FirstOf}. + * Test case for {@link ItemOfIterable}. * * @author Kirill (g4s8.public@gmail.com) * @version $Id$ - * @since 0.1 + * @since 0.7 * @checkstyle JavadocMethodCheck (500 lines) */ -public final class FirstOfTest { +public final class ItemOfIterableTest { @Test public void firstElementTest() throws Exception { MatcherAssert.assertThat( "Can't take the first item from the iterable", - new FirstOf<>( + new ItemOfIterable<>( // @checkstyle MagicNumber (1 line) - Arrays.asList(1, 2, 3) + new ArrayAsIterable<>(1, 2, 3) ), new ScalarHasValue<>(1) ); } + @Test + public void elementByPosTest() throws Exception { + MatcherAssert.assertThat( + "Can't take the item by position from the iterable", + new ItemOfIterable<>( + // @checkstyle MagicNumber (1 line) + new ArrayAsIterable<>(1, 2, 3), + 1 + ), + new ScalarHasValue<>(2) + ); + } + @Test(expected = IOException.class) public void failForEmptyCollectionTest() throws Exception { - new FirstOf<>(Collections.emptyList()).asValue(); + new ItemOfIterable<>(Collections.emptyList()).asValue(); } @Test public void fallbackTest() throws Exception { + final String fallback = "fallback"; MatcherAssert.assertThat( "Can't fallback to default value", - new FirstOf<>( + new ItemOfIterable<>( Collections.emptyList(), - 1 + fallback ), - new ScalarHasValue<>(1) + new ScalarHasValue<>(fallback) ); } } diff --git a/src/test/java/org/cactoos/list/ItemOfIteratorTest.java b/src/test/java/org/cactoos/list/ItemOfIteratorTest.java new file mode 100644 index 0000000000..d103b16dd7 --- /dev/null +++ b/src/test/java/org/cactoos/list/ItemOfIteratorTest.java @@ -0,0 +1,101 @@ +/** + * 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.list; + +import java.io.IOException; +import java.util.Collections; +import org.cactoos.ScalarHasValue; +import org.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test Case for {@link ItemOfIterator}. + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.7 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class ItemOfIteratorTest { + + @Test + public void firstElementTest() throws Exception { + MatcherAssert.assertThat( + "Can't take the first item from the iterator", + new ItemOfIterator<>( + // @checkstyle MagicNumber (1 line) + new ArrayAsIterable<>(1, 2, 3).iterator() + ), + new ScalarHasValue<>(1) + ); + } + + @Test + public void elementByPosTest() throws Exception { + MatcherAssert.assertThat( + "Can't take the item by position from the iterator", + new ItemOfIterator<>( + // @checkstyle MagicNumber (1 line) + new ArrayAsIterable<>(1, 2, 3).iterator(), + 1 + ), + new ScalarHasValue<>(2) + ); + } + + @Test(expected = IOException.class) + public void failForEmptyCollectionTest() throws Exception { + new ItemOfIterator<>(Collections.emptyIterator()).asValue(); + } + + @Test(expected = IOException.class) + public void failForNegativePositionTest() throws Exception { + new ItemOfIterator<>( + // @checkstyle MagicNumber (1 line) + new ArrayAsIterable<>(1, 2, 3).iterator(), + -1 + ).asValue(); + } + + @Test + public void fallbackTest() throws Exception { + final String fallback = "fallback"; + MatcherAssert.assertThat( + "Can't fallback to default value", + new ItemOfIterator<>( + Collections.emptyIterator(), + fallback + ), + new ScalarHasValue<>(fallback) + ); + } + + @Test(expected = IOException.class) + public void failForPosMoreLengthTest() throws Exception { + new ItemOfIterator<>( + // @checkstyle MagicNumberCheck (2 lines) + new ArrayAsIterable<>(1, 2, 3).iterator(), + 3 + ).asValue(); + } +} From 96d92c37f956e702f1a40eae1f1672e9a2b99d04 Mon Sep 17 00:00:00 2001 From: Vseslav Sekorin Date: Fri, 16 Jun 2017 13:47:59 +0300 Subject: [PATCH 099/169] Rename --- src/main/java/org/cactoos/{Logical.java => LogTerm.java} | 2 +- src/main/java/org/cactoos/func/False.java | 4 ++-- src/main/java/org/cactoos/func/True.java | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename src/main/java/org/cactoos/{Logical.java => LogTerm.java} (98%) diff --git a/src/main/java/org/cactoos/Logical.java b/src/main/java/org/cactoos/LogTerm.java similarity index 98% rename from src/main/java/org/cactoos/Logical.java rename to src/main/java/org/cactoos/LogTerm.java index a8d276d090..ca0fc60fe5 100644 --- a/src/main/java/org/cactoos/Logical.java +++ b/src/main/java/org/cactoos/LogTerm.java @@ -32,7 +32,7 @@ * @version $Id$ * @since 0.5 */ -public interface Logical { +public interface LogTerm { /** * Convert to boolean value. diff --git a/src/main/java/org/cactoos/func/False.java b/src/main/java/org/cactoos/func/False.java index c57fd9fd7f..52f23cd6e9 100644 --- a/src/main/java/org/cactoos/func/False.java +++ b/src/main/java/org/cactoos/func/False.java @@ -23,7 +23,7 @@ */ package org.cactoos.func; -import org.cactoos.Logical; +import org.cactoos.LogTerm; /** * Logical false. @@ -34,7 +34,7 @@ * @version $Id$ * @since 0.5 */ -public final class False implements Logical { +public final class False implements LogTerm { @Override public Boolean asValue() throws Exception { diff --git a/src/main/java/org/cactoos/func/True.java b/src/main/java/org/cactoos/func/True.java index 4648f7c1ac..31f29064f4 100644 --- a/src/main/java/org/cactoos/func/True.java +++ b/src/main/java/org/cactoos/func/True.java @@ -23,7 +23,7 @@ */ package org.cactoos.func; -import org.cactoos.Logical; +import org.cactoos.LogTerm; /** * Logical truth. @@ -34,7 +34,7 @@ * @version $Id$ * @since 0.5 */ -public final class True implements Logical { +public final class True implements LogTerm { @Override public Boolean asValue() throws Exception { From 30aa7a3840083d17f3928311a452e5badb0dfaff Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 16 Jun 2017 15:59:14 +0300 Subject: [PATCH 100/169] #158: SortedIterable and SortedIterator --- .../java/org/cactoos/list/SortedIterable.java | 96 +++++++++++++++++++ .../java/org/cactoos/list/SortedIterator.java | 89 +++++++++++++++++ .../org/cactoos/list/SortedIterableTest.java | 82 ++++++++++++++++ 3 files changed, 267 insertions(+) create mode 100644 src/main/java/org/cactoos/list/SortedIterable.java create mode 100644 src/main/java/org/cactoos/list/SortedIterator.java create mode 100644 src/test/java/org/cactoos/list/SortedIterableTest.java diff --git a/src/main/java/org/cactoos/list/SortedIterable.java b/src/main/java/org/cactoos/list/SortedIterable.java new file mode 100644 index 0000000000..29aaeccd76 --- /dev/null +++ b/src/main/java/org/cactoos/list/SortedIterable.java @@ -0,0 +1,96 @@ +/** + * 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.list; + +import java.util.Comparator; +import java.util.Iterator; + +/** + * Sorted iterable. + * + *

There is no thread-safety guarantee.

+ * + * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) + * @version $Id$ + * @param Element type + * @since 0.7 + */ +public final class + SortedIterable> implements + Iterable { + + /** + * Decorated iterable. + */ + private final Iterable iterable; + + /** + * Comparator. + */ + private final Comparator comparator; + + /** + * Ctor. + * @param src The underlying iterable + */ + @SafeVarargs + @SuppressWarnings("varargs") + public SortedIterable(final T... src) { + this(new ArrayAsIterable<>(src)); + } + + /** + * Ctor. + * @param src The underlying iterable + */ + public SortedIterable(final Iterable src) { + this(Comparator.naturalOrder(), src); + } + + /** + * Ctor. + * @param src The underlying iterable + * @param cmp The comparator + */ + @SafeVarargs + @SuppressWarnings("varargs") + public SortedIterable(final Comparator cmp, final T... src) { + this(cmp, new ArrayAsIterable<>(src)); + } + + /** + * Ctor. + * @param src The underlying iterable + * @param cmp The comparator + */ + public SortedIterable(final Comparator cmp, final Iterable src) { + this.iterable = src; + this.comparator = cmp; + } + + @Override + public Iterator iterator() { + return new SortedIterator<>(this.comparator, this.iterable.iterator()); + } +} diff --git a/src/main/java/org/cactoos/list/SortedIterator.java b/src/main/java/org/cactoos/list/SortedIterator.java new file mode 100644 index 0000000000..2ae36a15e6 --- /dev/null +++ b/src/main/java/org/cactoos/list/SortedIterator.java @@ -0,0 +1,89 @@ +/** + * 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.list; + +import java.util.Comparator; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import org.cactoos.func.StickyScalar; +import org.cactoos.func.UncheckedScalar; + +/** + * Sorted iterator. + * + *

There is no thread-safety guarantee.

+ * + * @author Dusan Rychnovsky (dusan.rychnovsky@gmail.com) + * @version $Id$ + * @param Element type + * @since 0.7 + */ +public final class + SortedIterator> implements + Iterator { + + /** + * Sorted one. + */ + private final UncheckedScalar> sorted; + + /** + * Ctor. + * @param src The underlying iterator + */ + public SortedIterator(final Iterator src) { + this(Comparator.naturalOrder(), src); + } + + /** + * Ctor. + * @param src The underlying iterator + * @param cmp The comparator + */ + public SortedIterator(final Comparator cmp, final Iterator src) { + this.sorted = new UncheckedScalar<>( + new StickyScalar<>( + () -> { + final List items = new LinkedList<>(); + while (src.hasNext()) { + items.add(src.next()); + } + items.sort(cmp); + return items.iterator(); + } + ) + ); + } + + @Override + public boolean hasNext() { + return this.sorted.asValue().hasNext(); + } + + @Override + public T next() { + return this.sorted.asValue().next(); + } +} diff --git a/src/test/java/org/cactoos/list/SortedIterableTest.java b/src/test/java/org/cactoos/list/SortedIterableTest.java new file mode 100644 index 0000000000..afce12b66f --- /dev/null +++ b/src/test/java/org/cactoos/list/SortedIterableTest.java @@ -0,0 +1,82 @@ +/** + * 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.list; + +import java.util.Collections; +import java.util.Comparator; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link SortedIterable}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.7 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) + */ +public final class SortedIterableTest { + + @Test + public void sortsAnArray() throws Exception { + MatcherAssert.assertThat( + "Can't sort an iterable", + new SortedIterable<>( + new ArrayAsIterable<>( + 3, 2, 10, 44, -6, 0 + ) + ), + Matchers.hasItems(-6, 0, 2, 3, 10, 44) + ); + } + + @Test + @SuppressWarnings("PMD.AvoidDuplicateLiterals") + public void sortsAnArrayWithComparator() throws Exception { + MatcherAssert.assertThat( + "Can't sort an iterable with a comparator", + new SortedIterable<>( + Comparator.reverseOrder(), + new ArrayAsIterable<>( + "a", "c", "hello", "dude", "Friend" + ) + ), + Matchers.hasItems("hello", "dude", "c", "a", "Friend") + ); + } + + @Test + public void sortsAnEmptyArray() throws Exception { + MatcherAssert.assertThat( + "Can't sort an empty iterable", + new SortedIterable( + Collections.emptyList() + ), + Matchers.iterableWithSize(0) + ); + } + +} From 4826cc44d099d2f6e525edb61f6771e9299dbed8 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 16 Jun 2017 16:57:40 +0300 Subject: [PATCH 101/169] #159 fixed --- src/test/java/org/cactoos/io/StickyInputTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/cactoos/io/StickyInputTest.java b/src/test/java/org/cactoos/io/StickyInputTest.java index 29db28038c..d0022b8ce2 100644 --- a/src/test/java/org/cactoos/io/StickyInputTest.java +++ b/src/test/java/org/cactoos/io/StickyInputTest.java @@ -23,6 +23,7 @@ */ package org.cactoos.io; +import org.cactoos.Input; import org.cactoos.TextHasString; import org.cactoos.func.FuncAsMatcher; import org.cactoos.func.RepeatedFunc; @@ -51,7 +52,7 @@ public void readsFileContent() { ) ), new FuncAsMatcher<>( - new RepeatedFunc<>( + new RepeatedFunc( input -> new InputAsBytes( new TeeInput(input, new DeadOutput()) // @checkstyle MagicNumber (2 lines) From 3a21e5e1182caa77336f1c7751b6356d38317c9a Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 16 Jun 2017 17:01:08 +0300 Subject: [PATCH 102/169] #161 fix --- src/main/java/org/cactoos/func/ProcAsFunc.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/func/ProcAsFunc.java b/src/main/java/org/cactoos/func/ProcAsFunc.java index 33d2caf3e0..ac41c7f061 100644 --- a/src/main/java/org/cactoos/func/ProcAsFunc.java +++ b/src/main/java/org/cactoos/func/ProcAsFunc.java @@ -46,17 +46,33 @@ public final class ProcAsFunc implements Func { */ private final Proc proc; + /** + * The result. + */ + private final Y result; + /** * Ctor. * @param prc The proc */ public ProcAsFunc(final Proc prc) { + this(prc, null); + } + + /** + * Ctor. + * @param prc The proc + * @param rslt Result to return + * @since 0.7 + */ + public ProcAsFunc(final Proc prc, final Y rslt) { this.proc = prc; + this.result = rslt; } @Override public Y apply(final X input) throws Exception { this.proc.exec(input); - return null; + return this.result; } } From e3ca4afd12d2adb3965dac2467048f308c82879a Mon Sep 17 00:00:00 2001 From: TimmeeY Date: Fri, 16 Jun 2017 15:59:30 +0200 Subject: [PATCH 103/169] NaturalNumbers as Iterable --- .../java/org/cactoos/list/NaturalNumbers.java | 42 +++++++++++++ .../org/cactoos/list/NaturalNumbersTest.java | 59 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/main/java/org/cactoos/list/NaturalNumbers.java create mode 100644 src/test/java/org/cactoos/list/NaturalNumbersTest.java diff --git a/src/main/java/org/cactoos/list/NaturalNumbers.java b/src/main/java/org/cactoos/list/NaturalNumbers.java new file mode 100644 index 0000000000..5e910e878e --- /dev/null +++ b/src/main/java/org/cactoos/list/NaturalNumbers.java @@ -0,0 +1,42 @@ +/** + * 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.list; + +import java.util.Iterator; +import java.util.stream.LongStream; + +/** + * Iterable providing Natural Numbers. + * + * @author Tim Hinkes (timmeey@timmeey.de) + * @version $Id$ + * @since 0.4 + */ +public final class NaturalNumbers implements Iterable { + + @Override + public Iterator iterator() { + return LongStream.range(0, Long.MAX_VALUE).iterator(); + } +} diff --git a/src/test/java/org/cactoos/list/NaturalNumbersTest.java b/src/test/java/org/cactoos/list/NaturalNumbersTest.java new file mode 100644 index 0000000000..f24eb5f8e0 --- /dev/null +++ b/src/test/java/org/cactoos/list/NaturalNumbersTest.java @@ -0,0 +1,59 @@ +/** + * 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.list; + +import org.cactoos.ScalarHasValue; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link NaturalNumbers}. + * + * @author Tim Hinkes (timmeey@timmeey.de) + * @version $Id$ + * @since 0.4 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class NaturalNumbersTest { + + @Test + public void containsSequentialNaturalNumbers() { + MatcherAssert.assertThat( + "Can't get sequential natural numbers", + new NaturalNumbers(), + // @checkstyle MagicNumber (1 line) + Matchers.hasItems(0L, 1L, 5L, 10L) + ); + } + + @Test + public void notStartsWithNegativeNumbers() { + MatcherAssert.assertThat( + "Contains negative Numbers", + new FirstOf(new NaturalNumbers()), + new ScalarHasValue<>(0L) + ); + } +} From 9ef1f52cb1270ec561ff677efefbbb491244d0ad Mon Sep 17 00:00:00 2001 From: TimmeeY Date: Fri, 16 Jun 2017 16:27:27 +0200 Subject: [PATCH 104/169] Increased Version --- src/main/java/org/cactoos/list/NaturalNumbers.java | 2 +- src/test/java/org/cactoos/list/NaturalNumbersTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/list/NaturalNumbers.java b/src/main/java/org/cactoos/list/NaturalNumbers.java index 5e910e878e..6466e70585 100644 --- a/src/main/java/org/cactoos/list/NaturalNumbers.java +++ b/src/main/java/org/cactoos/list/NaturalNumbers.java @@ -31,7 +31,7 @@ * * @author Tim Hinkes (timmeey@timmeey.de) * @version $Id$ - * @since 0.4 + * @since 0.7 */ public final class NaturalNumbers implements Iterable { diff --git a/src/test/java/org/cactoos/list/NaturalNumbersTest.java b/src/test/java/org/cactoos/list/NaturalNumbersTest.java index f24eb5f8e0..381efdb276 100644 --- a/src/test/java/org/cactoos/list/NaturalNumbersTest.java +++ b/src/test/java/org/cactoos/list/NaturalNumbersTest.java @@ -33,7 +33,7 @@ * * @author Tim Hinkes (timmeey@timmeey.de) * @version $Id$ - * @since 0.4 + * @since 0.7 * @checkstyle JavadocMethodCheck (500 lines) */ public final class NaturalNumbersTest { From 2c7c4eb81b0bd02831b7551d15bbed3c96bfa032 Mon Sep 17 00:00:00 2001 From: TimmeeY Date: Fri, 16 Jun 2017 16:49:42 +0200 Subject: [PATCH 105/169] Fixed test changed from FirstOf to ItemOfIterable --- src/test/java/org/cactoos/list/NaturalNumbersTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/cactoos/list/NaturalNumbersTest.java b/src/test/java/org/cactoos/list/NaturalNumbersTest.java index 381efdb276..5a442f1371 100644 --- a/src/test/java/org/cactoos/list/NaturalNumbersTest.java +++ b/src/test/java/org/cactoos/list/NaturalNumbersTest.java @@ -52,7 +52,7 @@ public void containsSequentialNaturalNumbers() { public void notStartsWithNegativeNumbers() { MatcherAssert.assertThat( "Contains negative Numbers", - new FirstOf(new NaturalNumbers()), + new ItemOfIterable(new NaturalNumbers(), 0), new ScalarHasValue<>(0L) ); } From 3865cf4e773f0658796b12830e98f1b68eb404c8 Mon Sep 17 00:00:00 2001 From: Ilia Rogozhin Date: Fri, 16 Jun 2017 18:16:12 +0300 Subject: [PATCH 106/169] Added tests for CycledIterator, and CycledIterable. Fixed other mistakes in CycledIterator from #138 --- .../java/org/cactoos/list/CycledIterable.java | 4 ++- .../java/org/cactoos/list/CycledIterator.java | 10 ++++-- .../org/cactoos/list/CycledIterableTest.java | 33 +++++++++++++++++++ .../org/cactoos/list/CycledIteratorTest.java | 33 +++++++++++++++++++ 4 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/cactoos/list/CycledIterableTest.java create mode 100644 src/test/java/org/cactoos/list/CycledIteratorTest.java diff --git a/src/main/java/org/cactoos/list/CycledIterable.java b/src/main/java/org/cactoos/list/CycledIterable.java index df06bd3720..d89c6a93ee 100644 --- a/src/main/java/org/cactoos/list/CycledIterable.java +++ b/src/main/java/org/cactoos/list/CycledIterable.java @@ -29,10 +29,12 @@ /** * Cycled Iterable. * + *

There is no thread-safety guarantee. + * * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) * @version $Id$ * @param Type of item - * @since 0.6 + * @since 0.7 */ public final class CycledIterable implements Iterable { diff --git a/src/main/java/org/cactoos/list/CycledIterator.java b/src/main/java/org/cactoos/list/CycledIterator.java index f1bdea700e..64132acb34 100644 --- a/src/main/java/org/cactoos/list/CycledIterator.java +++ b/src/main/java/org/cactoos/list/CycledIterator.java @@ -30,10 +30,12 @@ /** * Cycled Iterator. * + *

There is no thread-safety guarantee. + * * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) * @version $Id$ * @param Type of item - * @since 0.6 + * @since 0.7 */ public final class CycledIterator implements Iterator { @@ -53,16 +55,18 @@ public final class CycledIterator implements Iterator { */ public CycledIterator(final Iterable iterable) { this.iterable = iterable; - this.iterator = this.iterable.iterator(); } @Override public boolean hasNext() { - return this.iterator.hasNext() || this.iterable.iterator().hasNext(); + return this.iterable.iterator().hasNext(); } @Override public T next() { + if (this.iterator == null) { + this.iterator = this.iterable.iterator(); + } if (!this.iterator.hasNext()) { this.iterator = this.iterable.iterator(); if (!this.iterator.hasNext()) { diff --git a/src/test/java/org/cactoos/list/CycledIterableTest.java b/src/test/java/org/cactoos/list/CycledIterableTest.java new file mode 100644 index 0000000000..7564b9953b --- /dev/null +++ b/src/test/java/org/cactoos/list/CycledIterableTest.java @@ -0,0 +1,33 @@ +package org.cactoos.list; + +import org.cactoos.ScalarHasValue; +import org.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test Case for {@link CycledIterable}. + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.7 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class CycledIterableTest { + + @Test + public void repeatIterableTest() throws Exception { + MatcherAssert.assertThat( + "Can't repeat iterable", + new ItemOfIterable<>( + new CycledIterable<>( + new ArrayAsIterable<>( + "one", "two", "three" + ) + ), + 7 + ), + new ScalarHasValue<>( + "two" + ) + ); + } +} diff --git a/src/test/java/org/cactoos/list/CycledIteratorTest.java b/src/test/java/org/cactoos/list/CycledIteratorTest.java new file mode 100644 index 0000000000..e0856ab77b --- /dev/null +++ b/src/test/java/org/cactoos/list/CycledIteratorTest.java @@ -0,0 +1,33 @@ +package org.cactoos.list; + +import org.cactoos.ScalarHasValue; +import org.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test Case for {@link CycledIterator}. + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.7 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class CycledIteratorTest { + + @Test + public void repeatIteratorTest() throws Exception { + MatcherAssert.assertThat( + "Can't repeat iterator", + new ItemOfIterator<>( + new CycledIterator<>( + new ArrayAsIterable<>( + "one", "two", "three" + ) + ), + 7 + ), + new ScalarHasValue<>( + "two" + ) + ); + } +} From cf22715649cda011776de439b975c16efd00c67b Mon Sep 17 00:00:00 2001 From: Ilia Rogozhin Date: Fri, 16 Jun 2017 18:21:04 +0300 Subject: [PATCH 107/169] Fixed checkstyle and PMD mistakes. --- .../org/cactoos/list/CycledIterableTest.java | 29 +++++++++++++++++-- .../org/cactoos/list/CycledIteratorTest.java | 29 +++++++++++++++++-- 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/cactoos/list/CycledIterableTest.java b/src/test/java/org/cactoos/list/CycledIterableTest.java index 7564b9953b..481ec29e40 100644 --- a/src/test/java/org/cactoos/list/CycledIterableTest.java +++ b/src/test/java/org/cactoos/list/CycledIterableTest.java @@ -1,3 +1,26 @@ +/** + * 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.list; import org.cactoos.ScalarHasValue; @@ -15,18 +38,20 @@ public final class CycledIterableTest { @Test public void repeatIterableTest() throws Exception { + final String expected = "two"; MatcherAssert.assertThat( "Can't repeat iterable", new ItemOfIterable<>( new CycledIterable<>( new ArrayAsIterable<>( - "one", "two", "three" + "one", expected, "three" ) ), + // @checkstyle MagicNumberCheck (1 line) 7 ), new ScalarHasValue<>( - "two" + expected ) ); } diff --git a/src/test/java/org/cactoos/list/CycledIteratorTest.java b/src/test/java/org/cactoos/list/CycledIteratorTest.java index e0856ab77b..1a5fcdd28a 100644 --- a/src/test/java/org/cactoos/list/CycledIteratorTest.java +++ b/src/test/java/org/cactoos/list/CycledIteratorTest.java @@ -1,3 +1,26 @@ +/** + * 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.list; import org.cactoos.ScalarHasValue; @@ -15,18 +38,20 @@ public final class CycledIteratorTest { @Test public void repeatIteratorTest() throws Exception { + final String expected = "two"; MatcherAssert.assertThat( "Can't repeat iterator", new ItemOfIterator<>( new CycledIterator<>( new ArrayAsIterable<>( - "one", "two", "three" + "one", expected, "three" ) ), + // @checkstyle MagicNumberCheck (1 line) 7 ), new ScalarHasValue<>( - "two" + expected ) ); } From b5fcb748d8b9419538d33bec373b97eb9adbe2ac Mon Sep 17 00:00:00 2001 From: englishman Date: Fri, 16 Jun 2017 13:09:25 -0400 Subject: [PATCH 108/169] change external test resource to file system file --- src/test/java/org/cactoos/io/LengthOfInputTest.java | 4 ++-- src/test/java/org/cactoos/io/StickyInputTest.java | 4 ++-- src/test/java/org/cactoos/io/UrlAsInputTest.java | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/cactoos/io/LengthOfInputTest.java b/src/test/java/org/cactoos/io/LengthOfInputTest.java index 21e4df09f3..ace424e8c6 100644 --- a/src/test/java/org/cactoos/io/LengthOfInputTest.java +++ b/src/test/java/org/cactoos/io/LengthOfInputTest.java @@ -75,11 +75,11 @@ public void readsRealUrl() throws IOException { new LengthOfInput( new UrlAsInput( // @checkstyle LineLength (1 line) - "https://raw.githubusercontent.com/yegor256/cactoos/0.5/pom.xml" + "file:src/test/resources/org/cactoos/large-text.txt" ) ).asValue(), // @checkstyle MagicNumber (1 line) - Matchers.equalTo(5960L) + Matchers.equalTo(73471L) ); } diff --git a/src/test/java/org/cactoos/io/StickyInputTest.java b/src/test/java/org/cactoos/io/StickyInputTest.java index 29db28038c..ead842d12b 100644 --- a/src/test/java/org/cactoos/io/StickyInputTest.java +++ b/src/test/java/org/cactoos/io/StickyInputTest.java @@ -71,13 +71,13 @@ public void readsRealUrl() { new StickyInput( new UrlAsInput( // @checkstyle LineLength (1 line) - "https://raw.githubusercontent.com/yegor256/cactoos/0.5/pom.xml" + "file:src/test/resources/org/cactoos/large-text.txt" ) ) ) ), new TextHasString( - Matchers.endsWith("\n") + Matchers.endsWith("est laborum.\n") ) ); } diff --git a/src/test/java/org/cactoos/io/UrlAsInputTest.java b/src/test/java/org/cactoos/io/UrlAsInputTest.java index a4157084be..dc5583708f 100644 --- a/src/test/java/org/cactoos/io/UrlAsInputTest.java +++ b/src/test/java/org/cactoos/io/UrlAsInputTest.java @@ -77,18 +77,18 @@ public void readsRealUrl() throws IOException { } @Test - public void readsHttpsUrl() { + public void readsStringUrl() { MatcherAssert.assertThat( "Can't fetch bytes from the HTTPS URL", new BytesAsText( new InputAsBytes( new UrlAsInput( // @checkstyle LineLength (1 line) - "https://raw.githubusercontent.com/yegor256/cactoos/0.5/pom.xml" + "file:src/test/resources/org/cactoos/large-text.txt" ) ) ), - new TextHasString(Matchers.containsString(" Date: Fri, 16 Jun 2017 16:22:40 -0400 Subject: [PATCH 109/169] typo fix --- src/main/java/org/cactoos/list/AllOf.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/list/AllOf.java b/src/main/java/org/cactoos/list/AllOf.java index b5cf9e184c..0e46ddd8dc 100644 --- a/src/main/java/org/cactoos/list/AllOf.java +++ b/src/main/java/org/cactoos/list/AllOf.java @@ -35,7 +35,7 @@ * *

 new AllOf(
  *   new TransformedIterable<String>(
- *     new IdentityArrayList<>("hello", "world"),
+ *     new IterableAsList<>("hello", "world"),
  *     new ProcAsFunc<>(i -> System.out.println(i))
  *   )
  * ).asValue();
From 39ac08c44c9d54bed4d468228bfe298de692230a Mon Sep 17 00:00:00 2001 From: englishman Date: Fri, 16 Jun 2017 16:35:04 -0400 Subject: [PATCH 110/169] html fix --- src/main/java/org/cactoos/list/AllOf.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/list/AllOf.java b/src/main/java/org/cactoos/list/AllOf.java index 0e46ddd8dc..6e4dc923e1 100644 --- a/src/main/java/org/cactoos/list/AllOf.java +++ b/src/main/java/org/cactoos/list/AllOf.java @@ -35,7 +35,7 @@ * *
 new AllOf(
  *   new TransformedIterable<String>(
- *     new IterableAsList<>("hello", "world"),
+ *     new IterableAsList<String>("hello", "world"),
  *     new ProcAsFunc<>(i -> System.out.println(i))
  *   )
  * ).asValue();
From faa19f7850fd0e4ef8dfb069d2ba20031070ff91 Mon Sep 17 00:00:00 2001 From: Vseslav Sekorin Date: Sat, 17 Jun 2017 01:53:16 +0300 Subject: [PATCH 111/169] Add ScalarAsLogTerm --- .../org/cactoos/func/ScalarAsLogTerm.java | 57 +++++++++++++++++++ .../org/cactoos/func/ScalarAsLogTermTest.java | 49 ++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 src/main/java/org/cactoos/func/ScalarAsLogTerm.java create mode 100644 src/test/java/org/cactoos/func/ScalarAsLogTermTest.java diff --git a/src/main/java/org/cactoos/func/ScalarAsLogTerm.java b/src/main/java/org/cactoos/func/ScalarAsLogTerm.java new file mode 100644 index 0000000000..6231125d31 --- /dev/null +++ b/src/main/java/org/cactoos/func/ScalarAsLogTerm.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.func; + +import org.cactoos.LogTerm; +import org.cactoos.Scalar; + +/** + * Boolean scalar as {@link LogTerm}. + * + *

There is no thread-safety guarantee. + * + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @since 0.5 + */ +public final class ScalarAsLogTerm implements LogTerm { + + /** + * The scalar. + */ + private final Scalar scalar; + + /** + * Ctor. + * @param scalar The scalar + */ + public ScalarAsLogTerm(final Scalar scalar) { + this.scalar = scalar; + } + + @Override + public Boolean asValue() throws Exception { + return this.scalar.asValue(); + } +} diff --git a/src/test/java/org/cactoos/func/ScalarAsLogTermTest.java b/src/test/java/org/cactoos/func/ScalarAsLogTermTest.java new file mode 100644 index 0000000000..9b484f14c2 --- /dev/null +++ b/src/test/java/org/cactoos/func/ScalarAsLogTermTest.java @@ -0,0 +1,49 @@ +/** + * 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.Scalar; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link ScalarAsLogTerm}. + * + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @since 0.5 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class ScalarAsLogTermTest { + + @Test + public void asValue() throws Exception { + final Scalar value = () -> true; + MatcherAssert.assertThat( + value.asValue(), + Matchers.equalTo(new ScalarAsLogTerm(value).asValue()) + ); + } +} From b83c5460c81061d17c67ef0824102c47f94cb5e1 Mon Sep 17 00:00:00 2001 From: alex-semenyuk Date: Mon, 19 Jun 2017 08:26:22 +0300 Subject: [PATCH 112/169] Optimize IterableAsList #157 --- .../java/org/cactoos/list/IterableAsList.java | 181 ++++++++++++++---- 1 file changed, 148 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index 12e73c26a2..d2a4a1fd98 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -23,33 +23,32 @@ */ package org.cactoos.list; -import java.util.AbstractList; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; import java.util.List; +import java.util.ListIterator; +import org.cactoos.func.StickyScalar; import org.cactoos.func.UncheckedScalar; -import org.cactoos.text.FormattedText; -import org.cactoos.text.UncheckedText; /** * Iterable as {@link List}. * *

There is no thread-safety guarantee. * + * @author Alexey Semenyuk (semenyukalexey88@gmail.com) * @author Kirill (g4s8.public@gmail.com) * @version $Id$ * @param List type * @since 0.1 */ -public final class IterableAsList extends AbstractList { +@SuppressWarnings("PMD.TooManyMethods") +public final class IterableAsList implements List { /** - * Iterable source. + * List source. */ - private final Iterable source; - - /** - * Iterable length. - */ - private final UncheckedScalar length; + private final UncheckedScalar> list; /** * Ctor. @@ -68,37 +67,153 @@ public IterableAsList(final T... array) { * @param iterable An {@link Iterable} */ public IterableAsList(final Iterable iterable) { - super(); - this.source = iterable; - this.length = new UncheckedScalar<>( - new LengthOfIterable(iterable) + this.list = new UncheckedScalar<>( + new StickyScalar<>( + () -> { + final List temp = new ArrayList<>(0); + for (final T elem : iterable) { + temp.add(elem); + } + return temp; + } + ) + ); + } + + @Override + public int size() { + return this.list.asValue().size(); + } + + @Override + public boolean isEmpty() { + return this.list.asValue().isEmpty(); + } + + @Override + public boolean contains(final Object object) { + return this.list.asValue().contains(object); + } + + @Override + public Iterator iterator() { + return this.list.asValue().iterator(); + } + + @Override + public Object[] toArray() { + return this.list.asValue().toArray(); + } + + @Override + @SuppressWarnings("PMD.UseVarargs") + public Y[] toArray(final Y[] array) { + return this.list.asValue().toArray(array); + } + + @Override + public boolean add(final T element) { + throw new UnsupportedOperationException( + "#add(final T element) is not supported" + ); + } + + @Override + public boolean remove(final Object object) { + throw new UnsupportedOperationException( + "#remove(final Object object) is not supported" + ); + } + + @Override + public boolean containsAll(final Collection collection) { + return this.list.asValue().containsAll(collection); + } + + @Override + public boolean addAll(final Collection collection) { + throw new UnsupportedOperationException( + "#addAll(final Collection collection) is not supported" + ); + } + + @Override + public boolean addAll(final int index, + final Collection collection) { + throw new UnsupportedOperationException( + "#addAll() is not supported" + ); + } + + @Override + public boolean removeAll(final Collection collection) { + throw new UnsupportedOperationException( + "#removeAll(final Collection collection) is not supported" + ); + } + + @Override + public boolean retainAll(final Collection collection) { + throw new UnsupportedOperationException( + "#retainAll(final Collection collection) is not supported" + ); + } + + @Override + public void clear() { + throw new UnsupportedOperationException( + "#clear() is not supported" ); } @Override public T get(final int index) { - int position = 0; - for (final T elem : this.source) { - if (position == index) { - return elem; - } - position += 1; - } - throw new IndexOutOfBoundsException( - new UncheckedText( - new FormattedText( - "index=%d, bounds=[%d; %d]", - index, - 0, - this.size() - ) - ).asString() + return this.list.asValue().get(index); + } + + @Override + public T set(final int index, final T element) { + throw new UnsupportedOperationException( + "#set() is not supported" ); } @Override - public int size() { - return this.length.asValue(); + public void add(final int index, final T element) { + throw new UnsupportedOperationException( + "#add(final int index, final T element) is not supported" + ); + } + + @Override + public T remove(final int index) { + throw new UnsupportedOperationException( + "#remove(final int index) is not supported" + ); } + @Override + public int indexOf(final Object object) { + return this.list.asValue().indexOf(object); + } + + @Override + public int lastIndexOf(final Object object) { + return this.list.asValue().lastIndexOf(object); + } + + @Override + public ListIterator listIterator() { + return this.list.asValue().listIterator(); + } + + @Override + public ListIterator listIterator(final int index) { + return this.list.asValue().listIterator(index); + } + + @Override + public List subList(final int fromindex, final int toindex) { + return this.list.asValue().subList(fromindex, toindex); + } } From 5771bc881e19fd56c1ab3ca05a59dee5b47b4272 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 19 Jun 2017 10:05:09 +0200 Subject: [PATCH 113/169] validate for null --- src/main/java/org/cactoos/func/RepeatedFunc.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/org/cactoos/func/RepeatedFunc.java b/src/main/java/org/cactoos/func/RepeatedFunc.java index 37f6adca59..5b2d55a4ab 100644 --- a/src/main/java/org/cactoos/func/RepeatedFunc.java +++ b/src/main/java/org/cactoos/func/RepeatedFunc.java @@ -24,6 +24,7 @@ package org.cactoos.func; import org.cactoos.Func; +import org.cactoos.text.FormattedText; /** * Func that repeats its calculation a few times before @@ -67,6 +68,14 @@ public Y apply(final X input) throws Exception { for (int idx = 0; idx < this.times; ++idx) { result = this.func.apply(input); } + if (result == null) { + throw new IllegalArgumentException( + new FormattedText( + "Repeat counter is equal or less than zero: %d", + this.times + ).asString() + ); + } return result; } From 5da7bbd6ee662653a090da58cbf63ecaf3dac548 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 19 Jun 2017 10:11:05 +0200 Subject: [PATCH 114/169] more tests --- .../java/org/cactoos/io/ResourceAsInputTest.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/test/java/org/cactoos/io/ResourceAsInputTest.java b/src/test/java/org/cactoos/io/ResourceAsInputTest.java index 6ead5ef855..cf3a7e9d25 100644 --- a/src/test/java/org/cactoos/io/ResourceAsInputTest.java +++ b/src/test/java/org/cactoos/io/ResourceAsInputTest.java @@ -23,6 +23,7 @@ */ package org.cactoos.io; +import java.io.IOException; import java.util.Arrays; import org.cactoos.text.BytesAsText; import org.hamcrest.MatcherAssert; @@ -93,4 +94,15 @@ public void readAbsentResourceTest() throws Exception { ); } + @Test(expected = IOException.class) + public void throwsWhenResourceIsAbsent() throws Exception { + new BytesAsText( + new InputAsBytes( + new ResourceAsInput( + "bar/this-resource-is-definitely-absent.txt" + ) + ) + ).asString(); + } + } From bc330ce4c368324a4dddba90672f5d9a004710e3 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 19 Jun 2017 10:13:50 +0200 Subject: [PATCH 115/169] travis fix --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 297e4b6cdb..9b13c0f7e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,8 +5,8 @@ cache: - $HOME/.m2 script: - set -e - - mvn clean install -Pqulice --errors --batch-mode - mvn clean site -Psite --errors --batch-mode + - mvn clean install -Pqulice --errors --batch-mode env: global: - MAVEN_OPTS="-Xmx256m" From 4d8ede22daca8643750d67fe87e3c00eaf5e51cc Mon Sep 17 00:00:00 2001 From: VseslavSekorin Date: Mon, 19 Jun 2017 11:36:54 +0300 Subject: [PATCH 116/169] remove LogTerm --- src/main/java/org/cactoos/LogTerm.java | 44 -------------- src/main/java/org/cactoos/func/False.java | 6 +- .../org/cactoos/func/ScalarAsLogTerm.java | 57 ------------------- src/main/java/org/cactoos/func/True.java | 6 +- src/test/java/org/cactoos/func/FalseTest.java | 2 +- .../org/cactoos/func/ScalarAsLogTermTest.java | 49 ---------------- src/test/java/org/cactoos/func/TrueTest.java | 2 +- 7 files changed, 8 insertions(+), 158 deletions(-) delete mode 100644 src/main/java/org/cactoos/LogTerm.java delete mode 100644 src/main/java/org/cactoos/func/ScalarAsLogTerm.java delete mode 100644 src/test/java/org/cactoos/func/ScalarAsLogTermTest.java diff --git a/src/main/java/org/cactoos/LogTerm.java b/src/main/java/org/cactoos/LogTerm.java deleted file mode 100644 index ca0fc60fe5..0000000000 --- a/src/main/java/org/cactoos/LogTerm.java +++ /dev/null @@ -1,44 +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; - -/** - * Logical value. - * - *

There is no thread-safety guarantee. - * - * @author Vseslav Sekorin (vssekorin@gmail.com) - * @version $Id$ - * @since 0.5 - */ -public interface LogTerm { - - /** - * Convert to boolean value. - * - * @return The value - * @throws Exception If fails - */ - Boolean asValue() throws Exception; -} diff --git a/src/main/java/org/cactoos/func/False.java b/src/main/java/org/cactoos/func/False.java index 52f23cd6e9..d70e038fdb 100644 --- a/src/main/java/org/cactoos/func/False.java +++ b/src/main/java/org/cactoos/func/False.java @@ -23,7 +23,7 @@ */ package org.cactoos.func; -import org.cactoos.LogTerm; +import org.cactoos.Scalar; /** * Logical false. @@ -32,9 +32,9 @@ * * @author Vseslav Sekorin (vssekorin@gmail.com) * @version $Id$ - * @since 0.5 + * @since 0.7 */ -public final class False implements LogTerm { +public final class False implements Scalar { @Override public Boolean asValue() throws Exception { diff --git a/src/main/java/org/cactoos/func/ScalarAsLogTerm.java b/src/main/java/org/cactoos/func/ScalarAsLogTerm.java deleted file mode 100644 index 6231125d31..0000000000 --- a/src/main/java/org/cactoos/func/ScalarAsLogTerm.java +++ /dev/null @@ -1,57 +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.func; - -import org.cactoos.LogTerm; -import org.cactoos.Scalar; - -/** - * Boolean scalar as {@link LogTerm}. - * - *

There is no thread-safety guarantee. - * - * @author Vseslav Sekorin (vssekorin@gmail.com) - * @version $Id$ - * @since 0.5 - */ -public final class ScalarAsLogTerm implements LogTerm { - - /** - * The scalar. - */ - private final Scalar scalar; - - /** - * Ctor. - * @param scalar The scalar - */ - public ScalarAsLogTerm(final Scalar scalar) { - this.scalar = scalar; - } - - @Override - public Boolean asValue() throws Exception { - return this.scalar.asValue(); - } -} diff --git a/src/main/java/org/cactoos/func/True.java b/src/main/java/org/cactoos/func/True.java index 31f29064f4..8f3584ceae 100644 --- a/src/main/java/org/cactoos/func/True.java +++ b/src/main/java/org/cactoos/func/True.java @@ -23,7 +23,7 @@ */ package org.cactoos.func; -import org.cactoos.LogTerm; +import org.cactoos.Scalar; /** * Logical truth. @@ -32,9 +32,9 @@ * * @author Vseslav Sekorin (vssekorin@gmail.com) * @version $Id$ - * @since 0.5 + * @since 0.7 */ -public final class True implements LogTerm { +public final class True implements Scalar { @Override public Boolean asValue() throws Exception { diff --git a/src/test/java/org/cactoos/func/FalseTest.java b/src/test/java/org/cactoos/func/FalseTest.java index ccc6c2bbd7..9da6bf2454 100644 --- a/src/test/java/org/cactoos/func/FalseTest.java +++ b/src/test/java/org/cactoos/func/FalseTest.java @@ -32,7 +32,7 @@ * * @author Vseslav Sekorin (vssekorin@gmail.com) * @version $Id$ - * @since 0.5 + * @since 0.7 * @checkstyle JavadocMethodCheck (500 lines) */ public final class FalseTest { diff --git a/src/test/java/org/cactoos/func/ScalarAsLogTermTest.java b/src/test/java/org/cactoos/func/ScalarAsLogTermTest.java deleted file mode 100644 index 9b484f14c2..0000000000 --- a/src/test/java/org/cactoos/func/ScalarAsLogTermTest.java +++ /dev/null @@ -1,49 +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.func; - -import org.cactoos.Scalar; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link ScalarAsLogTerm}. - * - * @author Vseslav Sekorin (vssekorin@gmail.com) - * @version $Id$ - * @since 0.5 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class ScalarAsLogTermTest { - - @Test - public void asValue() throws Exception { - final Scalar value = () -> true; - MatcherAssert.assertThat( - value.asValue(), - Matchers.equalTo(new ScalarAsLogTerm(value).asValue()) - ); - } -} diff --git a/src/test/java/org/cactoos/func/TrueTest.java b/src/test/java/org/cactoos/func/TrueTest.java index 9d49c9890a..cd446c6612 100644 --- a/src/test/java/org/cactoos/func/TrueTest.java +++ b/src/test/java/org/cactoos/func/TrueTest.java @@ -32,7 +32,7 @@ * * @author Vseslav Sekorin (vssekorin@gmail.com) * @version $Id$ - * @since 0.5 + * @since 0.7 * @checkstyle JavadocMethodCheck (500 lines) */ public final class TrueTest { From 5fcdcb667f546e900d084c4c501c7582c06957bb Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 19 Jun 2017 10:43:29 +0200 Subject: [PATCH 117/169] #176: MapAsProperties --- .../org/cactoos/list/MapAsProperties.java | 73 +++++++++++++++++++ .../org/cactoos/list/MapAsPropertiesTest.java | 60 +++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/main/java/org/cactoos/list/MapAsProperties.java create mode 100644 src/test/java/org/cactoos/list/MapAsPropertiesTest.java diff --git a/src/main/java/org/cactoos/list/MapAsProperties.java b/src/main/java/org/cactoos/list/MapAsProperties.java new file mode 100644 index 0000000000..127771f1b2 --- /dev/null +++ b/src/main/java/org/cactoos/list/MapAsProperties.java @@ -0,0 +1,73 @@ +/** + * 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.list; + +import java.util.Map; +import java.util.Properties; +import org.cactoos.Scalar; +import org.cactoos.func.StickyScalar; +import org.cactoos.func.UncheckedScalar; + +/** + * Map as {@link java.util.Properties}. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.7 + */ +public final class MapAsProperties implements Scalar { + + /** + * The result. + */ + private final UncheckedScalar result; + + /** + * Ctor. + * @param map The map with properties + */ + public MapAsProperties(final Map map) { + this.result = new UncheckedScalar<>( + new StickyScalar<>( + () -> { + final Properties props = new Properties(); + for (final Map.Entry entry : map.entrySet()) { + props.setProperty( + entry.getKey().toString(), + entry.getValue().toString() + ); + } + return props; + } + ) + ); + } + + @Override + public Properties asValue() { + return this.result.asValue(); + } +} diff --git a/src/test/java/org/cactoos/list/MapAsPropertiesTest.java b/src/test/java/org/cactoos/list/MapAsPropertiesTest.java new file mode 100644 index 0000000000..0b0c9181ee --- /dev/null +++ b/src/test/java/org/cactoos/list/MapAsPropertiesTest.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.list; + +import java.util.AbstractMap; +import java.util.Properties; +import org.cactoos.ScalarHasValue; +import org.cactoos.func.FuncAsMatcher; +import org.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test case for {@link MapAsProperties}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.7 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class MapAsPropertiesTest { + + @Test + public void convertsMapToProperties() { + MatcherAssert.assertThat( + "Can't convert map to properties", + new MapAsProperties( + new IterableAsMap( + new AbstractMap.SimpleEntry<>(0, "hello, world"), + new AbstractMap.SimpleEntry<>(1, "how are you?") + ) + ), + new ScalarHasValue<>( + new FuncAsMatcher( + props -> props.getProperty("0").endsWith(", world") + ) + ) + ); + } +} From f9b77f44945e26c4868535027dd90b96670f673e Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 19 Jun 2017 10:51:46 +0200 Subject: [PATCH 118/169] #176: renames and more ctors --- ...enatedIterable.java => ConcatIterable.java} | 10 +++++----- ...enatedIterator.java => ConcatIterator.java} | 6 +++--- .../org/cactoos/list/IterableAsBooleans.java | 2 +- .../java/org/cactoos/list/MapAsProperties.java | 18 ++++++++++++++++++ ...formedIterable.java => MappedIterable.java} | 8 ++++---- ...formedIterator.java => MappedIterator.java} | 6 +++--- src/test/java/org/cactoos/list/AllOfTest.java | 2 +- src/test/java/org/cactoos/list/AnyOfTest.java | 2 +- ...erableTest.java => ConcatIterableTest.java} | 6 +++--- ...erableTest.java => MappedIterableTest.java} | 8 ++++---- 10 files changed, 43 insertions(+), 25 deletions(-) rename src/main/java/org/cactoos/list/{ConcatenatedIterable.java => ConcatIterable.java} (87%) rename src/main/java/org/cactoos/list/{ConcatenatedIterator.java => ConcatIterator.java} (91%) rename src/main/java/org/cactoos/list/{TransformedIterable.java => MappedIterable.java} (89%) rename src/main/java/org/cactoos/list/{TransformedIterator.java => MappedIterator.java} (92%) rename src/test/java/org/cactoos/list/{ConcatenatedIterableTest.java => ConcatIterableTest.java} (93%) rename src/test/java/org/cactoos/list/{TransformedIterableTest.java => MappedIterableTest.java} (92%) diff --git a/src/main/java/org/cactoos/list/ConcatenatedIterable.java b/src/main/java/org/cactoos/list/ConcatIterable.java similarity index 87% rename from src/main/java/org/cactoos/list/ConcatenatedIterable.java rename to src/main/java/org/cactoos/list/ConcatIterable.java index f178053900..616f5f1cbf 100644 --- a/src/main/java/org/cactoos/list/ConcatenatedIterable.java +++ b/src/main/java/org/cactoos/list/ConcatIterable.java @@ -36,7 +36,7 @@ * @param Type of item * @since 0.1 */ -public final class ConcatenatedIterable implements Iterable { +public final class ConcatIterable implements Iterable { /** * Iterables. @@ -49,7 +49,7 @@ public final class ConcatenatedIterable implements Iterable { */ @SafeVarargs @SuppressWarnings("varargs") - public ConcatenatedIterable(final Iterable... items) { + public ConcatIterable(final Iterable... items) { this(new IterableAsList<>(items)); } @@ -57,14 +57,14 @@ public ConcatenatedIterable(final Iterable... items) { * Ctor. * @param items Items to concatenate */ - public ConcatenatedIterable(final Iterable> items) { + public ConcatIterable(final Iterable> items) { this.list = items; } @Override public Iterator iterator() { - return new ConcatenatedIterator<>( - new TransformedIterable<>( + return new ConcatIterator<>( + new MappedIterable<>( this.list, new StickyFunc<>(Iterable::iterator) ) diff --git a/src/main/java/org/cactoos/list/ConcatenatedIterator.java b/src/main/java/org/cactoos/list/ConcatIterator.java similarity index 91% rename from src/main/java/org/cactoos/list/ConcatenatedIterator.java rename to src/main/java/org/cactoos/list/ConcatIterator.java index 4daa30adf7..c8578749b9 100644 --- a/src/main/java/org/cactoos/list/ConcatenatedIterator.java +++ b/src/main/java/org/cactoos/list/ConcatIterator.java @@ -35,7 +35,7 @@ * @param Type of item * @since 0.1 */ -public final class ConcatenatedIterator implements Iterator { +public final class ConcatIterator implements Iterator { /** * Iterables. @@ -48,7 +48,7 @@ public final class ConcatenatedIterator implements Iterator { */ @SafeVarargs @SuppressWarnings("varargs") - public ConcatenatedIterator(final Iterator... items) { + public ConcatIterator(final Iterator... items) { this(new IterableAsList<>(items)); } @@ -56,7 +56,7 @@ public ConcatenatedIterator(final Iterator... items) { * Ctor. * @param items Items to concatenate */ - public ConcatenatedIterator(final Iterable> items) { + public ConcatIterator(final Iterable> items) { this.list = items; } diff --git a/src/main/java/org/cactoos/list/IterableAsBooleans.java b/src/main/java/org/cactoos/list/IterableAsBooleans.java index f903bfd3ec..d1a47e7e94 100644 --- a/src/main/java/org/cactoos/list/IterableAsBooleans.java +++ b/src/main/java/org/cactoos/list/IterableAsBooleans.java @@ -74,7 +74,7 @@ public IterableAsBooleans(final Iterable src, @Override public Iterator iterator() { - return new TransformedIterator<>( + return new MappedIterator<>( this.iterable.iterator(), this.func ); diff --git a/src/main/java/org/cactoos/list/MapAsProperties.java b/src/main/java/org/cactoos/list/MapAsProperties.java index 127771f1b2..a6abc60768 100644 --- a/src/main/java/org/cactoos/list/MapAsProperties.java +++ b/src/main/java/org/cactoos/list/MapAsProperties.java @@ -23,6 +23,7 @@ */ package org.cactoos.list; +import java.util.AbstractMap; import java.util.Map; import java.util.Properties; import org.cactoos.Scalar; @@ -45,6 +46,23 @@ public final class MapAsProperties implements Scalar { */ private final UncheckedScalar result; + /** + * Ctor. + * @param entries The map with properties + */ + public MapAsProperties(final Map.Entry... entries) { + this( + new IterableAsMap<>( + new MappedIterable, Map.Entry>( + new ArrayAsIterable<>(entries), + input -> new AbstractMap.SimpleEntry<>( + input.getKey().toString(), input.getValue().toString() + ) + ) + ) + ); + } + /** * Ctor. * @param map The map with properties diff --git a/src/main/java/org/cactoos/list/TransformedIterable.java b/src/main/java/org/cactoos/list/MappedIterable.java similarity index 89% rename from src/main/java/org/cactoos/list/TransformedIterable.java rename to src/main/java/org/cactoos/list/MappedIterable.java index 7c0b7cdbe4..a80bfe5652 100644 --- a/src/main/java/org/cactoos/list/TransformedIterable.java +++ b/src/main/java/org/cactoos/list/MappedIterable.java @@ -27,7 +27,7 @@ import org.cactoos.Func; /** - * Transformed iterable. + * Mapped iterable. * *

There is no thread-safety guarantee. * @@ -37,7 +37,7 @@ * @param Type of target item * @since 0.1 */ -public final class TransformedIterable implements Iterable { +public final class MappedIterable implements Iterable { /** * Iterable. @@ -54,14 +54,14 @@ public final class TransformedIterable implements Iterable { * @param src Source iterable * @param fnc Func */ - public TransformedIterable(final Iterable src, final Func fnc) { + public MappedIterable(final Iterable src, final Func fnc) { this.iterable = src; this.func = fnc; } @Override public Iterator iterator() { - return new TransformedIterator<>( + return new MappedIterator<>( this.iterable.iterator(), this.func ); } diff --git a/src/main/java/org/cactoos/list/TransformedIterator.java b/src/main/java/org/cactoos/list/MappedIterator.java similarity index 92% rename from src/main/java/org/cactoos/list/TransformedIterator.java rename to src/main/java/org/cactoos/list/MappedIterator.java index fe3424e550..155262ba78 100644 --- a/src/main/java/org/cactoos/list/TransformedIterator.java +++ b/src/main/java/org/cactoos/list/MappedIterator.java @@ -28,7 +28,7 @@ import org.cactoos.func.UncheckedFunc; /** - * Transformed iterator. + * Mapped iterator. * *

There is no thread-safety guarantee. * @@ -38,7 +38,7 @@ * @param Type of target item * @since 0.1 */ -public final class TransformedIterator implements Iterator { +public final class MappedIterator implements Iterator { /** * Iterator. @@ -55,7 +55,7 @@ public final class TransformedIterator implements Iterator { * @param src Source iterable * @param fnc Func */ - public TransformedIterator(final Iterator src, final Func fnc) { + public MappedIterator(final Iterator src, final Func fnc) { this.iterator = src; this.func = fnc; } diff --git a/src/test/java/org/cactoos/list/AllOfTest.java b/src/test/java/org/cactoos/list/AllOfTest.java index baa1f2bb96..8bef1a3c1e 100644 --- a/src/test/java/org/cactoos/list/AllOfTest.java +++ b/src/test/java/org/cactoos/list/AllOfTest.java @@ -49,7 +49,7 @@ public void iteratesList() { MatcherAssert.assertThat( "Can't iterate a list with a procedure", new AllOf( - new TransformedIterable<>( + new MappedIterable<>( new ArrayAsIterable<>("hello", "world"), list::add ) diff --git a/src/test/java/org/cactoos/list/AnyOfTest.java b/src/test/java/org/cactoos/list/AnyOfTest.java index 884478e128..6fae23a3fe 100644 --- a/src/test/java/org/cactoos/list/AnyOfTest.java +++ b/src/test/java/org/cactoos/list/AnyOfTest.java @@ -41,7 +41,7 @@ public void iteratesList() { MatcherAssert.assertThat( "Can't iterate a list", new AnyOf( - new TransformedIterable<>( + new MappedIterable<>( new ArrayAsIterable<>("a", "file", "is", "corrupt"), txt -> txt.length() > 2 ) diff --git a/src/test/java/org/cactoos/list/ConcatenatedIterableTest.java b/src/test/java/org/cactoos/list/ConcatIterableTest.java similarity index 93% rename from src/test/java/org/cactoos/list/ConcatenatedIterableTest.java rename to src/test/java/org/cactoos/list/ConcatIterableTest.java index 4cd4662c36..e7a07cc1fa 100644 --- a/src/test/java/org/cactoos/list/ConcatenatedIterableTest.java +++ b/src/test/java/org/cactoos/list/ConcatIterableTest.java @@ -28,13 +28,13 @@ import org.junit.Test; /** - * Test case for {@link ConcatenatedIterable}. + * Test case for {@link ConcatIterable}. * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @since 0.1 * @checkstyle JavadocMethodCheck (500 lines) */ -public final class ConcatenatedIterableTest { +public final class ConcatIterableTest { @Test @SuppressWarnings("unchecked") @@ -42,7 +42,7 @@ public void transformsList() { MatcherAssert.assertThat( "Can't concatenate iterables together", new LengthOfIterable( - new ConcatenatedIterable<>( + new ConcatIterable<>( new ArrayAsIterable<>("hello", "world", "друг"), new ArrayAsIterable<>("how", "are", "you"), new ArrayAsIterable<>("what's", "up") diff --git a/src/test/java/org/cactoos/list/TransformedIterableTest.java b/src/test/java/org/cactoos/list/MappedIterableTest.java similarity index 92% rename from src/test/java/org/cactoos/list/TransformedIterableTest.java rename to src/test/java/org/cactoos/list/MappedIterableTest.java index b2e8cf1153..2f0fa290e8 100644 --- a/src/test/java/org/cactoos/list/TransformedIterableTest.java +++ b/src/test/java/org/cactoos/list/MappedIterableTest.java @@ -33,19 +33,19 @@ import org.junit.Test; /** - * Test case for {@link TransformedIterable}. + * Test case for {@link MappedIterable}. * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @since 0.1 * @checkstyle JavadocMethodCheck (500 lines) */ -public final class TransformedIterableTest { +public final class MappedIterableTest { @Test public void transformsList() throws IOException { MatcherAssert.assertThat( "Can't transform an iterable", - new TransformedIterable( + new MappedIterable( new ArrayAsIterable<>( "hello", "world", "друг" ), @@ -59,7 +59,7 @@ public void transformsList() throws IOException { public void transformsEmptyList() { MatcherAssert.assertThat( "Can't transform an empty iterable", - new TransformedIterable( + new MappedIterable( Collections.emptyList(), input -> new UpperText(new StringAsText(input)) ), From 6bcdf41c977dd4f3d91ce45d35fbc037e050661e Mon Sep 17 00:00:00 2001 From: VseslavSekorin Date: Mon, 19 Jun 2017 15:01:03 +0300 Subject: [PATCH 119/169] Neg --- src/main/java/org/cactoos/func/Neg.java | 56 +++++++++++++++++++++ src/test/java/org/cactoos/func/NegTest.java | 55 ++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/main/java/org/cactoos/func/Neg.java create mode 100644 src/test/java/org/cactoos/func/NegTest.java diff --git a/src/main/java/org/cactoos/func/Neg.java b/src/main/java/org/cactoos/func/Neg.java new file mode 100644 index 0000000000..8999393d40 --- /dev/null +++ b/src/main/java/org/cactoos/func/Neg.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.func; + +import org.cactoos.Scalar; + +/** + * Negative. + * + *

There is no thread-safety guarantee. + * + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @since 0.7 + */ +public final class Neg implements Scalar { + + /** + * The origin scalar. + */ + private final Scalar origin; + + /** + * Ctor. + * @param origin The scalar + */ + public Neg(final Scalar origin) { + this.origin = origin; + } + + @Override + public Boolean asValue() throws Exception { + return !this.origin.asValue(); + } +} diff --git a/src/test/java/org/cactoos/func/NegTest.java b/src/test/java/org/cactoos/func/NegTest.java new file mode 100644 index 0000000000..0fcccc08e8 --- /dev/null +++ b/src/test/java/org/cactoos/func/NegTest.java @@ -0,0 +1,55 @@ +/** + * 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.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link Neg}. + * + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @since 0.7 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class NegTest { + + @Test + public void trueToFalse() throws Exception { + MatcherAssert.assertThat( + new Neg(new True()).asValue(), + Matchers.equalTo(new False().asValue()) + ); + } + + @Test + public void falseToTrue() throws Exception { + MatcherAssert.assertThat( + new Neg(new False()).asValue(), + Matchers.equalTo(new True().asValue()) + ); + } +} From 10521db101fde118b82ce58272c2afb8132655a0 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 19 Jun 2017 14:36:28 +0200 Subject: [PATCH 120/169] #117 ArrayAsIterable fixed --- .../org/cactoos/list/ArrayAsIterable.java | 7 +-- .../org/cactoos/list/ArrayAsIterableTest.java | 50 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/cactoos/list/ArrayAsIterableTest.java diff --git a/src/main/java/org/cactoos/list/ArrayAsIterable.java b/src/main/java/org/cactoos/list/ArrayAsIterable.java index 5ceb323499..31eee32980 100644 --- a/src/main/java/org/cactoos/list/ArrayAsIterable.java +++ b/src/main/java/org/cactoos/list/ArrayAsIterable.java @@ -25,6 +25,7 @@ import java.util.Arrays; import java.util.Iterator; +import org.cactoos.func.UncheckedScalar; /** * Array as iterable. @@ -41,7 +42,7 @@ public final class ArrayAsIterable implements Iterable { /** * The array. */ - private final X[] array; + private final UncheckedScalar> result; /** * Ctor. @@ -50,12 +51,12 @@ public final class ArrayAsIterable implements Iterable { @SafeVarargs @SuppressWarnings("varargs") public ArrayAsIterable(final X... items) { - this.array = items; + this.result = new UncheckedScalar<>(() -> Arrays.asList(items)); } @Override public Iterator iterator() { - return Arrays.asList(this.array).iterator(); + return this.result.asValue().iterator(); } } diff --git a/src/test/java/org/cactoos/list/ArrayAsIterableTest.java b/src/test/java/org/cactoos/list/ArrayAsIterableTest.java new file mode 100644 index 0000000000..2e5ea64713 --- /dev/null +++ b/src/test/java/org/cactoos/list/ArrayAsIterableTest.java @@ -0,0 +1,50 @@ +/** + * 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.list; + +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link ArrayAsIterable}. + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.7 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class ArrayAsIterableTest { + + @Test + public void convertsArrayToIterable() { + MatcherAssert.assertThat( + "Can't convert array to iterable", + new ArrayAsIterable<>( + "hello", "world" + ), + Matchers.iterableWithSize(2) + ); + } + +} From f755c1f0098c10cd9321552247c744d98ab8404c Mon Sep 17 00:00:00 2001 From: VseslavSekorin Date: Mon, 19 Jun 2017 16:29:52 +0300 Subject: [PATCH 121/169] Ternare with test --- src/main/java/org/cactoos/func/Ternary.java | 81 +++++++++++++++++++ .../java/org/cactoos/func/TernaryTest.java | 64 +++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 src/main/java/org/cactoos/func/Ternary.java create mode 100644 src/test/java/org/cactoos/func/TernaryTest.java diff --git a/src/main/java/org/cactoos/func/Ternary.java b/src/main/java/org/cactoos/func/Ternary.java new file mode 100644 index 0000000000..31731f0478 --- /dev/null +++ b/src/main/java/org/cactoos/func/Ternary.java @@ -0,0 +1,81 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.func; + +import org.cactoos.Scalar; + +/** + * Ternary operation. + * + *

There is no thread-safety guarantee. + * + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @param Type of item. + * @since 0.7 + */ +public final class Ternary implements Scalar { + + /** + * The condition. + */ + private final Scalar condition; + + /** + * The consequent. + */ + private final T cons; + + /** + * The alternative. + */ + private final T alter; + + /** + * Ctor. + * @param condition The condition + * @param cons The consequent + * @param alter The alternative + */ + public Ternary( + final Scalar condition, + final T cons, + final T alter + ) { + this.condition = condition; + this.cons = cons; + this.alter = alter; + } + + @Override + public T asValue() throws Exception { + final T result; + if (this.condition.asValue()) { + result = this.cons; + } else { + result = this.alter; + } + return result; + } +} diff --git a/src/test/java/org/cactoos/func/TernaryTest.java b/src/test/java/org/cactoos/func/TernaryTest.java new file mode 100644 index 0000000000..ea6d656977 --- /dev/null +++ b/src/test/java/org/cactoos/func/TernaryTest.java @@ -0,0 +1,64 @@ +/** + * 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.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link Ternary}. + * + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @since 0.7 + * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) + */ +public final class TernaryTest { + + @Test + public void conditionTrue() throws Exception { + MatcherAssert.assertThat( + new Ternary<>( + new True(), + 6, + 16 + ).asValue(), + Matchers.equalTo(6) + ); + } + + @Test + public void conditionFalse() throws Exception { + MatcherAssert.assertThat( + new Ternary<>( + new False(), + 6, + 16 + ).asValue(), + Matchers.equalTo(16) + ); + } +} From 4e55966f1bf07d1d448142d152b2504928718ef6 Mon Sep 17 00:00:00 2001 From: VseslavSekorin Date: Mon, 19 Jun 2017 16:47:03 +0300 Subject: [PATCH 122/169] Update @since --- src/main/java/org/cactoos/func/Ternary.java | 2 +- src/test/java/org/cactoos/func/TernaryTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/func/Ternary.java b/src/main/java/org/cactoos/func/Ternary.java index 31731f0478..4a8ed9c153 100644 --- a/src/main/java/org/cactoos/func/Ternary.java +++ b/src/main/java/org/cactoos/func/Ternary.java @@ -33,7 +33,7 @@ * @author Vseslav Sekorin (vssekorin@gmail.com) * @version $Id$ * @param Type of item. - * @since 0.7 + * @since 0.8 */ public final class Ternary implements Scalar { diff --git a/src/test/java/org/cactoos/func/TernaryTest.java b/src/test/java/org/cactoos/func/TernaryTest.java index ea6d656977..873c7b92d3 100644 --- a/src/test/java/org/cactoos/func/TernaryTest.java +++ b/src/test/java/org/cactoos/func/TernaryTest.java @@ -32,7 +32,7 @@ * * @author Vseslav Sekorin (vssekorin@gmail.com) * @version $Id$ - * @since 0.7 + * @since 0.8 * @checkstyle JavadocMethodCheck (500 lines) * @checkstyle MagicNumberCheck (500 lines) */ From cee6b2d8e71098606507a091823d6badeecb132d Mon Sep 17 00:00:00 2001 From: yegor256 Date: Mon, 19 Jun 2017 16:43:52 +0200 Subject: [PATCH 123/169] #180: StickyIterable --- .../org/cactoos/list/ArrayAsIterable.java | 7 +-- .../java/org/cactoos/list/StickyIterable.java | 59 +++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/cactoos/list/StickyIterable.java diff --git a/src/main/java/org/cactoos/list/ArrayAsIterable.java b/src/main/java/org/cactoos/list/ArrayAsIterable.java index 31eee32980..5ceb323499 100644 --- a/src/main/java/org/cactoos/list/ArrayAsIterable.java +++ b/src/main/java/org/cactoos/list/ArrayAsIterable.java @@ -25,7 +25,6 @@ import java.util.Arrays; import java.util.Iterator; -import org.cactoos.func.UncheckedScalar; /** * Array as iterable. @@ -42,7 +41,7 @@ public final class ArrayAsIterable implements Iterable { /** * The array. */ - private final UncheckedScalar> result; + private final X[] array; /** * Ctor. @@ -51,12 +50,12 @@ public final class ArrayAsIterable implements Iterable { @SafeVarargs @SuppressWarnings("varargs") public ArrayAsIterable(final X... items) { - this.result = new UncheckedScalar<>(() -> Arrays.asList(items)); + this.array = items; } @Override public Iterator iterator() { - return this.result.asValue().iterator(); + return Arrays.asList(this.array).iterator(); } } diff --git a/src/main/java/org/cactoos/list/StickyIterable.java b/src/main/java/org/cactoos/list/StickyIterable.java new file mode 100644 index 0000000000..7584a8455f --- /dev/null +++ b/src/main/java/org/cactoos/list/StickyIterable.java @@ -0,0 +1,59 @@ +/** + * 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.list; + +import java.util.Iterator; +import org.cactoos.func.UncheckedScalar; + +/** + * Iterable that returns an iterator only once. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of item + * @since 0.1 + */ +public final class StickyIterable implements Iterable { + + /** + * The cache. + */ + private final UncheckedScalar> cache; + + /** + * Ctor. + * @param iterable The iterable + */ + public StickyIterable(final Iterable iterable) { + this.cache = new UncheckedScalar<>(iterable::iterator); + } + + @Override + public Iterator iterator() { + return this.cache.asValue(); + } + +} From 97b6ae28b9274af324a3ce0ce6d8f24f1fa3be86 Mon Sep 17 00:00:00 2001 From: Ilia Date: Mon, 19 Jun 2017 20:05:24 +0300 Subject: [PATCH 124/169] Fix bug implementation an Iterating Adapter Update version --- .../java/org/cactoos/list/CycledIterable.java | 2 +- .../java/org/cactoos/list/CycledIterator.java | 17 +++++++---------- .../org/cactoos/list/CycledIterableTest.java | 2 +- .../org/cactoos/list/CycledIteratorTest.java | 2 +- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/cactoos/list/CycledIterable.java b/src/main/java/org/cactoos/list/CycledIterable.java index d89c6a93ee..ede1f74621 100644 --- a/src/main/java/org/cactoos/list/CycledIterable.java +++ b/src/main/java/org/cactoos/list/CycledIterable.java @@ -34,7 +34,7 @@ * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) * @version $Id$ * @param Type of item - * @since 0.7 + * @since 0.8 */ public final class CycledIterable implements Iterable { diff --git a/src/main/java/org/cactoos/list/CycledIterator.java b/src/main/java/org/cactoos/list/CycledIterator.java index 64132acb34..81580bb528 100644 --- a/src/main/java/org/cactoos/list/CycledIterator.java +++ b/src/main/java/org/cactoos/list/CycledIterator.java @@ -35,7 +35,7 @@ * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) * @version $Id$ * @param Type of item - * @since 0.7 + * @since 0.8 */ public final class CycledIterator implements Iterator { @@ -59,19 +59,16 @@ public CycledIterator(final Iterable iterable) { @Override public boolean hasNext() { - return this.iterable.iterator().hasNext(); + if (this.iterator == null || !this.iterator.hasNext()) { + this.iterator = this.iterable.iterator(); + } + return this.iterator.hasNext(); } @Override public T next() { - if (this.iterator == null) { - this.iterator = this.iterable.iterator(); - } - if (!this.iterator.hasNext()) { - this.iterator = this.iterable.iterator(); - if (!this.iterator.hasNext()) { - throw new NoSuchElementException(); - } + if (!this.hasNext()) { + throw new NoSuchElementException(); } return this.iterator.next(); } diff --git a/src/test/java/org/cactoos/list/CycledIterableTest.java b/src/test/java/org/cactoos/list/CycledIterableTest.java index 481ec29e40..733b137f70 100644 --- a/src/test/java/org/cactoos/list/CycledIterableTest.java +++ b/src/test/java/org/cactoos/list/CycledIterableTest.java @@ -31,7 +31,7 @@ * Test Case for {@link CycledIterable}. * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) * @version $Id$ - * @since 0.7 + * @since 0.8 * @checkstyle JavadocMethodCheck (500 lines) */ public final class CycledIterableTest { diff --git a/src/test/java/org/cactoos/list/CycledIteratorTest.java b/src/test/java/org/cactoos/list/CycledIteratorTest.java index 1a5fcdd28a..406ac03e39 100644 --- a/src/test/java/org/cactoos/list/CycledIteratorTest.java +++ b/src/test/java/org/cactoos/list/CycledIteratorTest.java @@ -31,7 +31,7 @@ * Test Case for {@link CycledIterator}. * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) * @version $Id$ - * @since 0.7 + * @since 0.8 * @checkstyle JavadocMethodCheck (500 lines) */ public final class CycledIteratorTest { From 9b4868a85012f5ead9969186e9c03398e4b04390 Mon Sep 17 00:00:00 2001 From: Ilia Date: Mon, 19 Jun 2017 20:27:18 +0300 Subject: [PATCH 125/169] Added missing unit tests --- .../org/cactoos/list/CycledIterableTest.java | 16 +++++++++++++++- .../org/cactoos/list/CycledIteratorTest.java | 9 +++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/cactoos/list/CycledIterableTest.java b/src/test/java/org/cactoos/list/CycledIterableTest.java index 733b137f70..503d471f03 100644 --- a/src/test/java/org/cactoos/list/CycledIterableTest.java +++ b/src/test/java/org/cactoos/list/CycledIterableTest.java @@ -23,6 +23,7 @@ */ package org.cactoos.list; +import java.util.Collections; import org.cactoos.ScalarHasValue; import org.hamcrest.MatcherAssert; import org.junit.Test; @@ -47,7 +48,7 @@ public void repeatIterableTest() throws Exception { "one", expected, "three" ) ), - // @checkstyle MagicNumberCheck (1 line) + // @checkstyle MagicNumberCheck (1 line)< 7 ), new ScalarHasValue<>( @@ -55,4 +56,17 @@ public void repeatIterableTest() throws Exception { ) ); } + + @Test() + public void notCycledEmptyTest() throws Exception { + MatcherAssert.assertThat( + "Can't generate an empty iterable", + new LengthOfIterable( + new CycledIterable<>( + Collections::emptyIterator + ) + ), + new ScalarHasValue<>(0) + ); + } } diff --git a/src/test/java/org/cactoos/list/CycledIteratorTest.java b/src/test/java/org/cactoos/list/CycledIteratorTest.java index 406ac03e39..e60efa0763 100644 --- a/src/test/java/org/cactoos/list/CycledIteratorTest.java +++ b/src/test/java/org/cactoos/list/CycledIteratorTest.java @@ -23,6 +23,8 @@ */ package org.cactoos.list; +import java.util.Collections; +import java.util.NoSuchElementException; import org.cactoos.ScalarHasValue; import org.hamcrest.MatcherAssert; import org.junit.Test; @@ -55,4 +57,11 @@ public void repeatIteratorTest() throws Exception { ) ); } + + @Test(expected = NoSuchElementException.class) + public void notCycledEmptyTest() throws Exception { + new CycledIterator<>( + Collections::emptyIterator + ).next(); + } } From 48bc2ec4fc8d6b4c745fe411e23f28d5f9c03af8 Mon Sep 17 00:00:00 2001 From: Ilia Date: Mon, 19 Jun 2017 22:10:11 +0300 Subject: [PATCH 126/169] Introduced SkippedIterable and SkippedIterator --- .../org/cactoos/list/SkippedIterable.java | 64 +++++++++++++++ .../org/cactoos/list/SkippedIterator.java | 79 +++++++++++++++++++ .../org/cactoos/list/SkippedIterableTest.java | 57 +++++++++++++ .../org/cactoos/list/SkippedIteratorTest.java | 67 ++++++++++++++++ 4 files changed, 267 insertions(+) create mode 100644 src/main/java/org/cactoos/list/SkippedIterable.java create mode 100644 src/main/java/org/cactoos/list/SkippedIterator.java create mode 100644 src/test/java/org/cactoos/list/SkippedIterableTest.java create mode 100644 src/test/java/org/cactoos/list/SkippedIteratorTest.java diff --git a/src/main/java/org/cactoos/list/SkippedIterable.java b/src/main/java/org/cactoos/list/SkippedIterable.java new file mode 100644 index 0000000000..79c55ef92e --- /dev/null +++ b/src/main/java/org/cactoos/list/SkippedIterable.java @@ -0,0 +1,64 @@ +/** + * 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.list; + +import java.util.Iterator; + +/** + * Skipped iterable. + * + *

There is no thread-safety guarantee.

+ * + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @param Element type + * @since 0.8 + */ +public final class SkippedIterable implements Iterable { + + /** + * Decorated iterable. + */ + private final Iterable iterable; + + /** + * Count skip elements. + */ + private final int skip; + + /** + * Ctor. + * @param iterable Decorated iterable + * @param skip Count skip elements + */ + public SkippedIterable(final Iterable iterable, final int skip) { + this.iterable = iterable; + this.skip = skip; + } + + @Override + public Iterator iterator() { + return new SkippedIterator<>(this.iterable.iterator(), this.skip); + } +} diff --git a/src/main/java/org/cactoos/list/SkippedIterator.java b/src/main/java/org/cactoos/list/SkippedIterator.java new file mode 100644 index 0000000000..7a8f26f052 --- /dev/null +++ b/src/main/java/org/cactoos/list/SkippedIterator.java @@ -0,0 +1,79 @@ +/** + * 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.list; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +/** + * Skipped iterator. + * + *

There is no thread-safety guarantee.

+ * + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @param Element type + * @since 0.8 + */ +public final class SkippedIterator implements Iterator { + + /** + * Decorated iterator. + */ + private final Iterator iterator; + + /** + * Count skip elements. + */ + private int skip; + + /** + * Ctor. + * @param iterator Decorated iterator + * @param skip Count skip elements + */ + public SkippedIterator(final Iterator iterator, final int skip) { + this.iterator = iterator; + this.skip = skip; + } + + @Override + public boolean hasNext() { + if (this.skip > 0) { + while (this.skip > 0 && this.iterator.hasNext()) { + this.iterator.next(); + --this.skip; + } + } + return this.iterator.hasNext(); + } + + @Override + public T next() { + if (!this.hasNext()) { + throw new NoSuchElementException(); + } + return this.iterator.next(); + } +} diff --git a/src/test/java/org/cactoos/list/SkippedIterableTest.java b/src/test/java/org/cactoos/list/SkippedIterableTest.java new file mode 100644 index 0000000000..be13d8ce58 --- /dev/null +++ b/src/test/java/org/cactoos/list/SkippedIterableTest.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.list; + +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test Case for {@link SkippedIterable}. + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.8 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class SkippedIterableTest { + + @Test + @SuppressWarnings("PMD.AvoidDuplicateLiterals") + public void skipIterable() throws Exception { + MatcherAssert.assertThat( + "Can't skip elements in iterable", + new SkippedIterable<>( + new ArrayAsIterable<>( + "one", "two", "three", "four" + ), + 2 + ), + Matchers.contains( + "three", + "four" + ) + ); + } + +} diff --git a/src/test/java/org/cactoos/list/SkippedIteratorTest.java b/src/test/java/org/cactoos/list/SkippedIteratorTest.java new file mode 100644 index 0000000000..1bbd485af6 --- /dev/null +++ b/src/test/java/org/cactoos/list/SkippedIteratorTest.java @@ -0,0 +1,67 @@ +/** + * 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.list; + +import java.util.NoSuchElementException; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test Case for {@link SkippedIterator}. + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.8 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class SkippedIteratorTest { + + @Test + @SuppressWarnings("PMD.AvoidDuplicateLiterals") + public void skipIterator() throws Exception { + MatcherAssert.assertThat( + "Can't skip elements in iterator", + () -> new SkippedIterator<>( + new ArrayAsIterable<>( + "one", "two", "three", "four" + ).iterator(), + 2 + ), + Matchers.contains( + "three", + "four" + ) + ); + } + + @Test(expected = NoSuchElementException.class) + public void errorSkippedMoreThanExists() throws Exception { + new SkippedIterator<>( + new ArrayAsIterable<>( + "one", "two" + ).iterator(), + 2 + ).next(); + } +} From d3d07da16446b384fd5897e8db03b95b6cf9ab8b Mon Sep 17 00:00:00 2001 From: Ilia Date: Mon, 19 Jun 2017 23:22:51 +0300 Subject: [PATCH 127/169] Extra check deleted --- src/main/java/org/cactoos/list/SkippedIterator.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/cactoos/list/SkippedIterator.java b/src/main/java/org/cactoos/list/SkippedIterator.java index 7a8f26f052..4774f1038e 100644 --- a/src/main/java/org/cactoos/list/SkippedIterator.java +++ b/src/main/java/org/cactoos/list/SkippedIterator.java @@ -60,11 +60,9 @@ public SkippedIterator(final Iterator iterator, final int skip) { @Override public boolean hasNext() { - if (this.skip > 0) { - while (this.skip > 0 && this.iterator.hasNext()) { - this.iterator.next(); - --this.skip; - } + while (this.skip > 0 && this.iterator.hasNext()) { + this.iterator.next(); + --this.skip; } return this.iterator.hasNext(); } From 20e567b18cb27abc118bdcdfff41985a9082f206 Mon Sep 17 00:00:00 2001 From: Vseslav Sekorin Date: Tue, 20 Jun 2017 01:13:06 +0300 Subject: [PATCH 128/169] LogConj with test --- src/main/java/org/cactoos/func/LogConj.java | 77 +++++++++++++++ .../java/org/cactoos/func/LogConjTest.java | 93 +++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 src/main/java/org/cactoos/func/LogConj.java create mode 100644 src/test/java/org/cactoos/func/LogConjTest.java diff --git a/src/main/java/org/cactoos/func/LogConj.java b/src/main/java/org/cactoos/func/LogConj.java new file mode 100644 index 0000000000..ef1c7e89ae --- /dev/null +++ b/src/main/java/org/cactoos/func/LogConj.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.func; + +import java.util.Iterator; +import org.cactoos.Scalar; + +/** + * Logical conjunction. + * + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @since 0.8 + */ +public final class LogConj implements Scalar { + + /** + * The iterator. + */ + private final Iterable> iterable; + + /** + * Ctor. + * @param iterable The iterable + */ + public LogConj(final Iterable> iterable) { + this.iterable = iterable; + } + + @Override + public Boolean asValue() throws Exception { + final Iterator> iterator = this.iterable.iterator(); + return this.conjunction(iterator, true); + } + + /** + * Conjunction. + * + * @param iterator The iterator + * @param value Previous value + * @return The result + * @throws Exception If fails + */ + private Boolean conjunction( + final Iterator> iterator, + final boolean value + ) throws Exception { + final Boolean result; + if (iterator.hasNext() && value) { + result = this.conjunction(iterator, iterator.next().asValue()); + } else { + result = value; + } + return result; + } +} diff --git a/src/test/java/org/cactoos/func/LogConjTest.java b/src/test/java/org/cactoos/func/LogConjTest.java new file mode 100644 index 0000000000..5a736bcfc6 --- /dev/null +++ b/src/test/java/org/cactoos/func/LogConjTest.java @@ -0,0 +1,93 @@ +/** + * 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.Scalar; +import org.cactoos.list.ArrayAsIterable; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link LogConj}. + * + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @since 0.8 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class LogConjTest { + + @Test + public void allTrue() throws Exception { + MatcherAssert.assertThat( + new LogConj( + new ArrayAsIterable>( + new True(), + new True(), + new True() + ) + ).asValue(), + Matchers.equalTo(true) + ); + } + + @Test + public void oneFalse() throws Exception { + MatcherAssert.assertThat( + new LogConj( + new ArrayAsIterable>( + new True(), + new False(), + new True() + ) + ).asValue(), + Matchers.equalTo(false) + ); + } + + @Test + public void allFalse() throws Exception { + MatcherAssert.assertThat( + new LogConj( + new ArrayAsIterable>( + new False(), + new False(), + new False() + ) + ).asValue(), + Matchers.equalTo(false) + ); + } + + @Test + public void emptyIterator() throws Exception { + MatcherAssert.assertThat( + new LogConj( + new ArrayAsIterable>() + ).asValue(), + Matchers.equalTo(true) + ); + } +} From 346c571bba6fdb2edcb2665afb33cea72f108413 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 20 Jun 2017 07:46:23 +0200 Subject: [PATCH 129/169] #183: RetryFunc --- src/test/java/org/cactoos/func/RetryFunc.java | 97 +++++++++++++++++++ .../java/org/cactoos/func/RetryFuncTest.java | 58 +++++++++++ 2 files changed, 155 insertions(+) create mode 100644 src/test/java/org/cactoos/func/RetryFunc.java create mode 100644 src/test/java/org/cactoos/func/RetryFuncTest.java diff --git a/src/test/java/org/cactoos/func/RetryFunc.java b/src/test/java/org/cactoos/func/RetryFunc.java new file mode 100644 index 0000000000..4ed70018cf --- /dev/null +++ b/src/test/java/org/cactoos/func/RetryFunc.java @@ -0,0 +1,97 @@ +/** + * 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.Func; +import org.cactoos.text.FormattedText; + +/** + * Func that will try a few times before throwing an exception. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of input + * @param Type of output + * @since 0.8 + */ +public final class RetryFunc implements Func { + + /** + * Original func. + */ + private final Func func; + + /** + * Maximum number of attempts to make. + */ + private final int max; + + /** + * Ctor. + * @param fnc Func original + */ + public RetryFunc(final Func fnc) { + // @checkstyle MagicNumberCheck (1 line) + this(fnc, 3); + } + + /** + * Ctor. + * @param fnc Func original + * @param attempts Maximum number of attempts + */ + public RetryFunc(final Func fnc, final int attempts) { + this.func = fnc; + this.max = attempts; + } + + @Override + @SuppressWarnings("PMD.AvoidCatchingGenericException") + public Y apply(final X input) throws Exception { + int attempt = 0; + Exception error = new IllegalArgumentException( + new FormattedText( + "Maximum number of attempts is too small: %d", + this.max + ).asString() + ); + while (attempt < this.max) { + try { + return this.func.apply(input); + } catch (final InterruptedException ex) { + Thread.currentThread().interrupt(); + error = ex; + break; + // @checkstyle IllegalCatchCheck (1 line) + } catch (final Exception ex) { + error = ex; + } + ++attempt; + } + throw error; + } + +} diff --git a/src/test/java/org/cactoos/func/RetryFuncTest.java b/src/test/java/org/cactoos/func/RetryFuncTest.java new file mode 100644 index 0000000000..3afbd601cc --- /dev/null +++ b/src/test/java/org/cactoos/func/RetryFuncTest.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.func; + +import java.security.SecureRandom; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link RetryFunc}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.8 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class RetryFuncTest { + + @Test + public void runsFuncMultipleTimes() throws Exception { + MatcherAssert.assertThat( + new RetryFunc<>( + input -> { + // @checkstyle MagicNumberCheck (1 line) + if (new SecureRandom().nextDouble() > 0.3d) { + throw new IllegalArgumentException("May happen"); + } + return 0; + }, + Integer.MAX_VALUE + ).apply(true), + Matchers.equalTo(0) + ); + } + +} From 6a63b36cb7ba5ac9ecffe8209aac27b23dce6f84 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 20 Jun 2017 10:49:08 +0200 Subject: [PATCH 130/169] #180: test --- .../org/cactoos/list/IterableAsListTest.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/test/java/org/cactoos/list/IterableAsListTest.java b/src/test/java/org/cactoos/list/IterableAsListTest.java index a0f418d9f8..393b1a1d6a 100644 --- a/src/test/java/org/cactoos/list/IterableAsListTest.java +++ b/src/test/java/org/cactoos/list/IterableAsListTest.java @@ -24,6 +24,8 @@ package org.cactoos.list; import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; @@ -82,4 +84,18 @@ public void highBoundTest() throws Exception { // @checkstyle MagicNumber (1 line) new IterableAsList<>(Collections.nCopies(10, 0)).get(11); } + + @Test + public void sensesChangesInIterable() throws Exception { + final AtomicInteger size = new AtomicInteger(2); + final List list = new IterableAsList<>( + () -> Collections.nCopies(size.incrementAndGet(), 0).iterator() + ); + MatcherAssert.assertThat( + "Can't sense the changes in the underlying iterable", + list.size(), + Matchers.not(Matchers.equalTo(list.size())) + ); + } + } From a472910b6aa72443d370a21d664f363de72bce8c Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 20 Jun 2017 11:17:42 +0200 Subject: [PATCH 131/169] #180: another test --- .../java/org/cactoos/list/RepeatIterable.java | 23 ++++++++++++++++-- .../java/org/cactoos/list/RepeatIterator.java | 24 +++++++++++++++++-- .../org/cactoos/list/IterableAsMapTest.java | 23 ++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/cactoos/list/RepeatIterable.java b/src/main/java/org/cactoos/list/RepeatIterable.java index f03a57cc8d..54ec35e22b 100644 --- a/src/main/java/org/cactoos/list/RepeatIterable.java +++ b/src/main/java/org/cactoos/list/RepeatIterable.java @@ -24,6 +24,8 @@ package org.cactoos.list; import java.util.Iterator; +import org.cactoos.Scalar; +import org.cactoos.func.UncheckedScalar; /** * Repeat an element. @@ -41,7 +43,7 @@ public final class RepeatIterable implements Iterable { /** * Element to repeat. */ - private final T element; + private final UncheckedScalar element; /** * Repeat count. @@ -50,11 +52,28 @@ public final class RepeatIterable implements Iterable { /** * Ctor. - * * @param elm To repeat * @param cnt Count */ public RepeatIterable(final T elm, final int cnt) { + this(() -> elm, cnt); + } + + /** + * Ctor. + * @param elm To repeat + * @param cnt Count + */ + public RepeatIterable(final Scalar elm, final int cnt) { + this(new UncheckedScalar(elm), cnt); + } + + /** + * Ctor. + * @param elm To repeat + * @param cnt Count + */ + public RepeatIterable(final UncheckedScalar elm, final int cnt) { this.element = elm; this.count = cnt; } diff --git a/src/main/java/org/cactoos/list/RepeatIterator.java b/src/main/java/org/cactoos/list/RepeatIterator.java index 1e8a369217..4d24d8b3b2 100644 --- a/src/main/java/org/cactoos/list/RepeatIterator.java +++ b/src/main/java/org/cactoos/list/RepeatIterator.java @@ -24,6 +24,8 @@ package org.cactoos.list; import java.util.Iterator; +import org.cactoos.Scalar; +import org.cactoos.func.UncheckedScalar; /** * Repeat an element. @@ -41,7 +43,7 @@ public final class RepeatIterator implements Iterator { /** * The element to repeat. */ - private final T element; + private final UncheckedScalar element; /** * How many more repeats will happen. @@ -54,6 +56,24 @@ public final class RepeatIterator implements Iterator { * @param max How many times to repeat */ public RepeatIterator(final T elm, final int max) { + this(() -> elm, max); + } + + /** + * Ctor. + * @param elm Element to repeat + * @param max How many times to repeat + */ + public RepeatIterator(final Scalar elm, final int max) { + this(new UncheckedScalar(elm), max); + } + + /** + * Ctor. + * @param elm Element to repeat + * @param max How many times to repeat + */ + public RepeatIterator(final UncheckedScalar elm, final int max) { this.element = elm; this.left = max; } @@ -66,6 +86,6 @@ public boolean hasNext() { @Override public T next() { --this.left; - return this.element; + return this.element.asValue(); } } diff --git a/src/test/java/org/cactoos/list/IterableAsMapTest.java b/src/test/java/org/cactoos/list/IterableAsMapTest.java index 5c8ee2b1c0..4f99ff62ce 100644 --- a/src/test/java/org/cactoos/list/IterableAsMapTest.java +++ b/src/test/java/org/cactoos/list/IterableAsMapTest.java @@ -23,7 +23,10 @@ */ package org.cactoos.list; +import java.security.SecureRandom; import java.util.AbstractMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; @@ -52,4 +55,24 @@ public void convertsIterableToMap() { ) ); } + + @Test + public void sensesChangesInMap() throws Exception { + final AtomicInteger size = new AtomicInteger(2); + final Map map = new IterableAsMap<>( + () -> new RepeatIterator<>( + () -> new AbstractMap.SimpleEntry<>( + new SecureRandom().nextInt(), + 1 + ), + size.incrementAndGet() + ) + ); + MatcherAssert.assertThat( + "Can't sense the changes in the underlying map", + map.size(), + Matchers.not(Matchers.equalTo(map.size())) + ); + } + } From cc57441369df8bd1e09aa25f1db106531e167865 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 20 Jun 2017 11:19:20 +0200 Subject: [PATCH 132/169] #180: another test --- .../org/cactoos/list/MapAsPropertiesTest.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/test/java/org/cactoos/list/MapAsPropertiesTest.java b/src/test/java/org/cactoos/list/MapAsPropertiesTest.java index 0b0c9181ee..9d8323f16d 100644 --- a/src/test/java/org/cactoos/list/MapAsPropertiesTest.java +++ b/src/test/java/org/cactoos/list/MapAsPropertiesTest.java @@ -23,11 +23,14 @@ */ package org.cactoos.list; +import java.security.SecureRandom; import java.util.AbstractMap; import java.util.Properties; +import java.util.concurrent.atomic.AtomicInteger; import org.cactoos.ScalarHasValue; import org.cactoos.func.FuncAsMatcher; import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; import org.junit.Test; /** @@ -57,4 +60,26 @@ public void convertsMapToProperties() { ) ); } + + @Test + public void sensesChangesInMap() throws Exception { + final AtomicInteger size = new AtomicInteger(2); + final MapAsProperties props = new MapAsProperties( + new IterableAsMap<>( + () -> new RepeatIterator<>( + () -> new AbstractMap.SimpleEntry<>( + new SecureRandom().nextInt(), + 1 + ), + size.incrementAndGet() + ) + ) + ); + MatcherAssert.assertThat( + "Can't sense the changes in the underlying map", + props.asValue().size(), + Matchers.not(Matchers.equalTo(props.asValue().size())) + ); + } + } From 8a9fd4d77c645c7ed24bfd24e37db802d601702b Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 20 Jun 2017 11:41:27 +0200 Subject: [PATCH 133/169] #180: modified --- .../java/org/cactoos/list/IterableAsList.java | 72 ++++--- .../java/org/cactoos/list/IterableAsMap.java | 64 +++--- .../java/org/cactoos/list/StickyList.java | 200 ++++++++++++++++++ 3 files changed, 275 insertions(+), 61 deletions(-) create mode 100644 src/main/java/org/cactoos/list/StickyList.java diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index d2a4a1fd98..c36a75e088 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -23,13 +23,10 @@ */ package org.cactoos.list; -import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.ListIterator; -import org.cactoos.func.StickyScalar; -import org.cactoos.func.UncheckedScalar; /** * Iterable as {@link List}. @@ -41,14 +38,17 @@ * @version $Id$ * @param List type * @since 0.1 + * @todo #180:30min This class is not implemented fully. Most methods + * are not yet supported. Let's implement them, each time with a use + * case specific method. And let's make sure they all are tested. */ @SuppressWarnings("PMD.TooManyMethods") public final class IterableAsList implements List { /** - * List source. + * The source. */ - private final UncheckedScalar> list; + private final Iterable iterable; /** * Ctor. @@ -64,51 +64,47 @@ public IterableAsList(final T... array) { /** * Ctor. * - * @param iterable An {@link Iterable} + * @param src An {@link Iterable} */ - public IterableAsList(final Iterable iterable) { - this.list = new UncheckedScalar<>( - new StickyScalar<>( - () -> { - final List temp = new ArrayList<>(0); - for (final T elem : iterable) { - temp.add(elem); - } - return temp; - } - ) - ); + public IterableAsList(final Iterable src) { + this.iterable = src; } @Override public int size() { - return this.list.asValue().size(); + return new LengthOfIterable(this.iterable).asValue(); } @Override public boolean isEmpty() { - return this.list.asValue().isEmpty(); + return !this.iterable.iterator().hasNext(); } @Override public boolean contains(final Object object) { - return this.list.asValue().contains(object); + throw new UnsupportedOperationException( + "#contains() not implemented yet" + ); } @Override public Iterator iterator() { - return this.list.asValue().iterator(); + return this.iterable.iterator(); } @Override public Object[] toArray() { - return this.list.asValue().toArray(); + throw new UnsupportedOperationException( + "#toArray() not implemented yet" + ); } @Override @SuppressWarnings("PMD.UseVarargs") public Y[] toArray(final Y[] array) { - return this.list.asValue().toArray(array); + throw new UnsupportedOperationException( + "#toArray(array) not implemented yet" + ); } @Override @@ -127,7 +123,9 @@ public boolean remove(final Object object) { @Override public boolean containsAll(final Collection collection) { - return this.list.asValue().containsAll(collection); + throw new UnsupportedOperationException( + "#containsAll() not implemented yet" + ); } @Override @@ -168,7 +166,9 @@ public void clear() { @Override public T get(final int index) { - return this.list.asValue().get(index); + throw new UnsupportedOperationException( + "#get() not implemented yet" + ); } @Override @@ -194,26 +194,36 @@ public T remove(final int index) { @Override public int indexOf(final Object object) { - return this.list.asValue().indexOf(object); + throw new UnsupportedOperationException( + "#indexOf() not implemented yet" + ); } @Override public int lastIndexOf(final Object object) { - return this.list.asValue().lastIndexOf(object); + throw new UnsupportedOperationException( + "#lastIndexOf() not implemented yet" + ); } @Override public ListIterator listIterator() { - return this.list.asValue().listIterator(); + throw new UnsupportedOperationException( + "#listIterator() not implemented yet" + ); } @Override public ListIterator listIterator(final int index) { - return this.list.asValue().listIterator(index); + throw new UnsupportedOperationException( + "#listIterator(index) not implemented yet" + ); } @Override public List subList(final int fromindex, final int toindex) { - return this.list.asValue().subList(fromindex, toindex); + throw new UnsupportedOperationException( + "#subList() not implemented yet" + ); } } diff --git a/src/main/java/org/cactoos/list/IterableAsMap.java b/src/main/java/org/cactoos/list/IterableAsMap.java index b1f316434c..59b5070bbe 100644 --- a/src/main/java/org/cactoos/list/IterableAsMap.java +++ b/src/main/java/org/cactoos/list/IterableAsMap.java @@ -24,11 +24,9 @@ package org.cactoos.list; import java.util.Collection; -import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; -import org.cactoos.func.StickyScalar; -import org.cactoos.func.UncheckedScalar; /** * Iterable as {@link Map}. @@ -40,65 +38,65 @@ * @param Type of key * @param Type of value * @since 0.4 + * @todo #180:30min This class is not implemented fully. Most methods + * are not yet supported. Let's implement them, each time with a use + * case specific method. And let's make sure they all are tested. This + * is very similar to what we have in IterableAsList. */ public final class IterableAsMap implements Map { /** - * The map. + * The iterable. */ - private final UncheckedScalar> map; + private final Iterable> entries; /** * Ctor. - * @param entries Entries for the map + * @param list List of entries */ @SafeVarargs @SuppressWarnings("varargs") - public IterableAsMap(final Map.Entry... entries) { - this(new ArrayAsIterable<>(entries)); + public IterableAsMap(final Map.Entry... list) { + this(new ArrayAsIterable<>(list)); } /** * Ctor. - * @param entries Entries for the map + * @param list Entries for the entries */ - public IterableAsMap(final Iterable> entries) { - this.map = new UncheckedScalar<>( - new StickyScalar<>( - () -> { - final Map temp = new HashMap<>(0); - for (final Map.Entry entry : entries) { - temp.put(entry.getKey(), entry.getValue()); - } - return temp; - } - ) - ); + public IterableAsMap(final Iterable> list) { + this.entries = list; } @Override public int size() { - return this.map.asValue().size(); + return new LengthOfIterable(this.entries).asValue(); } @Override public boolean isEmpty() { - return this.map.asValue().isEmpty(); + return !this.entries.iterator().hasNext(); } @Override public boolean containsKey(final Object key) { - return this.map.asValue().containsKey(key); + throw new UnsupportedOperationException( + "#containsKey() not implemented yet" + ); } @Override public boolean containsValue(final Object value) { - return this.map.asValue().containsValue(value); + throw new UnsupportedOperationException( + "#containsValue() not implemented yet" + ); } @Override public Y get(final Object key) { - return this.map.asValue().get(key); + throw new UnsupportedOperationException( + "#get() not implemented yet" + ); } @Override @@ -116,7 +114,7 @@ public Y remove(final Object key) { } @Override - public void putAll(final Map entries) { + public void putAll(final Map list) { throw new UnsupportedOperationException( "#putAll() is not supported" ); @@ -131,17 +129,23 @@ public void clear() { @Override public Set keySet() { - return this.map.asValue().keySet(); + throw new UnsupportedOperationException( + "#keySet() not implemented yet" + ); } @Override public Collection values() { - return this.map.asValue().values(); + throw new UnsupportedOperationException( + "#values() not implemented yet" + ); } @Override public Set> entrySet() { - return this.map.asValue().entrySet(); + return new HashSet<>( + new IterableAsList<>(this.entries) + ); } } diff --git a/src/main/java/org/cactoos/list/StickyList.java b/src/main/java/org/cactoos/list/StickyList.java new file mode 100644 index 0000000000..a01b970991 --- /dev/null +++ b/src/main/java/org/cactoos/list/StickyList.java @@ -0,0 +1,200 @@ +/** + * 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.list; + +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.ListIterator; +import org.cactoos.func.UncheckedScalar; + +/** + * List decorator that goes through list only once. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of item + * @since 0.8 + */ +public final class StickyList implements List { + + /** + * The cache. + */ + private final UncheckedScalar> cache; + + /** + * Ctor. + * @param list The iterable + */ + public StickyList(final List list) { + this.cache = new UncheckedScalar<>( + () -> { + final List temp = new LinkedList<>(); + temp.addAll(list); + return temp; + } + ); + } + + @Override + public int size() { + return this.cache.asValue().size(); + } + + @Override + public boolean isEmpty() { + return this.cache.asValue().isEmpty(); + } + + @Override + public boolean contains(final Object object) { + return this.cache.asValue().contains(object); + } + + @Override + public Iterator iterator() { + return this.cache.asValue().iterator(); + } + + @Override + public Object[] toArray() { + return this.cache.asValue().toArray(); + } + + @Override + @SuppressWarnings("PMD.UseVarargs") + public Y[] toArray(final Y[] array) { + return this.cache.asValue().toArray(array); + } + + @Override + public boolean add(final X element) { + throw new UnsupportedOperationException( + "#add(final T element) is not supported" + ); + } + + @Override + public boolean remove(final Object object) { + throw new UnsupportedOperationException( + "#remove(final Object object) is not supported" + ); + } + + @Override + public boolean containsAll(final Collection collection) { + return this.cache.asValue().containsAll(collection); + } + + @Override + public boolean addAll(final Collection collection) { + throw new UnsupportedOperationException( + "#addAll(final Collection collection) is not supported" + ); + } + + @Override + public boolean addAll(final int index, + final Collection collection) { + throw new UnsupportedOperationException( + "#addAll() is not supported" + ); + } + + @Override + public boolean removeAll(final Collection collection) { + throw new UnsupportedOperationException( + "#removeAll(final Collection collection) is not supported" + ); + } + + @Override + public boolean retainAll(final Collection collection) { + throw new UnsupportedOperationException( + "#retainAll(final Collection collection) is not supported" + ); + } + + @Override + public void clear() { + throw new UnsupportedOperationException( + "#clear() is not supported" + ); + } + + @Override + public X get(final int index) { + return this.cache.asValue().get(index); + } + + @Override + public X set(final int index, final X element) { + throw new UnsupportedOperationException( + "#set() is not supported" + ); + } + + @Override + public void add(final int index, final X element) { + throw new UnsupportedOperationException( + "#add(final int index, final T element) is not supported" + ); + } + + @Override + public X remove(final int index) { + throw new UnsupportedOperationException( + "#remove(final int index) is not supported" + ); + } + + @Override + public int indexOf(final Object object) { + return this.cache.asValue().indexOf(object); + } + + @Override + public int lastIndexOf(final Object object) { + return this.cache.asValue().lastIndexOf(object); + } + + @Override + public ListIterator listIterator() { + return this.cache.asValue().listIterator(); + } + + @Override + public ListIterator listIterator(final int index) { + return this.cache.asValue().listIterator(index); + } + + @Override + public List subList(final int fromindex, final int toindex) { + return this.cache.asValue().subList(fromindex, toindex); + } +} From 1fdb74b556eeea43cbce8c3b0ec494152db7352f Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 20 Jun 2017 11:48:35 +0200 Subject: [PATCH 134/169] #180: StickyMap --- .../org/cactoos/list/MapAsProperties.java | 34 ++--- .../java/org/cactoos/list/StickyList.java | 2 +- src/main/java/org/cactoos/list/StickyMap.java | 132 ++++++++++++++++++ 3 files changed, 146 insertions(+), 22 deletions(-) create mode 100644 src/main/java/org/cactoos/list/StickyMap.java diff --git a/src/main/java/org/cactoos/list/MapAsProperties.java b/src/main/java/org/cactoos/list/MapAsProperties.java index a6abc60768..d70723b306 100644 --- a/src/main/java/org/cactoos/list/MapAsProperties.java +++ b/src/main/java/org/cactoos/list/MapAsProperties.java @@ -27,8 +27,6 @@ import java.util.Map; import java.util.Properties; import org.cactoos.Scalar; -import org.cactoos.func.StickyScalar; -import org.cactoos.func.UncheckedScalar; /** * Map as {@link java.util.Properties}. @@ -42,9 +40,9 @@ public final class MapAsProperties implements Scalar { /** - * The result. + * The map. */ - private final UncheckedScalar result; + private final Map map; /** * Ctor. @@ -65,27 +63,21 @@ public MapAsProperties(final Map.Entry... entries) { /** * Ctor. - * @param map The map with properties + * @param src The map with properties */ - public MapAsProperties(final Map map) { - this.result = new UncheckedScalar<>( - new StickyScalar<>( - () -> { - final Properties props = new Properties(); - for (final Map.Entry entry : map.entrySet()) { - props.setProperty( - entry.getKey().toString(), - entry.getValue().toString() - ); - } - return props; - } - ) - ); + public MapAsProperties(final Map src) { + this.map = src; } @Override public Properties asValue() { - return this.result.asValue(); + final Properties props = new Properties(); + for (final Map.Entry entry : this.map.entrySet()) { + props.setProperty( + entry.getKey().toString(), + entry.getValue().toString() + ); + } + return props; } } diff --git a/src/main/java/org/cactoos/list/StickyList.java b/src/main/java/org/cactoos/list/StickyList.java index a01b970991..87db2af9a7 100644 --- a/src/main/java/org/cactoos/list/StickyList.java +++ b/src/main/java/org/cactoos/list/StickyList.java @@ -31,7 +31,7 @@ import org.cactoos.func.UncheckedScalar; /** - * List decorator that goes through list only once. + * List decorator that goes through the list only once. * *

There is no thread-safety guarantee. * diff --git a/src/main/java/org/cactoos/list/StickyMap.java b/src/main/java/org/cactoos/list/StickyMap.java new file mode 100644 index 0000000000..0b84362b11 --- /dev/null +++ b/src/main/java/org/cactoos/list/StickyMap.java @@ -0,0 +1,132 @@ +/** + * 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.list; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import org.cactoos.func.UncheckedScalar; + +/** + * Map decorator that goes through the map only once. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of key + * @param Type of value + * @since 0.8 + */ +public final class StickyMap implements Map { + + /** + * The cache. + */ + private final UncheckedScalar> cache; + + /** + * Ctor. + * @param map The map + */ + public StickyMap(final Map map) { + this.cache = new UncheckedScalar<>( + () -> { + final Map temp = new HashMap<>(); + temp.putAll(map); + return temp; + } + ); + } + + @Override + public int size() { + return this.cache.asValue().size(); + } + + @Override + public boolean isEmpty() { + return this.cache.asValue().isEmpty(); + } + + @Override + public boolean containsKey(final Object key) { + return this.cache.asValue().containsKey(key); + } + + @Override + public boolean containsValue(final Object value) { + return this.cache.asValue().containsValue(value); + } + + @Override + public Y get(final Object key) { + return this.cache.asValue().get(key); + } + + @Override + public Y put(final X key, final Y value) { + throw new UnsupportedOperationException( + "#put() is not supported" + ); + } + + @Override + public Y remove(final Object key) { + throw new UnsupportedOperationException( + "#remove() is not supported" + ); + } + + @Override + public void putAll(final Map map) { + throw new UnsupportedOperationException( + "#putAll() is not supported" + ); + } + + @Override + public void clear() { + throw new UnsupportedOperationException( + "#clear() is not supported" + ); + } + + @Override + public Set keySet() { + return this.cache.asValue().keySet(); + } + + @Override + public Collection values() { + return this.cache.asValue().values(); + } + + @Override + public Set> entrySet() { + return this.cache.asValue().entrySet(); + } + +} From 445c22f50c5c29f75c96a59b36af93eb0309052b Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 20 Jun 2017 12:22:05 +0200 Subject: [PATCH 135/169] #180: implemented --- .../java/org/cactoos/list/IterableAsList.java | 56 ++++++++----------- .../java/org/cactoos/list/IterableAsMap.java | 43 +++++++------- .../java/org/cactoos/list/StickyList.java | 33 +++++------ src/main/java/org/cactoos/list/StickyMap.java | 22 ++++---- .../org/cactoos/list/MapAsPropertiesTest.java | 1 + 5 files changed, 72 insertions(+), 83 deletions(-) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index c36a75e088..8fc492b79b 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -25,6 +25,7 @@ import java.util.Collection; import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.ListIterator; @@ -38,9 +39,6 @@ * @version $Id$ * @param List type * @since 0.1 - * @todo #180:30min This class is not implemented fully. Most methods - * are not yet supported. Let's implement them, each time with a use - * case specific method. And let's make sure they all are tested. */ @SuppressWarnings("PMD.TooManyMethods") public final class IterableAsList implements List { @@ -82,9 +80,7 @@ public boolean isEmpty() { @Override public boolean contains(final Object object) { - throw new UnsupportedOperationException( - "#contains() not implemented yet" - ); + return this.list().contains(object); } @Override @@ -94,17 +90,13 @@ public Iterator iterator() { @Override public Object[] toArray() { - throw new UnsupportedOperationException( - "#toArray() not implemented yet" - ); + return this.list().toArray(); } @Override @SuppressWarnings("PMD.UseVarargs") public Y[] toArray(final Y[] array) { - throw new UnsupportedOperationException( - "#toArray(array) not implemented yet" - ); + return this.list().toArray(array); } @Override @@ -123,9 +115,7 @@ public boolean remove(final Object object) { @Override public boolean containsAll(final Collection collection) { - throw new UnsupportedOperationException( - "#containsAll() not implemented yet" - ); + return this.list().containsAll(collection); } @Override @@ -166,9 +156,7 @@ public void clear() { @Override public T get(final int index) { - throw new UnsupportedOperationException( - "#get() not implemented yet" - ); + return this.list().get(index); } @Override @@ -194,36 +182,38 @@ public T remove(final int index) { @Override public int indexOf(final Object object) { - throw new UnsupportedOperationException( - "#indexOf() not implemented yet" - ); + return this.list().indexOf(object); } @Override public int lastIndexOf(final Object object) { - throw new UnsupportedOperationException( - "#lastIndexOf() not implemented yet" - ); + return this.list().lastIndexOf(object); } @Override public ListIterator listIterator() { - throw new UnsupportedOperationException( - "#listIterator() not implemented yet" - ); + return this.list().listIterator(); } @Override public ListIterator listIterator(final int index) { - throw new UnsupportedOperationException( - "#listIterator(index) not implemented yet" - ); + return this.list().listIterator(index); } @Override public List subList(final int fromindex, final int toindex) { - throw new UnsupportedOperationException( - "#subList() not implemented yet" - ); + return this.list().subList(fromindex, toindex); + } + + /** + * Build a list. + * @return List + */ + private List list() { + final List list = new LinkedList<>(); + for (final T item : this.iterable) { + list.add(item); + } + return list; } } diff --git a/src/main/java/org/cactoos/list/IterableAsMap.java b/src/main/java/org/cactoos/list/IterableAsMap.java index 59b5070bbe..87bceb2cd4 100644 --- a/src/main/java/org/cactoos/list/IterableAsMap.java +++ b/src/main/java/org/cactoos/list/IterableAsMap.java @@ -24,7 +24,7 @@ package org.cactoos.list; import java.util.Collection; -import java.util.HashSet; +import java.util.HashMap; import java.util.Map; import java.util.Set; @@ -38,11 +38,8 @@ * @param Type of key * @param Type of value * @since 0.4 - * @todo #180:30min This class is not implemented fully. Most methods - * are not yet supported. Let's implement them, each time with a use - * case specific method. And let's make sure they all are tested. This - * is very similar to what we have in IterableAsList. */ +@SuppressWarnings("PMD.TooManyMethods") public final class IterableAsMap implements Map { /** @@ -80,23 +77,17 @@ public boolean isEmpty() { @Override public boolean containsKey(final Object key) { - throw new UnsupportedOperationException( - "#containsKey() not implemented yet" - ); + return this.map().containsKey(key); } @Override public boolean containsValue(final Object value) { - throw new UnsupportedOperationException( - "#containsValue() not implemented yet" - ); + return this.map().containsValue(value); } @Override public Y get(final Object key) { - throw new UnsupportedOperationException( - "#get() not implemented yet" - ); + return this.map().get(key); } @Override @@ -129,23 +120,29 @@ public void clear() { @Override public Set keySet() { - throw new UnsupportedOperationException( - "#keySet() not implemented yet" - ); + return this.map().keySet(); } @Override public Collection values() { - throw new UnsupportedOperationException( - "#values() not implemented yet" - ); + return this.map().values(); } @Override public Set> entrySet() { - return new HashSet<>( - new IterableAsList<>(this.entries) - ); + return this.map().entrySet(); + } + + /** + * Make a map. + * @return Map + */ + private Map map() { + final Map temp = new HashMap<>(0); + for (final Map.Entry entry : this.entries) { + temp.put(entry.getKey(), entry.getValue()); + } + return temp; } } diff --git a/src/main/java/org/cactoos/list/StickyList.java b/src/main/java/org/cactoos/list/StickyList.java index 87db2af9a7..7977a25b3f 100644 --- a/src/main/java/org/cactoos/list/StickyList.java +++ b/src/main/java/org/cactoos/list/StickyList.java @@ -40,19 +40,20 @@ * @param Type of item * @since 0.8 */ +@SuppressWarnings("PMD.TooManyMethods") public final class StickyList implements List { /** - * The cache. + * The gate. */ - private final UncheckedScalar> cache; + private final UncheckedScalar> gate; /** * Ctor. * @param list The iterable */ public StickyList(final List list) { - this.cache = new UncheckedScalar<>( + this.gate = new UncheckedScalar<>( () -> { final List temp = new LinkedList<>(); temp.addAll(list); @@ -63,33 +64,33 @@ public StickyList(final List list) { @Override public int size() { - return this.cache.asValue().size(); + return this.gate.asValue().size(); } @Override public boolean isEmpty() { - return this.cache.asValue().isEmpty(); + return this.gate.asValue().isEmpty(); } @Override public boolean contains(final Object object) { - return this.cache.asValue().contains(object); + return this.gate.asValue().contains(object); } @Override public Iterator iterator() { - return this.cache.asValue().iterator(); + return this.gate.asValue().iterator(); } @Override public Object[] toArray() { - return this.cache.asValue().toArray(); + return this.gate.asValue().toArray(); } @Override @SuppressWarnings("PMD.UseVarargs") public Y[] toArray(final Y[] array) { - return this.cache.asValue().toArray(array); + return this.gate.asValue().toArray(array); } @Override @@ -108,7 +109,7 @@ public boolean remove(final Object object) { @Override public boolean containsAll(final Collection collection) { - return this.cache.asValue().containsAll(collection); + return this.gate.asValue().containsAll(collection); } @Override @@ -149,7 +150,7 @@ public void clear() { @Override public X get(final int index) { - return this.cache.asValue().get(index); + return this.gate.asValue().get(index); } @Override @@ -175,26 +176,26 @@ public X remove(final int index) { @Override public int indexOf(final Object object) { - return this.cache.asValue().indexOf(object); + return this.gate.asValue().indexOf(object); } @Override public int lastIndexOf(final Object object) { - return this.cache.asValue().lastIndexOf(object); + return this.gate.asValue().lastIndexOf(object); } @Override public ListIterator listIterator() { - return this.cache.asValue().listIterator(); + return this.gate.asValue().listIterator(); } @Override public ListIterator listIterator(final int index) { - return this.cache.asValue().listIterator(index); + return this.gate.asValue().listIterator(index); } @Override public List subList(final int fromindex, final int toindex) { - return this.cache.asValue().subList(fromindex, toindex); + return this.gate.asValue().subList(fromindex, toindex); } } diff --git a/src/main/java/org/cactoos/list/StickyMap.java b/src/main/java/org/cactoos/list/StickyMap.java index 0b84362b11..f8ba6f49f4 100644 --- a/src/main/java/org/cactoos/list/StickyMap.java +++ b/src/main/java/org/cactoos/list/StickyMap.java @@ -43,16 +43,16 @@ public final class StickyMap implements Map { /** - * The cache. + * The gate. */ - private final UncheckedScalar> cache; + private final UncheckedScalar> gate; /** * Ctor. * @param map The map */ public StickyMap(final Map map) { - this.cache = new UncheckedScalar<>( + this.gate = new UncheckedScalar<>( () -> { final Map temp = new HashMap<>(); temp.putAll(map); @@ -63,27 +63,27 @@ public StickyMap(final Map map) { @Override public int size() { - return this.cache.asValue().size(); + return this.gate.asValue().size(); } @Override public boolean isEmpty() { - return this.cache.asValue().isEmpty(); + return this.gate.asValue().isEmpty(); } @Override public boolean containsKey(final Object key) { - return this.cache.asValue().containsKey(key); + return this.gate.asValue().containsKey(key); } @Override public boolean containsValue(final Object value) { - return this.cache.asValue().containsValue(value); + return this.gate.asValue().containsValue(value); } @Override public Y get(final Object key) { - return this.cache.asValue().get(key); + return this.gate.asValue().get(key); } @Override @@ -116,17 +116,17 @@ public void clear() { @Override public Set keySet() { - return this.cache.asValue().keySet(); + return this.gate.asValue().keySet(); } @Override public Collection values() { - return this.cache.asValue().values(); + return this.gate.asValue().values(); } @Override public Set> entrySet() { - return this.cache.asValue().entrySet(); + return this.gate.asValue().entrySet(); } } diff --git a/src/test/java/org/cactoos/list/MapAsPropertiesTest.java b/src/test/java/org/cactoos/list/MapAsPropertiesTest.java index 9d8323f16d..1f4c9bbef4 100644 --- a/src/test/java/org/cactoos/list/MapAsPropertiesTest.java +++ b/src/test/java/org/cactoos/list/MapAsPropertiesTest.java @@ -40,6 +40,7 @@ * @version $Id$ * @since 0.7 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ public final class MapAsPropertiesTest { From ebc18dd72fe4a954391288520d85d3b997346d53 Mon Sep 17 00:00:00 2001 From: VseslavSekorin Date: Tue, 20 Jun 2017 13:26:08 +0300 Subject: [PATCH 136/169] Rename: LogConj -> And --- .../java/org/cactoos/func/{LogConj.java => And.java} | 6 ++++-- .../cactoos/func/{LogConjTest.java => AndTest.java} | 12 ++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) rename src/main/java/org/cactoos/func/{LogConj.java => And.java} (93%) rename src/test/java/org/cactoos/func/{LogConjTest.java => AndTest.java} (94%) diff --git a/src/main/java/org/cactoos/func/LogConj.java b/src/main/java/org/cactoos/func/And.java similarity index 93% rename from src/main/java/org/cactoos/func/LogConj.java rename to src/main/java/org/cactoos/func/And.java index ef1c7e89ae..37d947dcbc 100644 --- a/src/main/java/org/cactoos/func/LogConj.java +++ b/src/main/java/org/cactoos/func/And.java @@ -29,11 +29,13 @@ /** * Logical conjunction. * + *

There is no thread-safety guarantee. + * * @author Vseslav Sekorin (vssekorin@gmail.com) * @version $Id$ * @since 0.8 */ -public final class LogConj implements Scalar { +public final class And implements Scalar { /** * The iterator. @@ -44,7 +46,7 @@ public final class LogConj implements Scalar { * Ctor. * @param iterable The iterable */ - public LogConj(final Iterable> iterable) { + public And(final Iterable> iterable) { this.iterable = iterable; } diff --git a/src/test/java/org/cactoos/func/LogConjTest.java b/src/test/java/org/cactoos/func/AndTest.java similarity index 94% rename from src/test/java/org/cactoos/func/LogConjTest.java rename to src/test/java/org/cactoos/func/AndTest.java index 5a736bcfc6..8f842a3f86 100644 --- a/src/test/java/org/cactoos/func/LogConjTest.java +++ b/src/test/java/org/cactoos/func/AndTest.java @@ -30,19 +30,19 @@ import org.junit.Test; /** - * Test case for {@link LogConj}. + * Test case for {@link And}. * * @author Vseslav Sekorin (vssekorin@gmail.com) * @version $Id$ * @since 0.8 * @checkstyle JavadocMethodCheck (500 lines) */ -public final class LogConjTest { +public final class AndTest { @Test public void allTrue() throws Exception { MatcherAssert.assertThat( - new LogConj( + new And( new ArrayAsIterable>( new True(), new True(), @@ -56,7 +56,7 @@ public void allTrue() throws Exception { @Test public void oneFalse() throws Exception { MatcherAssert.assertThat( - new LogConj( + new And( new ArrayAsIterable>( new True(), new False(), @@ -70,7 +70,7 @@ public void oneFalse() throws Exception { @Test public void allFalse() throws Exception { MatcherAssert.assertThat( - new LogConj( + new And( new ArrayAsIterable>( new False(), new False(), @@ -84,7 +84,7 @@ public void allFalse() throws Exception { @Test public void emptyIterator() throws Exception { MatcherAssert.assertThat( - new LogConj( + new And( new ArrayAsIterable>() ).asValue(), Matchers.equalTo(true) From 291c9028621d606cfc8a44f499343e516f10a98b Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 20 Jun 2017 12:31:19 +0200 Subject: [PATCH 137/169] #180: tests --- .../java/org/cactoos/list/StickyList.java | 15 +++-- src/main/java/org/cactoos/list/StickyMap.java | 13 ++-- .../org/cactoos/list/MapAsPropertiesTest.java | 8 ++- .../java/org/cactoos/list/StickyListTest.java | 58 +++++++++++++++++ .../java/org/cactoos/list/StickyMapTest.java | 65 +++++++++++++++++++ 5 files changed, 145 insertions(+), 14 deletions(-) create mode 100644 src/test/java/org/cactoos/list/StickyListTest.java create mode 100644 src/test/java/org/cactoos/list/StickyMapTest.java diff --git a/src/main/java/org/cactoos/list/StickyList.java b/src/main/java/org/cactoos/list/StickyList.java index 7977a25b3f..34c57d46fe 100644 --- a/src/main/java/org/cactoos/list/StickyList.java +++ b/src/main/java/org/cactoos/list/StickyList.java @@ -28,6 +28,7 @@ import java.util.LinkedList; import java.util.List; import java.util.ListIterator; +import org.cactoos.func.StickyScalar; import org.cactoos.func.UncheckedScalar; /** @@ -52,13 +53,15 @@ public final class StickyList implements List { * Ctor. * @param list The iterable */ - public StickyList(final List list) { + public StickyList(final Collection list) { this.gate = new UncheckedScalar<>( - () -> { - final List temp = new LinkedList<>(); - temp.addAll(list); - return temp; - } + new StickyScalar<>( + () -> { + final List temp = new LinkedList<>(); + temp.addAll(list); + return temp; + } + ) ); } diff --git a/src/main/java/org/cactoos/list/StickyMap.java b/src/main/java/org/cactoos/list/StickyMap.java index f8ba6f49f4..d2cc2b563e 100644 --- a/src/main/java/org/cactoos/list/StickyMap.java +++ b/src/main/java/org/cactoos/list/StickyMap.java @@ -27,6 +27,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; +import org.cactoos.func.StickyScalar; import org.cactoos.func.UncheckedScalar; /** @@ -53,11 +54,13 @@ public final class StickyMap implements Map { */ public StickyMap(final Map map) { this.gate = new UncheckedScalar<>( - () -> { - final Map temp = new HashMap<>(); - temp.putAll(map); - return temp; - } + new StickyScalar<>( + () -> { + final Map temp = new HashMap<>(0); + temp.putAll(map); + return temp; + } + ) ); } diff --git a/src/test/java/org/cactoos/list/MapAsPropertiesTest.java b/src/test/java/org/cactoos/list/MapAsPropertiesTest.java index 1f4c9bbef4..75cf15409a 100644 --- a/src/test/java/org/cactoos/list/MapAsPropertiesTest.java +++ b/src/test/java/org/cactoos/list/MapAsPropertiesTest.java @@ -49,9 +49,11 @@ public void convertsMapToProperties() { MatcherAssert.assertThat( "Can't convert map to properties", new MapAsProperties( - new IterableAsMap( - new AbstractMap.SimpleEntry<>(0, "hello, world"), - new AbstractMap.SimpleEntry<>(1, "how are you?") + new StickyMap<>( + new IterableAsMap( + new AbstractMap.SimpleEntry<>(0, "hello, world"), + new AbstractMap.SimpleEntry<>(1, "how are you?") + ) ) ), new ScalarHasValue<>( diff --git a/src/test/java/org/cactoos/list/StickyListTest.java b/src/test/java/org/cactoos/list/StickyListTest.java new file mode 100644 index 0000000000..bca5df73a9 --- /dev/null +++ b/src/test/java/org/cactoos/list/StickyListTest.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.list; + +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link StickyList}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.8 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class StickyListTest { + + @Test + public void ignoresChangesInIterable() throws Exception { + final AtomicInteger size = new AtomicInteger(2); + final List list = new StickyList<>( + new IterableAsList<>( + () -> Collections.nCopies(size.incrementAndGet(), 0).iterator() + ) + ); + MatcherAssert.assertThat( + "Can't ignore the changes in the underlying iterable", + list.size(), + Matchers.equalTo(list.size()) + ); + } + +} diff --git a/src/test/java/org/cactoos/list/StickyMapTest.java b/src/test/java/org/cactoos/list/StickyMapTest.java new file mode 100644 index 0000000000..47198e916b --- /dev/null +++ b/src/test/java/org/cactoos/list/StickyMapTest.java @@ -0,0 +1,65 @@ +/** + * 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.list; + +import java.security.SecureRandom; +import java.util.AbstractMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link StickyMap}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.8 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class StickyMapTest { + + @Test + public void ignoresChangesInMap() throws Exception { + final AtomicInteger size = new AtomicInteger(2); + final Map map = new StickyMap<>( + new IterableAsMap<>( + () -> new RepeatIterator<>( + () -> new AbstractMap.SimpleEntry<>( + new SecureRandom().nextInt(), + 1 + ), + size.incrementAndGet() + ) + ) + ); + MatcherAssert.assertThat( + "Can't ignore the changes in the underlying map", + map.size(), + Matchers.equalTo(map.size()) + ); + } + +} From 6d4467ec1cd502982115db912bcae1d8fe983b7a Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 20 Jun 2017 12:54:01 +0200 Subject: [PATCH 138/169] #180 more ctors --- src/main/java/org/cactoos/list/StickyList.java | 18 ++++++++++++++++++ src/main/java/org/cactoos/list/StickyMap.java | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/main/java/org/cactoos/list/StickyList.java b/src/main/java/org/cactoos/list/StickyList.java index 34c57d46fe..fe96759d05 100644 --- a/src/main/java/org/cactoos/list/StickyList.java +++ b/src/main/java/org/cactoos/list/StickyList.java @@ -49,6 +49,24 @@ public final class StickyList implements List { */ private final UncheckedScalar> gate; + /** + * Ctor. + * @param items The array + */ + @SafeVarargs + @SuppressWarnings("varargs") + public StickyList(final X... items) { + this(new ArrayAsIterable<>(items)); + } + + /** + * Ctor. + * @param items The array + */ + public StickyList(final Iterable items) { + this(new IterableAsList<>(items)); + } + /** * Ctor. * @param list The iterable diff --git a/src/main/java/org/cactoos/list/StickyMap.java b/src/main/java/org/cactoos/list/StickyMap.java index d2cc2b563e..503677ba19 100644 --- a/src/main/java/org/cactoos/list/StickyMap.java +++ b/src/main/java/org/cactoos/list/StickyMap.java @@ -48,6 +48,24 @@ public final class StickyMap implements Map { */ private final UncheckedScalar> gate; + /** + * Ctor. + * @param list List of entries + */ + @SafeVarargs + @SuppressWarnings("varargs") + public StickyMap(final Map.Entry... list) { + this(new ArrayAsIterable<>(list)); + } + + /** + * Ctor. + * @param list Entries for the entries + */ + public StickyMap(final Iterable> list) { + this(new IterableAsMap<>(list)); + } + /** * Ctor. * @param map The map From 7686a0e5b0fca5647fd13fee69ffe6820df24af6 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 20 Jun 2017 12:55:43 +0200 Subject: [PATCH 139/169] javadoc --- src/main/java/org/cactoos/list/IterableAsList.java | 6 ++++++ src/main/java/org/cactoos/list/IterableAsMap.java | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index 8fc492b79b..4a1fd52e8c 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -32,12 +32,18 @@ /** * Iterable as {@link List}. * + *

If you don't need this List to re-fresh its content on every call, + * by doing round-trips to the encapsulated iterable, use + * {@link StickyList}.

+ * *

There is no thread-safety guarantee. * * @author Alexey Semenyuk (semenyukalexey88@gmail.com) * @author Kirill (g4s8.public@gmail.com) + * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param List type + * @see StickyList * @since 0.1 */ @SuppressWarnings("PMD.TooManyMethods") diff --git a/src/main/java/org/cactoos/list/IterableAsMap.java b/src/main/java/org/cactoos/list/IterableAsMap.java index 87bceb2cd4..12babbebb1 100644 --- a/src/main/java/org/cactoos/list/IterableAsMap.java +++ b/src/main/java/org/cactoos/list/IterableAsMap.java @@ -31,12 +31,17 @@ /** * Iterable as {@link Map}. * + *

If you don't need this Map to re-fresh its content on every call, + * by doing round-trips to the encapsulated iterable, use + * {@link StickyMap}.

+ * *

There is no thread-safety guarantee. * * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @param Type of key * @param Type of value + * @see StickyMap * @since 0.4 */ @SuppressWarnings("PMD.TooManyMethods") From b49bb2a211262877c9dca4de018557ce884c6a4e Mon Sep 17 00:00:00 2001 From: VseslavSekorin Date: Tue, 20 Jun 2017 14:02:28 +0300 Subject: [PATCH 140/169] Or with test --- src/main/java/org/cactoos/func/Or.java | 79 +++++++++++++++++ src/test/java/org/cactoos/func/OrTest.java | 99 ++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 src/main/java/org/cactoos/func/Or.java create mode 100644 src/test/java/org/cactoos/func/OrTest.java diff --git a/src/main/java/org/cactoos/func/Or.java b/src/main/java/org/cactoos/func/Or.java new file mode 100644 index 0000000000..0470bdb771 --- /dev/null +++ b/src/main/java/org/cactoos/func/Or.java @@ -0,0 +1,79 @@ +/** + * 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 java.util.Iterator; +import org.cactoos.Scalar; + +/** + * Logical disjunction. + * + *

There is no thread-safety guarantee. + * + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @since 0.8 + */ +public final class Or implements Scalar { + + /** + * The iterator. + */ + private final Iterable> iterable; + + /** + * Ctor. + * @param iterable The iterable + */ + public Or(final Iterable> iterable) { + this.iterable = iterable; + } + + @Override + public Boolean asValue() throws Exception { + final Iterator> iterator = this.iterable.iterator(); + return this.disjunction(iterator, false); + } + + /** + * Disjunction. + * + * @param iterator The iterator + * @param value Previous value + * @return The result + * @throws Exception If fails + */ + private Boolean disjunction( + final Iterator> iterator, + final boolean value + ) throws Exception { + final Boolean result; + if (iterator.hasNext() && !value) { + result = this.disjunction(iterator, iterator.next().asValue()); + } else { + result = value; + } + return result; + } +} diff --git a/src/test/java/org/cactoos/func/OrTest.java b/src/test/java/org/cactoos/func/OrTest.java new file mode 100644 index 0000000000..1f3042004c --- /dev/null +++ b/src/test/java/org/cactoos/func/OrTest.java @@ -0,0 +1,99 @@ +/** + * 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.Scalar; +import org.cactoos.list.ArrayAsIterable; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link Or}. + * + * @author Vseslav Sekorin (vssekorin@gmail.com) + * @version $Id$ + * @since 0.8 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class OrTest { + + @Test + public void allFalse() throws Exception { + MatcherAssert.assertThat( + new Or( + new ArrayAsIterable>( + new False(), + new False(), + new False(), + new False(), + new False() + ) + ).asValue(), + Matchers.equalTo(false) + ); + } + + @Test + public void oneTrue() throws Exception { + MatcherAssert.assertThat( + new Or( + new ArrayAsIterable>( + new False(), + new True(), + new False(), + new False(), + new False() + ) + ).asValue(), + Matchers.equalTo(true) + ); + } + + @Test + public void allTrue() throws Exception { + MatcherAssert.assertThat( + new Or( + new ArrayAsIterable>( + new True(), + new True(), + new True(), + new True(), + new True() + ) + ).asValue(), + Matchers.equalTo(true) + ); + } + + @Test + public void emptyIterator() throws Exception { + MatcherAssert.assertThat( + new Or( + new ArrayAsIterable>() + ).asValue(), + Matchers.equalTo(false) + ); + } +} From 9f0c01cc33ed83268c33cda8121c5fe578e29ada Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 20 Jun 2017 13:02:34 +0200 Subject: [PATCH 141/169] javadoc --- src/main/java/org/cactoos/list/IterableAsList.java | 6 +++++- src/main/java/org/cactoos/list/IterableAsMap.java | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index 4a1fd52e8c..918f3049e2 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -32,7 +32,11 @@ /** * Iterable as {@link List}. * - *

If you don't need this List to re-fresh its content on every call, + *

This class should be used very carefully. You must understand that + * it will fetch the entire content of the encapsulated {@link List} on each + * method call. It doesn't cache the data anyhow.

+ * + *

If you don't need this {@link List} to re-fresh its content on every call, * by doing round-trips to the encapsulated iterable, use * {@link StickyList}.

* diff --git a/src/main/java/org/cactoos/list/IterableAsMap.java b/src/main/java/org/cactoos/list/IterableAsMap.java index 12babbebb1..791f7c25d0 100644 --- a/src/main/java/org/cactoos/list/IterableAsMap.java +++ b/src/main/java/org/cactoos/list/IterableAsMap.java @@ -31,7 +31,11 @@ /** * Iterable as {@link Map}. * - *

If you don't need this Map to re-fresh its content on every call, + *

This class should be used very carefully. You must understand that + * it will fetch the entire content of the encapsulated {@link Map} on each + * method call. It doesn't cache the data anyhow.

+ * + *

If you don't need this {@link Map} to re-fresh its content on every call, * by doing round-trips to the encapsulated iterable, use * {@link StickyMap}.

* From 56f72e5f215099717846d6f16f7a0ddd0108a1d6 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 20 Jun 2017 19:11:43 +0200 Subject: [PATCH 142/169] #179: InputAsProperties --- .../org/cactoos/io/InputAsProperties.java | 64 +++++++++++++++++++ .../org/cactoos/io/InputAsPropertiesTest.java | 60 +++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/main/java/org/cactoos/io/InputAsProperties.java create mode 100644 src/test/java/org/cactoos/io/InputAsPropertiesTest.java diff --git a/src/main/java/org/cactoos/io/InputAsProperties.java b/src/main/java/org/cactoos/io/InputAsProperties.java new file mode 100644 index 0000000000..734c6b1bd4 --- /dev/null +++ b/src/main/java/org/cactoos/io/InputAsProperties.java @@ -0,0 +1,64 @@ +/** + * 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.io.InputStream; +import java.util.Properties; +import org.cactoos.Input; +import org.cactoos.Scalar; + +/** + * Input as {@link Properties}. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.8 + */ +public final class InputAsProperties implements Scalar { + + /** + * The input. + */ + private final Input source; + + /** + * Ctor. + * @param input The input + */ + public InputAsProperties(final Input input) { + this.source = input; + } + + @Override + public Properties asValue() throws IOException { + final Properties props = new Properties(); + try (final InputStream input = this.source.stream()) { + props.load(input); + } + return props; + } +} diff --git a/src/test/java/org/cactoos/io/InputAsPropertiesTest.java b/src/test/java/org/cactoos/io/InputAsPropertiesTest.java new file mode 100644 index 0000000000..fa903e9a30 --- /dev/null +++ b/src/test/java/org/cactoos/io/InputAsPropertiesTest.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.io; + +import org.cactoos.ScalarHasValue; +import org.cactoos.func.FuncAsMatcher; +import org.cactoos.text.TextAsBytes; +import org.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test case for {@link InputAsProperties}. + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.8 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class InputAsPropertiesTest { + + @Test + public void readsInputContent() { + MatcherAssert.assertThat( + "Can't read properties from an input", + new InputAsProperties( + new BytesAsInput( + new TextAsBytes( + "foo=Hello, world!\nbar=works fine?\n" + ) + ) + ), + new ScalarHasValue<>( + new FuncAsMatcher<>( + props -> "Hello, world!".equals(props.getProperty("foo")) + ) + ) + ); + } + +} From b9005af251e970173469d261a4f2d56ca267e469 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Tue, 20 Jun 2017 19:23:37 +0200 Subject: [PATCH 143/169] #179: type --- src/test/java/org/cactoos/io/InputAsPropertiesTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/cactoos/io/InputAsPropertiesTest.java b/src/test/java/org/cactoos/io/InputAsPropertiesTest.java index fa903e9a30..22ddf936be 100644 --- a/src/test/java/org/cactoos/io/InputAsPropertiesTest.java +++ b/src/test/java/org/cactoos/io/InputAsPropertiesTest.java @@ -23,6 +23,7 @@ */ package org.cactoos.io; +import java.util.Properties; import org.cactoos.ScalarHasValue; import org.cactoos.func.FuncAsMatcher; import org.cactoos.text.TextAsBytes; @@ -50,7 +51,7 @@ public void readsInputContent() { ) ), new ScalarHasValue<>( - new FuncAsMatcher<>( + new FuncAsMatcher( props -> "Hello, world!".equals(props.getProperty("foo")) ) ) From 3f4259227af4970f42cdca40ce8f77f01cd4d353 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 21 Jun 2017 07:41:59 +0200 Subject: [PATCH 144/169] #114 StickyIterable --- .../java/org/cactoos/list/StickyIterable.java | 21 +++++-- .../org/cactoos/list/StickyIterableTest.java | 57 +++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 src/test/java/org/cactoos/list/StickyIterableTest.java diff --git a/src/main/java/org/cactoos/list/StickyIterable.java b/src/main/java/org/cactoos/list/StickyIterable.java index 7584a8455f..075fb4ed10 100644 --- a/src/main/java/org/cactoos/list/StickyIterable.java +++ b/src/main/java/org/cactoos/list/StickyIterable.java @@ -23,7 +23,10 @@ */ package org.cactoos.list; +import java.util.Collection; import java.util.Iterator; +import java.util.LinkedList; +import org.cactoos.func.StickyScalar; import org.cactoos.func.UncheckedScalar; /** @@ -39,21 +42,31 @@ public final class StickyIterable implements Iterable { /** - * The cache. + * The gate. */ - private final UncheckedScalar> cache; + private final UncheckedScalar> gate; /** * Ctor. * @param iterable The iterable */ public StickyIterable(final Iterable iterable) { - this.cache = new UncheckedScalar<>(iterable::iterator); + this.gate = new UncheckedScalar<>( + new StickyScalar<>( + () -> { + final Collection temp = new LinkedList<>(); + for (final X item : iterable) { + temp.add(item); + } + return temp; + } + ) + ); } @Override public Iterator iterator() { - return this.cache.asValue(); + return this.gate.asValue().iterator(); } } diff --git a/src/test/java/org/cactoos/list/StickyIterableTest.java b/src/test/java/org/cactoos/list/StickyIterableTest.java new file mode 100644 index 0000000000..f42e358e51 --- /dev/null +++ b/src/test/java/org/cactoos/list/StickyIterableTest.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.list; + +import java.util.Collections; +import java.util.concurrent.atomic.AtomicInteger; +import org.cactoos.ScalarHasValue; +import org.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test case for {@link StickyIterable}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.8 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class StickyIterableTest { + + @Test + public void ignoresChangesInIterable() throws Exception { + final AtomicInteger size = new AtomicInteger(2); + final Iterable list = new StickyIterable<>( + new IterableAsList<>( + () -> Collections.nCopies(size.incrementAndGet(), 0).iterator() + ) + ); + MatcherAssert.assertThat( + "Can't ignore the changes in the underlying iterable", + new LengthOfIterable(list), + new ScalarHasValue<>(new LengthOfIterable(list).asValue()) + ); + } + +} From 6a27707f274a1ddaacd0a40779e85584a204fc87 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 21 Jun 2017 08:50:28 +0200 Subject: [PATCH 145/169] #85 StickyIterator --- .../org/cactoos/list/EndlessIterator.java | 22 +++++- .../java/org/cactoos/list/StickyIterable.java | 2 +- .../java/org/cactoos/list/StickyIterator.java | 76 +++++++++++++++++++ .../org/cactoos/list/StickyIteratorTest.java | 68 +++++++++++++++++ 4 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/cactoos/list/StickyIterator.java create mode 100644 src/test/java/org/cactoos/list/StickyIteratorTest.java diff --git a/src/main/java/org/cactoos/list/EndlessIterator.java b/src/main/java/org/cactoos/list/EndlessIterator.java index ada8996ffb..aef99db174 100644 --- a/src/main/java/org/cactoos/list/EndlessIterator.java +++ b/src/main/java/org/cactoos/list/EndlessIterator.java @@ -24,6 +24,8 @@ package org.cactoos.list; import java.util.Iterator; +import org.cactoos.Scalar; +import org.cactoos.func.UncheckedScalar; /** * Iterator that never ends. @@ -41,13 +43,29 @@ public final class EndlessIterator implements Iterator { /** * The element to repeat. */ - private final T element; + private final UncheckedScalar element; /** * Ctor. * @param elm Element to repeat */ public EndlessIterator(final T elm) { + this(() -> elm); + } + + /** + * Ctor. + * @param elm Element to repeat + */ + public EndlessIterator(final Scalar elm) { + this(new UncheckedScalar<>(elm)); + } + + /** + * Ctor. + * @param elm Element to repeat + */ + public EndlessIterator(final UncheckedScalar elm) { this.element = elm; } @@ -58,6 +76,6 @@ public boolean hasNext() { @Override public T next() { - return this.element; + return this.element.asValue(); } } diff --git a/src/main/java/org/cactoos/list/StickyIterable.java b/src/main/java/org/cactoos/list/StickyIterable.java index 075fb4ed10..3d6ab4e3a5 100644 --- a/src/main/java/org/cactoos/list/StickyIterable.java +++ b/src/main/java/org/cactoos/list/StickyIterable.java @@ -30,7 +30,7 @@ import org.cactoos.func.UncheckedScalar; /** - * Iterable that returns an iterator only once. + * Iterable that returns the same set of elements, always. * *

There is no thread-safety guarantee. * diff --git a/src/main/java/org/cactoos/list/StickyIterator.java b/src/main/java/org/cactoos/list/StickyIterator.java new file mode 100644 index 0000000000..40373d2255 --- /dev/null +++ b/src/main/java/org/cactoos/list/StickyIterator.java @@ -0,0 +1,76 @@ +/** + * 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.list; + +import java.util.Collection; +import java.util.Iterator; +import java.util.LinkedList; +import org.cactoos.func.StickyScalar; +import org.cactoos.func.UncheckedScalar; + +/** + * Iterator that returns the same set of elements always. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of item + * @since 0.8 + */ +public final class StickyIterator implements Iterator { + + /** + * The gate. + */ + private final UncheckedScalar> gate; + + /** + * Ctor. + * @param src The iterable + */ + public StickyIterator(final Iterator src) { + this.gate = new UncheckedScalar<>( + new StickyScalar<>( + () -> { + final Collection temp = new LinkedList<>(); + while (src.hasNext()) { + temp.add(src.next()); + } + return temp.iterator(); + } + ) + ); + } + + @Override + public boolean hasNext() { + return this.gate.asValue().hasNext(); + } + + @Override + public X next() { + return this.gate.asValue().next(); + } +} diff --git a/src/test/java/org/cactoos/list/StickyIteratorTest.java b/src/test/java/org/cactoos/list/StickyIteratorTest.java new file mode 100644 index 0000000000..99a3aab6db --- /dev/null +++ b/src/test/java/org/cactoos/list/StickyIteratorTest.java @@ -0,0 +1,68 @@ +/** + * 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.list; + +import java.util.concurrent.atomic.AtomicInteger; +import org.cactoos.Text; +import org.cactoos.TextHasString; +import org.cactoos.text.FormattedText; +import org.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test case for {@link StickyIterator}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.8 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class StickyIteratorTest { + + @Test + public void ignoresChangesInIterable() throws Exception { + final AtomicInteger count = new AtomicInteger(2); + final Text text = new FormattedText( + "%s", + String.join( + ", ", + () -> new MappedIterator<>( + new StickyIterator<>( + new LimitedIterator<>( + new EndlessIterator<>(count::incrementAndGet), + 2 + ) + ), + Object::toString + ) + ) + ); + MatcherAssert.assertThat( + "Can't ignore the changes in the underlying iterator", + text, + new TextHasString(text.asString()) + ); + } + +} From b8094529af8c9377e94c274a1641e63827723bac Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 21 Jun 2017 15:20:36 +0200 Subject: [PATCH 146/169] #loops: warnings off --- src/main/java/org/cactoos/list/ArrayAsIterable.java | 1 - src/main/java/org/cactoos/list/ConcatIterable.java | 1 - src/main/java/org/cactoos/list/ConcatIterator.java | 1 - src/main/java/org/cactoos/list/IterableAsList.java | 1 - src/main/java/org/cactoos/list/IterableAsMap.java | 1 - src/main/java/org/cactoos/list/SortedIterable.java | 2 -- src/main/java/org/cactoos/list/StickyList.java | 1 - src/main/java/org/cactoos/list/StickyMap.java | 1 - src/test/java/org/cactoos/list/StickyMapTest.java | 12 ++++++++++++ 9 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/cactoos/list/ArrayAsIterable.java b/src/main/java/org/cactoos/list/ArrayAsIterable.java index 5ceb323499..adfad62b9c 100644 --- a/src/main/java/org/cactoos/list/ArrayAsIterable.java +++ b/src/main/java/org/cactoos/list/ArrayAsIterable.java @@ -48,7 +48,6 @@ public final class ArrayAsIterable implements Iterable { * @param items The array */ @SafeVarargs - @SuppressWarnings("varargs") public ArrayAsIterable(final X... items) { this.array = items; } diff --git a/src/main/java/org/cactoos/list/ConcatIterable.java b/src/main/java/org/cactoos/list/ConcatIterable.java index 616f5f1cbf..606be04b31 100644 --- a/src/main/java/org/cactoos/list/ConcatIterable.java +++ b/src/main/java/org/cactoos/list/ConcatIterable.java @@ -48,7 +48,6 @@ public final class ConcatIterable implements Iterable { * @param items Items to concatenate */ @SafeVarargs - @SuppressWarnings("varargs") public ConcatIterable(final Iterable... items) { this(new IterableAsList<>(items)); } diff --git a/src/main/java/org/cactoos/list/ConcatIterator.java b/src/main/java/org/cactoos/list/ConcatIterator.java index c8578749b9..49a7eef18d 100644 --- a/src/main/java/org/cactoos/list/ConcatIterator.java +++ b/src/main/java/org/cactoos/list/ConcatIterator.java @@ -47,7 +47,6 @@ public final class ConcatIterator implements Iterator { * @param items Items to concatenate */ @SafeVarargs - @SuppressWarnings("varargs") public ConcatIterator(final Iterator... items) { this(new IterableAsList<>(items)); } diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index 918f3049e2..6928da5621 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -64,7 +64,6 @@ public final class IterableAsList implements List { * @param array An array of some elements */ @SafeVarargs - @SuppressWarnings("varargs") public IterableAsList(final T... array) { this(new ArrayAsIterable<>(array)); } diff --git a/src/main/java/org/cactoos/list/IterableAsMap.java b/src/main/java/org/cactoos/list/IterableAsMap.java index 791f7c25d0..f36724c0b0 100644 --- a/src/main/java/org/cactoos/list/IterableAsMap.java +++ b/src/main/java/org/cactoos/list/IterableAsMap.java @@ -61,7 +61,6 @@ public final class IterableAsMap implements Map { * @param list List of entries */ @SafeVarargs - @SuppressWarnings("varargs") public IterableAsMap(final Map.Entry... list) { this(new ArrayAsIterable<>(list)); } diff --git a/src/main/java/org/cactoos/list/SortedIterable.java b/src/main/java/org/cactoos/list/SortedIterable.java index 29aaeccd76..96f487fa55 100644 --- a/src/main/java/org/cactoos/list/SortedIterable.java +++ b/src/main/java/org/cactoos/list/SortedIterable.java @@ -55,7 +55,6 @@ * @param src The underlying iterable */ @SafeVarargs - @SuppressWarnings("varargs") public SortedIterable(final T... src) { this(new ArrayAsIterable<>(src)); } @@ -74,7 +73,6 @@ public SortedIterable(final Iterable src) { * @param cmp The comparator */ @SafeVarargs - @SuppressWarnings("varargs") public SortedIterable(final Comparator cmp, final T... src) { this(cmp, new ArrayAsIterable<>(src)); } diff --git a/src/main/java/org/cactoos/list/StickyList.java b/src/main/java/org/cactoos/list/StickyList.java index fe96759d05..1927fc4769 100644 --- a/src/main/java/org/cactoos/list/StickyList.java +++ b/src/main/java/org/cactoos/list/StickyList.java @@ -54,7 +54,6 @@ public final class StickyList implements List { * @param items The array */ @SafeVarargs - @SuppressWarnings("varargs") public StickyList(final X... items) { this(new ArrayAsIterable<>(items)); } diff --git a/src/main/java/org/cactoos/list/StickyMap.java b/src/main/java/org/cactoos/list/StickyMap.java index 503677ba19..ef8bd880ff 100644 --- a/src/main/java/org/cactoos/list/StickyMap.java +++ b/src/main/java/org/cactoos/list/StickyMap.java @@ -53,7 +53,6 @@ public final class StickyMap implements Map { * @param list List of entries */ @SafeVarargs - @SuppressWarnings("varargs") public StickyMap(final Map.Entry... list) { this(new ArrayAsIterable<>(list)); } diff --git a/src/test/java/org/cactoos/list/StickyMapTest.java b/src/test/java/org/cactoos/list/StickyMapTest.java index 47198e916b..17692a16df 100644 --- a/src/test/java/org/cactoos/list/StickyMapTest.java +++ b/src/test/java/org/cactoos/list/StickyMapTest.java @@ -62,4 +62,16 @@ public void ignoresChangesInMap() throws Exception { ); } + @Test + public void decoratesEntries() throws Exception { + MatcherAssert.assertThat( + "Can't decorate a list of entries", + new StickyMap<>( + new AbstractMap.SimpleEntry<>("first", "Jeffrey"), + new AbstractMap.SimpleEntry<>("last", "Lebowski") + ), + Matchers.hasValue(Matchers.endsWith("ski")) + ); + } + } From 235a672ec6bbe7ec77069bb98d20ad785d1a545d Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 21 Jun 2017 21:11:43 +0300 Subject: [PATCH 147/169] #loops: less warnings --- README.md | 29 +++++++++++-------- pom.xml | 2 +- .../java/org/cactoos/list/MappedIterator.java | 1 - .../java/org/cactoos/list/StickyIterator.java | 9 ++++++ .../java/org/cactoos/text/ArrayAsBytes.java | 3 +- .../org/cactoos/list/ConcatIterableTest.java | 3 +- .../java/org/cactoos/list/StickyListTest.java | 9 ++++++ .../java/org/cactoos/list/StickyMapTest.java | 2 +- 8 files changed, 39 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 9b8a21a6ad..ad3ec7f0c5 100644 --- a/README.md +++ b/README.md @@ -202,12 +202,15 @@ for (String name : names) { This is its object-oriented alternative (no streams!): ```java -new IterableAsBoolean<>( - names, - new ProcAsFunc<>( - n -> { - System.out.printf("Hello, %s!\n", n); - } +new And<>( + new MappedIterable<>( + names, + new ProcAsFunc<>( + n -> { + System.out.printf("Hello, %s!\n", n); + }, + () -> true + ) ) ).asValue(); ``` @@ -223,12 +226,14 @@ while (!ready) { Here is its object-oriented alternative: ```java -new IterableAsBoolean<>( - new EndlessIterable<>(ready), - r -> { - System.out.prinln("Still waiting..."); - return !ready; - } +new And<>( + new MappedIterable<>( + new EndlessIterable<>(ready), + r -> { + System.out.prinln("Still waiting..."); + return !ready; + } + ) ).asValue(); ``` diff --git a/pom.xml b/pom.xml index 2843a28630..3c0d043a3b 100644 --- a/pom.xml +++ b/pom.xml @@ -29,7 +29,7 @@ com.jcabi parent - 0.48.1 + 0.48.5 org.cactoos cactoos diff --git a/src/main/java/org/cactoos/list/MappedIterator.java b/src/main/java/org/cactoos/list/MappedIterator.java index 155262ba78..41125ec778 100644 --- a/src/main/java/org/cactoos/list/MappedIterator.java +++ b/src/main/java/org/cactoos/list/MappedIterator.java @@ -66,7 +66,6 @@ public boolean hasNext() { } @Override - @SuppressWarnings("PMD.AvoidCatchingGenericException") public Y next() { return new UncheckedFunc<>(this.func).apply(this.iterator.next()); } diff --git a/src/main/java/org/cactoos/list/StickyIterator.java b/src/main/java/org/cactoos/list/StickyIterator.java index 40373d2255..8fd13294e7 100644 --- a/src/main/java/org/cactoos/list/StickyIterator.java +++ b/src/main/java/org/cactoos/list/StickyIterator.java @@ -46,6 +46,15 @@ public final class StickyIterator implements Iterator { */ private final UncheckedScalar> gate; + /** + * Ctor. + * @param items Items to iterate + */ + @SafeVarargs + public StickyIterator(final X... items) { + this(new ArrayAsIterable<>(items).iterator()); + } + /** * Ctor. * @param src The iterable diff --git a/src/main/java/org/cactoos/text/ArrayAsBytes.java b/src/main/java/org/cactoos/text/ArrayAsBytes.java index 6f545703e7..56f3b8dec8 100644 --- a/src/main/java/org/cactoos/text/ArrayAsBytes.java +++ b/src/main/java/org/cactoos/text/ArrayAsBytes.java @@ -45,8 +45,7 @@ public final class ArrayAsBytes implements Bytes { * Ctor. * @param bts Bytes to encapsulate */ - @SuppressWarnings("PMD.ArrayIsStoredDirectly") - public ArrayAsBytes(final byte[] bts) { + public ArrayAsBytes(final byte... bts) { this.bytes = bts; } diff --git a/src/test/java/org/cactoos/list/ConcatIterableTest.java b/src/test/java/org/cactoos/list/ConcatIterableTest.java index e7a07cc1fa..04ecce3b8b 100644 --- a/src/test/java/org/cactoos/list/ConcatIterableTest.java +++ b/src/test/java/org/cactoos/list/ConcatIterableTest.java @@ -37,12 +37,11 @@ public final class ConcatIterableTest { @Test - @SuppressWarnings("unchecked") public void transformsList() { MatcherAssert.assertThat( "Can't concatenate iterables together", new LengthOfIterable( - new ConcatIterable<>( + new ConcatIterable( new ArrayAsIterable<>("hello", "world", "друг"), new ArrayAsIterable<>("how", "are", "you"), new ArrayAsIterable<>("what's", "up") diff --git a/src/test/java/org/cactoos/list/StickyListTest.java b/src/test/java/org/cactoos/list/StickyListTest.java index bca5df73a9..b62dfa3a58 100644 --- a/src/test/java/org/cactoos/list/StickyListTest.java +++ b/src/test/java/org/cactoos/list/StickyListTest.java @@ -55,4 +55,13 @@ public void ignoresChangesInIterable() throws Exception { ); } + @Test + public void decoratesArray() throws Exception { + MatcherAssert.assertThat( + "Can't decorate an array of numbers", + new StickyList<>(-1, 0).size(), + Matchers.equalTo(2) + ); + } + } diff --git a/src/test/java/org/cactoos/list/StickyMapTest.java b/src/test/java/org/cactoos/list/StickyMapTest.java index 17692a16df..a37fbd7cb5 100644 --- a/src/test/java/org/cactoos/list/StickyMapTest.java +++ b/src/test/java/org/cactoos/list/StickyMapTest.java @@ -66,7 +66,7 @@ public void ignoresChangesInMap() throws Exception { public void decoratesEntries() throws Exception { MatcherAssert.assertThat( "Can't decorate a list of entries", - new StickyMap<>( + new StickyMap( new AbstractMap.SimpleEntry<>("first", "Jeffrey"), new AbstractMap.SimpleEntry<>("last", "Lebowski") ), From 6f0380511fac5cdb5d74a797e40f33921d07ec86 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 21 Jun 2017 21:17:38 +0300 Subject: [PATCH 148/169] #loops: more --- .../java/org/cactoos/func/AlwaysTrueFunc.java | 80 ---------------- src/main/java/org/cactoos/func/And.java | 19 +++- src/main/java/org/cactoos/func/Or.java | 19 +++- src/main/java/org/cactoos/list/AllOf.java | 92 ------------------- src/main/java/org/cactoos/list/AnyOf.java | 64 ------------- .../org/cactoos/list/FilteredIterator.java | 4 +- .../java/org/cactoos/list/ItemOfIterable.java | 2 +- .../java/org/cactoos/list/ItemOfIterator.java | 10 +- .../org/cactoos/list/IterableAsBoolean.java | 80 ---------------- .../org/cactoos/list/IterableAsBooleans.java | 83 ----------------- src/test/java/org/cactoos/func/AndTest.java | 73 ++++++++++++--- src/test/java/org/cactoos/func/OrTest.java | 32 +++---- src/test/java/org/cactoos/list/AllOfTest.java | 92 ------------------- src/test/java/org/cactoos/list/AnyOfTest.java | 53 ----------- 14 files changed, 105 insertions(+), 598 deletions(-) delete mode 100644 src/main/java/org/cactoos/func/AlwaysTrueFunc.java delete mode 100644 src/main/java/org/cactoos/list/AllOf.java delete mode 100644 src/main/java/org/cactoos/list/AnyOf.java delete mode 100644 src/main/java/org/cactoos/list/IterableAsBoolean.java delete mode 100644 src/main/java/org/cactoos/list/IterableAsBooleans.java delete mode 100644 src/test/java/org/cactoos/list/AllOfTest.java delete mode 100644 src/test/java/org/cactoos/list/AnyOfTest.java diff --git a/src/main/java/org/cactoos/func/AlwaysTrueFunc.java b/src/main/java/org/cactoos/func/AlwaysTrueFunc.java deleted file mode 100644 index fb396e03f9..0000000000 --- a/src/main/java/org/cactoos/func/AlwaysTrueFunc.java +++ /dev/null @@ -1,80 +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.func; - -import org.cactoos.Func; -import org.cactoos.Proc; - -/** - * Func as that is always true. - * - *

You may want to use this decorator when you need - * a procedure that returns boolean instead of a function:

- * - *
 List<String> list = new LinkedList<>();
- * new AllOf(
- *   new IterableAsBooleans<String>(
- *     Collections.emptyList(),
- *     new AlwaysTrueFunc<>(list::add)
- *   )
- * ).asValue();
- * 
- * - *

There is no thread-safety guarantee. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @param Type of input - * @since 0.2 - */ -public final class AlwaysTrueFunc implements Func { - - /** - * Original func. - */ - private final Func func; - - /** - * Ctor. - * @param proc Encapsulated proc - */ - public AlwaysTrueFunc(final Proc proc) { - this(new ProcAsFunc<>(proc)); - } - - /** - * Ctor. - * @param fnc Encapsulated func - */ - public AlwaysTrueFunc(final Func fnc) { - this.func = fnc; - } - - @Override - public Boolean apply(final X input) throws Exception { - this.func.apply(input); - return true; - } - -} diff --git a/src/main/java/org/cactoos/func/And.java b/src/main/java/org/cactoos/func/And.java index 37d947dcbc..91ba5c074b 100644 --- a/src/main/java/org/cactoos/func/And.java +++ b/src/main/java/org/cactoos/func/And.java @@ -25,6 +25,7 @@ import java.util.Iterator; import org.cactoos.Scalar; +import org.cactoos.list.ArrayAsIterable; /** * Logical conjunction. @@ -44,16 +45,24 @@ public final class And implements Scalar { /** * Ctor. - * @param iterable The iterable + * @param src The iterable */ - public And(final Iterable> iterable) { - this.iterable = iterable; + @SafeVarargs + public And(final Scalar... src) { + this(new ArrayAsIterable<>(src)); + } + + /** + * Ctor. + * @param src The iterable + */ + public And(final Iterable> src) { + this.iterable = src; } @Override public Boolean asValue() throws Exception { - final Iterator> iterator = this.iterable.iterator(); - return this.conjunction(iterator, true); + return this.conjunction(this.iterable.iterator(), true); } /** diff --git a/src/main/java/org/cactoos/func/Or.java b/src/main/java/org/cactoos/func/Or.java index 0470bdb771..efe68f03b3 100644 --- a/src/main/java/org/cactoos/func/Or.java +++ b/src/main/java/org/cactoos/func/Or.java @@ -25,6 +25,7 @@ import java.util.Iterator; import org.cactoos.Scalar; +import org.cactoos.list.ArrayAsIterable; /** * Logical disjunction. @@ -44,16 +45,24 @@ public final class Or implements Scalar { /** * Ctor. - * @param iterable The iterable + * @param src The iterable */ - public Or(final Iterable> iterable) { - this.iterable = iterable; + @SafeVarargs + public Or(final Scalar... src) { + this(new ArrayAsIterable<>(src)); + } + + /** + * Ctor. + * @param src The iterable + */ + public Or(final Iterable> src) { + this.iterable = src; } @Override public Boolean asValue() throws Exception { - final Iterator> iterator = this.iterable.iterator(); - return this.disjunction(iterator, false); + return this.disjunction(this.iterable.iterator(), false); } /** diff --git a/src/main/java/org/cactoos/list/AllOf.java b/src/main/java/org/cactoos/list/AllOf.java deleted file mode 100644 index 6e4dc923e1..0000000000 --- a/src/main/java/org/cactoos/list/AllOf.java +++ /dev/null @@ -1,92 +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.list; - -import org.cactoos.Scalar; - -/** - * Is {@code true} when all items in the collection are {@code true}. - * - *

You can use this class in order to iterate all items - * in the collection. This is very similar to the {@code forEach()} - * in steams, but more object oriented. For example, if you want - * to print all items from the array:

- * - *
 new AllOf(
- *   new TransformedIterable<String>(
- *     new IterableAsList<String>("hello", "world"),
- *     new ProcAsFunc<>(i -> System.out.println(i))
- *   )
- * ).asValue();
- * - *

Or even more compact, through {@link IterableAsBooleans}:

- * - *
 new AllOf(
- *   new IterableAsBooleans<String>(
- *     new IterableAsList<String>("hello", "world"),
- *     i -> System.out.println(i)
- *   )
- * ).asValue();
- * - *

Or you can even use {@link IterableAsBoolean}:

- * - *
 new IterableAsBoolean<String>(
- *   new IterableAsList<String>("hello", "world"),
- *   i -> System.out.println(i)
- * ).asValue();
- * - *

There is no thread-safety guarantee. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @since 0.1 - */ -public final class AllOf implements Scalar { - - /** - * Iterable. - */ - private final Iterable iterable; - - /** - * Ctor. - * @param src Source iterable - */ - public AllOf(final Iterable src) { - this.iterable = src; - } - - @Override - public Boolean asValue() { - boolean success = true; - for (final Boolean item : this.iterable) { - if (!item) { - success = false; - break; - } - } - return success; - } - -} diff --git a/src/main/java/org/cactoos/list/AnyOf.java b/src/main/java/org/cactoos/list/AnyOf.java deleted file mode 100644 index a08b63a7cd..0000000000 --- a/src/main/java/org/cactoos/list/AnyOf.java +++ /dev/null @@ -1,64 +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.list; - -import org.cactoos.Scalar; - -/** - * Is {@code true} when any item in the collection is {@code true}. - * - *

There is no thread-safety guarantee. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @since 0.1 - */ -public final class AnyOf implements Scalar { - - /** - * Iterable. - */ - private final Iterable iterable; - - /** - * Ctor. - * @param src Source iterable - */ - public AnyOf(final Iterable src) { - this.iterable = src; - } - - @Override - public Boolean asValue() { - boolean success = false; - for (final Boolean item : this.iterable) { - if (item) { - success = true; - break; - } - } - return success; - } - -} diff --git a/src/main/java/org/cactoos/list/FilteredIterator.java b/src/main/java/org/cactoos/list/FilteredIterator.java index a25f6b445b..0cc335b18a 100644 --- a/src/main/java/org/cactoos/list/FilteredIterator.java +++ b/src/main/java/org/cactoos/list/FilteredIterator.java @@ -100,7 +100,7 @@ public boolean hasNext() { public X next() { if (!this.hasNext()) { throw new NoSuchElementException( - "No more elements with fits the condition." + "No more elements that fit the condition" ); } return this.buffer.poll(); @@ -109,7 +109,7 @@ public X next() { @Override public void remove() { throw new UnsupportedOperationException( - "FilteredIterator does not support remove Operation" + "#remove() is not supported" ); } diff --git a/src/main/java/org/cactoos/list/ItemOfIterable.java b/src/main/java/org/cactoos/list/ItemOfIterable.java index 3cc971be36..0a7b5d29d5 100644 --- a/src/main/java/org/cactoos/list/ItemOfIterable.java +++ b/src/main/java/org/cactoos/list/ItemOfIterable.java @@ -109,7 +109,7 @@ public ItemOfIterable(final Iterable src, final int pos) { itr -> { throw new IOException( new FormattedText( - "Iterable %s hasn't element from position %d", + "Iterable %s doesn't have element at position #%d", itr, pos ).asString() diff --git a/src/main/java/org/cactoos/list/ItemOfIterator.java b/src/main/java/org/cactoos/list/ItemOfIterator.java index f5bae33408..99166ce0a5 100644 --- a/src/main/java/org/cactoos/list/ItemOfIterator.java +++ b/src/main/java/org/cactoos/list/ItemOfIterator.java @@ -66,10 +66,7 @@ public ItemOfIterator(final Iterator src) { this( src, itr -> { - throw new IOException( - new FormattedText("Iterator %s is empty", itr.iterator()) - .asString() - ); + throw new IOException("Iterator is empty"); } ); } @@ -110,8 +107,7 @@ public ItemOfIterator(final Iterator src, final int pos) { itr -> { throw new IOException( new FormattedText( - "Iterator %s hasn't element from position %d", - itr.iterator(), + "Iterator doesn't have an element at #%d position", pos ).asString() ); @@ -141,7 +137,7 @@ public T asValue() throws Exception { if (this.pos < 0) { throw new IOException( new FormattedText( - "Position must be nonnegative! But position = %d", + "The position must be non-negative: %d", this.pos ).asString() ); diff --git a/src/main/java/org/cactoos/list/IterableAsBoolean.java b/src/main/java/org/cactoos/list/IterableAsBoolean.java deleted file mode 100644 index 6613937b83..0000000000 --- a/src/main/java/org/cactoos/list/IterableAsBoolean.java +++ /dev/null @@ -1,80 +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.list; - -import org.cactoos.Func; -import org.cactoos.Scalar; - -/** - * Iterable into boolean. - * - *

You can use this class, for example, in order to iterate through - * a collection of items:

- * - *
 new IterableAsBoolean<>(
- *   new IterableAsList<String>("hello", "world"),
- *   i -> System.out.println(i)
- * ).asValue();
- * - *

There is no thread-safety guarantee. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @param Type of source item - * @see IterableAsBooleans - * @since 0.1 - */ -public final class IterableAsBoolean implements Scalar { - - /** - * Iterable. - */ - private final Iterable iterable; - - /** - * Proc. - */ - private final Func func; - - /** - * Ctor. - * @param src Source iterable - * @param fnc Func - */ - public IterableAsBoolean(final Iterable src, - final Func fnc) { - this.iterable = src; - this.func = fnc; - } - - @Override - public Boolean asValue() { - return new AllOf( - new IterableAsBooleans<>( - this.iterable, - this.func - ) - ).asValue(); - } -} diff --git a/src/main/java/org/cactoos/list/IterableAsBooleans.java b/src/main/java/org/cactoos/list/IterableAsBooleans.java deleted file mode 100644 index d1a47e7e94..0000000000 --- a/src/main/java/org/cactoos/list/IterableAsBooleans.java +++ /dev/null @@ -1,83 +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.list; - -import java.util.Iterator; -import org.cactoos.Func; - -/** - * Iterable into booleans. - * - *

You can use this class, for example, in order to iterate through - * a collection of items, in combination with {@link AllOf}:

- * - *
 new AllOf(
- *   new IterableAsBooleans<String>(
- *     new IterableAsList<String>("hello", "world"),
- *     i -> System.out.println(i)
- *   )
- * ).asValue();
- * - *

Also, consider a much shorter version with {@link IterableAsBoolean}.

- * - *

There is no thread-safety guarantee. - * - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @param Type of source item - * @see IterableAsBoolean - * @since 0.1 - */ -public final class IterableAsBooleans implements Iterable { - - /** - * Iterable. - */ - private final Iterable iterable; - - /** - * Func. - */ - private final Func func; - - /** - * Ctor. - * @param src Source iterable - * @param fnc Func - */ - public IterableAsBooleans(final Iterable src, - final Func fnc) { - this.iterable = src; - this.func = fnc; - } - - @Override - public Iterator iterator() { - return new MappedIterator<>( - this.iterable.iterator(), - this.func - ); - } - -} diff --git a/src/test/java/org/cactoos/func/AndTest.java b/src/test/java/org/cactoos/func/AndTest.java index 8f842a3f86..443eaaf382 100644 --- a/src/test/java/org/cactoos/func/AndTest.java +++ b/src/test/java/org/cactoos/func/AndTest.java @@ -23,8 +23,12 @@ */ package org.cactoos.func; -import org.cactoos.Scalar; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; +import org.cactoos.ScalarHasValue; import org.cactoos.list.ArrayAsIterable; +import org.cactoos.list.MappedIterable; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; @@ -43,11 +47,9 @@ public final class AndTest { public void allTrue() throws Exception { MatcherAssert.assertThat( new And( - new ArrayAsIterable>( - new True(), - new True(), - new True() - ) + new True(), + new True(), + new True() ).asValue(), Matchers.equalTo(true) ); @@ -57,11 +59,9 @@ public void allTrue() throws Exception { public void oneFalse() throws Exception { MatcherAssert.assertThat( new And( - new ArrayAsIterable>( - new True(), - new False(), - new True() - ) + new True(), + new False(), + new True() ).asValue(), Matchers.equalTo(false) ); @@ -71,7 +71,7 @@ public void oneFalse() throws Exception { public void allFalse() throws Exception { MatcherAssert.assertThat( new And( - new ArrayAsIterable>( + new ArrayAsIterable<>( new False(), new False(), new False() @@ -84,10 +84,53 @@ public void allFalse() throws Exception { @Test public void emptyIterator() throws Exception { MatcherAssert.assertThat( - new And( - new ArrayAsIterable>() - ).asValue(), + new And(Collections.emptyList()).asValue(), Matchers.equalTo(true) ); } + + @Test + public void iteratesList() { + final List list = new LinkedList<>(); + MatcherAssert.assertThat( + "Can't iterate a list with a procedure", + new And( + new MappedIterable<>( + new ArrayAsIterable<>("hello", "world"), + new ProcAsFunc<>(list::add, () -> true) + ) + ), + new ScalarHasValue<>( + Matchers.allOf( + Matchers.equalTo(true), + new FuncAsMatcher<>( + value -> list.size() == 2 + ) + ) + ) + ); + } + + @Test + public void iteratesEmptyList() { + final List list = new LinkedList<>(); + MatcherAssert.assertThat( + "Can't iterate a list", + new And( + new MappedIterable<>( + Collections.emptyList(), + new ProcAsFunc<>(list::add, () -> true) + ) + ), + new ScalarHasValue<>( + Matchers.allOf( + Matchers.equalTo(true), + new FuncAsMatcher<>( + value -> list.isEmpty() + ) + ) + ) + ); + } + } diff --git a/src/test/java/org/cactoos/func/OrTest.java b/src/test/java/org/cactoos/func/OrTest.java index 1f3042004c..d217fddfbc 100644 --- a/src/test/java/org/cactoos/func/OrTest.java +++ b/src/test/java/org/cactoos/func/OrTest.java @@ -23,7 +23,7 @@ */ package org.cactoos.func; -import org.cactoos.Scalar; +import java.util.Collections; import org.cactoos.list.ArrayAsIterable; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; @@ -43,13 +43,11 @@ public final class OrTest { public void allFalse() throws Exception { MatcherAssert.assertThat( new Or( - new ArrayAsIterable>( - new False(), - new False(), - new False(), - new False(), - new False() - ) + new False(), + new False(), + new False(), + new False(), + new False() ).asValue(), Matchers.equalTo(false) ); @@ -59,13 +57,11 @@ public void allFalse() throws Exception { public void oneTrue() throws Exception { MatcherAssert.assertThat( new Or( - new ArrayAsIterable>( - new False(), - new True(), - new False(), - new False(), - new False() - ) + new False(), + new True(), + new False(), + new False(), + new False() ).asValue(), Matchers.equalTo(true) ); @@ -75,7 +71,7 @@ public void oneTrue() throws Exception { public void allTrue() throws Exception { MatcherAssert.assertThat( new Or( - new ArrayAsIterable>( + new ArrayAsIterable<>( new True(), new True(), new True(), @@ -90,9 +86,7 @@ public void allTrue() throws Exception { @Test public void emptyIterator() throws Exception { MatcherAssert.assertThat( - new Or( - new ArrayAsIterable>() - ).asValue(), + new Or(Collections.emptyList()).asValue(), Matchers.equalTo(false) ); } diff --git a/src/test/java/org/cactoos/list/AllOfTest.java b/src/test/java/org/cactoos/list/AllOfTest.java deleted file mode 100644 index 8bef1a3c1e..0000000000 --- a/src/test/java/org/cactoos/list/AllOfTest.java +++ /dev/null @@ -1,92 +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.list; - -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; -import org.cactoos.Proc; -import org.cactoos.ScalarHasValue; -import org.cactoos.func.AlwaysTrueFunc; -import org.cactoos.func.FuncAsMatcher; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link AllOf}. - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @since 0.1 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class AllOfTest { - - @Test - public void iteratesList() { - final List list = new LinkedList<>(); - MatcherAssert.assertThat( - "Can't iterate a list with a procedure", - new AllOf( - new MappedIterable<>( - new ArrayAsIterable<>("hello", "world"), - list::add - ) - ), - new ScalarHasValue<>( - Matchers.allOf( - Matchers.equalTo(true), - new FuncAsMatcher<>( - value -> list.size() == 2 - ) - ) - ) - ); - } - - @Test - public void iteratesEmptyList() { - final List list = new LinkedList<>(); - MatcherAssert.assertThat( - "Can't iterate a list", - new AllOf( - new IterableAsBooleans<>( - Collections.emptyList(), - new AlwaysTrueFunc<>( - (Proc) list::add - ) - ) - ), - new ScalarHasValue<>( - Matchers.allOf( - Matchers.equalTo(true), - new FuncAsMatcher<>( - value -> list.isEmpty() - ) - ) - ) - ); - } - -} diff --git a/src/test/java/org/cactoos/list/AnyOfTest.java b/src/test/java/org/cactoos/list/AnyOfTest.java deleted file mode 100644 index 6fae23a3fe..0000000000 --- a/src/test/java/org/cactoos/list/AnyOfTest.java +++ /dev/null @@ -1,53 +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.list; - -import org.cactoos.ScalarHasValue; -import org.hamcrest.MatcherAssert; -import org.junit.Test; - -/** - * Test case for {@link AnyOf}. - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @since 0.1 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class AnyOfTest { - - @Test - public void iteratesList() { - MatcherAssert.assertThat( - "Can't iterate a list", - new AnyOf( - new MappedIterable<>( - new ArrayAsIterable<>("a", "file", "is", "corrupt"), - txt -> txt.length() > 2 - ) - ), - new ScalarHasValue<>(true) - ); - } - -} From b867f676bdde99a94d7efe338086eb88930290f7 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 21 Jun 2017 21:42:41 +0300 Subject: [PATCH 149/169] #loops: types --- src/test/java/org/cactoos/func/AndTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/cactoos/func/AndTest.java b/src/test/java/org/cactoos/func/AndTest.java index 443eaaf382..e836e7a1dd 100644 --- a/src/test/java/org/cactoos/func/AndTest.java +++ b/src/test/java/org/cactoos/func/AndTest.java @@ -26,6 +26,7 @@ import java.util.Collections; import java.util.LinkedList; import java.util.List; +import org.cactoos.Scalar; import org.cactoos.ScalarHasValue; import org.cactoos.list.ArrayAsIterable; import org.cactoos.list.MappedIterable; @@ -117,7 +118,7 @@ public void iteratesEmptyList() { MatcherAssert.assertThat( "Can't iterate a list", new And( - new MappedIterable<>( + new MappedIterable>( Collections.emptyList(), new ProcAsFunc<>(list::add, () -> true) ) From 207c18f8c3a5a59f3b7af59b7ee8747ec775074d Mon Sep 17 00:00:00 2001 From: yegor256 Date: Wed, 21 Jun 2017 21:53:34 +0300 Subject: [PATCH 150/169] #loops: types --- src/test/java/org/cactoos/func/AndTest.java | 2 +- src/test/java/org/cactoos/func/OrTest.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/cactoos/func/AndTest.java b/src/test/java/org/cactoos/func/AndTest.java index e836e7a1dd..20506fc80c 100644 --- a/src/test/java/org/cactoos/func/AndTest.java +++ b/src/test/java/org/cactoos/func/AndTest.java @@ -72,7 +72,7 @@ public void oneFalse() throws Exception { public void allFalse() throws Exception { MatcherAssert.assertThat( new And( - new ArrayAsIterable<>( + new ArrayAsIterable>( new False(), new False(), new False() diff --git a/src/test/java/org/cactoos/func/OrTest.java b/src/test/java/org/cactoos/func/OrTest.java index d217fddfbc..ca5219cbdb 100644 --- a/src/test/java/org/cactoos/func/OrTest.java +++ b/src/test/java/org/cactoos/func/OrTest.java @@ -24,6 +24,7 @@ package org.cactoos.func; import java.util.Collections; +import org.cactoos.Scalar; import org.cactoos.list.ArrayAsIterable; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; @@ -71,7 +72,7 @@ public void oneTrue() throws Exception { public void allTrue() throws Exception { MatcherAssert.assertThat( new Or( - new ArrayAsIterable<>( + new ArrayAsIterable>( new True(), new True(), new True(), From a51b4d13f07c17aec676ed7a1ac1d311eaeb4d6b Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 22 Jun 2017 07:21:12 +0300 Subject: [PATCH 151/169] #loops: checkstyle --- src/test/java/org/cactoos/func/AndTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/org/cactoos/func/AndTest.java b/src/test/java/org/cactoos/func/AndTest.java index 20506fc80c..90dd7c896c 100644 --- a/src/test/java/org/cactoos/func/AndTest.java +++ b/src/test/java/org/cactoos/func/AndTest.java @@ -41,6 +41,7 @@ * @version $Id$ * @since 0.8 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) */ public final class AndTest { From 613e56b06a7d2402317cc76e21f05d978a4d1ea4 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 22 Jun 2017 07:55:19 +0300 Subject: [PATCH 152/169] #189 more ctors --- README.md | 47 ++++++++++------------- src/main/java/org/cactoos/func/And.java | 50 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index ad3ec7f0c5..7365e97c81 100644 --- a/README.md +++ b/README.md @@ -142,8 +142,8 @@ new IterableAsCollection<>( To iterate a collection: ```java -new AllOf( - new TransformedIterable<>( +new And( + new MappedIterable<>( new ArrayAsIterable<>("how", "are", "you"), new ProcAsFunc<>( input -> { @@ -157,11 +157,9 @@ new AllOf( Or even more compact: ```java -new IterableAsBoolean( - new ArrayAsIterable<>("how", "are", "you"), - new ProcAsFunc<>( - input -> System.out.printf("Item: %s\n", i) - ) +new And( + input -> System.out.printf("Item: %s\n", i), + "how", "are", "you" ).asValue(); ``` @@ -171,9 +169,11 @@ To sort a list of words in the file: List sorted = new SortedList<>( new IterableAsList<>( new TextAsLines( - new InputAsText( - new FileAsInput( - new File("/tmp/names.txt") + new BytesAsText( + new InputAsBytes( + new FileAsInput( + new File("/tmp/names.txt") + ) ) ) ) @@ -185,7 +185,7 @@ To count elements in an iterable: ```java int total = new LengthOfIterable( - new ArrayAsIterable<>("how", "are", "you") + "how", "are", "you" ).asValue(); ``` @@ -203,15 +203,10 @@ This is its object-oriented alternative (no streams!): ```java new And<>( - new MappedIterable<>( - names, - new ProcAsFunc<>( - n -> { - System.out.printf("Hello, %s!\n", n); - }, - () -> true - ) - ) + names, + n -> { + System.out.printf("Hello, %s!\n", n); + } ).asValue(); ``` @@ -227,13 +222,11 @@ Here is its object-oriented alternative: ```java new And<>( - new MappedIterable<>( - new EndlessIterable<>(ready), - r -> { - System.out.prinln("Still waiting..."); - return !ready; - } - ) + new EndlessIterable<>(ready), + r -> { + System.out.prinln("Still waiting..."); + return !ready; + } ).asValue(); ``` diff --git a/src/main/java/org/cactoos/func/And.java b/src/main/java/org/cactoos/func/And.java index 91ba5c074b..8adc224e22 100644 --- a/src/main/java/org/cactoos/func/And.java +++ b/src/main/java/org/cactoos/func/And.java @@ -24,8 +24,11 @@ package org.cactoos.func; import java.util.Iterator; +import org.cactoos.Func; +import org.cactoos.Proc; import org.cactoos.Scalar; import org.cactoos.list.ArrayAsIterable; +import org.cactoos.list.MappedIterable; /** * Logical conjunction. @@ -43,6 +46,53 @@ public final class And implements Scalar { */ private final Iterable> iterable; + /** + * Ctor. + * @param proc Proc to map + * @param src The iterable + * @param Type of items in the iterable + */ + @SafeVarargs + public And(final Proc proc, final X... src) { + this(new ProcAsFunc<>(proc, true), src); + } + + /** + * Ctor. + * @param func Func to map + * @param src The iterable + * @param Type of items in the iterable + */ + @SafeVarargs + public And(final Func func, final X... src) { + this(new ArrayAsIterable<>(src), func); + } + + /** + * Ctor. + * @param src The iterable + * @param proc Proc to use + * @param Type of items in the iterable + */ + public And(final Iterable src, final Proc proc) { + this(src, new ProcAsFunc<>(proc, true)); + } + + /** + * Ctor. + * @param src The iterable + * @param func Func to map + * @param Type of items in the iterable + */ + public And(final Iterable src, final Func func) { + this( + new MappedIterable<>( + src, + item -> (Scalar) () -> func.apply(item) + ) + ); + } + /** * Ctor. * @param src The iterable From aa2eb706af003f489c55ea2edee9b39b36f46be2 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 22 Jun 2017 08:01:53 +0300 Subject: [PATCH 153/169] no recursion --- src/main/java/org/cactoos/func/And.java | 28 +++++++------------------ src/main/java/org/cactoos/func/Or.java | 28 +++++++------------------ 2 files changed, 14 insertions(+), 42 deletions(-) diff --git a/src/main/java/org/cactoos/func/And.java b/src/main/java/org/cactoos/func/And.java index 8adc224e22..4c8b422a8a 100644 --- a/src/main/java/org/cactoos/func/And.java +++ b/src/main/java/org/cactoos/func/And.java @@ -23,7 +23,6 @@ */ package org.cactoos.func; -import java.util.Iterator; import org.cactoos.Func; import org.cactoos.Proc; import org.cactoos.Scalar; @@ -112,27 +111,14 @@ public And(final Iterable> src) { @Override public Boolean asValue() throws Exception { - return this.conjunction(this.iterable.iterator(), true); - } - - /** - * Conjunction. - * - * @param iterator The iterator - * @param value Previous value - * @return The result - * @throws Exception If fails - */ - private Boolean conjunction( - final Iterator> iterator, - final boolean value - ) throws Exception { - final Boolean result; - if (iterator.hasNext() && value) { - result = this.conjunction(iterator, iterator.next().asValue()); - } else { - result = value; + boolean result = true; + for (final Scalar item : this.iterable) { + if (!item.asValue()) { + result = false; + break; + } } return result; } + } diff --git a/src/main/java/org/cactoos/func/Or.java b/src/main/java/org/cactoos/func/Or.java index efe68f03b3..ff424ae921 100644 --- a/src/main/java/org/cactoos/func/Or.java +++ b/src/main/java/org/cactoos/func/Or.java @@ -23,7 +23,6 @@ */ package org.cactoos.func; -import java.util.Iterator; import org.cactoos.Scalar; import org.cactoos.list.ArrayAsIterable; @@ -62,27 +61,14 @@ public Or(final Iterable> src) { @Override public Boolean asValue() throws Exception { - return this.disjunction(this.iterable.iterator(), false); - } - - /** - * Disjunction. - * - * @param iterator The iterator - * @param value Previous value - * @return The result - * @throws Exception If fails - */ - private Boolean disjunction( - final Iterator> iterator, - final boolean value - ) throws Exception { - final Boolean result; - if (iterator.hasNext() && !value) { - result = this.disjunction(iterator, iterator.next().asValue()); - } else { - result = value; + boolean result = false; + for (final Scalar item : this.iterable) { + if (item.asValue()) { + result = true; + break; + } } return result; } + } From 3850f5b12de9bb6c1a3cef2f0082454e06645b5d Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 22 Jun 2017 08:10:44 +0300 Subject: [PATCH 154/169] extra ctors --- README.md | 2 +- .../java/org/cactoos/text/BytesAsText.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7365e97c81..3aa8975b70 100644 --- a/README.md +++ b/README.md @@ -168,7 +168,7 @@ To sort a list of words in the file: ```java List sorted = new SortedList<>( new IterableAsList<>( - new TextAsLines( + new SplitText( new BytesAsText( new InputAsBytes( new FileAsInput( diff --git a/src/main/java/org/cactoos/text/BytesAsText.java b/src/main/java/org/cactoos/text/BytesAsText.java index b76b49947a..481305da41 100644 --- a/src/main/java/org/cactoos/text/BytesAsText.java +++ b/src/main/java/org/cactoos/text/BytesAsText.java @@ -27,7 +27,9 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import org.cactoos.Bytes; +import org.cactoos.Input; import org.cactoos.Text; +import org.cactoos.io.InputAsBytes; /** * Bytes as Text. @@ -53,6 +55,15 @@ public final class BytesAsText implements Text { */ private final Charset charset; + /** + * Ctor. + * @param input Bytes from the input + * @since 0.8 + */ + public BytesAsText(final Input input) { + this(new InputAsBytes(input)); + } + /** * Ctor. * @param bts Bytes to encapsulate @@ -69,6 +80,16 @@ public BytesAsText(final Bytes bts) { this(bts, StandardCharsets.UTF_8); } + /** + * Ctor. + * @param input Input + * @param cset Charset + * @since 0.8 + */ + public BytesAsText(final Input input, final Charset cset) { + this(new InputAsBytes(input), cset); + } + /** * Ctor. * @param bts Bytes to encapsulate From 5d012b3a358b63f8ec9a84c2a825798c780b6508 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 22 Jun 2017 08:11:06 +0300 Subject: [PATCH 155/169] extra ctor --- src/main/java/org/cactoos/io/BytesAsInput.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/org/cactoos/io/BytesAsInput.java b/src/main/java/org/cactoos/io/BytesAsInput.java index 0b13b0f5c5..400d324eed 100644 --- a/src/main/java/org/cactoos/io/BytesAsInput.java +++ b/src/main/java/org/cactoos/io/BytesAsInput.java @@ -28,6 +28,7 @@ import java.io.InputStream; import org.cactoos.Bytes; import org.cactoos.Input; +import org.cactoos.Text; import org.cactoos.text.ArrayAsBytes; import org.cactoos.text.TextAsBytes; @@ -47,6 +48,15 @@ public final class BytesAsInput implements Input { */ private final Bytes source; + /** + * Ctor. + * @param text The text + * @since 0.8 + */ + public BytesAsInput(final Text text) { + this(new TextAsBytes(text)); + } + /** * Ctor. * @param text The text From ae141b0b77a15f2e91b46bcf373c6e73882f3b57 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 22 Jun 2017 08:14:55 +0300 Subject: [PATCH 156/169] more ctors --- src/main/java/org/cactoos/io/FileAsInput.java | 24 +++++++++++++++++-- .../java/org/cactoos/io/FileAsOutput.java | 24 +++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/cactoos/io/FileAsInput.java b/src/main/java/org/cactoos/io/FileAsInput.java index e7611e8393..1c418996e1 100644 --- a/src/main/java/org/cactoos/io/FileAsInput.java +++ b/src/main/java/org/cactoos/io/FileAsInput.java @@ -28,6 +28,8 @@ import java.io.FileNotFoundException; import java.io.InputStream; import org.cactoos.Input; +import org.cactoos.Scalar; +import org.cactoos.func.UncheckedScalar; /** * File as Input. @@ -43,19 +45,37 @@ public final class FileAsInput implements Input { /** * The file. */ - private final File file; + private final UncheckedScalar file; /** * Ctor. * @param src The file */ public FileAsInput(final File src) { + this(() -> src); + } + + /** + * Ctor. + * @param src The file + * @since 0.8 + */ + public FileAsInput(final Scalar src) { + this(new UncheckedScalar<>(src)); + } + + /** + * Ctor. + * @param src The file + * @since 0.8 + */ + public FileAsInput(final UncheckedScalar src) { this.file = src; } @Override public InputStream stream() throws FileNotFoundException { - return new FileInputStream(this.file); + return new FileInputStream(this.file.asValue()); } } diff --git a/src/main/java/org/cactoos/io/FileAsOutput.java b/src/main/java/org/cactoos/io/FileAsOutput.java index 0a2a056cd3..7724910709 100644 --- a/src/main/java/org/cactoos/io/FileAsOutput.java +++ b/src/main/java/org/cactoos/io/FileAsOutput.java @@ -28,6 +28,8 @@ import java.io.FileOutputStream; import java.io.OutputStream; import org.cactoos.Output; +import org.cactoos.Scalar; +import org.cactoos.func.UncheckedScalar; /** * File as Output. @@ -43,19 +45,37 @@ public final class FileAsOutput implements Output { /** * The file. */ - private final File file; + private final UncheckedScalar file; /** * Ctor. * @param src The file */ public FileAsOutput(final File src) { + this(() -> src); + } + + /** + * Ctor. + * @param src The file + * @since 0.8 + */ + public FileAsOutput(final Scalar src) { + this(new UncheckedScalar<>(src)); + } + + /** + * Ctor. + * @param src The file + * @since 0.8 + */ + public FileAsOutput(final UncheckedScalar src) { this.file = src; } @Override public OutputStream stream() throws FileNotFoundException { - return new FileOutputStream(this.file); + return new FileOutputStream(this.file.asValue()); } } From ee5213e35d925c27b7fa956b671f3ce773e8ecbf Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 22 Jun 2017 08:19:34 +0300 Subject: [PATCH 157/169] texts --- src/main/java/org/cactoos/io/NotNullInput.java | 2 +- src/main/java/org/cactoos/io/NotNullOutput.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/io/NotNullInput.java b/src/main/java/org/cactoos/io/NotNullInput.java index 4dbf4983b4..8d481c2537 100644 --- a/src/main/java/org/cactoos/io/NotNullInput.java +++ b/src/main/java/org/cactoos/io/NotNullInput.java @@ -54,7 +54,7 @@ public NotNullInput(final Input input) { @Override public InputStream stream() throws IOException { if (this.origin == null) { - throw new IOException("invalid input (null)"); + throw new IOException("NULL instead of a valid input"); } return this.origin.stream(); } diff --git a/src/main/java/org/cactoos/io/NotNullOutput.java b/src/main/java/org/cactoos/io/NotNullOutput.java index b79c46ed14..82b072e2fd 100644 --- a/src/main/java/org/cactoos/io/NotNullOutput.java +++ b/src/main/java/org/cactoos/io/NotNullOutput.java @@ -54,7 +54,7 @@ public NotNullOutput(final Output output) { @Override public OutputStream stream() throws IOException { if (this.origin == null) { - throw new IOException("invalid output (null)"); + throw new IOException("NULL instead of a valid output"); } return this.origin.stream(); } From c9cbc74f8429cd8fa02447a8e9eb9c3689900865 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Thu, 22 Jun 2017 10:34:28 +0300 Subject: [PATCH 158/169] blog link --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 3aa8975b70..6ae102ed2f 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,9 @@ Java version required: 1.8+. ## Input/Output +More about it here: +[Object-Oriented Declarative Input/Output in Cactoos](http://www.yegor256.com/2017/06/22/object-oriented-input-output-in-cactoos.html). + To read a text file in UTF-8: ```java From 8a93fdfc7ac76362939f3d6720c9f03d9d860e11 Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Thu, 22 Jun 2017 08:50:37 -0300 Subject: [PATCH 159/169] #201: NormalizedText --- .../java/org/cactoos/text/NormalizedText.java | 65 +++++++++++++++++++ .../org/cactoos/text/NormalizedTextTest.java | 49 ++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 src/main/java/org/cactoos/text/NormalizedText.java create mode 100644 src/test/java/org/cactoos/text/NormalizedTextTest.java diff --git a/src/main/java/org/cactoos/text/NormalizedText.java b/src/main/java/org/cactoos/text/NormalizedText.java new file mode 100644 index 0000000000..72e237c0a1 --- /dev/null +++ b/src/main/java/org/cactoos/text/NormalizedText.java @@ -0,0 +1,65 @@ +/** + * 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; + +/** + * Normalize (replace sequences of whitespace characters by a single space) + * a Text. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.9 + */ +public final class NormalizedText implements Text { + + /** + * The text. + */ + private final Text origin; + + /** + * Ctor. + * @param text A Text + */ + public NormalizedText(final String text) { + this(new StringAsText(text)); + } + + /** + * Ctor. + * @param text A Text + */ + public NormalizedText(final Text text) { + this.origin = text; + } + + @Override + public String asString() throws IOException { + return new TrimmedText(this.origin).asString().replaceAll("\\s+", " "); + } +} + diff --git a/src/test/java/org/cactoos/text/NormalizedTextTest.java b/src/test/java/org/cactoos/text/NormalizedTextTest.java new file mode 100644 index 0000000000..1a5058353d --- /dev/null +++ b/src/test/java/org/cactoos/text/NormalizedTextTest.java @@ -0,0 +1,49 @@ +/** + * 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 NormalizedText}. + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.3 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class NormalizedTextTest { + + @Test + public void normalizesText() throws IOException { + MatcherAssert.assertThat( + "Can't normalize a text", + new NormalizedText(" \t hello \t\tworld \t"), + new TextHasString("hello world") + ); + } + +} From 0eb79a4ead7f654cef2b0d81b528589b01521dc6 Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Thu, 22 Jun 2017 08:53:03 -0300 Subject: [PATCH 160/169] Updated version --- src/test/java/org/cactoos/text/NormalizedTextTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/cactoos/text/NormalizedTextTest.java b/src/test/java/org/cactoos/text/NormalizedTextTest.java index 1a5058353d..5192c6e5c5 100644 --- a/src/test/java/org/cactoos/text/NormalizedTextTest.java +++ b/src/test/java/org/cactoos/text/NormalizedTextTest.java @@ -32,7 +32,7 @@ * Test case for {@link NormalizedText}. * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ - * @since 0.3 + * @since 0.4 * @checkstyle JavadocMethodCheck (500 lines) */ public final class NormalizedTextTest { From 56c85ccf59111c3c2f1a0c74a9e7b67552332021 Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Thu, 22 Jun 2017 08:54:57 -0300 Subject: [PATCH 161/169] Updated to version 0.9 --- src/test/java/org/cactoos/text/NormalizedTextTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/cactoos/text/NormalizedTextTest.java b/src/test/java/org/cactoos/text/NormalizedTextTest.java index 5192c6e5c5..cb3bbfb81b 100644 --- a/src/test/java/org/cactoos/text/NormalizedTextTest.java +++ b/src/test/java/org/cactoos/text/NormalizedTextTest.java @@ -32,7 +32,7 @@ * Test case for {@link NormalizedText}. * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ - * @since 0.4 + * @since 0.9 * @checkstyle JavadocMethodCheck (500 lines) */ public final class NormalizedTextTest { From 5fa946b90ef04a557787e9357e976238bd78e4bd Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Thu, 22 Jun 2017 09:06:16 -0300 Subject: [PATCH 162/169] #201: RepeatedText --- .../java/org/cactoos/text/RepeatedText.java | 77 +++++++++++++++++++ .../org/cactoos/text/RepeatedTextTest.java | 58 ++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 src/main/java/org/cactoos/text/RepeatedText.java create mode 100644 src/test/java/org/cactoos/text/RepeatedTextTest.java diff --git a/src/main/java/org/cactoos/text/RepeatedText.java b/src/main/java/org/cactoos/text/RepeatedText.java new file mode 100644 index 0000000000..ea09013f60 --- /dev/null +++ b/src/main/java/org/cactoos/text/RepeatedText.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; + +/** + * Repeat an text count times. + * + *

There is no thread-safety guarantee. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.9 + */ +public final class RepeatedText implements Text { + + /** + * The text. + */ + private final Text origin; + + /** + * How many times repeat the Text. + */ + private final int count; + + /** + * Ctor. + * @param text A String + * @param count How many times repeat the Text + */ + public RepeatedText(final String text, final int count) { + this(new StringAsText(text), count); + } + + /** + * Ctor. + * @param text The Text + * @param count How many times repeat the Text + */ + public RepeatedText(final Text text, final int count) { + this.origin = text; + this.count = count; + } + + @Override + public String asString() throws IOException { + final StringBuilder out = new StringBuilder(); + for (int cnt = 0; cnt < this.count; ++cnt) { + out.append(this.origin.asString()); + } + return out.toString(); + } +} diff --git a/src/test/java/org/cactoos/text/RepeatedTextTest.java b/src/test/java/org/cactoos/text/RepeatedTextTest.java new file mode 100644 index 0000000000..75971ba590 --- /dev/null +++ b/src/test/java/org/cactoos/text/RepeatedTextTest.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.text; + +import org.cactoos.TextHasString; +import org.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test case for {@link RepeatedText}. + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.9 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class RepeatedTextTest { + + @Test + public void repeatsWordsText() { + MatcherAssert.assertThat( + "Can't repeats a text", + // @checkstyle MagicNumber (1 line) + new RepeatedText("hello", 2), + new TextHasString("hellohello") + ); + } + + @Test + public void repeatsCharText() { + MatcherAssert.assertThat( + "Can't repeats a char", + // @checkstyle MagicNumber (1 line) + new RepeatedText("A", 5), + new TextHasString("AAAAA") + ); + } +} From f23de41a9a0122d4c74759cf121c15bed8efb569 Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Thu, 22 Jun 2017 09:22:32 -0300 Subject: [PATCH 163/169] #205: JoinedText --- .../java/org/cactoos/text/JoinedText.java | 113 ++++++++++++++++++ .../java/org/cactoos/text/JoinedTextTest.java | 64 ++++++++++ 2 files changed, 177 insertions(+) create mode 100644 src/main/java/org/cactoos/text/JoinedText.java create mode 100644 src/test/java/org/cactoos/text/JoinedTextTest.java diff --git a/src/main/java/org/cactoos/text/JoinedText.java b/src/main/java/org/cactoos/text/JoinedText.java new file mode 100644 index 0000000000..b886bea385 --- /dev/null +++ b/src/main/java/org/cactoos/text/JoinedText.java @@ -0,0 +1,113 @@ +/** + * 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 java.util.StringJoiner; +import org.cactoos.Text; +import org.cactoos.list.ArrayAsIterable; +import org.cactoos.list.MappedIterable; + +/** + * Join a Text. + * + *

There is no thread-safety guarantee. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.9 + */ +public final class JoinedText implements Text { + + /** + * The texts. + */ + private final Iterable texts; + + /** + * The delimiter. + */ + private final Text delimiter; + + /** + * Ctor. + * @param delimiter Delimit among texts + * @param texts Texts to be joined + */ + public JoinedText(final String delimiter, final String... texts) { + this( + new StringAsText(delimiter), + new MappedIterable<>( + new ArrayAsIterable<>(texts), + text -> new StringAsText(text) + ) + ); + } + + /** + * Ctor. + * @param delimiter Delimit among texts + * @param texts Texts to be joined + */ + public JoinedText(final String delimiter, final Text... texts) { + this(delimiter, new ArrayAsIterable<>(texts)); + } + + /** + * Ctor. + * @param delimiter Delimit among texts + * @param texts Texts to be joined + */ + public JoinedText(final String delimiter, final Iterable texts) { + this(new StringAsText(delimiter), texts); + } + + /** + * Ctor. + * @param delimiter Delimit among texts + * @param texts Texts to be joined + */ + public JoinedText(final Text delimiter, final Text... texts) { + this(delimiter, new ArrayAsIterable<>(texts)); + } + + /** + * Ctor. + * @param delimiter Delimit among texts + * @param texts Texts to be joined + */ + public JoinedText(final Text delimiter, final Iterable texts) { + this.delimiter = delimiter; + this.texts = texts; + } + + @Override + public String asString() throws IOException { + final StringJoiner joint = new StringJoiner(this.delimiter.asString()); + for (final Text text : this.texts) { + joint.add(text.asString()); + } + return joint.toString(); + } +} diff --git a/src/test/java/org/cactoos/text/JoinedTextTest.java b/src/test/java/org/cactoos/text/JoinedTextTest.java new file mode 100644 index 0000000000..e05de1d579 --- /dev/null +++ b/src/test/java/org/cactoos/text/JoinedTextTest.java @@ -0,0 +1,64 @@ +/** + * 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 JoinedText}. + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.9 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class JoinedTextTest { + + @Test + @SuppressWarnings("PMD.AvoidDuplicateLiterals") + public void joinsStrings() throws IOException { + MatcherAssert.assertThat( + "Can't join strings", + new JoinedText(" ", "hello", "world"), + new TextHasString("hello world") + ); + } + + @Test + @SuppressWarnings("PMD.AvoidDuplicateLiterals") + public void joinsTexts() throws IOException { + MatcherAssert.assertThat( + "Can't join texts", + new JoinedText( + " ", + new StringAsText("hello"), + new StringAsText("world") + ), + new TextHasString("hello world") + ); + } + +} From 7fcc2be773a445919f91da4ac0ffb396333600e2 Mon Sep 17 00:00:00 2001 From: Fabricio Cabral Date: Thu, 22 Jun 2017 18:28:47 -0300 Subject: [PATCH 164/169] Removed duplicated literals --- src/test/java/org/cactoos/text/JoinedTextTest.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/cactoos/text/JoinedTextTest.java b/src/test/java/org/cactoos/text/JoinedTextTest.java index e05de1d579..b5f971781c 100644 --- a/src/test/java/org/cactoos/text/JoinedTextTest.java +++ b/src/test/java/org/cactoos/text/JoinedTextTest.java @@ -38,7 +38,6 @@ public final class JoinedTextTest { @Test - @SuppressWarnings("PMD.AvoidDuplicateLiterals") public void joinsStrings() throws IOException { MatcherAssert.assertThat( "Can't join strings", @@ -48,16 +47,15 @@ public void joinsStrings() throws IOException { } @Test - @SuppressWarnings("PMD.AvoidDuplicateLiterals") public void joinsTexts() throws IOException { MatcherAssert.assertThat( "Can't join texts", new JoinedText( " ", - new StringAsText("hello"), - new StringAsText("world") + new StringAsText("foo"), + new StringAsText("bar") ), - new TextHasString("hello world") + new TextHasString("foo bar") ); } From 3b3d34c8286104f954af277025c1ac4ec543b4fc Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 23 Jun 2017 09:03:11 +0300 Subject: [PATCH 165/169] #208: UncheckedInput, UncheckedOutput --- .../java/org/cactoos/io/InputAsLSInput.java | 16 ++--- .../java/org/cactoos/io/UncheckedInput.java | 59 +++++++++++++++++++ .../java/org/cactoos/io/UncheckedOutput.java | 59 +++++++++++++++++++ .../java/org/cactoos/text/StringAsUrl.java | 27 ++++----- .../java/org/cactoos/text/UrlAsString.java | 27 ++++----- src/test/java/org/cactoos/func/RetryFunc.java | 23 +++++--- 6 files changed, 159 insertions(+), 52 deletions(-) create mode 100644 src/main/java/org/cactoos/io/UncheckedInput.java create mode 100644 src/main/java/org/cactoos/io/UncheckedOutput.java diff --git a/src/main/java/org/cactoos/io/InputAsLSInput.java b/src/main/java/org/cactoos/io/InputAsLSInput.java index ba74890c05..0814935665 100644 --- a/src/main/java/org/cactoos/io/InputAsLSInput.java +++ b/src/main/java/org/cactoos/io/InputAsLSInput.java @@ -23,13 +23,13 @@ */ package org.cactoos.io; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.StandardCharsets; import org.cactoos.Input; import org.cactoos.text.BytesAsText; +import org.cactoos.text.UncheckedText; import org.w3c.dom.ls.LSInput; // @checkstyle AbbreviationAsWordInNameCheck (10 lines) @@ -102,11 +102,7 @@ public void setCharacterStream(final Reader stream) { @Override public InputStream getByteStream() { - try { - return this.input.stream(); - } catch (final IOException ex) { - throw new IllegalStateException(ex); - } + return new UncheckedInput(this.input).stream(); } @Override @@ -118,11 +114,9 @@ public void setByteStream(final InputStream stream) { @Override public String getStringData() { - try { - return new BytesAsText(new InputAsBytes(this.input)).asString(); - } catch (final IOException ex) { - throw new IllegalStateException(ex); - } + return new UncheckedText( + new BytesAsText(new InputAsBytes(this.input)) + ).asString(); } @Override diff --git a/src/main/java/org/cactoos/io/UncheckedInput.java b/src/main/java/org/cactoos/io/UncheckedInput.java new file mode 100644 index 0000000000..afea5f1aec --- /dev/null +++ b/src/main/java/org/cactoos/io/UncheckedInput.java @@ -0,0 +1,59 @@ +/** + * 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.InputStream; +import org.cactoos.Input; +import org.cactoos.func.UncheckedScalar; + +/** + * Input that doesn't throw checked {@link Exception}. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.9 + */ +public final class UncheckedInput implements Input { + + /** + * Original input. + */ + private final Input input; + + /** + * Ctor. + * @param ipt Input + */ + public UncheckedInput(final Input ipt) { + this.input = ipt; + } + + @Override + public InputStream stream() { + return new UncheckedScalar<>(this.input::stream).asValue(); + } + +} diff --git a/src/main/java/org/cactoos/io/UncheckedOutput.java b/src/main/java/org/cactoos/io/UncheckedOutput.java new file mode 100644 index 0000000000..f229810780 --- /dev/null +++ b/src/main/java/org/cactoos/io/UncheckedOutput.java @@ -0,0 +1,59 @@ +/** + * 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.OutputStream; +import org.cactoos.Output; +import org.cactoos.func.UncheckedScalar; + +/** + * Input that doesn't throw checked {@link Exception}. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.9 + */ +public final class UncheckedOutput implements Output { + + /** + * Original output. + */ + private final Output output; + + /** + * Ctor. + * @param opt Output + */ + public UncheckedOutput(final Output opt) { + this.output = opt; + } + + @Override + public OutputStream stream() { + return new UncheckedScalar<>(this.output::stream).asValue(); + } + +} diff --git a/src/main/java/org/cactoos/text/StringAsUrl.java b/src/main/java/org/cactoos/text/StringAsUrl.java index 92d5d95969..502b4025bb 100644 --- a/src/main/java/org/cactoos/text/StringAsUrl.java +++ b/src/main/java/org/cactoos/text/StringAsUrl.java @@ -24,7 +24,6 @@ package org.cactoos.text; import java.io.IOException; -import java.io.UncheckedIOException; import java.net.URLEncoder; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -71,32 +70,28 @@ public StringAsUrl(final Text url) { /** * Ctor. * @param url The URL as String - * @param encoding The encoding + * @param enc The encoding */ - public StringAsUrl(final String url, final Charset encoding) { - this(new StringAsText(url), encoding); + public StringAsUrl(final String url, final Charset enc) { + this(new StringAsText(url), enc); } /** * Ctor. * @param url The URL as Text - * @param encoding The encoding + * @param enc The encoding */ - public StringAsUrl(final Text url, final Charset encoding) { + public StringAsUrl(final Text url, final Charset enc) { this.source = url; - this.encoding = encoding; + this.encoding = enc; } @Override - public String asValue() { - try { - return URLEncoder.encode( - this.source.asString(), - this.encoding.name() - ); - } catch (final IOException ex) { - throw new UncheckedIOException(ex); - } + public String asValue() throws IOException { + return URLEncoder.encode( + this.source.asString(), + this.encoding.name() + ); } } diff --git a/src/main/java/org/cactoos/text/UrlAsString.java b/src/main/java/org/cactoos/text/UrlAsString.java index 81b6199f4f..3bbff71c17 100644 --- a/src/main/java/org/cactoos/text/UrlAsString.java +++ b/src/main/java/org/cactoos/text/UrlAsString.java @@ -24,7 +24,6 @@ package org.cactoos.text; import java.io.IOException; -import java.io.UncheckedIOException; import java.net.URLDecoder; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -71,32 +70,28 @@ public UrlAsString(final Text url) { /** * Ctor. * @param url The URL as String - * @param encoding The encoding + * @param enc The encoding */ - public UrlAsString(final String url, final Charset encoding) { - this(new StringAsText(url), encoding); + public UrlAsString(final String url, final Charset enc) { + this(new StringAsText(url), enc); } /** * Ctor. * @param url The URL as Text - * @param encoding The encoding + * @param enc The encoding */ - public UrlAsString(final Text url, final Charset encoding) { + public UrlAsString(final Text url, final Charset enc) { this.source = url; - this.encoding = encoding; + this.encoding = enc; } @Override - public String asValue() { - try { - return URLDecoder.decode( - this.source.asString(), - this.encoding.name() - ); - } catch (final IOException ex) { - throw new UncheckedIOException(ex); - } + public String asValue() throws IOException { + return URLDecoder.decode( + this.source.asString(), + this.encoding.name() + ); } } diff --git a/src/test/java/org/cactoos/func/RetryFunc.java b/src/test/java/org/cactoos/func/RetryFunc.java index 4ed70018cf..463a375e77 100644 --- a/src/test/java/org/cactoos/func/RetryFunc.java +++ b/src/test/java/org/cactoos/func/RetryFunc.java @@ -24,7 +24,6 @@ package org.cactoos.func; import org.cactoos.Func; -import org.cactoos.text.FormattedText; /** * Func that will try a few times before throwing an exception. @@ -45,9 +44,9 @@ public final class RetryFunc implements Func { private final Func func; /** - * Maximum number of attempts to make. + * Exit condition. */ - private final int max; + private final Func exit; /** * Ctor. @@ -64,8 +63,17 @@ public RetryFunc(final Func fnc) { * @param attempts Maximum number of attempts */ public RetryFunc(final Func fnc, final int attempts) { + this(fnc, attempt -> attempt >= attempts); + } + + /** + * Ctor. + * @param fnc Func original + * @param ext Exit condition, returns TRUE if there is no more reason to try + */ + public RetryFunc(final Func fnc, final Func ext) { this.func = fnc; - this.max = attempts; + this.exit = ext; } @Override @@ -73,12 +81,9 @@ public RetryFunc(final Func fnc, final int attempts) { public Y apply(final X input) throws Exception { int attempt = 0; Exception error = new IllegalArgumentException( - new FormattedText( - "Maximum number of attempts is too small: %d", - this.max - ).asString() + "An immediate exit, didn't have a chance to try at least once" ); - while (attempt < this.max) { + while (!this.exit.apply(attempt)) { try { return this.func.apply(input); } catch (final InterruptedException ex) { From 2a7364fd7c7271e4557649e07e5631e33a92c01e Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 23 Jun 2017 19:48:34 +0300 Subject: [PATCH 166/169] #208: doc --- src/main/java/org/cactoos/io/LengthOfInput.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/io/LengthOfInput.java b/src/main/java/org/cactoos/io/LengthOfInput.java index 33dc108df4..9a6b0cc337 100644 --- a/src/main/java/org/cactoos/io/LengthOfInput.java +++ b/src/main/java/org/cactoos/io/LengthOfInput.java @@ -29,7 +29,7 @@ import org.cactoos.Scalar; /** - * Length of Input. + * Length of {@link Input}. * *

There is no thread-safety guarantee. * From 4a750692e55885b1b23fba3aaef545971289d16c Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 23 Jun 2017 19:52:09 +0300 Subject: [PATCH 167/169] #208: more ctors --- .../org/cactoos/io/InputAsProperties.java | 31 +++++++++++++++++++ .../org/cactoos/io/InputAsPropertiesTest.java | 7 +---- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/cactoos/io/InputAsProperties.java b/src/main/java/org/cactoos/io/InputAsProperties.java index 734c6b1bd4..d243c92884 100644 --- a/src/main/java/org/cactoos/io/InputAsProperties.java +++ b/src/main/java/org/cactoos/io/InputAsProperties.java @@ -26,8 +26,12 @@ import java.io.IOException; import java.io.InputStream; import java.util.Properties; +import org.cactoos.Bytes; import org.cactoos.Input; import org.cactoos.Scalar; +import org.cactoos.Text; +import org.cactoos.text.StringAsText; +import org.cactoos.text.TextAsBytes; /** * Input as {@link Properties}. @@ -45,6 +49,33 @@ public final class InputAsProperties implements Scalar { */ private final Input source; + /** + * Ctor. + * @param text Text + * @since 0.9 + */ + public InputAsProperties(final String text) { + this(new StringAsText(text)); + } + + /** + * Ctor. + * @param text Text + * @since 0.9 + */ + public InputAsProperties(final Text text) { + this(new TextAsBytes(text)); + } + + /** + * Ctor. + * @param bytes Bytes + * @since 0.9 + */ + public InputAsProperties(final Bytes bytes) { + this(new BytesAsInput(bytes)); + } + /** * Ctor. * @param input The input diff --git a/src/test/java/org/cactoos/io/InputAsPropertiesTest.java b/src/test/java/org/cactoos/io/InputAsPropertiesTest.java index 22ddf936be..b4938f1445 100644 --- a/src/test/java/org/cactoos/io/InputAsPropertiesTest.java +++ b/src/test/java/org/cactoos/io/InputAsPropertiesTest.java @@ -26,7 +26,6 @@ import java.util.Properties; import org.cactoos.ScalarHasValue; import org.cactoos.func.FuncAsMatcher; -import org.cactoos.text.TextAsBytes; import org.hamcrest.MatcherAssert; import org.junit.Test; @@ -44,11 +43,7 @@ public void readsInputContent() { MatcherAssert.assertThat( "Can't read properties from an input", new InputAsProperties( - new BytesAsInput( - new TextAsBytes( - "foo=Hello, world!\nbar=works fine?\n" - ) - ) + "foo=Hello, world!\nbar=works fine?\n" ), new ScalarHasValue<>( new FuncAsMatcher( From e17d6ad96001de42fe142ec3c878d1d48b95d431 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 23 Jun 2017 20:18:06 +0300 Subject: [PATCH 168/169] #213: RetryScalar --- .../java/org/cactoos/func/RetryFunc.java | 0 .../java/org/cactoos/func/RetryScalar.java | 86 +++++++++++++++++++ 2 files changed, 86 insertions(+) rename src/{test => main}/java/org/cactoos/func/RetryFunc.java (100%) create mode 100644 src/main/java/org/cactoos/func/RetryScalar.java diff --git a/src/test/java/org/cactoos/func/RetryFunc.java b/src/main/java/org/cactoos/func/RetryFunc.java similarity index 100% rename from src/test/java/org/cactoos/func/RetryFunc.java rename to src/main/java/org/cactoos/func/RetryFunc.java diff --git a/src/main/java/org/cactoos/func/RetryScalar.java b/src/main/java/org/cactoos/func/RetryScalar.java new file mode 100644 index 0000000000..39c104acf5 --- /dev/null +++ b/src/main/java/org/cactoos/func/RetryScalar.java @@ -0,0 +1,86 @@ +/** + * 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.Func; +import org.cactoos.Scalar; + +/** + * Func that will try a few times before throwing an exception. + * + *

There is no thread-safety guarantee. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @param Type of output + * @since 0.9 + */ +public final class RetryScalar implements Scalar { + + /** + * Original scalar. + */ + private final Scalar scalar; + + /** + * Exit condition. + */ + private final Func exit; + + /** + * Ctor. + * @param slr Scalar original + */ + public RetryScalar(final Scalar slr) { + // @checkstyle MagicNumberCheck (1 line) + this(slr, 3); + } + + /** + * Ctor. + * @param slr Scalar original + * @param attempts Maximum number of attempts + */ + public RetryScalar(final Scalar slr, final int attempts) { + this(slr, attempt -> attempt >= attempts); + } + + /** + * Ctor. + * @param slr Func original + * @param ext Exit condition, returns TRUE if there is no more reason to try + */ + public RetryScalar(final Scalar slr, final Func ext) { + this.scalar = slr; + this.exit = ext; + } + + @Override + public T asValue() throws Exception { + return new RetryFunc<>( + (Func) input -> this.scalar.asValue(), + this.exit + ).apply(true); + } +} From 540a3c79edaf0f2a1e2f9e3052f9b6dc0f1fd7a6 Mon Sep 17 00:00:00 2001 From: yegor256 Date: Fri, 23 Jun 2017 20:19:24 +0300 Subject: [PATCH 169/169] #213: RetryScalarTest --- .../org/cactoos/func/RetryScalarTest.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/test/java/org/cactoos/func/RetryScalarTest.java diff --git a/src/test/java/org/cactoos/func/RetryScalarTest.java b/src/test/java/org/cactoos/func/RetryScalarTest.java new file mode 100644 index 0000000000..40b6d5c7bc --- /dev/null +++ b/src/test/java/org/cactoos/func/RetryScalarTest.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.func; + +import java.security.SecureRandom; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link RetryScalar}. + * + * @author Yegor Bugayenko (yegor256@gmail.com) + * @version $Id$ + * @since 0.9 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class RetryScalarTest { + + @Test + public void runsScalarMultipleTimes() throws Exception { + MatcherAssert.assertThat( + new RetryScalar<>( + () -> { + // @checkstyle MagicNumberCheck (1 line) + if (new SecureRandom().nextDouble() > 0.3d) { + throw new IllegalArgumentException("May happen"); + } + return 0; + }, + Integer.MAX_VALUE + ).asValue(), + Matchers.equalTo(0) + ); + } + +}