Skip to content

Commit

Permalink
yegor256#1325 - Sorted now implements SortedSet.
Browse files Browse the repository at this point in the history
  • Loading branch information
vzurauskas committed Mar 20, 2020
1 parent 41a409c commit 37f2408
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 5 deletions.
53 changes: 48 additions & 5 deletions src/main/java/org/cactoos/set/Sorted.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.Comparator;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import org.cactoos.iterable.IterableOf;

Expand All @@ -35,11 +36,14 @@
*
* @param <T> Set type
* @since 1.0.0
* @todo #1292:30min This class should also implements SortedSet
* from the java collection framework by delegating to the
* wrapped set. Some tests must be added for it.
*/
public final class Sorted<T> extends SetEnvelope<T> {
@SuppressWarnings("PMD.OnlyOneConstructorShouldDoInitialization")
public final class Sorted<T> extends SetEnvelope<T> implements SortedSet<T> {

/**
* The original Set this object delegates to.
*/
private final SortedSet<T> origin;

/**
* Ctor.
Expand All @@ -58,7 +62,46 @@ public Sorted(final Comparator<T> cmp, final T... array) {
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
public Sorted(final Comparator<T> cmp, final Iterable<T> src) {
super(new TreeSet<>(cmp));
this(new TreeSet<>(cmp));
src.forEach(super::add);
}

/**
* Primary ctor.
* @param origin The original SortedSet to delegate to.
*/
private Sorted(final SortedSet<T> origin) {
super(origin);
this.origin = origin;
}

@Override
public Comparator<? super T> comparator() {
return this.origin.comparator();
}

@Override
public SortedSet<T> subSet(final T begin, final T end) {
return this.origin.subSet(begin, end);
}

@Override
public SortedSet<T> headSet(final T end) {
return this.origin.headSet(end);
}

@Override
public SortedSet<T> tailSet(final T from) {
return this.origin.tailSet(from);
}

@Override
public T first() {
return this.origin.first();
}

@Override
public T last() {
return this.origin.last();
}
}
71 changes: 71 additions & 0 deletions src/test/java/org/cactoos/set/SortedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,75 @@ public void mustNotBeEqualToSortedSet() {
))
).affirm();
}

@Test
public void returnsCorrectComparator() {
final Comparator<Integer> comparator = Integer::compareTo;
new Assertion<>(
"Comparator must be the same",
new Sorted<>(comparator, 1, 2, 3).comparator(),
new IsEqual<>(comparator)
).affirm();
}

@Test
public void returnsSubset() {
new Assertion<>(
"Must return sorted subset",
new Sorted<>(Integer::compareTo, 3, 6, 1, 9, 3).subSet(3, 9),
new IsIterableContainingInOrder<>(
new ListOf<Matcher<? super Integer>>(
new IsEqual<>(3),
new IsEqual<>(6)
)
)
).affirm();
}

@Test
public void returnsHeadset() {
new Assertion<>(
"Must return sorted headset",
new Sorted<>(Integer::compareTo, 3, 6, 1, 9, 3).headSet(9),
new IsIterableContainingInOrder<>(
new ListOf<Matcher<? super Integer>>(
new IsEqual<>(1),
new IsEqual<>(3),
new IsEqual<>(6)
)
)
).affirm();
}

@Test
public void returnsTailset() {
new Assertion<>(
"Must return sorted tailset",
new Sorted<>(Integer::compareTo, 3, 6, 1, 9, 3).tailSet(6),
new IsIterableContainingInOrder<>(
new ListOf<Matcher<? super Integer>>(
new IsEqual<>(6),
new IsEqual<>(9)
)
)
).affirm();
}

@Test
public void returnsFirst() {
new Assertion<>(
"Must return first element",
new Sorted<>(Integer::compareTo, 3, 6, 1, 9, 3).first(),
new IsEqual<>(1)
).affirm();
}

@Test
public void returnsLast() {
new Assertion<>(
"Must return last element",
new Sorted<>(Integer::compareTo, 3, 6, 1, 9, 3).last(),
new IsEqual<>(9)
).affirm();
}
}

0 comments on commit 37f2408

Please sign in to comment.