Skip to content

Instantly share code, notes, and snippets.

@TomasMikula
Last active April 27, 2022 22:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TomasMikula/8883719 to your computer and use it in GitHub Desktop.
Save TomasMikula/8883719 to your computer and use it in GitHub Desktop.
Mapped view of a JavaFX ObservableList.

CC0

To the extent possible under law, Tomas Mikula has waived all copyright and related or neighboring rights to this work.

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();
}
});
}
}
@TomasMikula
Copy link
Author

Suggested as an implementation for RT-35741

@thomasnield
Copy link

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.

Copy link

ghost commented Oct 16, 2015

I started implementing the same thing and while debugging I found yours. Thanks!

@RAnders00
Copy link

RAnders00 commented Sep 3, 2016

Updated link for RT-35741: JDK-8091967 (The issue is still unresolved and categorized as tbd_major)

@rloschmann
Copy link

Hello Tomas, what is the license to use your code? Thanks!

@TomasMikula
Copy link
Author

Hi @rloschmann, I have added a license file with a link to CC0.

@rloschmann
Copy link

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