diff --git a/pom.xml b/pom.xml index cd9987de20..eb77207311 100644 --- a/pom.xml +++ b/pom.xml @@ -170,7 +170,7 @@ The MIT License (MIT) findbugs:org.cactoos.map.MapEntry findbugs:org.cactoos.scalar.NumberOf findbugs:org.cactoos.text.TextEnvelopeTest - findbugs:org.cactoos.iterable.IterableEnvelope + findbugs:org.cactoos.iterable.IterableOf findbugs:org.cactoos.iterator.Cycled findbugs:org.cactoos.iterator.Endless diff --git a/src/main/java/org/cactoos/collection/CollectionEnvelope.java b/src/main/java/org/cactoos/collection/CollectionEnvelope.java index 721388198b..3001c1414d 100644 --- a/src/main/java/org/cactoos/collection/CollectionEnvelope.java +++ b/src/main/java/org/cactoos/collection/CollectionEnvelope.java @@ -40,6 +40,11 @@ * @param 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( { diff --git a/src/main/java/org/cactoos/experimental/Threads.java b/src/main/java/org/cactoos/experimental/Threads.java index a36be72147..fa2b2e0024 100644 --- a/src/main/java/org/cactoos/experimental/Threads.java +++ b/src/main/java/org/cactoos/experimental/Threads.java @@ -111,15 +111,19 @@ private Threads( final Func>, Collection>> fnc, final Iterable> 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); + } + } + ) + ); } } diff --git a/src/main/java/org/cactoos/iterable/Cycled.java b/src/main/java/org/cactoos/iterable/Cycled.java index b777adc7bd..c59da9dc62 100644 --- a/src/main/java/org/cactoos/iterable/Cycled.java +++ b/src/main/java/org/cactoos/iterable/Cycled.java @@ -49,7 +49,7 @@ public Cycled(final T... itr) { * @param itr Iterable */ public Cycled(final Iterable itr) { - super(() -> () -> new org.cactoos.iterator.Cycled<>(itr)); + super(new IterableOf<>(() -> new org.cactoos.iterator.Cycled<>(itr))); } } diff --git a/src/main/java/org/cactoos/iterable/Endless.java b/src/main/java/org/cactoos/iterable/Endless.java index 8a24267503..0f8cf689ac 100644 --- a/src/main/java/org/cactoos/iterable/Endless.java +++ b/src/main/java/org/cactoos/iterable/Endless.java @@ -39,7 +39,7 @@ public final class Endless extends IterableEnvelope { * @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))); } } diff --git a/src/main/java/org/cactoos/iterable/Filtered.java b/src/main/java/org/cactoos/iterable/Filtered.java index edb7a2f5d2..f2c024263d 100644 --- a/src/main/java/org/cactoos/iterable/Filtered.java +++ b/src/main/java/org/cactoos/iterable/Filtered.java @@ -65,9 +65,11 @@ public Filtered(final Func fnc, final X... src) { * @param src Source iterable */ public Filtered(final Func fnc, final Iterable src) { - super(() -> () -> new org.cactoos.iterator.Filtered<>( - fnc, src.iterator() - )); + super( + new IterableOf<>( + () -> new org.cactoos.iterator.Filtered<>(fnc, src.iterator()) + ) + ); } } diff --git a/src/main/java/org/cactoos/iterable/HeadOf.java b/src/main/java/org/cactoos/iterable/HeadOf.java index 270f23ef46..d6a981ac30 100644 --- a/src/main/java/org/cactoos/iterable/HeadOf.java +++ b/src/main/java/org/cactoos/iterable/HeadOf.java @@ -49,8 +49,13 @@ public HeadOf(final int num, final T... src) { * @param iterable Decorated iterable */ public HeadOf(final int num, final Iterable iterable) { - super(() -> () -> new org.cactoos.iterator.HeadOf<>( - num, iterable.iterator() - )); + super( + new IterableOf<>( + () -> new org.cactoos.iterator.HeadOf<>( + num, + iterable.iterator() + ) + ) + ); } } diff --git a/src/main/java/org/cactoos/iterable/IterableEnvelope.java b/src/main/java/org/cactoos/iterable/IterableEnvelope.java index e36221b30a..5a4785b1a5 100644 --- a/src/main/java/org/cactoos/iterable/IterableEnvelope.java +++ b/src/main/java/org/cactoos/iterable/IterableEnvelope.java @@ -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. @@ -39,66 +32,39 @@ * * @param Type of item * @since 0.24 - * @checkstyle AbstractClassNameCheck (500 lines) */ -@SuppressWarnings("PMD.AbstractNaming") public abstract class IterableEnvelope implements Iterable { /** * The iterable. */ - private final Unchecked> iterable; + private final Iterable wrapped; /** * Ctor. - * @param scalar The source + * @param wrapped The wrapped iterable */ - public IterableEnvelope(final Scalar> scalar) { - this.iterable = new Unchecked<>(scalar); + public IterableEnvelope(final Iterable wrapped) { + this.wrapped = wrapped; } @Override public final Iterator 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(); } } diff --git a/src/main/java/org/cactoos/iterable/IterableOf.java b/src/main/java/org/cactoos/iterable/IterableOf.java index 85d75ef589..024eb296a8 100644 --- a/src/main/java/org/cactoos/iterable/IterableOf.java +++ b/src/main/java/org/cactoos/iterable/IterableOf.java @@ -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. @@ -40,8 +46,15 @@ * * @param Type of item * @since 0.12 + * @checkstyle ClassDataAbstractionCouplingCheck (550 lines) */ -public final class IterableOf extends IterableEnvelope { +@SuppressWarnings("PMD.OnlyOneConstructorShouldDoInitialization") +public final class IterableOf implements Iterable { + + /** + * The encapsulated iterator. + */ + private final Scalar> itr; /** * Ctor. @@ -77,13 +90,11 @@ public IterableOf(final Iterator list) { * @param 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 > IterableOf( final Scalar first, final Func next ) { @@ -124,8 +135,55 @@ public X next() { * Ctor. * @param sclr The encapsulated iterator of x */ - private IterableOf(final Scalar> sclr) { - super(() -> () -> new Unchecked<>(sclr).value()); + public IterableOf(final Scalar> sclr) { + this.itr = sclr; } + @Override + public Iterator 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(); + } } diff --git a/src/main/java/org/cactoos/iterable/IterableOfBooleans.java b/src/main/java/org/cactoos/iterable/IterableOfBooleans.java index 2ec14e2437..915eeeede4 100644 --- a/src/main/java/org/cactoos/iterable/IterableOfBooleans.java +++ b/src/main/java/org/cactoos/iterable/IterableOfBooleans.java @@ -37,6 +37,6 @@ public final class IterableOfBooleans extends IterableEnvelope { * @param values Boolean values */ public IterableOfBooleans(final boolean... values) { - super(() -> () -> new IteratorOfBooleans(values)); + super(new IterableOf<>(() -> new IteratorOfBooleans(values))); } } diff --git a/src/main/java/org/cactoos/iterable/IterableOfBytes.java b/src/main/java/org/cactoos/iterable/IterableOfBytes.java index 3e09a33aa9..f7470ba560 100644 --- a/src/main/java/org/cactoos/iterable/IterableOfBytes.java +++ b/src/main/java/org/cactoos/iterable/IterableOfBytes.java @@ -37,6 +37,6 @@ public final class IterableOfBytes extends IterableEnvelope { * @param bytes Bytes */ public IterableOfBytes(final byte... bytes) { - super(() -> new IterableOf<>(new IteratorOfBytes(bytes))); + super(new IterableOf<>(() -> new IteratorOfBytes(bytes))); } } diff --git a/src/main/java/org/cactoos/iterable/IterableOfChars.java b/src/main/java/org/cactoos/iterable/IterableOfChars.java index 370984be1b..8a75ffeaf0 100644 --- a/src/main/java/org/cactoos/iterable/IterableOfChars.java +++ b/src/main/java/org/cactoos/iterable/IterableOfChars.java @@ -37,6 +37,6 @@ public final class IterableOfChars extends IterableEnvelope { * @param chars Characters */ public IterableOfChars(final char... chars) { - super(() -> new IterableOf<>(new IteratorOfChars(chars))); + super(new IterableOf<>(() -> new IteratorOfChars(chars))); } } diff --git a/src/main/java/org/cactoos/iterable/IterableOfDoubles.java b/src/main/java/org/cactoos/iterable/IterableOfDoubles.java index 02d9bb7e85..46054d69f2 100644 --- a/src/main/java/org/cactoos/iterable/IterableOfDoubles.java +++ b/src/main/java/org/cactoos/iterable/IterableOfDoubles.java @@ -37,6 +37,6 @@ public final class IterableOfDoubles extends IterableEnvelope { * @param values Double values */ public IterableOfDoubles(final double... values) { - super(() -> new IterableOf<>(new IteratorOfDoubles(values))); + super(new IterableOf<>(() -> new IteratorOfDoubles(values))); } } diff --git a/src/main/java/org/cactoos/iterable/IterableOfFloats.java b/src/main/java/org/cactoos/iterable/IterableOfFloats.java index 665c2c0353..4f04c1ca74 100644 --- a/src/main/java/org/cactoos/iterable/IterableOfFloats.java +++ b/src/main/java/org/cactoos/iterable/IterableOfFloats.java @@ -37,6 +37,6 @@ public final class IterableOfFloats extends IterableEnvelope { * @param values Float values */ public IterableOfFloats(final float... values) { - super(() -> () -> new IteratorOfFloats(values)); + super(new IterableOf<>(() -> new IteratorOfFloats(values))); } } diff --git a/src/main/java/org/cactoos/iterable/IterableOfInts.java b/src/main/java/org/cactoos/iterable/IterableOfInts.java index 4ab7107fbe..33fde89b0b 100644 --- a/src/main/java/org/cactoos/iterable/IterableOfInts.java +++ b/src/main/java/org/cactoos/iterable/IterableOfInts.java @@ -37,6 +37,6 @@ public final class IterableOfInts extends IterableEnvelope { * @param values Integer values */ public IterableOfInts(final int... values) { - super(() -> () -> new IteratorOfInts(values)); + super(new IterableOf<>(() -> new IteratorOfInts(values))); } } diff --git a/src/main/java/org/cactoos/iterable/IterableOfLongs.java b/src/main/java/org/cactoos/iterable/IterableOfLongs.java index 15f575645d..e71ece39e2 100644 --- a/src/main/java/org/cactoos/iterable/IterableOfLongs.java +++ b/src/main/java/org/cactoos/iterable/IterableOfLongs.java @@ -37,6 +37,6 @@ public final class IterableOfLongs extends IterableEnvelope { * @param values Long values */ public IterableOfLongs(final long... values) { - super(() -> () -> new IteratorOfLongs(values)); + super(new IterableOf<>(() -> new IteratorOfLongs(values))); } } diff --git a/src/main/java/org/cactoos/iterable/IterableOfShorts.java b/src/main/java/org/cactoos/iterable/IterableOfShorts.java index d863610155..b6e3ab019d 100644 --- a/src/main/java/org/cactoos/iterable/IterableOfShorts.java +++ b/src/main/java/org/cactoos/iterable/IterableOfShorts.java @@ -38,6 +38,6 @@ public final class IterableOfShorts extends IterableEnvelope { */ @SuppressWarnings("PMD.AvoidUsingShortType") public IterableOfShorts(final short... values) { - super(() -> () -> new IteratorOfShorts(values)); + super(new IterableOf<>(() -> new IteratorOfShorts(values))); } } diff --git a/src/main/java/org/cactoos/iterable/Joined.java b/src/main/java/org/cactoos/iterable/Joined.java index fe11f0f13c..45a940c851 100644 --- a/src/main/java/org/cactoos/iterable/Joined.java +++ b/src/main/java/org/cactoos/iterable/Joined.java @@ -23,9 +23,7 @@ */ package org.cactoos.iterable; -import java.util.Collection; import java.util.Iterator; -import java.util.LinkedList; /** * A few Iterables joined together. @@ -61,8 +59,9 @@ public Joined(final Iterator> items) { * @param items Iterable * @since 0.32 */ + @SuppressWarnings("unchecked") public Joined(final T item, final Iterable items) { - super(() -> new Joined(new IterableOf(item), items)); + super(new Joined<>(new IterableOf<>(item), items)); } /** @@ -70,13 +69,13 @@ public Joined(final T item, final Iterable items) { * @param items Items to concatenate */ public Joined(final Iterable> items) { - super(() -> { - final Collection> iterators = new LinkedList<>(); - for (final Iterable item : items) { - iterators.add(item.iterator()); - } - return () -> new org.cactoos.iterator.Joined<>(iterators); - }); + super( + new IterableOf<>( + () -> new org.cactoos.iterator.Joined<>( + new Mapped<>(Iterable::iterator, items) + ) + ) + ); } } diff --git a/src/main/java/org/cactoos/iterable/Mapped.java b/src/main/java/org/cactoos/iterable/Mapped.java index 08985bf0af..16c49ca5da 100644 --- a/src/main/java/org/cactoos/iterable/Mapped.java +++ b/src/main/java/org/cactoos/iterable/Mapped.java @@ -24,7 +24,6 @@ package org.cactoos.iterable; import org.cactoos.Func; -import org.cactoos.text.TextOf; /** * Mapped iterable. @@ -53,13 +52,10 @@ public Mapped(final Func fnc, final X... src) { * @param src Source iterable */ public Mapped(final Func fnc, final Iterable src) { - super(() -> () -> new org.cactoos.iterator.Mapped<>( - fnc, src.iterator() - )); - } - - @Override - public String toString() { - return new TextOf(this).toString(); + super( + new IterableOf<>( + () -> new org.cactoos.iterator.Mapped<>(fnc, src.iterator()) + ) + ); } } diff --git a/src/main/java/org/cactoos/iterable/Partitioned.java b/src/main/java/org/cactoos/iterable/Partitioned.java index 3e6dd4d42b..27bfab7f09 100644 --- a/src/main/java/org/cactoos/iterable/Partitioned.java +++ b/src/main/java/org/cactoos/iterable/Partitioned.java @@ -49,9 +49,14 @@ public Partitioned(final int size, final T...items) { * @param iterable The source {@link Iterable}. */ public Partitioned(final int size, final Iterable iterable) { - super(() -> () -> new org.cactoos.iterator.Partitioned<>( - size, iterable.iterator() - )); + super( + new IterableOf<>( + () -> new org.cactoos.iterator.Partitioned<>( + size, + iterable.iterator() + ) + ) + ); } } diff --git a/src/main/java/org/cactoos/iterable/RangeOf.java b/src/main/java/org/cactoos/iterable/RangeOf.java index 911e98d0b3..773f9ccfcf 100644 --- a/src/main/java/org/cactoos/iterable/RangeOf.java +++ b/src/main/java/org/cactoos/iterable/RangeOf.java @@ -48,28 +48,29 @@ } ) public RangeOf(final T min, final T max, final Func incrementor) { - super(() -> new IterableOf<>( - new Iterator() { - private final UncheckedFunc inc = - new UncheckedFunc<>(incrementor); - private T value = min; + super( + new IterableOf<>( + () -> new Iterator() { + private final UncheckedFunc inc = + new UncheckedFunc<>(incrementor); + private T value = min; - @Override - public boolean hasNext() { - return this.value.compareTo(max) < 1; - } + @Override + public boolean hasNext() { + return this.value.compareTo(max) < 1; + } - @Override - public T next() { - if (!this.hasNext()) { - throw new NoSuchElementException(); + @Override + public T next() { + if (!this.hasNext()) { + throw new NoSuchElementException(); + } + final T result = this.value; + this.value = this.inc.apply(this.value); + return result; } - final T result = this.value; - this.value = this.inc.apply(this.value); - return result; } - } - )); + ) + ); } - } diff --git a/src/main/java/org/cactoos/iterable/Repeated.java b/src/main/java/org/cactoos/iterable/Repeated.java index e4dd68f409..928c629ba8 100644 --- a/src/main/java/org/cactoos/iterable/Repeated.java +++ b/src/main/java/org/cactoos/iterable/Repeated.java @@ -60,9 +60,11 @@ public Repeated(final int total, final Scalar elm) { * @param item The element to repeat */ public Repeated(final int total, final Unchecked item) { - super(() -> () -> new org.cactoos.iterator.Repeated<>( - total, item - )); + super( + new IterableOf<>( + () -> new org.cactoos.iterator.Repeated<>(total, item) + ) + ); } } diff --git a/src/main/java/org/cactoos/iterable/Reversed.java b/src/main/java/org/cactoos/iterable/Reversed.java index 9f2ea9a09e..885219dcbd 100644 --- a/src/main/java/org/cactoos/iterable/Reversed.java +++ b/src/main/java/org/cactoos/iterable/Reversed.java @@ -58,7 +58,7 @@ public Reversed(final Iterable src) { * @param reversed Reversed collection */ public Reversed(final org.cactoos.collection.Reversed reversed) { - super(() -> reversed); + super(reversed); } } diff --git a/src/main/java/org/cactoos/iterable/Shuffled.java b/src/main/java/org/cactoos/iterable/Shuffled.java index 786c9db835..b74cdbc02d 100644 --- a/src/main/java/org/cactoos/iterable/Shuffled.java +++ b/src/main/java/org/cactoos/iterable/Shuffled.java @@ -48,7 +48,11 @@ public Shuffled(final T... src) { * @param src The underlying iterable */ public Shuffled(final Iterable src) { - super(() -> () -> new org.cactoos.iterator.Shuffled<>(src.iterator())); + super( + new IterableOf<>( + () -> new org.cactoos.iterator.Shuffled<>(src.iterator()) + ) + ); } } diff --git a/src/main/java/org/cactoos/iterable/Skipped.java b/src/main/java/org/cactoos/iterable/Skipped.java index 7706e452a4..f1fca21e47 100644 --- a/src/main/java/org/cactoos/iterable/Skipped.java +++ b/src/main/java/org/cactoos/iterable/Skipped.java @@ -49,9 +49,13 @@ public Skipped(final int skip, final T... src) { * @param iterable Decorated iterable */ public Skipped(final int skip, final Iterable iterable) { - super(() -> () -> new org.cactoos.iterator.Skipped<>( - skip, - iterable.iterator() - )); + super( + new IterableOf<>( + () -> new org.cactoos.iterator.Skipped<>( + skip, + iterable.iterator() + ) + ) + ); } } diff --git a/src/main/java/org/cactoos/iterable/Solid.java b/src/main/java/org/cactoos/iterable/Solid.java index 2d28bd4cc7..ac6e0cccbb 100644 --- a/src/main/java/org/cactoos/iterable/Solid.java +++ b/src/main/java/org/cactoos/iterable/Solid.java @@ -23,8 +23,6 @@ */ package org.cactoos.iterable; -import org.cactoos.scalar.NoNulls; - /** * An {@link Iterable} that is both synchronized and sticky. * @@ -49,13 +47,7 @@ public Solid(final X... src) { * @param iterable The iterable */ public Solid(final Iterable iterable) { - super( - new NoNulls<>( - new org.cactoos.scalar.Solid<>( - () -> new Synced<>(new Sticky<>(iterable)) - ) - ) - ); + super(new Synced<>(new Sticky<>(iterable))); } } diff --git a/src/main/java/org/cactoos/iterable/Sorted.java b/src/main/java/org/cactoos/iterable/Sorted.java index c3998137ac..d0f2fa2e77 100644 --- a/src/main/java/org/cactoos/iterable/Sorted.java +++ b/src/main/java/org/cactoos/iterable/Sorted.java @@ -74,8 +74,10 @@ public Sorted(final Comparator cmp, final T... src) { * @param cmp The comparator */ public Sorted(final Comparator cmp, final Iterable src) { - super(() -> () -> new org.cactoos.iterator.Sorted<>( - cmp, src.iterator() - )); + super( + new IterableOf<>( + () -> new org.cactoos.iterator.Sorted<>(cmp, src.iterator()) + ) + ); } } diff --git a/src/main/java/org/cactoos/iterable/Sticky.java b/src/main/java/org/cactoos/iterable/Sticky.java index d876b6f3ea..36f4c1a66f 100644 --- a/src/main/java/org/cactoos/iterable/Sticky.java +++ b/src/main/java/org/cactoos/iterable/Sticky.java @@ -25,6 +25,7 @@ import java.util.Collection; import java.util.LinkedList; +import org.cactoos.scalar.Mapped; /** * Iterable that returns the same set of elements, always. @@ -51,14 +52,19 @@ public Sticky(final X... src) { */ public Sticky(final Iterable iterable) { super( - new org.cactoos.scalar.Sticky<>( - () -> { - final Collection temp = new LinkedList<>(); - for (final X item : iterable) { - temp.add(item); - } - return temp; - } + new IterableOf<>( + new Mapped<>( + Iterable::iterator, + new org.cactoos.scalar.Sticky>( + () -> { + final Collection temp = new LinkedList<>(); + for (final X item : iterable) { + temp.add(item); + } + return temp; + } + ) + ) ) ); } diff --git a/src/main/java/org/cactoos/iterable/TailOf.java b/src/main/java/org/cactoos/iterable/TailOf.java index 252dcd4062..2262a92029 100644 --- a/src/main/java/org/cactoos/iterable/TailOf.java +++ b/src/main/java/org/cactoos/iterable/TailOf.java @@ -49,8 +49,12 @@ public TailOf(final int num, final T... src) { * @param iterable Decorated iterable */ public TailOf(final int num, final Iterable iterable) { - super(() -> () -> new org.cactoos.iterator.TailOf<>( - num, iterable.iterator() - )); + super( + new IterableOf<>( + () -> new org.cactoos.iterator.TailOf<>( + num, iterable.iterator() + ) + ) + ); } } diff --git a/src/main/java/org/cactoos/list/ListEnvelope.java b/src/main/java/org/cactoos/list/ListEnvelope.java index c2079aa74e..322a974042 100644 --- a/src/main/java/org/cactoos/list/ListEnvelope.java +++ b/src/main/java/org/cactoos/list/ListEnvelope.java @@ -38,6 +38,11 @@ * @param Element type * @since 0.23 * @checkstyle AbstractClassNameCheck (500 lines) + * @todo #947:30min ListEnvelope should only delegates all the methods + * of List to the wrapped List. See IterableEnvelope for an example. + * If needed ListOf should have some methods that were previously here + * and implement List instead of extending ListEnvelope. + * Again see IterableOf for an example. */ @SuppressWarnings( { diff --git a/src/main/java/org/cactoos/scalar/Mapped.java b/src/main/java/org/cactoos/scalar/Mapped.java new file mode 100644 index 0000000000..f4515eaa79 --- /dev/null +++ b/src/main/java/org/cactoos/scalar/Mapped.java @@ -0,0 +1,72 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017-2019 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.scalar; + +import java.io.IOException; +import org.cactoos.Func; +import org.cactoos.Scalar; + +/** + * {@link Scalar} that apply a {@link Func} to the result + * of another {@link Scalar}. + * + *

There is no thread-safety guarantee. + * + *

This class implements {@link Scalar}, which throws a checked + * {@link IOException}. This may not be convenient in many cases. To make + * it more convenient and get rid of the checked exception you can + * use the {@link Unchecked} decorator.

+ * + * @param Type of wrapped scalar result + * @param Type of result + * @since 1.0.0 + */ +public final class Mapped implements Scalar { + + /** + * Original scalar. + */ + private final Scalar origin; + + /** + * Func. + */ + private final Func func; + + /** + * Ctor. + * @param func Fun to apply + * @param scalar Encapsulated scalar + */ + public Mapped(final Func func, final Scalar scalar) { + this.origin = scalar; + this.func = func; + } + + @Override + public U value() throws Exception { + return this.func.apply(this.origin.value()); + } + +} diff --git a/src/test/java/org/cactoos/iterable/IterableEnvelopeTest.java b/src/test/java/org/cactoos/iterable/IterableEnvelopeTest.java deleted file mode 100644 index 03c5ebb977..0000000000 --- a/src/test/java/org/cactoos/iterable/IterableEnvelopeTest.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2017-2019 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.iterable; - -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import org.hamcrest.core.IsEqual; -import org.hamcrest.core.IsNot; -import org.junit.Test; -import org.llorllale.cactoos.matchers.Assertion; - -/** - * Test case for {@link IterableEnvelope}. - * - * @since 0.35 - * @checkstyle JavadocMethodCheck (500 lines) - */ -public final class IterableEnvelopeTest { - - @Test(expected = UnsupportedOperationException.class) - public void returnsIteratorWithUnsupportedRemove() { - final IterableEnvelope list = new IterableEnvelope( - () -> { - final List inner = new LinkedList<>(); - inner.add("eleven"); - return inner; - } - ) { }; - final Iterator iterator = list.iterator(); - iterator.next(); - iterator.remove(); - } - - @Test - public void notEqualsToObjectOfAnotherType() { - new Assertion<>( - "Must not equal to object of another type", - new IterableOf<>(), - new IsNot<>(new IsEqual<>(new Object())) - ).affirm(); - } - - @Test - public void notEqualsToIterableWithDifferentElements() { - final IterableOf first = new IterableOf<>(1, 2); - final IterableOf second = new IterableOf<>(1, 3); - new Assertion<>( - "Must not equal to Iterable with different elements", - first, - new IsNot<>(new IsEqual<>(second)) - ).affirm(); - } - - @Test - public void isEqualToItself() { - final IterableOf iterable = new IterableOf<>(1, 2); - new Assertion<>( - "Must be equal to itself", - iterable, - new IsEqual<>(iterable) - ).affirm(); - } - - @Test - public void isEqualToIterableWithTheSameElements() { - final IterableOf iterable = new IterableOf<>(1, 2); - new Assertion<>( - "Must be equal to Iterable with the same elements", - iterable, - new IsEqual<>(new IterableOf<>(1, 2)) - ).affirm(); - } - - @Test - public void equalToEmptyIterable() { - final IterableOf iterable = new IterableOf<>(); - new Assertion<>( - "Empty Iterable must be equal to empty Iterable", - iterable, - new IsEqual<>(new IterableOf<>()) - ).affirm(); - } - - @Test - public void differentHashCode() { - final IterableOf first = new IterableOf<>(1, 2); - final IterableOf second = new IterableOf<>(2, 1); - new Assertion<>( - "Must have different hashCode for Iterables with different content", - first.hashCode(), - new IsNot<>(new IsEqual<>(second.hashCode())) - ).affirm(); - } - - @Test - public void equalHashCode() { - final IterableOf iterable = new IterableOf<>(1, 2); - new Assertion<>( - "Must have equal hashCode for Iterables with equal content", - iterable.hashCode(), - new IsEqual<>(new IterableOf<>(1, 2).hashCode()) - ).affirm(); - } -} diff --git a/src/test/java/org/cactoos/iterable/IterableOfTest.java b/src/test/java/org/cactoos/iterable/IterableOfTest.java index a9bdf1154e..54864721d9 100644 --- a/src/test/java/org/cactoos/iterable/IterableOfTest.java +++ b/src/test/java/org/cactoos/iterable/IterableOfTest.java @@ -32,7 +32,9 @@ import org.hamcrest.Matchers; import org.hamcrest.collection.IsIterableWithSize; import org.hamcrest.core.IsEqual; +import org.hamcrest.core.IsNot; import org.junit.Test; +import org.llorllale.cactoos.matchers.Assertion; /** * Test case for {@link IterableOf}. @@ -132,4 +134,74 @@ public void reportTotalPagedLength() throws Exception { ) ); } + @Test + public void notEqualsToObjectOfAnotherType() { + new Assertion<>( + "Must not equal to object of another type", + new IterableOf<>(), + new IsNot<>(new IsEqual<>(new Object())) + ).affirm(); + } + + @Test + public void notEqualsToIterableWithDifferentElements() { + final IterableOf first = new IterableOf<>(1, 2); + final IterableOf second = new IterableOf<>(1, 3); + new Assertion<>( + "Must not equal to Iterable with different elements", + first, + new IsNot<>(new IsEqual<>(second)) + ).affirm(); + } + + @Test + public void isEqualToItself() { + final IterableOf iterable = new IterableOf<>(1, 2); + new Assertion<>( + "Must be equal to itself", + iterable, + new IsEqual<>(iterable) + ).affirm(); + } + + @Test + public void isEqualToIterableWithTheSameElements() { + final IterableOf iterable = new IterableOf<>(1, 2); + new Assertion<>( + "Must be equal to Iterable with the same elements", + iterable, + new IsEqual<>(new IterableOf<>(1, 2)) + ).affirm(); + } + + @Test + public void equalToEmptyIterable() { + final IterableOf iterable = new IterableOf<>(); + new Assertion<>( + "Empty Iterable must be equal to empty Iterable", + iterable, + new IsEqual<>(new IterableOf<>()) + ).affirm(); + } + + @Test + public void differentHashCode() { + final IterableOf first = new IterableOf<>(1, 2); + final IterableOf second = new IterableOf<>(2, 1); + new Assertion<>( + "Must have different hashCode for Iterables with different content", + first.hashCode(), + new IsNot<>(new IsEqual<>(second.hashCode())) + ).affirm(); + } + + @Test + public void equalHashCode() { + final IterableOf iterable = new IterableOf<>(1, 2); + new Assertion<>( + "Must have equal hashCode for Iterables with equal content", + iterable.hashCode(), + new IsEqual<>(new IterableOf<>(1, 2).hashCode()) + ).affirm(); + } }