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
8467c1d
commit f2a05ac
Showing
6 changed files
with
308 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,74 @@ | ||
/** | ||
* 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> | ||
* | ||
* @author Victor Noel (victor.noel@crazydwarves.org) | ||
* @version $Id$ | ||
* @since 0.32 | ||
*/ | ||
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,75 @@ | ||
/** | ||
* 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> | ||
* | ||
* @author Victor Noel (victor.noel@crazydwarves.org) | ||
* @version $Id$ | ||
* @since 0.32 | ||
*/ | ||
@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()]; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
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,77 @@ | ||
/** | ||
* 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}. | ||
* | ||
* @author Victor Noel (victor.noel@crazydwarves.org) | ||
* @version $Id$ | ||
* @since 0.32 | ||
* @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() { | ||
MatcherAssert.assertThat( | ||
"hasNext is true for fully traversed iterator.", | ||
this.iteratorWithFetchedElements().hasNext(), | ||
new IsEqual<>(false) | ||
); | ||
} | ||
|
||
@Test(expected = NoSuchElementException.class) | ||
public void nonEmptyIteratorThrowsException() { | ||
this.iteratorWithFetchedElements().next(); | ||
} | ||
|
||
private IteratorOfLongs iteratorWithFetchedElements() { | ||
final IteratorOfLongs iterator = new IteratorOfLongs( | ||
1, 2, 3 | ||
); | ||
iterator.next(); | ||
iterator.next(); | ||
iterator.next(); | ||
return iterator; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
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,78 @@ | ||
/** | ||
* 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}. | ||
* | ||
* @author Victor Noel (victor.noel@crazydwarves.org) | ||
* @version $Id$ | ||
* @since 0.32 | ||
* @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() { | ||
MatcherAssert.assertThat( | ||
"hasNext is true for fully traversed iterator.", | ||
this.iteratorWithFetchedElements().hasNext(), | ||
new IsEqual<>(false) | ||
); | ||
} | ||
|
||
@Test(expected = NoSuchElementException.class) | ||
public void nonEmptyIteratorThrowsException() { | ||
this.iteratorWithFetchedElements().next(); | ||
} | ||
|
||
private IteratorOfShorts iteratorWithFetchedElements() { | ||
final IteratorOfShorts iterator = new IteratorOfShorts( | ||
new short[] {1, 2, 3 } | ||
); | ||
iterator.next(); | ||
iterator.next(); | ||
iterator.next(); | ||
return iterator; | ||
} | ||
} |