Skip to content

Commit

Permalink
fix TreeSet.ofAll with comparator and empty values (#2255)
Browse files Browse the repository at this point in the history
* test for TreeSet.removeAll case that removes all elements from the set

* fix TreeSet.ofAll() discarding the comparator when creating empty set

* removed unnecessary @SuppressWarnings

* test for TreeSet.ofAll with explicit comparator and empty initial values
  • Loading branch information
nfekete authored and danieldietrich committed Jan 6, 2019
1 parent 00784b9 commit 6179333
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
5 changes: 2 additions & 3 deletions vavr/src/main/java/io/vavr/collection/TreeSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ public static <T extends Comparable<? super T>> TreeSet<T> fill(int n, Supplier<
return fill(Comparators.naturalComparator(), n, s);
}

@SuppressWarnings("unchecked")
public static <T extends Comparable<? super T>> TreeSet<T> ofAll(Iterable<? extends T> values) {
return ofAll(Comparators.naturalComparator(), values);
}
Expand All @@ -201,10 +200,10 @@ public static <T extends Comparable<? super T>> TreeSet<T> ofAll(Iterable<? exte
public static <T> TreeSet<T> ofAll(Comparator<? super T> comparator, Iterable<? extends T> values) {
Objects.requireNonNull(comparator, "comparator is null");
Objects.requireNonNull(values, "values is null");
if (values instanceof TreeSet && ((TreeSet) values).comparator() == comparator) {
if (values instanceof TreeSet && ((TreeSet<?>) values).comparator() == comparator) {
return (TreeSet<T>) values;
} else {
return values.iterator().hasNext() ? new TreeSet<>(RedBlackTree.ofAll(comparator, values)) : (TreeSet<T>) empty();
return values.iterator().hasNext() ? new TreeSet<>(RedBlackTree.ofAll(comparator, values)) : empty(comparator);
}
}

Expand Down
19 changes: 19 additions & 0 deletions vavr/src/test/java/io/vavr/collection/TreeSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ public void shouldCollectNonEmpty() {

// -- construct

@Test
public void shouldConstructEmptySetWithExplicitComparator() {
final TreeSet<Integer> ts = TreeSet.<Integer> of(Comparators.naturalComparator()
.reversed())
.addAll(Array.ofAll(1, 2, 3));
assertThat(ts.toArray()).isEqualTo(Array.of(3, 2, 1));
}

@Test
public void shouldConstructStreamFromEmptyJavaStream() {
final TreeSet<Integer> actual = ofJavaStream(java.util.stream.Stream.<Integer>empty());
Expand Down Expand Up @@ -282,6 +290,17 @@ public void shouldKeepComparator() {
final List<Integer> expected = List.of(3, 2, 1);
assertThat(actual).isEqualTo(expected);
}

// -- removeAll

@Test
public void shouldKeepComparatorOnRemoveAll() {
final TreeSet<Integer> ts = TreeSet.of(Comparators.naturalComparator()
.reversed(), 1, 2, 3)
.removeAll(Array.ofAll(1, 2, 3))
.addAll(Array.ofAll(4, 5, 6));
assertThat(ts.toArray()).isEqualTo(Array.of(6, 5, 4));
}

// -- diff

Expand Down

0 comments on commit 6179333

Please sign in to comment.