-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
src/test/java/org/cactoos/collection/CollectionEnvelopeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
c19e8a9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
813-8130ed5b
discovered insrc/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.