Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Apr 1, 2020
2 parents 6e88165 + d49763d commit 6d4ccd9
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 337 deletions.
19 changes: 6 additions & 13 deletions src/main/java/org/cactoos/iterable/Reversed.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
package org.cactoos.iterable;

/**
* Reverse iterator.
* Reverse iterable.
*
* <p>This loads the whole wrapped {@link Iterable} in memory
* each time {@link #iterator()} is called in order to be able to reverse it.
*
* <p>There is no thread-safety guarantee.
*
Expand All @@ -33,15 +36,14 @@
* @since 0.9
*/
public final class Reversed<X> extends IterableEnvelope<X> {

/**
* Ctor.
* @param src Source iterable
* @since 0.23
*/
@SafeVarargs
public Reversed(final X... src) {
this(new org.cactoos.collection.Reversed<>(src));
this(new IterableOf<>(src));
}

/**
Expand All @@ -50,15 +52,6 @@ public Reversed(final X... src) {
* @since 0.23
*/
public Reversed(final Iterable<X> src) {
this(new org.cactoos.collection.Reversed<>(src));
}

/**
* Ctor.
* @param reversed Reversed collection
*/
public Reversed(final org.cactoos.collection.Reversed<X> reversed) {
super(reversed);
super(new IterableOf<>(() -> new org.cactoos.iterator.Reversed<>(src.iterator())));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,56 +21,76 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.collection;
package org.cactoos.iterator;

import java.util.Collections;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.List;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.Unchecked;
import java.util.ListIterator;
import org.cactoos.list.ListOf;

/**
* Reversed collection.
* Reverse iterator.
*
* <p>This loads the whole wrapped Iterator in memory in order
* to be able to reverse it.
*
* <p>There is no thread-safety guarantee.
*
* @param <X> Type of source item
* @since 1.16
* @todo #1242:30min Remove this class and replace it everywhere
* it was needed by the appropriate usage of Reversed from iterable
* (composed with ListOf or SetOf in case a copy is needed)
* or any other relevant concrete collection implementation.
* See #1242 for the rationale about this.
* @param <X> Type of item
* @since 1.0
* @todo #1300:30min For now this class is only tested through tests coming
* from iterables's Reversed: introduces tests for this class to enforce its
* contract and validate it works as expected.
*/
public final class Reversed<X> extends CollectionEnvelope<X> {
public final class Reversed<X> implements Iterator<X> {

/**
* Origin iterator to be reversed.
*/
private final ListIterator<X> origin;

/**
* Ctor.
* @param src Source collection
* @since 0.23
* @param src Source iterator
* @since 1.0
*/
@SafeVarargs
public Reversed(final X... src) {
this(new IterableOf<>(src));
this(new IteratorOf<>(src));
}

/**
* Ctor.
* @param src Source iterator
* @since 1.0
*/
public Reversed(final Iterator<X> src) {
this(new ListOf<>(src));
}

/**
* Ctor.
* @param src Source collection
* @param src Source list
*/
public Reversed(final Iterable<X> src) {
super(
new Unchecked<>(
new org.cactoos.scalar.Sticky<>(
() -> {
final List<X> items = new LinkedList<>();
src.forEach(items::add);
Collections.reverse(items);
return items;
}
)
).value()
);
private Reversed(final List<X> src) {
this(src.listIterator(src.size()));
}

/**
* Ctor.
* @param src Source list iterator
*/
private Reversed(final ListIterator<X> src) {
this.origin = src;
}

@Override
public boolean hasNext() {
return this.origin.hasPrevious();
}

@Override
public X next() {
return this.origin.previous();
}
}
Loading

2 comments on commit 6d4ccd9

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 6d4ccd9 Apr 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1242-011c38db disappeared from src/main/java/org/cactoos/collection/Reversed.java, that's why I closed #1300. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 6d4ccd9 Apr 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 1300-8636f38d discovered in src/main/java/org/cactoos/iterator/Reversed.java and submitted as #1337. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.