To the extent possible under law, Tomas Mikula has waived all copyright and related or neighboring rights to this work.
Last active
August 28, 2024 10:00
-
-
Save TomasMikula/8883719 to your computer and use it in GitHub Desktop.
Mapped view of a JavaFX ObservableList.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.function.Function; | |
import javafx.collections.ListChangeListener.Change; | |
import javafx.collections.ObservableList; | |
import javafx.collections.transformation.TransformationList; | |
public class MappedList<E, F> extends TransformationList<E, F> { | |
private final Function<F, E> mapper; | |
public MappedList(ObservableList<? extends F> source, Function<F, E> mapper) { | |
super(source); | |
this.mapper = mapper; | |
} | |
@Override | |
public int getSourceIndex(int index) { | |
return index; | |
} | |
@Override | |
public E get(int index) { | |
return mapper.apply(getSource().get(index)); | |
} | |
@Override | |
public int size() { | |
return getSource().size(); | |
} | |
@Override | |
protected void sourceChanged(Change<? extends F> c) { | |
fireChange(new Change<E>(this) { | |
@Override | |
public boolean wasAdded() { | |
return c.wasAdded(); | |
} | |
@Override | |
public boolean wasRemoved() { | |
return c.wasRemoved(); | |
} | |
@Override | |
public boolean wasReplaced() { | |
return c.wasReplaced(); | |
} | |
@Override | |
public boolean wasUpdated() { | |
return c.wasUpdated(); | |
} | |
@Override | |
public boolean wasPermutated() { | |
return c.wasPermutated(); | |
} | |
@Override | |
public int getPermutation(int i) { | |
return c.getPermutation(i); | |
} | |
@Override | |
protected int[] getPermutation() { | |
// This method is only called by the superclass methods | |
// wasPermutated() and getPermutation(int), which are | |
// both overriden by this class. There is no other way | |
// this method can be called. | |
throw new AssertionError("Unreachable code"); | |
} | |
@Override | |
public List<E> getRemoved() { | |
ArrayList<E> res = new ArrayList<>(c.getRemovedSize()); | |
for(F e: c.getRemoved()) { | |
res.add(mapper.apply(e)); | |
} | |
return res; | |
} | |
@Override | |
public int getFrom() { | |
return c.getFrom(); | |
} | |
@Override | |
public int getTo() { | |
return c.getTo(); | |
} | |
@Override | |
public boolean next() { | |
return c.next(); | |
} | |
@Override | |
public void reset() { | |
c.reset(); | |
} | |
}); | |
} | |
} |
Interesting implementation of TransformationList. I actually need an implementation exactly like this, except I need the mapped values to be distinct. I'll have to struggle through that and use this as a starting point.
I started implementing the same thing and while debugging I found yours. Thanks!
Updated link for RT-35741: JDK-8091967 (The issue is still unresolved and categorized as tbd_major
)
Hello Tomas, what is the license to use your code? Thanks!
Hi @rloschmann, I have added a license file with a link to CC0.
Hello Tomas, thanks a lot. I copied your code and added the missing implementation of int getViewIndex(int index).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Suggested as an implementation for RT-35741