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()) + ); + } + +}