Skip to content

Commit

Permalink
(yegor256#1183) Paged leverages IterableEnvelope
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoku committed Jan 10, 2020
1 parent 7c03d9c commit e0cfc8f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 83 deletions.
104 changes: 28 additions & 76 deletions src/main/java/org/cactoos/iterable/Paged.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@
import org.cactoos.Func;
import org.cactoos.Scalar;
import org.cactoos.func.UncheckedFunc;
import org.cactoos.scalar.And;
import org.cactoos.scalar.Folded;
import org.cactoos.scalar.Or;
import org.cactoos.scalar.Sticky;
import org.cactoos.scalar.SumOfInt;
import org.cactoos.scalar.Unchecked;

/**
Expand All @@ -41,16 +37,10 @@
* <p>There is no thread-safety guarantee.
*
* @param <X> Type of item
* @since 0.12
* @since 1.0
* @checkstyle ClassDataAbstractionCouplingCheck (550 lines)
*/
@SuppressWarnings("PMD.OnlyOneConstructorShouldDoInitialization")
public final class Paged<X> implements Iterable<X> {

/**
* The encapsulated iterator.
*/
private final Iterable<X> itr;
public final class Paged<X> extends IterableEnvelope<X> {

/**
* Paged iterable.
Expand All @@ -61,80 +51,42 @@ public final class Paged<X> implements Iterable<X> {
* @param next Subsequent bags of elements
* @param <I> Custom iterator
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
public <I extends Iterator<X>> Paged(
final Scalar<I> first, final Func<I, I> next
) {
// @checkstyle AnonInnerLengthCheck (30 lines)
this.itr = new IterableOf<>(
() -> new Iterator<X>() {
private Unchecked<I> current = new Unchecked<>(
new Sticky<>(first)
);
private final UncheckedFunc<I, I> subsequent =
new UncheckedFunc<>(next);
super(
new IterableOf<>(
() -> new Iterator<X>() {
private Unchecked<I> current = new Unchecked<>(
new Sticky<>(first)
);
private final UncheckedFunc<I, I> subsequent =
new UncheckedFunc<>(next);

@Override
public boolean hasNext() {
if (!this.current.value().hasNext()) {
final I next = this.subsequent.apply(
this.current.value()
);
this.current = new Unchecked<>(
new Sticky<>(() -> next)
);
@Override
public boolean hasNext() {
if (!this.current.value().hasNext()) {
final I next = this.subsequent.apply(
this.current.value()
);
this.current = new Unchecked<>(
new Sticky<>(() -> next)
);
}
return this.current.value().hasNext();
}
return this.current.value().hasNext();
}

@Override
public X next() {
if (this.hasNext()) {
return this.current.value().next();
@Override
public X next() {
if (this.hasNext()) {
return this.current.value().next();
}
throw new NoSuchElementException();
}
throw new NoSuchElementException();
}
}
);
}

@Override
public Iterator<X> iterator() {
return this.itr.iterator();
}

@Override
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings("EQ_UNUSUAL")
public boolean equals(final Object other) {
return new Unchecked<>(
new Or(
() -> other == this,
new And(
() -> other != null,
() -> Iterable.class.isAssignableFrom(other.getClass()),
() -> other.equals(this.itr)
)
)
).value();
}

// @checkstyle MagicNumberCheck (30 lines)
@Override
public int hashCode() {
return new Unchecked<>(
new Folded<>(
42,
(hash, entry) -> new SumOfInt(
() -> 37 * hash,
entry::hashCode
).value(),
this.itr
)
).value();
);
}

@Override
public String toString() {
return this.itr.toString();
}
}
13 changes: 6 additions & 7 deletions src/test/java/org/cactoos/iterable/PagedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.cactoos.iterable;

import java.util.Iterator;
import org.cactoos.Scalar;
import org.cactoos.iterator.IteratorOf;
import org.cactoos.scalar.LengthOf;
import org.cactoos.scalar.Ternary;
Expand All @@ -34,9 +35,7 @@

/**
* Test case for {@link Paged}.
* @since 0.12
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle ClassDataAbstractionCoupling (2 lines)
* @since 1.0
*/
public final class PagedTest {

Expand All @@ -55,9 +54,9 @@ public void containAllPagedContentInOrder() throws Exception {
new Paged<>(
() -> pages.next().iterator(),
page -> new Ternary<>(
() -> pages.hasNext(),
pages::hasNext,
() -> pages.next().iterator(),
() -> new IteratorOf<String>()
(Scalar<Iterator<String>>) IteratorOf::new
).value()
),
new IsEqual<>(new Joined<>(first, second, third))
Expand All @@ -79,9 +78,9 @@ public void reportTotalPagedLength() throws Exception {
new Paged<>(
() -> pages.next().iterator(),
page -> new Ternary<>(
() -> pages.hasNext(),
pages::hasNext,
() -> pages.next().iterator(),
() -> new IteratorOf<String>()
(Scalar<Iterator<String>>) IteratorOf::new
).value()
),
new IsIterableWithSize<>(
Expand Down

0 comments on commit e0cfc8f

Please sign in to comment.