Skip to content

Commit

Permalink
Fix IterableOfBytes to be traversable multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Dec 28, 2020
1 parent d0f20ed commit 019f965
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 37 deletions.
28 changes: 14 additions & 14 deletions src/main/java/org/cactoos/iterable/IterableOfBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/
package org.cactoos.iterable;

import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import org.cactoos.Bytes;
import org.cactoos.Text;
import org.cactoos.io.BytesOf;
import org.cactoos.iterator.IteratorOfBytes;

/**
Expand All @@ -37,33 +37,33 @@ public final class IterableOfBytes extends IterableEnvelope<Byte> {

/**
* Ctor.
* @param bytes Bytes
* @param txt Text
*/
public IterableOfBytes(final byte... bytes) {
this(new IteratorOfBytes(bytes));
public IterableOfBytes(final Text txt) {
this(new BytesOf(txt));
}

/**
* Ctor.
* @param bytes Iterator
* @param str String
*/
public IterableOfBytes(final Iterator<Byte> bytes) {
super(new IterableOf<>(bytes));
public IterableOfBytes(final String str) {
this(new BytesOf(str));
}

/**
* Ctor.
* @param str String
* @param bytes Bytes
*/
public IterableOfBytes(final String str) {
this(str.getBytes(StandardCharsets.UTF_8));
public IterableOfBytes(final byte... bytes) {
this(new BytesOf(bytes));
}

/**
* Ctor.
* @param txt Text
* @param bytes Bytes to iterate
*/
public IterableOfBytes(final Text txt) {
this(txt.toString());
public IterableOfBytes(final Bytes bytes) {
super(new IterableOf<>(() -> new IteratorOfBytes(bytes)));
}
}
16 changes: 8 additions & 8 deletions src/main/java/org/cactoos/iterable/IterableOfChars.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@
*/
public final class IterableOfChars extends IterableEnvelope<Character> {

/**
* Ctor.
* @param chars Characters
*/
public IterableOfChars(final char... chars) {
super(new IterableOf<>(() -> new IteratorOfChars(chars)));
}

/**
* Ctor.
* @param str String
Expand All @@ -56,4 +48,12 @@ public IterableOfChars(final String str) {
public IterableOfChars(final Text txt) {
this(txt.toString());
}

/**
* Ctor.
* @param chars Characters
*/
public IterableOfChars(final char... chars) {
super(new IterableOf<>(() -> new IteratorOfChars(chars)));
}
}
28 changes: 13 additions & 15 deletions src/main/java/org/cactoos/iterator/IteratorOfBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.cactoos.Bytes;
import org.cactoos.Text;
import org.cactoos.io.BytesOf;
import org.cactoos.io.UncheckedBytes;

/**
* Iterator that returns a set of bytes.
Expand All @@ -51,38 +52,35 @@ public final class IteratorOfBytes implements Iterator<Byte> {

/**
* Ctor.
* @param itms Items to iterate
* @param txt Text to iterate
*/
public IteratorOfBytes(final byte... itms) {
this.items = itms;
this.position = new AtomicInteger(0);
public IteratorOfBytes(final Text txt) {
this(new BytesOf(txt));
}

/**
* Ctor.
* @param txt Text to iterate
* @throws Exception If fails
* @param str String to iterate
*/
public IteratorOfBytes(final Text txt) throws Exception {
this(new BytesOf(txt));
public IteratorOfBytes(final String str) {
this(new BytesOf(str));
}

/**
* Ctor.
* @param bytes Bytes to iterate
* @throws Exception If fails
*/
public IteratorOfBytes(final Bytes bytes) throws Exception {
this(bytes.asBytes());
public IteratorOfBytes(final Bytes bytes) {
this(new UncheckedBytes(bytes).asBytes());
}

/**
* Ctor.
* @param str String to iterate
* @throws Exception If fails
* @param itms Items to iterate
*/
public IteratorOfBytes(final String str) throws Exception {
this(new BytesOf(str));
public IteratorOfBytes(final byte... itms) {
this.items = itms;
this.position = new AtomicInteger(0);
}

@Override
Expand Down

0 comments on commit 019f965

Please sign in to comment.