-
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
6 changed files
with
254 additions
and
7 deletions.
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
101 changes: 101 additions & 0 deletions
101
src/main/java/org/cactoos/list/ImmutableListIterator.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,101 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017-2019 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.ListIterator; | ||
|
||
/** | ||
* Immutable {@link ListIterator} that doesn't allow mutations. | ||
* | ||
* <p>There is no thread-safety guarantee. | ||
* | ||
* @param <T> Items type | ||
* @since 1.0 | ||
*/ | ||
public final class ImmutableListIterator<T> implements ListIterator<T> { | ||
|
||
/** | ||
* Original list iterator. | ||
*/ | ||
private final ListIterator<T> origin; | ||
|
||
/** | ||
* Ctor. | ||
* @param iter Original list iterator. | ||
*/ | ||
public ImmutableListIterator(final ListIterator<T> iter) { | ||
this.origin = iter; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return this.origin.hasNext(); | ||
} | ||
|
||
@Override | ||
public T next() { | ||
return this.origin.next(); | ||
} | ||
|
||
@Override | ||
public boolean hasPrevious() { | ||
return this.origin.hasPrevious(); | ||
} | ||
|
||
@Override | ||
public T previous() { | ||
return this.origin.previous(); | ||
} | ||
|
||
@Override | ||
public int nextIndex() { | ||
return this.origin.nextIndex(); | ||
} | ||
|
||
@Override | ||
public int previousIndex() { | ||
return this.origin.previousIndex(); | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
throw new UnsupportedOperationException( | ||
"List Iterator is read-only and doesn't allow removing items" | ||
); | ||
} | ||
|
||
@Override | ||
public void set(final T item) { | ||
throw new UnsupportedOperationException( | ||
"List Iterator is read-only and doesn't allow rewriting items" | ||
); | ||
} | ||
|
||
@Override | ||
public void add(final T item) { | ||
throw new UnsupportedOperationException( | ||
"List Iterator is read-only and doesn't allow adding items" | ||
); | ||
} | ||
} |
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
135 changes: 135 additions & 0 deletions
135
src/test/java/org/cactoos/list/ImmutableListIteratorTest.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,135 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017-2019 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 org.hamcrest.core.IsEqual; | ||
import org.junit.Test; | ||
import org.llorllale.cactoos.matchers.Assertion; | ||
import org.llorllale.cactoos.matchers.Throws; | ||
|
||
/** | ||
* Test case for {@link ImmutableListIterator}. | ||
* @since 1.0 | ||
* @checkstyle JavadocMethodCheck (500 lines) | ||
* @checkstyle JavadocTypeCheck (500 lines) | ||
* @checkstyle MagicNumberCheck (500 lines) | ||
*/ | ||
@SuppressWarnings({"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals"}) | ||
public final class ImmutableListIteratorTest { | ||
|
||
@Test | ||
public void mustReturnPreviousIndex() { | ||
new Assertion<>( | ||
"Must return next previous index", | ||
new ImmutableListIterator<>( | ||
new ListOf<>(1).listIterator() | ||
).previousIndex(), | ||
new IsEqual<>(-1) | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void mustReturnPreviousElement() { | ||
new Assertion<>( | ||
"Must return previous element", | ||
new ImmutableListIterator<>( | ||
new ListOf<>(3, 7).listIterator(1) | ||
).previous(), | ||
new IsEqual<>(3) | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void mustReturnNextIndex() { | ||
new Assertion<>( | ||
"Must return next index", | ||
new ImmutableListIterator<>( | ||
new ListOf<>(1).listIterator() | ||
).nextIndex(), | ||
new IsEqual<>(0) | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void mustReturnNextElement() { | ||
new Assertion<>( | ||
"Must return next element", | ||
new ImmutableListIterator<>( | ||
new ListOf<>(5, 11, 13).listIterator(1) | ||
).next(), | ||
new IsEqual<>(11) | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void mustRaiseErrorOnListIteratorRemove() { | ||
new Assertion<>( | ||
"Must throw error if modified with remove operation", | ||
() -> { | ||
new ImmutableListIterator<>( | ||
new ListOf<>(1, 2).listIterator() | ||
).remove(); | ||
return 0; | ||
}, | ||
new Throws<>( | ||
"List Iterator is read-only and doesn't allow removing items", | ||
UnsupportedOperationException.class | ||
) | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void mustRaiseErrorOnListIteratorAdd() { | ||
new Assertion<>( | ||
"Must throw error if modified with add operation", | ||
() -> { | ||
new ImmutableListIterator<>( | ||
new ListOf<>(1, 2).listIterator() | ||
).add(1); | ||
return 0; | ||
}, | ||
new Throws<>( | ||
"List Iterator is read-only and doesn't allow adding items", | ||
UnsupportedOperationException.class | ||
) | ||
).affirm(); | ||
} | ||
|
||
@Test | ||
public void mustRaiseErrorOnListIteratorSet() { | ||
new Assertion<>( | ||
"Must throw error if modified with set operation", | ||
() -> { | ||
new ImmutableListIterator<>( | ||
new ListOf<>(1, 2).listIterator() | ||
).set(1); | ||
return 0; | ||
}, | ||
new Throws<>( | ||
"List Iterator is read-only and doesn't allow rewriting items", | ||
UnsupportedOperationException.class | ||
) | ||
).affirm(); | ||
} | ||
} |
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
2136d9d
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
898-fb66ac0b
disappeared fromsrc/main/java/org/cactoos/list/Immutable.java
, that's why I closed #1219. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.2136d9d
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
1219-fd8dadee
discovered insrc/main/java/org/cactoos/list/ListIteratorOf.java
and submitted as #1230. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.