Skip to content

8233179: VetoableListDecorator#sort throws IllegalArgumentException "duplicate children" #1674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@

package com.sun.javafx.collections;

import com.sun.javafx.UnmodifiableArrayList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;

import javafx.beans.InvalidationListener;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;

public abstract class VetoableListDecorator<E> implements ObservableList<E> {
public abstract class VetoableListDecorator<E> implements ObservableList<E>, SortableList<E> {

private final ObservableList<E> list;
private int modCount;
Expand Down Expand Up @@ -100,23 +101,34 @@ public void removeListener(InvalidationListener listener) {
helper = ListListenerHelper.removeListener(helper, listener);
}

@Override
public final void doSort(Comparator<? super E> comparator) {
var sortedList = new ArrayList<>(list);
sortedList.sort(comparator);
setAllImpl(Collections.unmodifiableList(sortedList));
}

@Override
public boolean addAll(E... elements) {
return addAll(Arrays.asList(elements));
}

@Override
public boolean setAll(E... elements) {
return setAll(Arrays.asList(elements));
return setAllImpl(new UnmodifiableArrayList<>(elements, elements.length));
}

@Override
public boolean setAll(Collection<? extends E> col) {
List<E> elements = unmodifiableList(col);
onProposedChange(elements, 0, size());
return setAllImpl(unmodifiableList(col));
}

private boolean setAllImpl(List<E> unmodifiableList) {
onProposedChange(unmodifiableList, 0, size());
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you thought of moving all the checks and wrapping into setAllImpl()?

Regardless of that, the current code is good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The wrappers are slightly different (UnmodifiableArrayList vs Collections.unmodifiableList), so doing that would be a little less efficient.


try {
modCount++;
return list.setAll(elements);
return list.setAll(unmodifiableList);
} catch(Exception e) {
modCount--;
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -660,7 +661,14 @@ public void testSubListCreatedOnChangeValid() {
assertEquals(1, subLists.size());
subLists.get(0).size(); // Assert not throwing Exception
subLists.clear();

}

@Test
public void testSort() {
list.setAll("d", "b", "a", "c");
calls.clear();
list.sort(Comparator.naturalOrder());
assertEquals(List.of("a", "b", "c", "d"), list);
assertSingleCall(new String[] {"a", "b", "c", "d"}, new int[] {0, 4});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.sun.javafx.scene.input.PickResultChooser;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import javafx.scene.Group;
import javafx.scene.GroupShim;
Expand Down Expand Up @@ -277,6 +279,22 @@ public void testRemoveAddDiferentChild() {
assertEquals(5, ((NGGroup)NodeHelper.getPeer(g)).getChildren().size());
}

@Test
public void testSortChildren() {
Rectangle rect1 = new Rectangle();
rect1.setId("1");
Rectangle rect2 = new Rectangle();
rect2.setId("2");
Rectangle rect3 = new Rectangle();
rect3.setId("3");

Group g = new Group();
g.getChildren().addAll(rect3, rect1, rect2);
g.getChildren().sort(Comparator.comparing(node -> node.getId()));

assertEquals(List.of(rect1, rect2, rect3), g.getChildren());
}

@Test
public void testGetChildrenUnmodifiable() {
Rectangle rect1 = new Rectangle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import javafx.css.PseudoClass;
import javafx.scene.Group;
import javafx.scene.Node;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -212,7 +211,6 @@ void multipleNodes_removeInteriorRange() {
}

@Test
@Disabled("JDK-8233179")
void multipleNodes_permutation() {
var group = new Group();
var child1 = new Group();
Expand Down