From 7db7d9d4264740861a1b8bee3a6f90e9dece7476 Mon Sep 17 00:00:00 2001 From: andreoss Date: Mon, 31 Aug 2020 15:23:31 -0400 Subject: [PATCH 1/6] (#1220) Use `Immutable` instead of `Collections.unmodifiableList` --- src/main/java/org/cactoos/iterable/Matched.java | 4 ++-- src/main/java/org/cactoos/iterator/Partitioned.java | 4 ++-- src/main/java/org/cactoos/list/Immutable.java | 3 --- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/cactoos/iterable/Matched.java b/src/main/java/org/cactoos/iterable/Matched.java index 5ec85904d4..7cbce066f5 100644 --- a/src/main/java/org/cactoos/iterable/Matched.java +++ b/src/main/java/org/cactoos/iterable/Matched.java @@ -23,12 +23,12 @@ */ package org.cactoos.iterable; -import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import org.cactoos.BiFunc; import org.cactoos.Scalar; +import org.cactoos.list.Immutable; import org.cactoos.scalar.Unchecked; import org.cactoos.text.FormattedText; @@ -92,7 +92,7 @@ public Matched( ); } } - return Collections.unmodifiableList(rslt).iterator(); + return new Immutable<>(rslt).iterator(); } ); } diff --git a/src/main/java/org/cactoos/iterator/Partitioned.java b/src/main/java/org/cactoos/iterator/Partitioned.java index d194e4b7ef..e1998c97e6 100644 --- a/src/main/java/org/cactoos/iterator/Partitioned.java +++ b/src/main/java/org/cactoos/iterator/Partitioned.java @@ -23,11 +23,11 @@ */ package org.cactoos.iterator; -import java.util.Collections; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.NoSuchElementException; +import org.cactoos.list.Immutable; import org.cactoos.list.ListOf; /** @@ -72,7 +72,7 @@ public List next() { if (this.size < 1) { throw new IllegalArgumentException("Partition size < 1"); } - return Collections.unmodifiableList( + return new Immutable<>( new LinkedList<>( new ListOf<>( new Sliced<>(0, this.size, this.decorated) diff --git a/src/main/java/org/cactoos/list/Immutable.java b/src/main/java/org/cactoos/list/Immutable.java index d946f43b2c..be0bafb5a2 100644 --- a/src/main/java/org/cactoos/list/Immutable.java +++ b/src/main/java/org/cactoos/list/Immutable.java @@ -36,9 +36,6 @@ * @param Element type * @since 1.16 * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) - * @todo #898:30min Replace all the Collections.unmodifiableList - * with the {@link org.cactoos.list.Immutable} from the cactoos codebase. - * That should be done because Elegant Object principles are against static methods. */ @SuppressWarnings( { From 7c41f6a609343dfc23a019495e91df7adf7a93ad Mon Sep 17 00:00:00 2001 From: andreoss Date: Mon, 31 Aug 2020 15:43:33 -0400 Subject: [PATCH 2/6] (#1220) Ban usage of `Collections#unmodifiableList` --- src/test/resources/forbidden-apis.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/resources/forbidden-apis.txt b/src/test/resources/forbidden-apis.txt index f183d1254e..29e956f310 100644 --- a/src/test/resources/forbidden-apis.txt +++ b/src/test/resources/forbidden-apis.txt @@ -6,3 +6,6 @@ org.hamcrest.MatcherAssert#assertThat(java.lang.Object, org.hamcrest.Matcher) @defaultMessage Please specify failure reason org.junit.Assert#assertThat(java.lang.Object, org.hamcrest.Matcher) + +@defaultMessage Please avoid using static methods. +java.util.Collections#unmodifiableList(java.util.List) \ No newline at end of file From 7734ec0a084201bcd008e7db8e3bb55ec16cb0e9 Mon Sep 17 00:00:00 2001 From: andreoss Date: Fri, 4 Sep 2020 05:50:48 -0400 Subject: [PATCH 3/6] (#1220) Remove redundant object --- src/main/java/org/cactoos/iterator/Partitioned.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/org/cactoos/iterator/Partitioned.java b/src/main/java/org/cactoos/iterator/Partitioned.java index e1998c97e6..8aeceb7752 100644 --- a/src/main/java/org/cactoos/iterator/Partitioned.java +++ b/src/main/java/org/cactoos/iterator/Partitioned.java @@ -24,7 +24,6 @@ package org.cactoos.iterator; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; import java.util.NoSuchElementException; import org.cactoos.list.Immutable; @@ -73,11 +72,7 @@ public List next() { throw new IllegalArgumentException("Partition size < 1"); } return new Immutable<>( - new LinkedList<>( - new ListOf<>( - new Sliced<>(0, this.size, this.decorated) - ) - ) + new ListOf<>(new Sliced<>(0, this.size, this.decorated)) ); } From a6239a38f842c810064e5387157f6925df86129e Mon Sep 17 00:00:00 2001 From: andreoss Date: Fri, 4 Sep 2020 05:51:16 -0400 Subject: [PATCH 4/6] (#1220) Use iterator --- src/main/java/org/cactoos/iterable/Matched.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/iterable/Matched.java b/src/main/java/org/cactoos/iterable/Matched.java index 7cbce066f5..4d3b97098d 100644 --- a/src/main/java/org/cactoos/iterable/Matched.java +++ b/src/main/java/org/cactoos/iterable/Matched.java @@ -28,7 +28,7 @@ import java.util.List; import org.cactoos.BiFunc; import org.cactoos.Scalar; -import org.cactoos.list.Immutable; +import org.cactoos.iterator.Immutable; import org.cactoos.scalar.Unchecked; import org.cactoos.text.FormattedText; @@ -92,7 +92,7 @@ public Matched( ); } } - return new Immutable<>(rslt).iterator(); + return new Immutable<>(rslt.iterator()); } ); } From 9f5acb1ae6a2f2a0b47a6e2bfe6c4cf84bbed400 Mon Sep 17 00:00:00 2001 From: andreoss Date: Fri, 4 Sep 2020 06:15:55 -0400 Subject: [PATCH 5/6] Revert "(#1220) Use iterator" This reverts commit a6239a38f842c810064e5387157f6925df86129e. --- src/main/java/org/cactoos/iterable/Matched.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/cactoos/iterable/Matched.java b/src/main/java/org/cactoos/iterable/Matched.java index 4d3b97098d..7cbce066f5 100644 --- a/src/main/java/org/cactoos/iterable/Matched.java +++ b/src/main/java/org/cactoos/iterable/Matched.java @@ -28,7 +28,7 @@ import java.util.List; import org.cactoos.BiFunc; import org.cactoos.Scalar; -import org.cactoos.iterator.Immutable; +import org.cactoos.list.Immutable; import org.cactoos.scalar.Unchecked; import org.cactoos.text.FormattedText; @@ -92,7 +92,7 @@ public Matched( ); } } - return new Immutable<>(rslt.iterator()); + return new Immutable<>(rslt).iterator(); } ); } From 072fdcb4d0d76fb2cd87f926eb68a68e0d39ed85 Mon Sep 17 00:00:00 2001 From: andreoss Date: Fri, 4 Sep 2020 06:39:26 -0400 Subject: [PATCH 6/6] (#1220) Return immutable iterator --- src/main/java/org/cactoos/list/Immutable.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/list/Immutable.java b/src/main/java/org/cactoos/list/Immutable.java index be0bafb5a2..15220af0df 100644 --- a/src/main/java/org/cactoos/list/Immutable.java +++ b/src/main/java/org/cactoos/list/Immutable.java @@ -76,7 +76,7 @@ public boolean contains(final Object item) { @Override public Iterator iterator() { - return this.list.iterator(); + return new org.cactoos.iterator.Immutable<>(this.list.iterator()); } @Override