Skip to content

Commit

Permalink
IteratorOfLongs and IteratorOfShorts yegor256#838
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed May 15, 2018
1 parent 8bf936b commit 9ae1859
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 25 deletions.
17 changes: 2 additions & 15 deletions src/main/java/org/cactoos/iterable/IterableOfLongs.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,12 @@
*/
package org.cactoos.iterable;

import java.util.ArrayList;
import java.util.Collection;
import org.cactoos.iterator.IteratorOfLongs;

/**
* Iterable of long values.
*
* @since 1.0
* @todo #802:30min Introduce IteratorOfLongs and IteratorOfShorts which will
* take array of their related primitive types (long, short) and
* produce iterator of reference type (Long, Short).
* Refactor appropriate IterableOf* classes by using those newly created
* iterators to avoid unnecessary copying elements to a new array.
*/
public final class IterableOfLongs extends IterableEnvelope<Long> {

Expand All @@ -43,13 +37,6 @@ public final class IterableOfLongs extends IterableEnvelope<Long> {
* @param values Long values
*/
public IterableOfLongs(final long... values) {
super(() -> {
final Collection<Long> iterable =
new ArrayList<>(values.length);
for (final long value: values) {
iterable.add(value);
}
return iterable;
});
super(() -> () -> new IteratorOfLongs(values));
}
}
12 changes: 2 additions & 10 deletions src/main/java/org/cactoos/iterable/IterableOfShorts.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
*/
package org.cactoos.iterable;

import java.util.ArrayList;
import java.util.Collection;
import org.cactoos.iterator.IteratorOfShorts;

/**
* Iterable of short values.
Expand All @@ -39,13 +38,6 @@ public final class IterableOfShorts extends IterableEnvelope<Short> {
*/
@SuppressWarnings("PMD.AvoidUsingShortType")
public IterableOfShorts(final short... values) {
super(() -> {
final Collection<Short> iterable =
new ArrayList<>(values.length);
for (final short value: values) {
iterable.add(value);
}
return iterable;
});
super(() -> () -> new IteratorOfShorts(values));
}
}
72 changes: 72 additions & 0 deletions src/main/java/org/cactoos/iterator/IteratorOfLongs.java
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()];
}
}
73 changes: 73 additions & 0 deletions src/main/java/org/cactoos/iterator/IteratorOfShorts.java
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 src/test/java/org/cactoos/iterator/IteratorOfLongsTest.java
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 src/test/java/org/cactoos/iterator/IteratorOfShortsTest.java
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();
}
}

0 comments on commit 9ae1859

Please sign in to comment.