Skip to content
Open
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
12 changes: 10 additions & 2 deletions src/main/java/org/fxmisc/easybind/MappedList.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.fxmisc.easybind;

import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import javafx.collections.ListChangeListener.Change;
Expand All @@ -11,6 +13,7 @@
class MappedList<E, F> extends TransformationList<E, F> {

private final Function<? super F, ? extends E> mapper;
private final Map<F, E> map = new IdentityHashMap<>();

public MappedList(ObservableList<? extends F> source, Function<? super F, ? extends E> mapper) {
super(source);
Expand All @@ -24,7 +27,7 @@ public int getSourceIndex(int index) {

@Override
public E get(int index) {
return mapper.apply(getSource().get(index));
return map.computeIfAbsent(getSource().get(index), mapper::apply);
}

@Override
Expand Down Expand Up @@ -79,7 +82,7 @@ protected int[] getPermutation() {
public List<E> getRemoved() {
ArrayList<E> res = new ArrayList<>(c.getRemovedSize());
for(F e: c.getRemoved()) {
res.add(mapper.apply(e));
res.add(map.getOrDefault(e, mapper.apply(e)));
}
return res;
}
Expand All @@ -104,5 +107,10 @@ public void reset() {
c.reset();
}
});

c.reset();
while (c.next()) {
c.getRemoved().forEach(map::remove);
}
}
}