forked from yegor256/cactoos
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
8bf936b
commit 9ae1859
Showing
6 changed files
with
292 additions
and
25 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
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,72 @@ | ||
/* | ||
* 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.NoSuchElementException; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* {@link Iterator} that returns the {@code long}s as {@link Long}s. | ||
* | ||
* <p>There is no thread-safety guarantee.</p> | ||
* | ||
* @since 0.34 | ||
*/ | ||
public final class IteratorOfLongs implements Iterator<Long> { | ||
|
||
/** | ||
* The list of items to iterate. | ||
*/ | ||
private final long[] items; | ||
|
||
/** | ||
* Current position. | ||
*/ | ||
private final AtomicInteger position; | ||
|
||
/** | ||
* Ctor. | ||
* @param itms Items to iterate | ||
*/ | ||
public IteratorOfLongs(final long... itms) { | ||
this.items = itms; | ||
this.position = new AtomicInteger(0); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return this.position.intValue() < this.items.length; | ||
} | ||
|
||
@Override | ||
public Long next() { | ||
if (!this.hasNext()) { | ||
throw new NoSuchElementException( | ||
"The iterator doesn't have any more items" | ||
); | ||
} | ||
return this.items[this.position.getAndIncrement()]; | ||
} | ||
} |
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,73 @@ | ||
/* | ||
* 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.NoSuchElementException; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* {@link Iterator} that returns the {@code short}s as {@link Short}s. | ||
* | ||
* <p>There is no thread-safety guarantee.</p> | ||
* | ||
* @since 0.34 | ||
*/ | ||
@SuppressWarnings("PMD.AvoidUsingShortType") | ||
public final class IteratorOfShorts implements Iterator<Short> { | ||
|
||
/** | ||
* The list of items to iterate. | ||
*/ | ||
private final short[] items; | ||
|
||
/** | ||
* Current position. | ||
*/ | ||
private final AtomicInteger position; | ||
|
||
/** | ||
* Ctor. | ||
* @param itms Items to iterate | ||
*/ | ||
public IteratorOfShorts(final short... itms) { | ||
this.items = itms; | ||
this.position = new AtomicInteger(0); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return this.position.intValue() < this.items.length; | ||
} | ||
|
||
@Override | ||
public Short next() { | ||
if (!this.hasNext()) { | ||
throw new NoSuchElementException( | ||
"The iterator doesn't have any more items" | ||
); | ||
} | ||
return this.items[this.position.getAndIncrement()]; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/test/java/org/cactoos/iterator/IteratorOfLongsTest.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,70 @@ | ||
/* | ||
* 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.NoSuchElementException; | ||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.core.IsEqual; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Tests for {@link IteratorOfLongs}. | ||
* | ||
* @since 0.34 | ||
* @checkstyle JavadocMethodCheck (500 lines) | ||
*/ | ||
public final class IteratorOfLongsTest { | ||
@Test | ||
public void emptyIteratorDoesNotHaveNext() { | ||
MatcherAssert.assertThat( | ||
"hasNext is true for empty iterator.", | ||
new IteratorOfLongs().hasNext(), | ||
new IsEqual<>(false) | ||
); | ||
} | ||
|
||
@Test(expected = NoSuchElementException.class) | ||
public void emptyIteratorThrowsException() { | ||
new IteratorOfLongs().next(); | ||
} | ||
|
||
@Test | ||
public void nonEmptyIteratorDoesNotHaveNext() { | ||
final IteratorOfLongs iterator = new IteratorOfLongs(1, 2); | ||
iterator.next(); | ||
iterator.next(); | ||
MatcherAssert.assertThat( | ||
"hasNext is true for fully traversed iterator.", | ||
iterator.hasNext(), | ||
new IsEqual<>(false) | ||
); | ||
} | ||
|
||
@Test(expected = NoSuchElementException.class) | ||
public void nonEmptyIteratorThrowsException() { | ||
final IteratorOfLongs iterator = new IteratorOfLongs(1); | ||
iterator.next(); | ||
iterator.next(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
src/test/java/org/cactoos/iterator/IteratorOfShortsTest.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,73 @@ | ||
/* | ||
* 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.NoSuchElementException; | ||
import org.hamcrest.MatcherAssert; | ||
import org.hamcrest.core.IsEqual; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Tests for {@link IteratorOfShorts}. | ||
* | ||
* @since 0.34 | ||
* @checkstyle JavadocMethodCheck (500 lines) | ||
*/ | ||
@SuppressWarnings("PMD.AvoidUsingShortType") | ||
public final class IteratorOfShortsTest { | ||
@Test | ||
public void emptyIteratorDoesNotHaveNext() { | ||
MatcherAssert.assertThat( | ||
"hasNext is true for empty iterator.", | ||
new IteratorOfShorts().hasNext(), | ||
new IsEqual<>(false) | ||
); | ||
} | ||
|
||
@Test(expected = NoSuchElementException.class) | ||
public void emptyIteratorThrowsException() { | ||
new IteratorOfShorts().next(); | ||
} | ||
|
||
@Test | ||
public void nonEmptyIteratorDoesNotHaveNext() { | ||
final IteratorOfShorts iterator = new IteratorOfShorts( | ||
(short) 1, (short) 2 | ||
); | ||
iterator.next(); | ||
iterator.next(); | ||
MatcherAssert.assertThat( | ||
"hasNext is true for fully traversed iterator.", | ||
iterator.hasNext(), | ||
new IsEqual<>(false) | ||
); | ||
} | ||
|
||
@Test(expected = NoSuchElementException.class) | ||
public void nonEmptyIteratorThrowsException() { | ||
final IteratorOfShorts iterator = new IteratorOfShorts((short) 1); | ||
iterator.next(); | ||
iterator.next(); | ||
} | ||
} |