Skip to content

Commit

Permalink
#185 review remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedran authored and Vedran committed May 3, 2018
1 parent 3f50772 commit b7be7be
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 144 deletions.
18 changes: 9 additions & 9 deletions src/main/java/org/cactoos/collection/HeadOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,31 @@ public final class HeadOf<T> extends CollectionEnvelope<T> {

/**
* 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<T> src) {
this(skip, new CollectionOf<T>(src));
public HeadOf(final int num, final Iterable<T> src) {
this(num, new CollectionOf<T>(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<T> src) {
public HeadOf(final int num, final Collection<T> src) {
super(() -> new CollectionOf<T>(
new org.cactoos.iterable.HeadOf<T>(skip, src)
new org.cactoos.iterable.HeadOf<T>(num, src)
));
}

Expand Down
18 changes: 9 additions & 9 deletions src/main/java/org/cactoos/collection/TailOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,31 @@ public final class TailOf<T> extends CollectionEnvelope<T> {

/**
* 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<T> src) {
this(skip, new CollectionOf<T>(src));
public TailOf(final int num, final Iterable<T> src) {
this(num, new CollectionOf<T>(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<T> src) {
public TailOf(final int num, final Collection<T> src) {
super(() -> new CollectionOf<T>(
new org.cactoos.iterable.TailOf<T>(skip, src)
new org.cactoos.iterable.TailOf<T>(num, src)
));
}
}
13 changes: 6 additions & 7 deletions src/main/java/org/cactoos/iterable/HeadOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,22 @@ public final class HeadOf<T> extends IterableEnvelope<T> {

/**
* 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<T> iterable) {
public HeadOf(final int num, final Iterable<T> iterable) {
super(() -> () -> new org.cactoos.iterator.HeadOf<>(
skip, iterable.iterator()
num, iterable.iterator()
));
}

}
12 changes: 6 additions & 6 deletions src/main/java/org/cactoos/iterable/TailOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ public final class TailOf<T> extends IterableEnvelope<T> {

/**
* 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<T> iterable) {
public TailOf(final int num, final Iterable<T> iterable) {
super(() -> () -> new org.cactoos.iterator.TailOf<>(
skip, iterable.iterator()
num, iterable.iterator()
));
}
}
23 changes: 13 additions & 10 deletions src/main/java/org/cactoos/iterator/HeadOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,29 @@ public final class HeadOf<T> implements Iterator<T> {
private final Iterator<T> 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<T> iterator) {
public HeadOf(final int num, final Iterator<T> 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
Expand All @@ -74,6 +76,7 @@ public T next() {
"The iterator doesn't have items any more"
);
}
++this.current;
return this.origin.next();
}
}
28 changes: 16 additions & 12 deletions src/main/java/org/cactoos/iterator/TailOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,35 @@
public final class TailOf<T> implements Iterator<T> {

/**
* Reversed iterator.
* Decorated iterator.
*/
private final Iterator<T> reversed;
private final Iterator<T> origin;

/**
* Ctor.
* @param iterator Decorated iterator
* @param skip Count skip elements
* @param num Number of tail elements
*/
public TailOf(final int skip, final Iterator<T> iterator) {
this.reversed = new HeadOf<>(
skip,
new LinkedList<>(
new CollectionOf<>(iterator)
).descendingIterator()
);
public TailOf(final int num, final Iterator<T> 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();
}
}
82 changes: 6 additions & 76 deletions src/test/java/org/cactoos/collection/HeadOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"
)
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/cactoos/collection/TailOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public void tailCollection() throws Exception {
"one", "two", "three", "four"
),
Matchers.contains(
"two",
"one"
"three",
"four"
)
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/cactoos/iterable/HeadOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<>(
Expand All @@ -47,8 +47,8 @@ public void skipIterable() throws Exception {
)
),
Matchers.contains(
"three",
"four"
"one",
"two"
)
);
}
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/org/cactoos/iterable/TailOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* @version $Id$
* @since 0.30.1
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle MagicNumberCheck (500 lines)
*/
public final class TailOfTest {

Expand All @@ -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"
)
);
}
Expand Down
Loading

0 comments on commit b7be7be

Please sign in to comment.