java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
javafx.collections.ObservableListBase<E>
javafx.collections.transformation.TransformationList<T,T>
io.github.palexdev.materialfx.collections.TransformableList<T>
- Type Parameters:
T- the items' type
- All Implemented Interfaces:
Iterable<T>,Collection<T>,List<T>,Observable,ObservableList<T>
A
TransformableList is a particular type of List which wraps another
List called "source" and allows manipulations such as: filter and sort, retaining
the original items' index.
Extends TransformationList, it's basically the same thing of a FilteredList
and a SortedList but combined into one.
A more detailed (and hopefully more clear) explanation about the "indexes retention mentioned above":
Think of this List as a View for the source list. The underlying data provided by the source is not guaranteed to be what the user sees but the items' properties are maintained. Let's see a brief example:
// Let's say I have this ObservableList
ObservableList<String> source = FXCollections.observableArrayList("A", "B", "C"):
// Now let's say I want to sort this list in reverse order (CBA) and that
// for some reason I still want A to be the element at index 0, B-1 and C-2
// This is exactly the purpose of the TransformableList...
TransformableList<String> transformed = new TransformableList<>(source);
transformed.setSorter(Comparator.reverseOrder());
// Now that the order is (CBA) let's see how the list behaves:
transformed.get(0); // Returns C
transformed.indexOf("C"); // Returns 2, the index is retrieved in the source list
transformed.viewToSource(0); // Returns 2, it maps an index of the transformed list to the index of the source list, at 0 we have C which is at index 2 in the source list
transformed.sourceToView(0); // Also returns 2, it maps an index of the source list to the index of the transformed list, at 0 we have C which is at index 2 in the transformed list
// To better see its behavior try to sort and filter the list at the same time.
// You'll notice that sometimes sourceToView will return a negative index because the item is not in the transformed list (after a filter operation)
Check computeIndexes() documentation to see how indexes are calculated.
setComparator(Comparator, boolean) with 'true' as argument,
as setComparator(Comparator) will always assume it is a natural order comparator. This is needed to make sourceToView(int)
properly work as it uses a binary search algorithm to find the right index.-
Property Summary
PropertiesTypePropertyDescriptionSpecifies the comparator used to sort the source list.Specifies the predicate used to filter the source list. -
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionTransformableList(ObservableList<? extends T> source) TransformableList(ObservableList<? extends T> source, Predicate<T> predicate) TransformableList(ObservableList<? extends T> source, Predicate<T> predicate, Comparator<T> comparator) -
Method Summary
Modifier and TypeMethodDescriptionSpecifies the comparator used to sort the source list.get(int index) Retrieves and return the item at the given index in the transformable list.Gets the value of the property comparator.Gets the value of the property predicate.intgetSourceIndex(int index) intgetViewIndex(int index) booleanSpecifies if a reversed comparator is being used.Specifies the predicate used to filter the source list.voidsetComparator(Comparator<T> comparator) Sets the value of the property comparator.voidsetComparator(Comparator<T> comparator, boolean reversed) This method is NECESSARY if using a reversed comparator, a special flag is set to true andsourceToView(int)behaves accordingly.voidsetPredicate(Predicate<T> predicate) Sets the value of the property predicate.voidsetReversed(boolean reversed) Communicates to the transformed list, specifically togetViewIndex(int), if the list is sorted in reversed order.intsize()protected voidsourceChanged(ListChangeListener.Change<? extends T> c) intsourceToView(int index) CallsgetViewIndex(int), just with a different name to be more clear.intviewToSource(int index) CallsgetSourceIndex(int), just with a different name to be more clear.Methods inherited from class javafx.collections.transformation.TransformationList
getSource, getSourceIndexFor, isInTransformationChainMethods inherited from class javafx.collections.ObservableListBase
addAll, addListener, addListener, beginChange, endChange, fireChange, hasListeners, nextAdd, nextPermutation, nextRemove, nextRemove, nextReplace, nextSet, nextUpdate, remove, removeAll, removeListener, removeListener, retainAll, setAll, setAllMethods inherited from class java.util.AbstractList
add, add, addAll, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subListMethods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
add, add, addAll, addAll, clear, contains, containsAll, equals, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, replaceAll, retainAll, set, sort, spliterator, subList, toArray, toArrayMethods inherited from interface javafx.beans.Observable
addListener, removeListenerMethods inherited from interface javafx.collections.ObservableList
addAll, addListener, filtered, remove, removeAll, removeListener, retainAll, setAll, setAll, sorted, sorted
-
Property Details
-
predicate
Specifies the predicate used to filter the source list.- See Also:
-
comparator
Specifies the comparator used to sort the source list.- See Also:
-
-
Constructor Details
-
TransformableList
-
TransformableList
-
TransformableList
public TransformableList(ObservableList<? extends T> source, Predicate<T> predicate, Comparator<T> comparator)
-
-
Method Details
-
viewToSource
public int viewToSource(int index) CallsgetSourceIndex(int), just with a different name to be more clear. Maps an index of the transformed list, to the index of the source list. -
sourceToView
public int sourceToView(int index) CallsgetViewIndex(int), just with a different name to be more clear. Maps an index of the source list, to the index of the transformed list. -
getPredicate
Gets the value of the property predicate.- Property description:
- Specifies the predicate used to filter the source list.
-
predicateProperty
Specifies the predicate used to filter the source list.- See Also:
-
setPredicate
Sets the value of the property predicate.- Property description:
- Specifies the predicate used to filter the source list.
-
getComparator
Gets the value of the property comparator.- Property description:
- Specifies the comparator used to sort the source list.
-
comparatorProperty
Specifies the comparator used to sort the source list.- See Also:
-
setComparator
Sets the value of the property comparator.- Property description:
- Specifies the comparator used to sort the source list.
-
setComparator
This method is NECESSARY if using a reversed comparator, a special flag is set to true andsourceToView(int)behaves accordingly. -
isReversed
public boolean isReversed()Specifies if a reversed comparator is being used. -
setReversed
public void setReversed(boolean reversed) Communicates to the transformed list, specifically togetViewIndex(int), if the list is sorted in reversed order. -
sourceChanged
Callsupdate().- Specified by:
sourceChangedin classTransformationList<T,T>
-
size
public int size()- Specified by:
sizein interfaceCollection<T>- Specified by:
sizein interfaceList<T>- Specified by:
sizein classAbstractCollection<T>- Returns:
- the number of items in the transformable list
-
get
Retrieves and return the item at the given index in the transformable list. This means transformations due topredicateProperty()orcomparatorProperty()are taken into account. -
getSourceIndex
public int getSourceIndex(int index) - Specified by:
getSourceIndexin classTransformationList<T,T>
-
getViewIndex
public int getViewIndex(int index) - Specified by:
getViewIndexin classTransformationList<T,T>
-