Skip to content

Commit

Permalink
Enforce immutability of IterableEnvelope through its iterator
Browse files Browse the repository at this point in the history
Ref: #862
  • Loading branch information
Isammoc committed May 29, 2018
1 parent a1e27f3 commit 1d88caf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/cactoos/iterable/IterableEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.Iterator;
import org.cactoos.Scalar;
import org.cactoos.iterator.Immutable;
import org.cactoos.scalar.UncheckedScalar;

/**
Expand Down Expand Up @@ -54,7 +55,9 @@ public IterableEnvelope(final Scalar<Iterable<X>> scalar) {

@Override
public final Iterator<X> iterator() {
return this.iterable.value().iterator();
return new Immutable<>(
this.iterable.value().iterator()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
* 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 {
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/org/cactoos/iterable/IterableEnvelopeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.iterable;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.junit.Test;

/**
* Test case for {@link IterableEnvelope}.
*
* @since 0.35
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class IterableEnvelopeTest {

@Test(expected = UnsupportedOperationException.class)
public void returnsIteratorWithUnsupportedRemove() {
final IterableEnvelope<String> list = new IterableEnvelope<String>(
() -> {
final List<String> inner = new LinkedList<>();
inner.add("eleven");
return inner;
}
) { };
final Iterator<String> iterator = list.iterator();
iterator.next();
iterator.remove();
}
}

1 comment on commit 1d88caf

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 1d88caf May 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 813-8130ed5b disappeared from src/test/java/org/cactoos/collection/CollectionEnvelopeTest.java, that's why I closed #862. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.