Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#947) IterableEnvelope only delegates and IterableOf has behaviour #1154

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> {
Copy link
Contributor

Choose a reason for hiding this comment

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

@victornoel what's the reason for implementing Iterable now instead of keep extending IterableEnvelope?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@scristalli it was kind of explained in the description: the envelope does not contain any specific behaviour that is implemented by IterableOf. IterableOf is not a decorator on Iterable, it's an implementation of Iterable. I could have extended IterableEnvelope, but I believe it would have been a design mistake semantically. Also, now that I think about it, it wouldn't have been possible to implement toString, equals and hashCode if I was extending IterableEnvelope.


/**
* 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