diff --git a/src/main/java/org/cactoos/collection/HeadOf.java b/src/main/java/org/cactoos/collection/HeadOf.java index 061521555b..c39196ecbc 100644 --- a/src/main/java/org/cactoos/collection/HeadOf.java +++ b/src/main/java/org/cactoos/collection/HeadOf.java @@ -40,31 +40,31 @@ public final class HeadOf extends CollectionEnvelope { /** * Ctor. - * @param skip How many to skip + * @param num Number of head elements * @param src Source elements */ @SafeVarargs - public HeadOf(final int skip, final T... src) { - this(skip, new IterableOf<>(src)); + public HeadOf(final int num, final T... src) { + this(num, new IterableOf<>(src)); } /** * Ctor. - * @param skip How many to skip + * @param num Number of head elements * @param src Source iterable */ - public HeadOf(final int skip, final Iterable src) { - this(skip, new CollectionOf(src)); + public HeadOf(final int num, final Iterable src) { + this(num, new CollectionOf(src)); } /** * Ctor. - * @param skip How many to skip + * @param num Number of head elements * @param src Source collection */ - public HeadOf(final int skip, final Collection src) { + public HeadOf(final int num, final Collection src) { super(() -> new CollectionOf( - new org.cactoos.iterable.HeadOf(skip, src) + new org.cactoos.iterable.HeadOf(num, src) )); } diff --git a/src/main/java/org/cactoos/collection/TailOf.java b/src/main/java/org/cactoos/collection/TailOf.java index eb84c4d382..d304ec3f41 100644 --- a/src/main/java/org/cactoos/collection/TailOf.java +++ b/src/main/java/org/cactoos/collection/TailOf.java @@ -40,31 +40,31 @@ public final class TailOf extends CollectionEnvelope { /** * Ctor. - * @param skip How many to skip + * @param num Number of tail elements * @param src Source elements */ @SafeVarargs - public TailOf(final int skip, final T... src) { - this(skip, new IterableOf<>(src)); + public TailOf(final int num, final T... src) { + this(num, new IterableOf<>(src)); } /** * Ctor. - * @param skip How many to skip + * @param num Number of tail elements * @param src Source iterable */ - public TailOf(final int skip, final Iterable src) { - this(skip, new CollectionOf(src)); + public TailOf(final int num, final Iterable src) { + this(num, new CollectionOf(src)); } /** * Ctor. - * @param skip How many to skip + * @param num Number of tail elements * @param src Source collection */ - public TailOf(final int skip, final Collection src) { + public TailOf(final int num, final Collection src) { super(() -> new CollectionOf( - new org.cactoos.iterable.TailOf(skip, src) + new org.cactoos.iterable.TailOf(num, src) )); } } diff --git a/src/main/java/org/cactoos/iterable/HeadOf.java b/src/main/java/org/cactoos/iterable/HeadOf.java index aabbec2fa8..7aaf84b21b 100644 --- a/src/main/java/org/cactoos/iterable/HeadOf.java +++ b/src/main/java/org/cactoos/iterable/HeadOf.java @@ -37,23 +37,22 @@ public final class HeadOf extends IterableEnvelope { /** * Ctor. - * @param skip How many to skip + * @param num Number of head elements * @param src The underlying iterable */ @SafeVarargs - public HeadOf(final int skip, final T... src) { - this(skip, new IterableOf<>(src)); + public HeadOf(final int num, final T... src) { + this(num, new IterableOf<>(src)); } /** * Ctor. - * @param skip Count skip elements + * @param num Number of head elements * @param iterable Decorated iterable */ - public HeadOf(final int skip, final Iterable iterable) { + public HeadOf(final int num, final Iterable iterable) { super(() -> () -> new org.cactoos.iterator.HeadOf<>( - skip, iterable.iterator() + num, iterable.iterator() )); } - } diff --git a/src/main/java/org/cactoos/iterable/TailOf.java b/src/main/java/org/cactoos/iterable/TailOf.java index 8a3879074b..099d7d278a 100644 --- a/src/main/java/org/cactoos/iterable/TailOf.java +++ b/src/main/java/org/cactoos/iterable/TailOf.java @@ -37,22 +37,22 @@ public final class TailOf extends IterableEnvelope { /** * Ctor. - * @param skip How many to skip from the bootm + * @param num Number of tail elements * @param src The underlying iterable */ @SafeVarargs - public TailOf(final int skip, final T... src) { - this(skip, new IterableOf<>(src)); + public TailOf(final int num, final T... src) { + this(num, new IterableOf<>(src)); } /** * Ctor. - * @param skip Count skip elements + * @param num Number of tail elements * @param iterable Decorated iterable */ - public TailOf(final int skip, final Iterable iterable) { + public TailOf(final int num, final Iterable iterable) { super(() -> () -> new org.cactoos.iterator.TailOf<>( - skip, iterable.iterator() + num, iterable.iterator() )); } } diff --git a/src/main/java/org/cactoos/iterator/HeadOf.java b/src/main/java/org/cactoos/iterator/HeadOf.java index 3178bf5e0e..e4b68690f6 100644 --- a/src/main/java/org/cactoos/iterator/HeadOf.java +++ b/src/main/java/org/cactoos/iterator/HeadOf.java @@ -44,27 +44,29 @@ public final class HeadOf implements Iterator { private final Iterator origin; /** - * Count skip elements. + * Number of head elements. */ - private int omit; + private final int head; + + /** + * Current element index. + */ + private int current; /** * Ctor. * @param iterator Decorated iterator - * @param skip Count skip elements + * @param num Num of head elements */ - public HeadOf(final int skip, final Iterator iterator) { + public HeadOf(final int num, final Iterator iterator) { this.origin = iterator; - this.omit = skip; + this.head = num; + this.current = 0; } @Override public boolean hasNext() { - while (this.omit > 0 && this.origin.hasNext()) { - this.origin.next(); - --this.omit; - } - return this.origin.hasNext(); + return this.current < this.head && this.origin.hasNext(); } @Override @@ -74,6 +76,7 @@ public T next() { "The iterator doesn't have items any more" ); } + ++this.current; return this.origin.next(); } } diff --git a/src/main/java/org/cactoos/iterator/TailOf.java b/src/main/java/org/cactoos/iterator/TailOf.java index 95c0ca4de2..bc9e69a845 100644 --- a/src/main/java/org/cactoos/iterator/TailOf.java +++ b/src/main/java/org/cactoos/iterator/TailOf.java @@ -39,31 +39,35 @@ public final class TailOf implements Iterator { /** - * Reversed iterator. + * Decorated iterator. */ - private final Iterator reversed; + private final Iterator origin; /** * Ctor. * @param iterator Decorated iterator - * @param skip Count skip elements + * @param num Number of tail elements */ - public TailOf(final int skip, final Iterator iterator) { - this.reversed = new HeadOf<>( - skip, - new LinkedList<>( - new CollectionOf<>(iterator) - ).descendingIterator() - ); + public TailOf(final int num, final Iterator iterator) { + this.origin = new LinkedList<>( + new CollectionOf<>( + new HeadOf<>( + num, + new LinkedList<>( + new CollectionOf<>(iterator) + ).descendingIterator() + ) + ) + ).descendingIterator(); } @Override public boolean hasNext() { - return this.reversed.hasNext(); + return this.origin.hasNext(); } @Override public T next() { - return this.reversed.next(); + return this.origin.next(); } } diff --git a/src/test/java/org/cactoos/collection/HeadOfTest.java b/src/test/java/org/cactoos/collection/HeadOfTest.java index dda010b478..48096e4ff8 100644 --- a/src/test/java/org/cactoos/collection/HeadOfTest.java +++ b/src/test/java/org/cactoos/collection/HeadOfTest.java @@ -24,7 +24,6 @@ package org.cactoos.collection; -import org.cactoos.iterable.IterableOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; @@ -35,92 +34,23 @@ * @version $Id$ * @since 0.29 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) */ public final class HeadOfTest { @Test @SuppressWarnings("PMD.AvoidDuplicateLiterals") - public void skipIterable() throws Exception { + public void headCollection() throws Exception { MatcherAssert.assertThat( "Can't skip elements in iterable", new HeadOf<>( - 2, - new IterableOf<>("one", "two", "three", "four") - ), - Matchers.contains( - "three", - "four" - ) - ); - } - - @Test - @SuppressWarnings("PMD.AvoidDuplicateLiterals") - public void skipArray() throws Exception { - MatcherAssert.assertThat( - "Can't skip elements in array", - new HeadOf<>( - 2, - "one", "two", "three", "four" - ), - Matchers.contains( - "three", - "four" - ) - ); - } - - @Test - @SuppressWarnings("PMD.AvoidDuplicateLiterals") - public void skipCollection() throws Exception { - MatcherAssert.assertThat( - "Can't skip elements in collection", - new HeadOf<>( - 2, - new CollectionOf<>("one", "two", "three", "four") - ), - Matchers.contains( - "three", - "four" - ) - ); - } - - @Test - public void skippedAllElements() throws Exception { - MatcherAssert.assertThat( - "Can't skip all elements", - new HeadOf<>( - 2, - "one", "two" - ), - Matchers.empty() - ); - } - - @Test - public void skippedMoreThanExists() throws Exception { - MatcherAssert.assertThat( - "Can't skip more than exists", - new HeadOf<>( - Integer.MAX_VALUE, - "one", "two" - ), - Matchers.empty() - ); - } - - @Test - @SuppressWarnings("PMD.AvoidDuplicateLiterals") - public void skippedNegativeSize() throws Exception { - MatcherAssert.assertThat( - "Can't process negative skipped size", - new HeadOf<>( - -1, + 3, "one", "two", "three", "four" ), Matchers.contains( - "one", "two", "three", "four" + "one", + "two", + "three" ) ); } diff --git a/src/test/java/org/cactoos/collection/TailOfTest.java b/src/test/java/org/cactoos/collection/TailOfTest.java index d40271cb2d..8d1b4802cb 100644 --- a/src/test/java/org/cactoos/collection/TailOfTest.java +++ b/src/test/java/org/cactoos/collection/TailOfTest.java @@ -47,8 +47,8 @@ public void tailCollection() throws Exception { "one", "two", "three", "four" ), Matchers.contains( - "two", - "one" + "three", + "four" ) ); } diff --git a/src/test/java/org/cactoos/iterable/HeadOfTest.java b/src/test/java/org/cactoos/iterable/HeadOfTest.java index 1033398b91..eab887254c 100644 --- a/src/test/java/org/cactoos/iterable/HeadOfTest.java +++ b/src/test/java/org/cactoos/iterable/HeadOfTest.java @@ -38,7 +38,7 @@ public final class HeadOfTest { @Test @SuppressWarnings("PMD.AvoidDuplicateLiterals") - public void skipIterable() throws Exception { + public void headIterable() throws Exception { MatcherAssert.assertThat( "Can't skip elements in iterable", new HeadOf<>( @@ -47,8 +47,8 @@ public void skipIterable() throws Exception { ) ), Matchers.contains( - "three", - "four" + "one", + "two" ) ); } diff --git a/src/test/java/org/cactoos/iterable/TailOfTest.java b/src/test/java/org/cactoos/iterable/TailOfTest.java index 01cd138593..3c00a2582a 100644 --- a/src/test/java/org/cactoos/iterable/TailOfTest.java +++ b/src/test/java/org/cactoos/iterable/TailOfTest.java @@ -33,6 +33,7 @@ * @version $Id$ * @since 0.30.1 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) */ public final class TailOfTest { @@ -42,12 +43,13 @@ public void tailIterable() throws Exception { MatcherAssert.assertThat( "Can't get tail portion of iterable", new TailOf<>( - 2, + 3, "one", "two", "three", "four" ), Matchers.contains( "two", - "one" + "three", + "four" ) ); } diff --git a/src/test/java/org/cactoos/iterator/HeadOfTest.java b/src/test/java/org/cactoos/iterator/HeadOfTest.java index 1d5a1e364a..f3babf3c6a 100644 --- a/src/test/java/org/cactoos/iterator/HeadOfTest.java +++ b/src/test/java/org/cactoos/iterator/HeadOfTest.java @@ -35,6 +35,7 @@ * @version $Id$ * @since 0.8 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) */ public final class HeadOfTest { @@ -50,16 +51,41 @@ public void headIterator() throws Exception { ).iterator() ), Matchers.contains( - "three", - "four" + "one", + "two" ) ); } + @Test + public void returnsIntactIterator() throws Exception { + MatcherAssert.assertThat( + new LengthOf( + new HeadOf<>( + 3, + new IterableOf<>( + "one", "two" + ).iterator() + ) + ).intValue(), + Matchers.equalTo(2) + ); + } + + @Test(expected = NoSuchElementException.class) + public void returnsEmptyIterator() throws Exception { + new HeadOf<>( + 0, + new IterableOf<>( + "one", "two" + ).iterator() + ).next(); + } + @Test(expected = NoSuchElementException.class) - public void errorSkippedMoreThanExists() throws Exception { + public void emptyIteratorForNegativeSize() throws Exception { new HeadOf<>( - 2, + -1, new IterableOf<>( "one", "two" ).iterator() diff --git a/src/test/java/org/cactoos/iterator/TailOfTest.java b/src/test/java/org/cactoos/iterator/TailOfTest.java index 17f2b127ff..e0a058334f 100644 --- a/src/test/java/org/cactoos/iterator/TailOfTest.java +++ b/src/test/java/org/cactoos/iterator/TailOfTest.java @@ -35,6 +35,7 @@ * @version $Id$ * @since 0.30.1 * @checkstyle JavadocMethodCheck (500 lines) + * @checkstyle MagicNumberCheck (500 lines) */ public final class TailOfTest { @@ -50,16 +51,41 @@ public void tailIterator() throws Exception { ).iterator() ), Matchers.contains( - "two", - "one" + "three", + "four" ) ); } + @Test + public void returnsIntactIterator() throws Exception { + MatcherAssert.assertThat( + new LengthOf( + new TailOf<>( + 3, + new IterableOf<>( + "one", "two" + ).iterator() + ) + ).intValue(), + Matchers.equalTo(2) + ); + } + + @Test(expected = NoSuchElementException.class) + public void returnsEmptyIterator() throws Exception { + new TailOf<>( + 0, + new IterableOf<>( + "one", "two" + ).iterator() + ).next(); + } + @Test(expected = NoSuchElementException.class) - public void errorSkippedMoreThanExists() throws Exception { + public void emptyIteratorForNegativeSize() throws Exception { new TailOf<>( - 2, + -1, new IterableOf<>( "one", "two" ).iterator()