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..ede1f74621 --- /dev/null +++ b/src/main/java/org/cactoos/list/CycledIterable.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.Iterator; + +/** + * Cycled Iterable. + * + *

There is no thread-safety guarantee. + * + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @param Type of item + * @since 0.8 + */ +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..81580bb528 --- /dev/null +++ b/src/main/java/org/cactoos/list/CycledIterator.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.list; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +/** + * Cycled Iterator. + * + *

There is no thread-safety guarantee. + * + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @param Type of item + * @since 0.8 + */ +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; + } + + @Override + public boolean hasNext() { + if (this.iterator == null || !this.iterator.hasNext()) { + this.iterator = this.iterable.iterator(); + } + 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/CycledIterableTest.java b/src/test/java/org/cactoos/list/CycledIterableTest.java new file mode 100644 index 0000000000..503d471f03 --- /dev/null +++ b/src/test/java/org/cactoos/list/CycledIterableTest.java @@ -0,0 +1,72 @@ +/** + * 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 CycledIterable}. + * @author Ilia Rogozhin (ilia.rogozhin@gmail.com) + * @version $Id$ + * @since 0.8 + * @checkstyle JavadocMethodCheck (500 lines) + */ +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", expected, "three" + ) + ), + // @checkstyle MagicNumberCheck (1 line)< + 7 + ), + new ScalarHasValue<>( + expected + ) + ); + } + + @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 new file mode 100644 index 0000000000..e60efa0763 --- /dev/null +++ b/src/test/java/org/cactoos/list/CycledIteratorTest.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.Collections; +import java.util.NoSuchElementException; +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.8 + * @checkstyle JavadocMethodCheck (500 lines) + */ +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", expected, "three" + ) + ), + // @checkstyle MagicNumberCheck (1 line) + 7 + ), + new ScalarHasValue<>( + expected + ) + ); + } + + @Test(expected = NoSuchElementException.class) + public void notCycledEmptyTest() throws Exception { + new CycledIterator<>( + Collections::emptyIterator + ).next(); + } +}