Skip to content

Commit

Permalink
(#1236) Immutable decorators are just simple decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Feb 1, 2020
1 parent 506a574 commit 90ed098
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 101 deletions.
7 changes: 1 addition & 6 deletions src/main/java/org/cactoos/collection/Immutable.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Iterator;

/**
* Collection that doesn't allow any modifications.
* Decorator that doesn't allow any mutation of the wrapped {@link Collection}.
*
* <p>There is no thread-safety guarantee.</p>
*
Expand All @@ -36,11 +36,6 @@
* @todo #898:30min Replace all the Collections.unmodifiableCollection
* with the {@link org.cactoos.collection.Immutable} from the cactoos codebase.
* That should be done because Elegant Object principles are against static methods.
* @todo #1224:30min Original collection should be copied inside this
* immutable decorator. That should be done because otherwise true immutability
* cannot be achieved.
* see: https://github.com/yegor256/cactoos/issues/1224 and
* https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
*/
@SuppressWarnings(
{
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterator/Immutable.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.Iterator;

/**
* Iterator that doesn't allow removal of elements.
* Decorator that doesn't allow removal from the wrapped {@link Iterator}.
*
* <p>There is no thread-safety guarantee.</p>
*
Expand Down
84 changes: 6 additions & 78 deletions src/main/java/org/cactoos/list/Immutable.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,13 @@
*/
package org.cactoos.list;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.cactoos.Scalar;
import org.cactoos.iterable.IterableOf;
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.text.TextOf;
import org.cactoos.text.UncheckedText;

/**
* {@link List} envelope that doesn't allow mutations.
* Decorator that doesn't allow mutations of the wrapped {@link List}.
*
* <p>There is no thread-safety guarantee.</p>
*
Expand All @@ -64,45 +54,12 @@ public final class Immutable<T> implements List<T> {
*/
private final List<T> list;

/**
* Ctor.
* @param items Source array
*/
@SafeVarargs
public Immutable(final T... items) {
this(new IterableOf<T>(items));
}

/**
* Ctor.
* @param src Source list
*/
public Immutable(final List<T> src) {
this(() -> new ListOf<>(src));
}

/**
* Ctor.
* @param src Source iterable
*/
public Immutable(final Iterable<T> src) {
this(() -> new ListOf<>(src));
}

/**
* Ctor.
* @param src Source collection
*/
public Immutable(final Collection<T> src) {
this(() -> new ListOf<>(src));
}

/**
* Ctor.
* @param slr The scalar
*/
public Immutable(final Scalar<List<T>> slr) {
this.list = new Unchecked<>(slr).value();
public Immutable(final List<T> src) {
this.list = src;
}

@Override
Expand Down Expand Up @@ -248,46 +205,17 @@ public List<T> subList(final int start, final int end) {
}

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

// @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();
return this.list.hashCode();
}

@Override
public String toString() {
return new UncheckedText(new TextOf(this)).asString();
return this.list.toString();
}
}
31 changes: 15 additions & 16 deletions src/test/java/org/cactoos/list/ImmutableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@
public class ImmutableTest {

@Test
public void innerListMustNotBeChanged() {
public void innerListIsDecorated() {
final List<String> strings = new ArrayList<>(Arrays.asList("a", "b", "c"));
final List<String> immutable = new Immutable<>(strings);
final int original = immutable.size();
strings.add("d");
new Assertion<>(
"inner list must not be changed",
original,
new IsEqual<>(immutable.size())
"Must reflect inner list in the decorator",
immutable,
new IsEqual<>(strings)
).affirm();
}

Expand Down Expand Up @@ -408,7 +407,7 @@ public void immutableSubList() {
public void notEqualsToObjectOfAnotherType() {
new Assertion<>(
"must not equal to object of another type",
new Immutable<>(),
new Immutable<>(new ListOf<>()),
new IsNot<>(new IsEqual<>(new Object()))
).affirm();
}
Expand All @@ -417,14 +416,14 @@ public void notEqualsToObjectOfAnotherType() {
public void notEqualsToListWithDifferentElements() {
new Assertion<>(
"must not equal to List with different elements",
new Immutable<>(1, 2),
new Immutable<>(new ListOf<>(1, 2)),
new IsNot<>(new IsEqual<>(new ListOf<>(1, 0)))
).affirm();
}

@Test
public void isEqualToItself() {
final List<Integer> list = new Immutable<>(1, 2);
final List<Integer> list = new Immutable<>(new ListOf<>(1, 2));
new Assertion<>(
"must be equal to itself",
list,
Expand All @@ -436,7 +435,7 @@ public void isEqualToItself() {
public void isEqualToListWithTheSameElements() {
new Assertion<>(
"must be equal to List with the same elements",
new Immutable<>(1, 2),
new Immutable<>(new ListOf<>(1, 2)),
new IsEqual<>(new ListOf<>(1, 2))
).affirm();
}
Expand All @@ -445,28 +444,28 @@ public void isEqualToListWithTheSameElements() {
public void equalToEmptyImmutable() {
new Assertion<>(
"empty Immutable must be equal to empty Immutable",
new Immutable<>(),
new IsEqual<>(new Immutable<>())
new Immutable<>(new ListOf<>()),
new IsEqual<>(new Immutable<>(new ListOf<>()))
).affirm();
}

@Test
public void testHashCode() {
new Assertion<>(
"hashCode() must be equal to hashCode of the corresponding List",
new Immutable<>(1, 2, 3).hashCode(),
new Immutable<>(new ListOf<>(1, 2, 3)).hashCode(),
new IsEqual<>(
new Immutable<>(1, 2, 3).hashCode()
new ListOf<>(1, 2, 3).hashCode()
)
).affirm();
}

@Test
public void testToString() {
new Assertion<>(
"toString() must be concatenation of nested elements",
new Immutable<>("a", "b", "c").toString(),
new IsEqual<>("a, b, c")
"toString() must be equal to toString of the corresponding List",
new Immutable<>(new ListOf<>("a", "b", "c")).toString(),
new IsEqual<>(new ListOf<>("a", "b", "c").toString())
).affirm();
}
}

1 comment on commit 90ed098

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 90ed098 Feb 3, 2020

Choose a reason for hiding this comment

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

Puzzle 1224-6c7b441d disappeared from src/main/java/org/cactoos/collection/Immutable.java, that's why I closed #1236. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.