Skip to content

Commit

Permalink
(#1169) Apply PECS for `iterable.Sorted'
Browse files Browse the repository at this point in the history
  • Loading branch information
andreoss committed Oct 20, 2020
1 parent 9d3b021 commit da8dcbe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/cactoos/iterable/Sorted.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public Sorted(final T... src) {
* @param src The underlying iterable
*/
@SuppressWarnings("unchecked")
public Sorted(final Iterable<T> src) {
this((Comparator<T>) Comparator.naturalOrder(), src);
public Sorted(final Iterable<? extends T> src) {
this((Comparator<? super T>) Comparator.naturalOrder(), src);
}

/**
Expand All @@ -64,7 +64,7 @@ public Sorted(final Iterable<T> src) {
* @param src The underlying iterable
*/
@SafeVarargs
public Sorted(final Comparator<T> cmp, final T... src) {
public Sorted(final Comparator<? super T> cmp, final T... src) {
this(cmp, new IterableOf<>(src));
}

Expand All @@ -73,7 +73,7 @@ public Sorted(final Comparator<T> cmp, final T... src) {
* @param cmp The comparator
* @param src The underlying iterable
*/
public Sorted(final Comparator<T> cmp, final Iterable<T> src) {
public Sorted(final Comparator<? super T> cmp, final Iterable<? extends T> src) {
super(
new IterableOf<>(
() -> new org.cactoos.iterator.Sorted<>(cmp, src.iterator())
Expand Down
28 changes: 21 additions & 7 deletions src/test/java/org/cactoos/iterable/SortedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Collections;
import java.util.Comparator;
import org.cactoos.list.ListOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;
Expand All @@ -43,40 +42,40 @@ final class SortedTest {

@Test
void sortsAnArray() throws Exception {
MatcherAssert.assertThat(
new Assertion<>(
"Can't sort an iterable",
new Sorted<>(
new IterableOf<>(
3, 2, 10, 44, -6, 0
)
),
Matchers.hasItems(-6, 0, 2, 3, 10, 44)
);
).affirm();
}

@Test
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
void sortsAnArrayWithComparator() throws Exception {
MatcherAssert.assertThat(
new Assertion<>(
"Can't sort an iterable with a comparator",
new Sorted<>(
Comparator.reverseOrder(), new IterableOf<>(
"a", "c", "hello", "dude", "Friend"
)
),
Matchers.hasItems("hello", "dude", "c", "a", "Friend")
);
).affirm();
}

@Test
void sortsAnEmptyArray() throws Exception {
MatcherAssert.assertThat(
new Assertion<>(
"Can't sort an empty iterable",
new Sorted<Integer>(
Collections.emptyList()
),
Matchers.iterableWithSize(0)
);
).affirm();
}

@Test
Expand All @@ -91,4 +90,19 @@ void sortsCollection() {
new IsEqual<>(new IterableOf<>("f", "o", "t", "t"))
).affirm();
}

@Test
void sortsByNymberComprator() throws Exception {
new Assertion<>(
"Must sort an iterable of numbers",
new Sorted<>(
(Number fst, Number snd) -> Integer.compare(fst.intValue(), snd.intValue()),
new IterableOf<Number>(3L, 1f, 2d)
),
new IsEqual<>(
new IterableOf<Number>(1f, 2d, 3L)
)
).affirm();
}

}

0 comments on commit da8dcbe

Please sign in to comment.