Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed May 11, 2018
2 parents 31dbd97 + c9a36b9 commit c19e8a9
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/org/cactoos/collection/CollectionEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -70,7 +71,7 @@ public final boolean isEmpty() {

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

@Override
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/org/cactoos/iterator/Immutable.java
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>There is no thread-safety guarantee.</p>
*
* @param <T> Element type
* @since 0.32
*/
public final class Immutable<T> implements Iterator<T> {

/**
* Decorated iterator.
*/
private final Iterator<T> iterator;

/**
* Ctor.
* @param iter Iterator to make immutable
*/
public Immutable(final Iterator<T> iter) {
this.iterator = iter;
}

@Override
public boolean hasNext() {
return this.iterator.hasNext();
}

@Override
public T next() {
return this.iterator.next();
}

}
54 changes: 54 additions & 0 deletions src/test/java/org/cactoos/collection/CollectionEnvelopeTest.java
Original file line number Diff line number Diff line change
@@ -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<String> list = new CollectionEnvelope<String>(
() -> {
final List<String> inner = new LinkedList<>();
inner.add("eleven");
return inner;
}
) { };
final Iterator<String> iterator = list.iterator();
iterator.next();
iterator.remove();
}
}
69 changes: 69 additions & 0 deletions src/test/java/org/cactoos/iterator/ImmutableTest.java
Original file line number Diff line number Diff line change
@@ -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<String> list = new LinkedList<>();
list.add("one");
final Iterator<String> immutable = new Immutable<>(list.iterator());
immutable.next();
immutable.remove();
}

@Test
public void decoratesNext() {
final int value = new Random().nextInt();
final Iterator<Integer> 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<Integer> immutable = new Immutable<>(
new IteratorOf<>(value)
);
MatcherAssert.assertThat(immutable.hasNext(), new IsEqual<>(true));
immutable.next();
MatcherAssert.assertThat(immutable.hasNext(), new IsEqual<>(false));
}
}

1 comment on commit c19e8a9

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on c19e8a9 May 11, 2018

Choose a reason for hiding this comment

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

Puzzle 813-8130ed5b discovered in src/test/java/org/cactoos/collection/CollectionEnvelopeTest.java and submitted as #862. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.