From 99373f59a91180bce39309073434ac1214339816 Mon Sep 17 00:00:00 2001 From: Ilia Date: Wed, 14 Jun 2017 22:52:08 +0300 Subject: [PATCH 1/5] 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 3865cf4e773f0658796b12830e98f1b68eb404c8 Mon Sep 17 00:00:00 2001 From: Ilia Rogozhin Date: Fri, 16 Jun 2017 18:16:12 +0300 Subject: [PATCH 2/5] 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 3/5] 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 97b6ae28b9274af324a3ce0ce6d8f24f1fa3be86 Mon Sep 17 00:00:00 2001 From: Ilia Date: Mon, 19 Jun 2017 20:05:24 +0300 Subject: [PATCH 4/5] 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 5/5] 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(); + } }