diff --git a/src/main/java/org/fxmisc/easybind/MappedList.java b/src/main/java/org/fxmisc/easybind/MappedList.java index e3969d6..bbb6012 100644 --- a/src/main/java/org/fxmisc/easybind/MappedList.java +++ b/src/main/java/org/fxmisc/easybind/MappedList.java @@ -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; @@ -11,6 +13,7 @@ class MappedList extends TransformationList { private final Function mapper; + private final Map map = new IdentityHashMap<>(); public MappedList(ObservableList source, Function mapper) { super(source); @@ -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 @@ -79,7 +82,7 @@ protected int[] getPermutation() { public List getRemoved() { ArrayList 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; } @@ -104,5 +107,10 @@ public void reset() { c.reset(); } }); + + c.reset(); + while (c.next()) { + c.getRemoved().forEach(map::remove); + } } }