JavaDoc для ListChangeListener предоставляет шаблон для обработки изменений. Однако я не знаю, как обрабатывать перестановки. Для каждого индекса я могу узнать, где находится новый индекс элемента, но я не знаю, что с ним делать. Это небольшая головоломка, не зависящая от языка программирования. ObservableList может только add(), remove(), set(), а также имеет итератор.
Если у меня есть исходный список [1,2,3] и я привязываю к нему список [], связанный список [1,2,3] должен соответствовать ему. Если исходный список меняет свой компаратор так, что исходный список теперь читается как [3,2,1], как мне сделать так, чтобы связанный список следовал за ним?
/**
* Binds a source list's elements to a destination list. Any changes made in
* the source list will reflect in the destination list.
*
* @param <SRC> The source list's object type.
* @param <DEST> The destination list's object type.
* @param dest The destination list that will be bound to the src list.
* @param src The source list to watch for changes, and propagate up to the
* destination list.
* @param transformer A function that will transform a source list data
* type, A, into a destination list data type, B.
*/
public static <SRC, DEST> void bindLists(
ObservableList<DEST> dest, ObservableList<SRC> src, Function<? super SRC, ? extends DEST> transformer) {
/*Add the initial data into the destination list.*/
for (SRC a : src) {
dest.add(transformer.apply(a));
}
/*Watch for future data to add to the destination list. Also watch for removal
of data form the source list to remove its respective item in the destination
list.*/
src.addListener((ListChangeListener.Change<? extends SRC> c) -> {
while (c.next()) {
if (c.wasPermutated()) {
/*How do you handle permutations? Do you remove and then add,
or add and then remove, or use set, or use a copy arraylist
and set the right indices? Removing/adding causes concurrent modifications.*/
for (int oldIndex = c.getFrom(); oldIndex < c.getTo(); oldIndex++) {
int newIndex = c.getPermutation(oldIndex);
dest.remove(oldIndex);
dest.add(newIndex, dest.get(oldIndex));
}
} else if (c.wasUpdated()) {
} else {
/*Respond to removed data.*/
for (SRC item : c.getRemoved()) {
int from = c.getFrom();
dest.remove(from);
}
/*Respond to added data.*/
for (SRC item : c.getAddedSubList()) {
int indexAdded = src.indexOf(item);
dest.add(indexAdded, transformer.apply(item));
}
}
}
});
}