Skip to content

Commit

Permalink
(#947) IterableEnvelope only delegates and IterableOf has behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Sep 15, 2019
1 parent 5ff9e6d commit b092754
Show file tree
Hide file tree
Showing 34 changed files with 410 additions and 287 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ The MIT License (MIT)
<exclude>findbugs:org.cactoos.map.MapEntry</exclude>
<exclude>findbugs:org.cactoos.scalar.NumberOf</exclude>
<exclude>findbugs:org.cactoos.text.TextEnvelopeTest</exclude>
<exclude>findbugs:org.cactoos.iterable.IterableEnvelope</exclude>
<exclude>findbugs:org.cactoos.iterable.IterableOf</exclude>
<exclude>findbugs:org.cactoos.iterator.Cycled</exclude>
<exclude>findbugs:org.cactoos.iterator.Endless</exclude>
</excludes>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/cactoos/collection/CollectionEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
* @param <X> Element type
* @since 0.23
* @checkstyle AbstractClassNameCheck (500 lines)
* @todo #947:30min CollectionEnvelope should extends IterableEnvelope and
* only delegates all the methods of Collection to the wrapped Collection.
* See IterableEnvelope for an example. If needed CollectionOf should have
* some methods that were previously here and implement Collection instead
* of extending CollectionEnvelope. Again see IterableOf for an example.
*/
@SuppressWarnings(
{
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/org/cactoos/experimental/Threads.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,19 @@ private Threads(
final Func<Collection<Callable<T>>, Collection<Future<T>>> fnc,
final Iterable<Scalar<T>> tasks
) {
super(() -> {
try {
return new Mapped<>(
Future::get,
fnc.apply(new Mapped<>(task -> task::value, tasks))
);
} catch (final Exception exp) {
throw new CompletionException(exp);
}
});
super(
new IterableOf<>(
() -> {
try {
return new Mapped<>(
Future::get,
fnc.apply(new Mapped<>(task -> task::value, tasks))
).iterator();
} catch (final Exception exp) {
throw new CompletionException(exp);
}
}
)
);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterable/Cycled.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Cycled(final T... itr) {
* @param itr Iterable
*/
public Cycled(final Iterable<T> itr) {
super(() -> () -> new org.cactoos.iterator.Cycled<>(itr));
super(new IterableOf<>(() -> new org.cactoos.iterator.Cycled<>(itr)));
}

}
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterable/Endless.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class Endless<T> extends IterableEnvelope<T> {
* @param item The item to repeat
*/
public Endless(final T item) {
super(() -> () -> new org.cactoos.iterator.Endless<>(item));
super(new IterableOf<>(() -> new org.cactoos.iterator.Endless<>(item)));
}

}
8 changes: 5 additions & 3 deletions src/main/java/org/cactoos/iterable/Filtered.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ public Filtered(final Func<X, Boolean> fnc, final X... src) {
* @param src Source iterable
*/
public Filtered(final Func<X, Boolean> fnc, final Iterable<X> src) {
super(() -> () -> new org.cactoos.iterator.Filtered<>(
fnc, src.iterator()
));
super(
new IterableOf<>(
() -> new org.cactoos.iterator.Filtered<>(fnc, src.iterator())
)
);
}

}
11 changes: 8 additions & 3 deletions src/main/java/org/cactoos/iterable/HeadOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@ public HeadOf(final int num, final T... src) {
* @param iterable Decorated iterable
*/
public HeadOf(final int num, final Iterable<T> iterable) {
super(() -> () -> new org.cactoos.iterator.HeadOf<>(
num, iterable.iterator()
));
super(
new IterableOf<>(
() -> new org.cactoos.iterator.HeadOf<>(
num,
iterable.iterator()
)
)
);
}
}
60 changes: 13 additions & 47 deletions src/main/java/org/cactoos/iterable/IterableEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@
package org.cactoos.iterable;

import java.util.Iterator;
import org.cactoos.Scalar;
import org.cactoos.iterator.Immutable;
import org.cactoos.scalar.And;
import org.cactoos.scalar.Folded;
import org.cactoos.scalar.Or;
import org.cactoos.scalar.SumOfInt;
import org.cactoos.scalar.Unchecked;

/**
* Iterable envelope.
Expand All @@ -39,66 +32,39 @@
*
* @param <X> Type of item
* @since 0.24
* @checkstyle AbstractClassNameCheck (500 lines)
*/
@SuppressWarnings("PMD.AbstractNaming")
public abstract class IterableEnvelope<X> implements Iterable<X> {

/**
* The iterable.
* The wrapped iterable.
*/
private final Unchecked<Iterable<X>> iterable;
private final Iterable<X> wrapped;

/**
* Ctor.
* @param scalar The source
* @param iterable The wrapped iterable
*/
public IterableEnvelope(final Scalar<Iterable<X>> scalar) {
this.iterable = new Unchecked<>(scalar);
public IterableEnvelope(final Iterable<X> iterable) {
this.wrapped = iterable;
}

@Override
public final Iterator<X> iterator() {
return new Immutable<>(
this.iterable.value().iterator()
);
return this.wrapped.iterator();
}

@Override
public final boolean equals(final Object other) {
return new Unchecked<>(
new Or(
() -> other == this,
new And(
() -> other != null,
() -> Iterable.class.isAssignableFrom(other.getClass()),
() -> {
final Iterable<?> compared = (Iterable<?>) other;
final Iterator<?> iterator = compared.iterator();
return new Unchecked<>(
new And(
(X input) -> input.equals(iterator.next()),
this
)
).value();
}
)
)
).value();
return this.wrapped.equals(other);
}

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

@Override
public final String toString() {
return this.wrapped.toString();
}
}
76 changes: 67 additions & 9 deletions src/main/java/org/cactoos/iterable/IterableOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@
import org.cactoos.Scalar;
import org.cactoos.func.UncheckedFunc;
import org.cactoos.iterator.IteratorOf;
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;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;

/**
* Array as iterable.
Expand All @@ -40,8 +46,15 @@
*
* @param <X> Type of item
* @since 0.12
* @checkstyle ClassDataAbstractionCouplingCheck (550 lines)
*/
public final class IterableOf<X> extends IterableEnvelope<X> {
@SuppressWarnings("PMD.OnlyOneConstructorShouldDoInitialization")
public final class IterableOf<X> implements Iterable<X> {

/**
* The encapsulated iterator.
*/
private final Scalar<Iterator<X>> itr;

/**
* Ctor.
Expand Down Expand Up @@ -77,13 +90,11 @@ public IterableOf(final Iterator<X> list) {
* @param <I> Custom iterator
* @param first First bag of elements
* @param next Subsequent bags of elements
* @todo #947:30min Move this constructor in its own class with its own
* tests and meaningful name (maybe Paged?). Then remove the
* ClassDataAbstractionCouplingCheck suppression for IterableOf.
*/
@SuppressWarnings(
{
"PMD.CallSuperInConstructor",
"PMD.ConstructorOnlyInitializesOrCallOtherConstructors"
}
)
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
public <I extends Iterator<X>> IterableOf(
final Scalar<I> first, final Func<I, I> next
) {
Expand Down Expand Up @@ -124,8 +135,55 @@ public X next() {
* Ctor.
* @param sclr The encapsulated iterator of x
*/
private IterableOf(final Scalar<Iterator<X>> sclr) {
super(() -> () -> new Unchecked<>(sclr).value());
public IterableOf(final Scalar<Iterator<X>> sclr) {
this.itr = sclr;
}

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

@Override
public boolean equals(final Object other) {
return new Unchecked<>(
new Or(
() -> other == this,
new And(
() -> other != null,
() -> Iterable.class.isAssignableFrom(other.getClass()),
() -> {
final Iterable<?> compared = (Iterable<?>) other;
final Iterator<?> iterator = compared.iterator();
return new Unchecked<>(
new And(
(X input) -> input.equals(iterator.next()),
this
)
).value();
}
)
)
).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
)
).value();
}

@Override
public String toString() {
return new UncheckedText(new TextOf(this)).asString();
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterable/IterableOfBooleans.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public final class IterableOfBooleans extends IterableEnvelope<Boolean> {
* @param values Boolean values
*/
public IterableOfBooleans(final boolean... values) {
super(() -> () -> new IteratorOfBooleans(values));
super(new IterableOf<>(() -> new IteratorOfBooleans(values)));
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterable/IterableOfBytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public final class IterableOfBytes extends IterableEnvelope<Byte> {
* @param bytes Bytes
*/
public IterableOfBytes(final byte... bytes) {
super(() -> new IterableOf<>(new IteratorOfBytes(bytes)));
super(new IterableOf<>(() -> new IteratorOfBytes(bytes)));
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterable/IterableOfChars.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public final class IterableOfChars extends IterableEnvelope<Character> {
* @param chars Characters
*/
public IterableOfChars(final char... chars) {
super(() -> new IterableOf<>(new IteratorOfChars(chars)));
super(new IterableOf<>(() -> new IteratorOfChars(chars)));
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterable/IterableOfDoubles.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public final class IterableOfDoubles extends IterableEnvelope<Double> {
* @param values Double values
*/
public IterableOfDoubles(final double... values) {
super(() -> new IterableOf<>(new IteratorOfDoubles(values)));
super(new IterableOf<>(() -> new IteratorOfDoubles(values)));
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterable/IterableOfFloats.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public final class IterableOfFloats extends IterableEnvelope<Float> {
* @param values Float values
*/
public IterableOfFloats(final float... values) {
super(() -> () -> new IteratorOfFloats(values));
super(new IterableOf<>(() -> new IteratorOfFloats(values)));
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterable/IterableOfInts.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public final class IterableOfInts extends IterableEnvelope<Integer> {
* @param values Integer values
*/
public IterableOfInts(final int... values) {
super(() -> () -> new IteratorOfInts(values));
super(new IterableOf<>(() -> new IteratorOfInts(values)));
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterable/IterableOfLongs.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ public final class IterableOfLongs extends IterableEnvelope<Long> {
* @param values Long values
*/
public IterableOfLongs(final long... values) {
super(() -> () -> new IteratorOfLongs(values));
super(new IterableOf<>(() -> new IteratorOfLongs(values)));
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterable/IterableOfShorts.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public final class IterableOfShorts extends IterableEnvelope<Short> {
*/
@SuppressWarnings("PMD.AvoidUsingShortType")
public IterableOfShorts(final short... values) {
super(() -> () -> new IteratorOfShorts(values));
super(new IterableOf<>(() -> new IteratorOfShorts(values)));
}
}
Loading

3 comments on commit b092754

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on b092754 Sep 15, 2019

Choose a reason for hiding this comment

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

Puzzle 947-0e04e40a discovered in src/main/java/org/cactoos/iterable/IterableOf.java and submitted as #1183. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on b092754 Sep 15, 2019

Choose a reason for hiding this comment

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

Puzzle 947-361be9e4 discovered in src/main/java/org/cactoos/collection/CollectionEnvelope.java and submitted as #1184. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on b092754 Sep 15, 2019

Choose a reason for hiding this comment

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

Puzzle 947-4efcb5fb discovered in src/main/java/org/cactoos/list/ListEnvelope.java and submitted as #1185. 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.