Module MaterialFX

Class TransformableList<T>

Type Parameters:
T - the items' type
All Implemented Interfaces:
Iterable<T>, Collection<T>, List<T>, Observable, ObservableList<T>

public class TransformableList<T> extends TransformationList<T,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.

IMPORTANT: If using a reversed comparator please use 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 Details

  • Constructor Details

  • Method Details

    • viewToSource

      public int viewToSource(int index)
      Calls getSourceIndex(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)
      Calls getViewIndex(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

      public Predicate<? super T> getPredicate()
      Gets the value of the property predicate.
      Property description:
      Specifies the predicate used to filter the source list.
    • predicateProperty

      public PredicateProperty<T> predicateProperty()
      Specifies the predicate used to filter the source list.
      See Also:
    • setPredicate

      public void setPredicate(Predicate<T> predicate)
      Sets the value of the property predicate.
      Property description:
      Specifies the predicate used to filter the source list.
    • getComparator

      public Comparator<T> getComparator()
      Gets the value of the property comparator.
      Property description:
      Specifies the comparator used to sort the source list.
    • comparatorProperty

      public ComparatorProperty<T> comparatorProperty()
      Specifies the comparator used to sort the source list.
      See Also:
    • setComparator

      public void setComparator(Comparator<T> comparator)
      Sets the value of the property comparator.
      Property description:
      Specifies the comparator used to sort the source list.
    • setComparator

      public void setComparator(Comparator<T> comparator, boolean reversed)
      This method is NECESSARY if using a reversed comparator, a special flag is set to true and sourceToView(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 to getViewIndex(int), if the list is sorted in reversed order.
    • sourceChanged

      protected void sourceChanged(ListChangeListener.Change<? extends T> c)

      Calls update().
      Specified by:
      sourceChanged in class TransformationList<T,T>
    • size

      public int size()
      Specified by:
      size in interface Collection<T>
      Specified by:
      size in interface List<T>
      Specified by:
      size in class AbstractCollection<T>
      Returns:
      the number of items in the transformable list
    • get

      public T get(int index)
      Retrieves and return the item at the given index in the transformable list. This means transformations due to predicateProperty() or comparatorProperty() are taken into account.
      Specified by:
      get in interface List<T>
      Specified by:
      get in class AbstractList<T>
    • getSourceIndex

      public int getSourceIndex(int index)
      Specified by:
      getSourceIndex in class TransformationList<T,T>
    • getViewIndex

      public int getViewIndex(int index)
      Specified by:
      getViewIndex in class TransformationList<T,T>