diff --git a/src/main/java/org/cactoos/iterable/Paged.java b/src/main/java/org/cactoos/iterable/Paged.java index e8162ce2f1..870ff243f6 100644 --- a/src/main/java/org/cactoos/iterable/Paged.java +++ b/src/main/java/org/cactoos/iterable/Paged.java @@ -24,13 +24,8 @@ package org.cactoos.iterable; import java.util.Iterator; -import java.util.NoSuchElementException; -import java.util.concurrent.atomic.AtomicReference; import org.cactoos.Func; import org.cactoos.Scalar; -import org.cactoos.func.UncheckedFunc; -import org.cactoos.scalar.Sticky; -import org.cactoos.scalar.Unchecked; /** * Paged iterable. @@ -41,11 +36,7 @@ * * @param Type of item * @since 0.47 - * @todo #1183:30m Continue refactoring and add `iterator.Paged` by - * extracting inner anon class from this class. The checkstyle suppression - * should be removed after that. */ -@SuppressWarnings("PMD.OnlyOneConstructorShouldDoInitialization") public final class Paged extends IterableEnvelope { /** @@ -55,47 +46,12 @@ public final class Paged extends IterableEnvelope { * @param next Subsequent bags of elements * @param Custom iterator */ - @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors") public > Paged( final Scalar first, final Func next ) { - // @checkstyle AnonInnerLengthCheck (30 lines) super( new IterableOf<>( - () -> new Iterator() { - private final AtomicReference> current = - new AtomicReference<>( - new Unchecked<>( - new Sticky<>(first) - ) - ); - - private final UncheckedFunc subsequent = - new UncheckedFunc<>(next); - - @Override - public boolean hasNext() { - if (!this.current.get().value().hasNext()) { - final I next = this.subsequent.apply( - this.current.get().value() - ); - this.current.set( - new Unchecked<>( - new Sticky<>(() -> next) - ) - ); - } - return this.current.get().value().hasNext(); - } - - @Override - public X next() { - if (this.hasNext()) { - return this.current.get().value().next(); - } - throw new NoSuchElementException(); - } - } + new org.cactoos.iterator.Paged<>(first, next) ) ); } diff --git a/src/main/java/org/cactoos/iterator/Paged.java b/src/main/java/org/cactoos/iterator/Paged.java new file mode 100644 index 0000000000..d4ba3d5a8f --- /dev/null +++ b/src/main/java/org/cactoos/iterator/Paged.java @@ -0,0 +1,99 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2020 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.AtomicReference; +import org.cactoos.Func; +import org.cactoos.Scalar; +import org.cactoos.func.UncheckedFunc; +import org.cactoos.scalar.Sticky; +import org.cactoos.scalar.Unchecked; + +/** + * Paged iterator. + * Elements will continue to be provided so long as {@code next} produces + * non-empty iterators. + * + *

There is no thread-safety guarantee. + * + * @param Subtype of item iterator + * @param Type of item + * @since 0.49 + * @todo #1464:30min We want to use {@code Iterator} directly in the class without + * introducing I, maybe using {@code Iterator} for example because, we don't + * have to constrain the {@link Func} to give exactly the same Iterator type as long as it + * is an iterator that provides X. {@link org.cactoos.iterable.Paged} should be changed too. + */ +public final class Paged, X> implements Iterator { + + /** + * Current element. + */ + private final AtomicReference> current; + + /** + * Function to get the next element. + */ + private final Func subsequent; + + /** + * Ctor. + * @param first First element. + * @param next Function to get the next element. + */ + public Paged(final Scalar first, final Func next) { + this.current = + new AtomicReference<>( + new Unchecked<>( + new Sticky<>(first) + ) + ); + this.subsequent = next; + } + + @Override + public boolean hasNext() { + if (!this.current.get().value().hasNext()) { + final I next = new UncheckedFunc<>(this.subsequent).apply( + this.current.get().value() + ); + this.current.set( + new Unchecked<>( + new Sticky<>(() -> next) + ) + ); + } + return this.current.get().value().hasNext(); + } + + @Override + public X next() { + if (this.hasNext()) { + return this.current.get().value().next(); + } + throw new NoSuchElementException(); + } +}