diff --git a/src/main/java/org/cactoos/collection/CollectionEnvelope.java b/src/main/java/org/cactoos/collection/CollectionEnvelope.java index fb28da3055..fc16a8285a 100644 --- a/src/main/java/org/cactoos/collection/CollectionEnvelope.java +++ b/src/main/java/org/cactoos/collection/CollectionEnvelope.java @@ -26,6 +26,7 @@ import java.util.Collection; import java.util.Iterator; import org.cactoos.Scalar; +import org.cactoos.iterator.Immutable; import org.cactoos.scalar.UncheckedScalar; /** @@ -70,7 +71,7 @@ public final boolean isEmpty() { @Override public final Iterator iterator() { - return this.col.value().iterator(); + return new Immutable<>(this.col.value().iterator()); } @Override diff --git a/src/main/java/org/cactoos/iterator/Immutable.java b/src/main/java/org/cactoos/iterator/Immutable.java new file mode 100644 index 0000000000..46eb050ef8 --- /dev/null +++ b/src/main/java/org/cactoos/iterator/Immutable.java @@ -0,0 +1,61 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 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.iterator; + +import java.util.Iterator; + +/** + * Iterator that doesn't allow removal of elements. + * + *

There is no thread-safety guarantee.

+ * + * @param Element type + * @since 0.32 + */ +public final class Immutable implements Iterator { + + /** + * Decorated iterator. + */ + private final Iterator iterator; + + /** + * Ctor. + * @param iter Iterator to make immutable + */ + public Immutable(final Iterator iter) { + this.iterator = iter; + } + + @Override + public boolean hasNext() { + return this.iterator.hasNext(); + } + + @Override + public T next() { + return this.iterator.next(); + } + +} diff --git a/src/test/java/org/cactoos/collection/CollectionEnvelopeTest.java b/src/test/java/org/cactoos/collection/CollectionEnvelopeTest.java new file mode 100644 index 0000000000..58d4f27e74 --- /dev/null +++ b/src/test/java/org/cactoos/collection/CollectionEnvelopeTest.java @@ -0,0 +1,54 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 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.collection; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import org.junit.Test; + +/** + * Test case for {@link CollectionEnvelope}. + * + * @since 0.32 + * @todo #813:30min IterableEnvelope appears to be read only but it returns + * mutable Iterator through which user can remove elements. Implement immutable + * Iterator for IterableEnvelope `iterator()` method. + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class CollectionEnvelopeTest { + @Test(expected = UnsupportedOperationException.class) + public void returnsIteratorWithUnsupportedRemove() { + final CollectionEnvelope list = new CollectionEnvelope( + () -> { + final List inner = new LinkedList<>(); + inner.add("eleven"); + return inner; + } + ) { }; + final Iterator iterator = list.iterator(); + iterator.next(); + iterator.remove(); + } +} diff --git a/src/test/java/org/cactoos/iterator/ImmutableTest.java b/src/test/java/org/cactoos/iterator/ImmutableTest.java new file mode 100644 index 0000000000..336d5f6230 --- /dev/null +++ b/src/test/java/org/cactoos/iterator/ImmutableTest.java @@ -0,0 +1,69 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 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.iterator; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Random; +import org.hamcrest.MatcherAssert; +import org.hamcrest.core.IsEqual; +import org.junit.Test; + +/** + * Test Case for {@link Immutable}. + * + * @since 0.32 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class ImmutableTest { + @Test(expected = UnsupportedOperationException.class) + public void doesNotAllowRemovingOfElements() { + final List list = new LinkedList<>(); + list.add("one"); + final Iterator immutable = new Immutable<>(list.iterator()); + immutable.next(); + immutable.remove(); + } + + @Test + public void decoratesNext() { + final int value = new Random().nextInt(); + final Iterator immutable = new Immutable<>( + new IteratorOf<>(value) + ); + MatcherAssert.assertThat(immutable.next(), new IsEqual<>(value)); + } + + @Test + public void decoratesHasNext() { + final int value = new Random().nextInt(); + final Iterator immutable = new Immutable<>( + new IteratorOf<>(value) + ); + MatcherAssert.assertThat(immutable.hasNext(), new IsEqual<>(true)); + immutable.next(); + MatcherAssert.assertThat(immutable.hasNext(), new IsEqual<>(false)); + } +}