Skip to content

Commit

Permalink
(yegor256#1184) CollectionEnvelope is just an envelope
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Nov 16, 2019
1 parent b9a9bee commit 4a54e9a
Show file tree
Hide file tree
Showing 24 changed files with 425 additions and 490 deletions.
86 changes: 4 additions & 82 deletions src/main/java/org/cactoos/collection/CollectionEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,17 @@
package org.cactoos.collection;

import java.util.Collection;
import java.util.Iterator;
import org.cactoos.Scalar;
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;
import org.cactoos.iterable.IterableEnvelope;

/**
* Base collection.
*
* <p>There is no thread-safety guarantee.</p>
* @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(
{
"PMD.TooManyMethods",
"PMD.AbstractNaming"
}
)
public abstract class CollectionEnvelope<X> implements Collection<X> {
public abstract class CollectionEnvelope<X>
extends IterableEnvelope<X> implements Collection<X> {

/**
* The wrapped collection.
Expand All @@ -63,17 +46,10 @@ public abstract class CollectionEnvelope<X> implements Collection<X> {
* @param col The wrapped collection
*/
public CollectionEnvelope(final Collection<X> col) {
super(col);
this.col = col;
}

/**
* Ctor.
* @param slr The scalar
*/
public CollectionEnvelope(final Scalar<Collection<X>> slr) {
this(new Unchecked<>(slr).value());
}

@Override
public final int size() {
return this.col.size();
Expand All @@ -84,11 +60,6 @@ public final boolean isEmpty() {
return this.col.isEmpty();
}

@Override
public final Iterator<X> iterator() {
return this.col.iterator();
}

@Override
public final boolean contains(final Object object) {
return this.col.contains(object);
Expand All @@ -100,7 +71,6 @@ public final Object[] toArray() {
}

@Override
@SuppressWarnings("PMD.UseVarargs")
public final <T> T[] toArray(final T[] array) {
return this.col.toArray(array);
}
Expand Down Expand Up @@ -139,52 +109,4 @@ public final boolean retainAll(final Collection<?> list) {
public final void clear() {
this.col.clear();
}

@Override
public final String toString() {
return this.col.toString();
}

@Override
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings("EQ_UNUSUAL")
public final boolean equals(final Object other) {
return new Unchecked<>(
new Or(
() -> other == this,
new And(
() -> other != null,
() -> Collection.class.isAssignableFrom(other.getClass()),
() -> {
final Collection<?> compared = (Collection<?>) other;
return this.size() == compared.size();
},
() -> {
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 final int hashCode() {
return new Unchecked<>(
new Folded<>(
42,
(hash, entry) -> new SumOfInt(
() -> 37 * hash,
entry::hashCode
).value(),
this
)
).value();
}
}
135 changes: 128 additions & 7 deletions src/main/java/org/cactoos/collection/CollectionOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@
*/
package org.cactoos.collection;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import org.cactoos.Scalar;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.And;
import org.cactoos.scalar.HashCode;
import org.cactoos.scalar.Or;
import org.cactoos.scalar.Unchecked;

/**
* Iterable as {@link Collection}.
Expand All @@ -43,7 +50,11 @@
* @see Sticky
* @since 0.1
*/
public final class CollectionOf<T> extends CollectionEnvelope<T> {
public final class CollectionOf<T> implements Collection<T> {
/**
* Collection.
*/
private final Unchecked<Collection<T>> col;

/**
* Ctor.
Expand All @@ -60,13 +71,123 @@ public CollectionOf(final T... array) {
* @param src An {@link Iterable}
*/
public CollectionOf(final Iterable<T> src) {
super(() -> {
final Collection<T> list = new LinkedList<>();
for (final T item : src) {
list.add(item);
this(
() -> {
final Collection<T> list = new LinkedList<>();
src.forEach(list::add);
return list;
}
return list;
});
);
}

/**
* Ctor.
* @param slr The scalar
*/
public CollectionOf(final Scalar<Collection<T>> slr) {
this.col = new Unchecked<>(slr);
}

@Override
public int size() {
return this.col.value().size();
}

@Override
public boolean isEmpty() {
return this.col.value().isEmpty();
}

@Override
public boolean contains(final Object obj) {
return this.col.value().contains(obj);
}

@Override
public Iterator<T> iterator() {
return this.col.value().iterator();
}

@Override
public Object[] toArray() {
return this.col.value().toArray();
}

@Override
public <X> X[] toArray(final X[] array) {
return this.col.value().toArray(array);
}

@Override
public boolean add(final T elem) {
return this.col.value().add(elem);
}

@Override
public boolean remove(final Object obj) {
return this.col.value().remove(obj);
}

@Override
public boolean containsAll(final Collection<?> other) {
return this.col.value().containsAll(other);
}

@Override
public boolean addAll(final Collection<? extends T> other) {
return this.col.value().addAll(other);
}

@Override
public boolean removeAll(final Collection<?> other) {
return this.col.value().removeAll(other);
}

@Override
public boolean retainAll(final Collection<?> other) {
return this.col.value().retainAll(other);
}

@Override
public void clear() {
this.col.value().clear();
}

@Override
public String toString() {
return this.col.value().toString();
}

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

@Override
public int hashCode() {
return new HashCode(this).value();
}
}
12 changes: 7 additions & 5 deletions src/main/java/org/cactoos/collection/Filtered.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* Filtered collection.
*
* <p>There is no thread-safety guarantee.
*
* @param <X> Type of source item
* @since 1.16
*/
Expand All @@ -53,10 +53,12 @@ public Filtered(final Func<X, Boolean> func, final X... src) {
* @param src Source collection
*/
public Filtered(final Func<X, Boolean> func, final Iterable<X> src) {
super(() -> new CollectionOf<>(
new org.cactoos.iterable.Filtered<>(
func, src
super(
new Sticky<>(
new org.cactoos.iterable.Filtered<>(
func, src
)
)
));
);
}
}
18 changes: 5 additions & 13 deletions src/main/java/org/cactoos/collection/HeadOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.cactoos.collection;

import java.util.Collection;
import org.cactoos.iterable.IterableOf;

/**
Expand Down Expand Up @@ -52,18 +51,11 @@ public HeadOf(final int num, final T... src) {
* @param src Source iterable
*/
public HeadOf(final int num, final Iterable<T> src) {
this(num, new CollectionOf<T>(src));
}

/**
* Ctor.
* @param num Number of head elements
* @param src Source collection
*/
public HeadOf(final int num, final Collection<T> src) {
super(() -> new CollectionOf<T>(
new org.cactoos.iterable.HeadOf<T>(num, src)
));
super(
new Sticky<>(
new org.cactoos.iterable.HeadOf<>(num, src)
)
);
}

}
13 changes: 8 additions & 5 deletions src/main/java/org/cactoos/collection/Joined.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package org.cactoos.collection;

import org.cactoos.iterable.IterableOf;

/**
* Joined collection.
*
Expand All @@ -39,17 +41,18 @@ public final class Joined<X> extends CollectionEnvelope<X> {
*/
@SafeVarargs
public Joined(final Iterable<X>... list) {
this(new CollectionOf<>(list));
this(new IterableOf<>(list));
}

/**
* Ctor.
* @param list Items to concatenate
*/
public Joined(final Iterable<Iterable<X>> list) {
super(() -> new CollectionOf<X>(
new org.cactoos.iterable.Joined<>(list)
));
super(
new Sticky<>(
new org.cactoos.iterable.Joined<>(list)
)
);
}

}
Loading

0 comments on commit 4a54e9a

Please sign in to comment.